|
| 1 | +# Compatibility function to allow flakes to be used by |
| 2 | +# non-flake-enabled Nix versions. Given a source tree containing a |
| 3 | +# 'flake.nix' and 'flake.lock' file, it fetches the flake inputs and |
| 4 | +# calls the flake's 'outputs' function. It then returns an attrset |
| 5 | +# containing 'defaultNix' (to be used in 'default.nix'), 'shellNix' |
| 6 | +# (to be used in 'shell.nix'). |
| 7 | + |
| 8 | +{ src, system ? builtins.currentSystem or "unknown-system" }: |
| 9 | + |
| 10 | +let |
| 11 | + |
| 12 | + lockFilePath = src + "/flake.lock"; |
| 13 | + |
| 14 | + lockFile = builtins.fromJSON (builtins.readFile lockFilePath); |
| 15 | + |
| 16 | + fetchTree = |
| 17 | + info: |
| 18 | + if info.type == "github" then |
| 19 | + { outPath = |
| 20 | + fetchTarball |
| 21 | + ({ url = "https://api.${info.host or "github.com"}/repos/${info.owner}/${info.repo}/tarball/${info.rev}"; } |
| 22 | + // (if info ? narHash then { sha256 = info.narHash; } else {}) |
| 23 | + ); |
| 24 | + rev = info.rev; |
| 25 | + shortRev = builtins.substring 0 7 info.rev; |
| 26 | + lastModified = info.lastModified; |
| 27 | + lastModifiedDate = formatSecondsSinceEpoch info.lastModified; |
| 28 | + narHash = info.narHash; |
| 29 | + } |
| 30 | + else if info.type == "git" then |
| 31 | + { outPath = |
| 32 | + builtins.fetchGit |
| 33 | + ({ url = info.url; } |
| 34 | + // (if info ? rev then { inherit (info) rev; } else {}) |
| 35 | + // (if info ? ref then { inherit (info) ref; } else {}) |
| 36 | + ); |
| 37 | + lastModified = info.lastModified; |
| 38 | + lastModifiedDate = formatSecondsSinceEpoch info.lastModified; |
| 39 | + narHash = info.narHash; |
| 40 | + } // (if info ? rev then { |
| 41 | + rev = info.rev; |
| 42 | + shortRev = builtins.substring 0 7 info.rev; |
| 43 | + } else { |
| 44 | + }) |
| 45 | + else if info.type == "path" then |
| 46 | + { outPath = builtins.path { path = info.path; }; |
| 47 | + narHash = info.narHash; |
| 48 | + } |
| 49 | + else if info.type == "tarball" then |
| 50 | + { outPath = |
| 51 | + fetchTarball |
| 52 | + ({ inherit (info) url; } |
| 53 | + // (if info ? narHash then { sha256 = info.narHash; } else {}) |
| 54 | + ); |
| 55 | + } |
| 56 | + else if info.type == "gitlab" then |
| 57 | + { inherit (info) rev narHash lastModified; |
| 58 | + outPath = |
| 59 | + fetchTarball |
| 60 | + ({ url = "https://${info.host or "gitlab.com"}/api/v4/projects/${info.owner}%2F${info.repo}/repository/archive.tar.gz?sha=${info.rev}"; } |
| 61 | + // (if info ? narHash then { sha256 = info.narHash; } else {}) |
| 62 | + ); |
| 63 | + shortRev = builtins.substring 0 7 info.rev; |
| 64 | + } |
| 65 | + else |
| 66 | + # FIXME: add Mercurial, tarball inputs. |
| 67 | + throw "flake input has unsupported input type '${info.type}'"; |
| 68 | + |
| 69 | + callFlake4 = flakeSrc: locks: |
| 70 | + let |
| 71 | + flake = import (flakeSrc + "/flake.nix"); |
| 72 | + |
| 73 | + inputs = builtins.mapAttrs (n: v: |
| 74 | + if v.flake or true |
| 75 | + then callFlake4 (fetchTree (v.locked // v.info)) v.inputs |
| 76 | + else fetchTree (v.locked // v.info)) locks; |
| 77 | + |
| 78 | + outputs = flakeSrc // (flake.outputs (inputs // {self = outputs;})); |
| 79 | + in |
| 80 | + assert flake.edition == 201909; |
| 81 | + outputs; |
| 82 | + |
| 83 | + callLocklessFlake = flakeSrc: |
| 84 | + let |
| 85 | + flake = import (flakeSrc + "/flake.nix"); |
| 86 | + outputs = flakeSrc // (flake.outputs ({ self = outputs; })); |
| 87 | + in outputs; |
| 88 | + |
| 89 | + rootSrc = let |
| 90 | + # Try to clean the source tree by using fetchGit, if this source |
| 91 | + # tree is a valid git repository. |
| 92 | + tryFetchGit = src: |
| 93 | + if isGit && !isShallow |
| 94 | + then |
| 95 | + let res = builtins.fetchGit src; |
| 96 | + in if res.rev == "0000000000000000000000000000000000000000" then removeAttrs res ["rev" "shortRev"] else res |
| 97 | + else { outPath = src; }; |
| 98 | + # NB git worktrees have a file for .git, so we don't check the type of .git |
| 99 | + isGit = builtins.pathExists (src + "/.git"); |
| 100 | + isShallow = builtins.pathExists (src + "/.git/shallow"); |
| 101 | + |
| 102 | + in |
| 103 | + { lastModified = 0; lastModifiedDate = formatSecondsSinceEpoch 0; } |
| 104 | + // (if src ? outPath then src else tryFetchGit src); |
| 105 | + |
| 106 | + # Format number of seconds in the Unix epoch as %Y%m%d%H%M%S. |
| 107 | + formatSecondsSinceEpoch = t: |
| 108 | + let |
| 109 | + rem = x: y: x - x / y * y; |
| 110 | + days = t / 86400; |
| 111 | + secondsInDay = rem t 86400; |
| 112 | + hours = secondsInDay / 3600; |
| 113 | + minutes = (rem secondsInDay 3600) / 60; |
| 114 | + seconds = rem t 60; |
| 115 | + |
| 116 | + # Courtesy of https://stackoverflow.com/a/32158604. |
| 117 | + z = days + 719468; |
| 118 | + era = (if z >= 0 then z else z - 146096) / 146097; |
| 119 | + doe = z - era * 146097; |
| 120 | + yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; |
| 121 | + y = yoe + era * 400; |
| 122 | + doy = doe - (365 * yoe + yoe / 4 - yoe / 100); |
| 123 | + mp = (5 * doy + 2) / 153; |
| 124 | + d = doy - (153 * mp + 2) / 5 + 1; |
| 125 | + m = mp + (if mp < 10 then 3 else -9); |
| 126 | + y' = y + (if m <= 2 then 1 else 0); |
| 127 | + |
| 128 | + pad = s: if builtins.stringLength s < 2 then "0" + s else s; |
| 129 | + in "${toString y'}${pad (toString m)}${pad (toString d)}${pad (toString hours)}${pad (toString minutes)}${pad (toString seconds)}"; |
| 130 | + |
| 131 | + allNodes = |
| 132 | + builtins.mapAttrs |
| 133 | + (key: node: |
| 134 | + let |
| 135 | + sourceInfo = |
| 136 | + if key == lockFile.root |
| 137 | + then rootSrc |
| 138 | + else fetchTree (node.info or {} // removeAttrs node.locked ["dir"]); |
| 139 | + |
| 140 | + subdir = if key == lockFile.root then "" else node.locked.dir or ""; |
| 141 | + |
| 142 | + flake = import (sourceInfo + (if subdir != "" then "/" else "") + subdir + "/flake.nix"); |
| 143 | + |
| 144 | + inputs = builtins.mapAttrs |
| 145 | + (inputName: inputSpec: allNodes.${resolveInput inputSpec}) |
| 146 | + (node.inputs or {}); |
| 147 | + |
| 148 | + # Resolve a input spec into a node name. An input spec is |
| 149 | + # either a node name, or a 'follows' path from the root |
| 150 | + # node. |
| 151 | + resolveInput = inputSpec: |
| 152 | + if builtins.isList inputSpec |
| 153 | + then getInputByPath lockFile.root inputSpec |
| 154 | + else inputSpec; |
| 155 | + |
| 156 | + # Follow an input path (e.g. ["dwarffs" "nixpkgs"]) from the |
| 157 | + # root node, returning the final node. |
| 158 | + getInputByPath = nodeName: path: |
| 159 | + if path == [] |
| 160 | + then nodeName |
| 161 | + else |
| 162 | + getInputByPath |
| 163 | + # Since this could be a 'follows' input, call resolveInput. |
| 164 | + (resolveInput lockFile.nodes.${nodeName}.inputs.${builtins.head path}) |
| 165 | + (builtins.tail path); |
| 166 | + |
| 167 | + outputs = flake.outputs (inputs // { self = result; }); |
| 168 | + |
| 169 | + result = outputs // sourceInfo // { inherit inputs; inherit outputs; inherit sourceInfo; }; |
| 170 | + in |
| 171 | + if node.flake or true then |
| 172 | + assert builtins.isFunction flake.outputs; |
| 173 | + result |
| 174 | + else |
| 175 | + sourceInfo |
| 176 | + ) |
| 177 | + lockFile.nodes; |
| 178 | + |
| 179 | + result = |
| 180 | + if !(builtins.pathExists lockFilePath) |
| 181 | + then callLocklessFlake rootSrc |
| 182 | + else if lockFile.version == 4 |
| 183 | + then callFlake4 rootSrc (lockFile.inputs) |
| 184 | + else if lockFile.version >= 5 && lockFile.version <= 7 |
| 185 | + then allNodes.${lockFile.root} |
| 186 | + else throw "lock file '${lockFilePath}' has unsupported version ${toString lockFile.version}"; |
| 187 | + |
| 188 | +in |
| 189 | + rec { |
| 190 | + defaultNix = |
| 191 | + result |
| 192 | + // (if result ? defaultPackage.${system} then { default = result.defaultPackage.${system}; } else {}); |
| 193 | + |
| 194 | + shellNix = |
| 195 | + defaultNix |
| 196 | + // (if result ? devShell.${system} then { default = result.devShell.${system}; } else {}); |
| 197 | + } |
0 commit comments