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

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
This commit is contained in:
NAHO 2024-09-11 20:13:13 +02:00
parent 58351e4428
commit fa06cc1b3d
No known key found for this signature in database
GPG key ID: 229CB671D09B95F5
2 changed files with 65 additions and 29 deletions

View file

@ -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

71
lib.nix
View file

@ -26,36 +26,47 @@ let
# eachSystem using defaultSystems
eachDefaultSystem = eachSystem defaultSystems;
# eachSystemPassThrough using defaultSystems
eachDefaultSystemPassThrough = eachSystemPassThrough defaultSystems;
# Builds a map from <attr>=value to <attr>.<system>=value for each system.
eachSystem =
systems: f:
builtins.foldl'
(
# Merge outputs for each system.
attrs: system:
let
ret = f system;
in
builtins.foldl' (
attrs: key:
attrs
// {
${key} = (attrs.${key} or { }) // {
${system} = ret.${key};
};
}
) attrs (builtins.attrNames ret)
)
{ }
(
if
!builtins ? currentSystem || builtins.elem builtins.currentSystem systems
then
systems
else
# Add the current system if the --impure flag is used.
systems ++ [ builtins.currentSystem ]
);
eachSystem = eachSystemOp (
# Merge outputs for each system.
f: attrs: system:
let
ret = f system;
in
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;
@ -202,8 +213,10 @@ let
defaultSystems
eachDefaultSystem
eachDefaultSystemMap
eachDefaultSystemPassThrough
eachSystem
eachSystemMap
eachSystemPassThrough
filterPackages
flattenTree
meld