Skip to content

Commit fa06cc1

Browse files
committed
lib: eachDefaultSystemPassThrough/eachSystemPassThrough: init
Expose the eachDefaultSystemPassThrough and eachSystemPassThrough functions to handle cases where the system key should not be injected by eachDefaultSystem and eachSystem: inputs.flake-utils.lib.eachDefaultSystem (system: { checks./*<SYSTEM>.*/"<CHECK>" = /* ... */; devShells./*<SYSTEM>.*/"<DEV_SHELL>" = /* ... */; packages./*<SYSTEM>.*/"<PACKAGE>" = /* ... */; }) // inputs.flake-utils.lib.eachDefaultSystemPassThrough (system: { homeConfigurations."<HOME_CONFIGURATION>" = /* ... */; nixosConfigurations."<NIXOS_CONFIGURATION>" = /* ... */; }) These functions prevent users from re-implementing simplified eachDefaultSystem and eachSystem versions to avoid system key injections, while benefiting from current and future complex logic, like handling the '--impure' flag. This addresses flake-utils' arguably biggest issue. [1] [1]: https://ayats.org/blog/no-flake-utils
1 parent 58351e4 commit fa06cc1

File tree

2 files changed

+65
-29
lines changed

2 files changed

+65
-29
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,36 +26,47 @@ let
2626
# eachSystem using defaultSystems
2727
eachDefaultSystem = eachSystem defaultSystems;
2828

29+
# eachSystemPassThrough using defaultSystems
30+
eachDefaultSystemPassThrough = eachSystemPassThrough defaultSystems;
31+
2932
# Builds a map from <attr>=value to <attr>.<system>=value for each system.
30-
eachSystem =
31-
systems: f:
32-
builtins.foldl'
33-
(
34-
# Merge outputs for each system.
35-
attrs: system:
36-
let
37-
ret = f system;
38-
in
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-
(
51-
if
52-
!builtins ? currentSystem || builtins.elem builtins.currentSystem systems
53-
then
54-
systems
55-
else
56-
# Add the current system if the --impure flag is used.
57-
systems ++ [ builtins.currentSystem ]
58-
);
33+
eachSystem = eachSystemOp (
34+
# Merge outputs for each system.
35+
f: attrs: system:
36+
let
37+
ret = f system;
38+
in
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+
);
5970

6071
# eachSystemMap using defaultSystems
6172
eachDefaultSystemMap = eachSystemMap defaultSystems;
@@ -202,8 +213,10 @@ let
202213
defaultSystems
203214
eachDefaultSystem
204215
eachDefaultSystemMap
216+
eachDefaultSystemPassThrough
205217
eachSystem
206218
eachSystemMap
219+
eachSystemPassThrough
207220
filterPackages
208221
flattenTree
209222
meld

0 commit comments

Comments
 (0)