Skip to content

Commit

Permalink
Merge pull request #119 from trueNAHO/lib-each-default-system-pass-th…
Browse files Browse the repository at this point in the history
…rough-each-system-pass-through-init
  • Loading branch information
zimbatm authored Sep 17, 2024
2 parents b1d9ab7 + fa06cc1 commit c1dfcf0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.


Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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
Expand Down
68 changes: 42 additions & 26 deletions lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -198,9 +212,11 @@ let
check-utils
defaultSystems
eachDefaultSystem
eachSystem
eachDefaultSystemMap
eachDefaultSystemPassThrough
eachSystem
eachSystemMap
eachSystemPassThrough
filterPackages
flattenTree
meld
Expand Down

0 comments on commit c1dfcf0

Please sign in to comment.