1
0
Fork 0
mirror of https://github.com/numtide/flake-utils.git synced 2024-12-14 11:47:31 +00:00

Merge pull request #119 from trueNAHO/lib-each-default-system-pass-through-each-system-pass-through-init

This commit is contained in:
Jonas Chevalier 2024-09-17 10:14:13 +02:00 committed by GitHub
commit c1dfcf0841
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 66 additions and 27 deletions

View file

@ -22,7 +22,7 @@ system = {
```
It's mainly useful to
detect typos and auto-complete if you use [rnix-lsp](https://github.com/nix-community/rnix-lsp).
Eg: instead of typing `"x86_64-linux"`, use `system.x86_64-linux`.
@ -83,6 +83,11 @@ eachSystem allSystems (system: { hello = 42; })
}
```
### `eachSystemPassThrough :: [<system>] -> (<system> -> attrs)`
Unlike `eachSystem`, this function does not inject the `${system}` key by merely
providing the system argument to the function.
### `eachDefaultSystem :: (<system> -> attrs)`
`eachSystem` pre-populated with `defaultSystems`.
@ -113,6 +118,24 @@ eachSystem allSystems (system: { hello = 42; })
}
```
### `eachDefaultSystemPassThrough :: (<system> -> attrs)`
`eachSystemPassThrough` pre-populated with `defaultSystems`.
#### Example
```nix
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>" = /* ... */;
})
```
### `meld :: attrs -> [ path ] -> attrs`
Meld merges subflakes using common inputs. Useful when you want to

68
lib.nix
View file

@ -26,33 +26,47 @@ let
# eachSystem using defaultSystems
eachDefaultSystem = eachSystem defaultSystems;
# Builds a map from <attr>=value to <attr>.<system>=value for each system
#
eachSystem = systems: f:
# eachSystemPassThrough using defaultSystems
eachDefaultSystemPassThrough = eachSystemPassThrough defaultSystems;
# Builds a map from <attr>=value to <attr>.<system>=value for each system.
eachSystem = eachSystemOp (
# Merge outputs for each system.
f: attrs: system:
let
# Merge together the outputs for all systems.
op = attrs: system:
let
ret = f system;
op = attrs: key: attrs //
{
${key} = (attrs.${key} or { })
// { ${system} = ret.${key}; };
}
;
in
builtins.foldl' op attrs (builtins.attrNames ret);
ret = f system;
in
builtins.foldl' op { }
(systems
++ # add the current system if --impure is used
(if builtins?currentSystem then
if builtins.elem builtins.currentSystem systems
then []
else [ builtins.currentSystem ]
else
[]))
;
builtins.foldl' (
attrs: key:
attrs
// {
${key} = (attrs.${key} or { }) // {
${system} = ret.${key};
};
}
) attrs (builtins.attrNames ret)
);
# Applies a merge operation accross systems.
eachSystemOp =
op: systems: f:
builtins.foldl' (op f) { } (
if
!builtins ? currentSystem || builtins.elem builtins.currentSystem systems
then
systems
else
# Add the current system if the --impure flag is used.
systems ++ [ builtins.currentSystem ]
);
# Merely provides the system argument to the function.
#
# Unlike eachSystem, this function does not inject the `${system}` key.
eachSystemPassThrough = eachSystemOp (
f: attrs: system:
attrs // (f system)
);
# eachSystemMap using defaultSystems
eachDefaultSystemMap = eachSystemMap defaultSystems;
@ -198,9 +212,11 @@ let
check-utils
defaultSystems
eachDefaultSystem
eachSystem
eachDefaultSystemMap
eachDefaultSystemPassThrough
eachSystem
eachSystemMap
eachSystemPassThrough
filterPackages
flattenTree
meld