Skip to content

Commit c1dfcf0

Browse files
authored
Merge pull request #119 from trueNAHO/lib-each-default-system-pass-through-each-system-pass-through-init
2 parents b1d9ab7 + fa06cc1 commit c1dfcf0

File tree

2 files changed

+66
-27
lines changed

2 files changed

+66
-27
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ system = {
2222
```
2323
It's mainly useful to
2424
detect typos and auto-complete if you use [rnix-lsp](https://github.com/nix-community/rnix-lsp).
25-
25+
2626
Eg: instead of typing `"x86_64-linux"`, use `system.x86_64-linux`.
2727

2828

@@ -83,6 +83,11 @@ eachSystem allSystems (system: { hello = 42; })
8383
}
8484
```
8585

86+
### `eachSystemPassThrough :: [<system>] -> (<system> -> attrs)`
87+
88+
Unlike `eachSystem`, this function does not inject the `${system}` key by merely
89+
providing the system argument to the function.
90+
8691
### `eachDefaultSystem :: (<system> -> attrs)`
8792

8893
`eachSystem` pre-populated with `defaultSystems`.
@@ -113,6 +118,24 @@ eachSystem allSystems (system: { hello = 42; })
113118
}
114119
```
115120

121+
### `eachDefaultSystemPassThrough :: (<system> -> attrs)`
122+
123+
`eachSystemPassThrough` pre-populated with `defaultSystems`.
124+
125+
#### Example
126+
127+
```nix
128+
inputs.flake-utils.lib.eachDefaultSystem (system: {
129+
checks./*<SYSTEM>.*/"<CHECK>" = /* ... */;
130+
devShells./*<SYSTEM>.*/"<DEV_SHELL>" = /* ... */;
131+
packages./*<SYSTEM>.*/"<PACKAGE>" = /* ... */;
132+
})
133+
// inputs.flake-utils.lib.eachDefaultSystemPassThrough (system: {
134+
homeConfigurations."<HOME_CONFIGURATION>" = /* ... */;
135+
nixosConfigurations."<NIXOS_CONFIGURATION>" = /* ... */;
136+
})
137+
```
138+
116139
### `meld :: attrs -> [ path ] -> attrs`
117140

118141
Meld merges subflakes using common inputs. Useful when you want to

lib.nix

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,47 @@ let
2626
# eachSystem using defaultSystems
2727
eachDefaultSystem = eachSystem defaultSystems;
2828

29-
# Builds a map from <attr>=value to <attr>.<system>=value for each system
30-
#
31-
eachSystem = systems: f:
29+
# eachSystemPassThrough using defaultSystems
30+
eachDefaultSystemPassThrough = eachSystemPassThrough defaultSystems;
31+
32+
# Builds a map from <attr>=value to <attr>.<system>=value for each system.
33+
eachSystem = eachSystemOp (
34+
# Merge outputs for each system.
35+
f: attrs: system:
3236
let
33-
# Merge together the outputs for all systems.
34-
op = attrs: system:
35-
let
36-
ret = f system;
37-
op = attrs: key: attrs //
38-
{
39-
${key} = (attrs.${key} or { })
40-
// { ${system} = ret.${key}; };
41-
}
42-
;
43-
in
44-
builtins.foldl' op attrs (builtins.attrNames ret);
37+
ret = f system;
4538
in
46-
builtins.foldl' op { }
47-
(systems
48-
++ # add the current system if --impure is used
49-
(if builtins?currentSystem then
50-
if builtins.elem builtins.currentSystem systems
51-
then []
52-
else [ builtins.currentSystem ]
53-
else
54-
[]))
55-
;
39+
builtins.foldl' (
40+
attrs: key:
41+
attrs
42+
// {
43+
${key} = (attrs.${key} or { }) // {
44+
${system} = ret.${key};
45+
};
46+
}
47+
) attrs (builtins.attrNames ret)
48+
);
49+
50+
# Applies a merge operation accross systems.
51+
eachSystemOp =
52+
op: systems: f:
53+
builtins.foldl' (op f) { } (
54+
if
55+
!builtins ? currentSystem || builtins.elem builtins.currentSystem systems
56+
then
57+
systems
58+
else
59+
# Add the current system if the --impure flag is used.
60+
systems ++ [ builtins.currentSystem ]
61+
);
62+
63+
# Merely provides the system argument to the function.
64+
#
65+
# Unlike eachSystem, this function does not inject the `${system}` key.
66+
eachSystemPassThrough = eachSystemOp (
67+
f: attrs: system:
68+
attrs // (f system)
69+
);
5670

5771
# eachSystemMap using defaultSystems
5872
eachDefaultSystemMap = eachSystemMap defaultSystems;
@@ -198,9 +212,11 @@ let
198212
check-utils
199213
defaultSystems
200214
eachDefaultSystem
201-
eachSystem
202215
eachDefaultSystemMap
216+
eachDefaultSystemPassThrough
217+
eachSystem
203218
eachSystemMap
219+
eachSystemPassThrough
204220
filterPackages
205221
flattenTree
206222
meld

0 commit comments

Comments
 (0)