-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcall-flake.nix
More file actions
97 lines (79 loc) · 2.68 KB
/
Copy pathcall-flake.nix
File metadata and controls
97 lines (79 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# This file was copied from nix itself, and tweaked to support patches.
# <https://github.com/NixOS/nix/blob/2.34.6/src/libflake/call-flake.nix>
{ applyPatches }:
# This is a helper to callFlake() to lazily fetch flake inputs.
# The contents of the JSON lock file, parsed.
lockFile:
# A mapping of lock file node IDs to { sourceInfo, patches ? [] } attrsets,
# - "${sourceInfo.outPath}/flake.nix" must exist
# - `patches` is a list of patches to apply to the flake source code before
# importing it.
overrides:
let
inherit (builtins) mapAttrs;
# Resolve a input spec into a node name. An input spec is
# either a node name, or a 'follows' path from the root
# node.
resolveInput =
inputSpec: if builtins.isList inputSpec then getInputByPath lockFile.root inputSpec else inputSpec;
# Follow an input attrpath (e.g. ["dwarffs" "nixpkgs"]) from the
# root node, returning the final node.
getInputByPath =
nodeName: path:
if path == [ ] then
nodeName
else
getInputByPath
# Since this could be a 'follows' input, call resolveInput.
(resolveInput lockFile.nodes.${nodeName}.inputs.${builtins.head path})
(builtins.tail path);
allNodes = mapAttrs (
key: node:
let
override = overrides.${key};
inherit (override) sourceInfo patches;
unpatchedOutPath = override.outPath;
outPath =
if patches == [ ] then
unpatchedOutPath
else
applyPatches {
name = "${key}-patched";
patches = patches;
src = unpatchedOutPath;
};
flake = import (outPath + "/flake.nix");
inputs = mapAttrs (inputName: inputSpec: allNodes.${resolveInput inputSpec}.result) (
node.inputs or { }
);
outputs = flake.outputs (inputs // { self = result; });
result =
outputs
# We add the sourceInfo attribute for its metadata, as they are
# relevant metadata for the flake. However, the outPath of the
# sourceInfo does not necessarily match the outPath of the flake,
# as the flake may be in a subdirectory of a source.
# This is shadowed in the next //
// sourceInfo
// {
# This shadows the sourceInfo.outPath
inherit outPath;
inherit inputs;
inherit outputs;
inherit sourceInfo;
_type = "flake";
};
in
{
inherit inputs;
result =
if node.flake or true then
assert builtins.isFunction flake.outputs;
result
else
sourceInfo // { inherit sourceInfo outPath; };
inherit outPath sourceInfo;
}
) lockFile.nodes;
in
allNodes.${lockFile.root}