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').
8 { src, system ? builtins.currentSystem or "unknown-system" }:
12 lockFilePath = src + "/flake.lock";
14 lockFile = builtins.fromJSON (builtins.readFile lockFilePath);
18 if info.type == "github" then
19 { outPath = fetchTarball "https://api.${info.host or "github.com"}/repos/${info.owner}/${info.repo}/tarball/${info.rev}";
21 shortRev = builtins.substring 0 7 info.rev;
22 lastModified = info.lastModified;
23 lastModifiedDate = formatSecondsSinceEpoch info.lastModified;
24 narHash = info.narHash;
26 else if info.type == "git" then
30 // (if info ? rev then { inherit (info) rev; } else {})
31 // (if info ? ref then { inherit (info) ref; } else {})
33 lastModified = info.lastModified;
34 lastModifiedDate = formatSecondsSinceEpoch info.lastModified;
35 narHash = info.narHash;
36 } // (if info ? rev then {
38 shortRev = builtins.substring 0 7 info.rev;
41 else if info.type == "path" then
42 { outPath = builtins.toPath (
43 if builtins.substring 0 1 info.path == "."
44 then builtins.toString src + "/" + info.path
47 narHash = info.narHash;
49 else if info.type == "tarball" then
50 { outPath = fetchTarball info.url;
51 narHash = info.narHash;
53 else if info.type == "gitlab" then
54 { inherit (info) rev narHash lastModified;
55 outPath = fetchTarball "https://${info.host or "gitlab.com"}/api/v4/projects/${info.owner}%2F${info.repo}/repository/archive.tar.gz?sha=${info.rev}";
56 shortRev = builtins.substring 0 7 info.rev;
59 # FIXME: add Mercurial, tarball inputs.
60 throw "flake input has unsupported input type '${info.type}'";
62 callFlake4 = flakeSrc: locks:
64 flake = import (flakeSrc + "/flake.nix");
66 inputs = builtins.mapAttrs (n: v:
68 then callFlake4 (fetchTree (v.locked // v.info)) v.inputs
69 else fetchTree (v.locked // v.info)) locks;
71 outputs = flakeSrc // (flake.outputs (inputs // {self = outputs;}));
73 assert flake.edition == 201909;
76 callLocklessFlake = flakeSrc:
78 flake = import (flakeSrc + "/flake.nix");
79 outputs = flakeSrc // (flake.outputs ({ self = outputs; }));
83 # Try to clean the source tree by using fetchGit, if this source
84 # tree is a valid git repository.
86 if isGit && !isShallow
88 let res = builtins.fetchGit src;
89 in if res.rev == "0000000000000000000000000000000000000000" then removeAttrs res ["rev" "shortRev"] else res
90 else { outPath = src; };
91 # NB git worktrees have a file for .git, so we don't check the type of .git
92 isGit = builtins.pathExists (src + "/.git");
93 isShallow = builtins.pathExists (src + "/.git/shallow");
96 { lastModified = 0; lastModifiedDate = formatSecondsSinceEpoch 0; }
97 // (if src ? outPath then src else tryFetchGit src);
99 # Format number of seconds in the Unix epoch as %Y%m%d%H%M%S.
100 formatSecondsSinceEpoch = t:
102 rem = x: y: x - x / y * y;
104 secondsInDay = rem t 86400;
105 hours = secondsInDay / 3600;
106 minutes = (rem secondsInDay 3600) / 60;
109 # Courtesy of https://stackoverflow.com/a/32158604.
111 era = (if z >= 0 then z else z - 146096) / 146097;
112 doe = z - era * 146097;
113 yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
115 doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
116 mp = (5 * doy + 2) / 153;
117 d = doy - (153 * mp + 2) / 5 + 1;
118 m = mp + (if mp < 10 then 3 else -9);
119 y' = y + (if m <= 2 then 1 else 0);
121 pad = s: if builtins.stringLength s < 2 then "0" + s else s;
122 in "${toString y'}${pad (toString m)}${pad (toString d)}${pad (toString hours)}${pad (toString minutes)}${pad (toString seconds)}";
129 if key == lockFile.root
131 else fetchTree (node.info or {} // removeAttrs node.locked ["dir"]);
133 subdir = if key == lockFile.root then "" else node.locked.dir or "";
135 flake = import (sourceInfo + (if subdir != "" then "/" else "") + subdir + "/flake.nix");
137 inputs = builtins.mapAttrs
138 (inputName: inputSpec: allNodes.${resolveInput inputSpec})
141 # Resolve a input spec into a node name. An input spec is
142 # either a node name, or a 'follows' path from the root
144 resolveInput = inputSpec:
145 if builtins.isList inputSpec
146 then getInputByPath lockFile.root inputSpec
149 # Follow an input path (e.g. ["dwarffs" "nixpkgs"]) from the
150 # root node, returning the final node.
151 getInputByPath = nodeName: path:
156 # Since this could be a 'follows' input, call resolveInput.
157 (resolveInput lockFile.nodes.${nodeName}.inputs.${builtins.head path})
158 (builtins.tail path);
160 outputs = flake.outputs (inputs // { self = result; });
162 result = outputs // sourceInfo // { inherit inputs; inherit outputs; inherit sourceInfo; };
164 if node.flake or true then
165 assert builtins.isFunction flake.outputs;
173 if !(builtins.pathExists lockFilePath)
174 then callLocklessFlake rootSrc
175 else if lockFile.version == 4
176 then callFlake4 rootSrc (lockFile.inputs)
177 else if lockFile.version >= 5 && lockFile.version <= 7
178 then allNodes.${lockFile.root}
179 else throw "lock file '${lockFilePath}' has unsupported version ${toString lockFile.version}";
185 // (if result ? defaultPackage.${system} then { default = result.defaultPackage.${system}; } else {});
189 // (if result ? devShell.${system} then { default = result.devShell.${system}; } else {});