|
| 1 | +# Adapted from https://github.com/edolstra/flake-compat/blob/master/default.nix |
| 2 | +# |
| 3 | +# This version only gives back the inputs. In that mode, flake becomes little |
| 4 | +# more than a niv replacement. |
| 5 | +{src ? ./.}: let |
| 6 | + lockFilePath = src + "/flake.lock"; |
| 7 | + |
| 8 | + lockFile = builtins.fromJSON (builtins.readFile lockFilePath); |
| 9 | + |
| 10 | + # Emulate builtins.fetchTree |
| 11 | + # |
| 12 | + # TODO: only implement polyfill if the builtin doesn't exist? |
| 13 | + fetchTree = info: |
| 14 | + if info.type == "github" |
| 15 | + then { |
| 16 | + outPath = fetchTarball { |
| 17 | + url = "https://api.${info.host or "github.com"}/repos/${info.owner}/${info.repo}/tarball/${info.rev}"; |
| 18 | + sha256 = info.narHash; |
| 19 | + }; |
| 20 | + rev = info.rev; |
| 21 | + shortRev = builtins.substring 0 7 info.rev; |
| 22 | + lastModified = info.lastModified; |
| 23 | + narHash = info.narHash; |
| 24 | + } |
| 25 | + else if info.type == "git" |
| 26 | + then |
| 27 | + { |
| 28 | + outPath = |
| 29 | + builtins.fetchGit |
| 30 | + ( |
| 31 | + { |
| 32 | + url = info.url; |
| 33 | + sha256 = info.narHash; |
| 34 | + } |
| 35 | + // ( |
| 36 | + if info ? rev |
| 37 | + then {inherit (info) rev;} |
| 38 | + else {} |
| 39 | + ) |
| 40 | + // ( |
| 41 | + if info ? ref |
| 42 | + then {inherit (info) ref;} |
| 43 | + else {} |
| 44 | + ) |
| 45 | + ); |
| 46 | + lastModified = info.lastModified; |
| 47 | + narHash = info.narHash; |
| 48 | + } |
| 49 | + // ( |
| 50 | + if info ? rev |
| 51 | + then { |
| 52 | + rev = info.rev; |
| 53 | + shortRev = builtins.substring 0 7 info.rev; |
| 54 | + } |
| 55 | + else {} |
| 56 | + ) |
| 57 | + else if info.type == "path" |
| 58 | + then { |
| 59 | + outPath = builtins.path {path = info.path;}; |
| 60 | + narHash = info.narHash; |
| 61 | + } |
| 62 | + else if info.type == "tarball" |
| 63 | + then { |
| 64 | + outPath = fetchTarball { |
| 65 | + url = info.url; |
| 66 | + sha256 = info.narHash; |
| 67 | + }; |
| 68 | + narHash = info.narHash; |
| 69 | + } |
| 70 | + else if info.type == "gitlab" |
| 71 | + then { |
| 72 | + inherit (info) rev narHash lastModified; |
| 73 | + outPath = fetchTarball { |
| 74 | + url = "https://${info.host or "gitlab.com"}/api/v4/projects/${info.owner}%2F${info.repo}/repository/archive.tar.gz?sha=${info.rev}"; |
| 75 | + sha256 = info.narHash; |
| 76 | + }; |
| 77 | + shortRev = builtins.substring 0 7 info.rev; |
| 78 | + } |
| 79 | + else |
| 80 | + # FIXME: add Mercurial, tarball inputs. |
| 81 | + throw "flake input has unsupported input type '${info.type}'"; |
| 82 | + |
| 83 | + allNodes = |
| 84 | + builtins.mapAttrs |
| 85 | + ( |
| 86 | + key: node: let |
| 87 | + sourceInfo = |
| 88 | + if key == lockFile.root |
| 89 | + then {} |
| 90 | + else fetchTree (node.info or {} // removeAttrs node.locked ["dir"]); |
| 91 | + |
| 92 | + inputs = |
| 93 | + builtins.mapAttrs |
| 94 | + (inputName: inputSpec: allNodes.${resolveInput inputSpec}) |
| 95 | + (node.inputs or {}); |
| 96 | + |
| 97 | + # Resolve a input spec into a node name. An input spec is |
| 98 | + # either a node name, or a 'follows' path from the root |
| 99 | + # node. |
| 100 | + resolveInput = inputSpec: |
| 101 | + if builtins.isList inputSpec |
| 102 | + then getInputByPath lockFile.root inputSpec |
| 103 | + else inputSpec; |
| 104 | + |
| 105 | + # Follow an input path (e.g. ["dwarffs" "nixpkgs"]) from the |
| 106 | + # root node, returning the final node. |
| 107 | + getInputByPath = nodeName: path: |
| 108 | + if path == [] |
| 109 | + then nodeName |
| 110 | + else |
| 111 | + getInputByPath |
| 112 | + # Since this could be a 'follows' input, call resolveInput. |
| 113 | + (resolveInput lockFile.nodes.${nodeName}.inputs.${builtins.head path}) |
| 114 | + (builtins.tail path); |
| 115 | + |
| 116 | + result = |
| 117 | + sourceInfo |
| 118 | + // { |
| 119 | + inherit inputs; |
| 120 | + inherit sourceInfo; |
| 121 | + }; |
| 122 | + in |
| 123 | + if node.flake or true |
| 124 | + then result |
| 125 | + else sourceInfo |
| 126 | + ) |
| 127 | + lockFile.nodes; |
| 128 | + |
| 129 | + result = |
| 130 | + if lockFile.version >= 5 && lockFile.version <= 7 |
| 131 | + then allNodes.${lockFile.root}.inputs |
| 132 | + else throw "lock file '${lockFilePath}' has unsupported version ${toString lockFile.version}"; |
| 133 | +in |
| 134 | + result |
0 commit comments