mirror of
https://github.com/hercules-ci/flake-parts.git
synced 2025-03-28 02:37:12 +00:00
Merge pull request #48 from hercules-ci/moduleWithSystem
Add `moduleWithSystem` flake module parameter
This commit is contained in:
commit
89cce5852d
3 changed files with 51 additions and 0 deletions
18
README.md
18
README.md
|
@ -66,6 +66,24 @@ See [flake.parts](https://flake.parts/options.html)
|
|||
|
||||
- `getSystem`: function from system string to the `config` of the appropriate `perSystem`.
|
||||
|
||||
- `moduleWithSystem`: function that brings the `perSystem` module arguments.
|
||||
This allows a module to reference the defining flake without introducing
|
||||
global variables (which may conflict).
|
||||
|
||||
```nix
|
||||
{ moduleWithSystem, ... }:
|
||||
{
|
||||
nixosModules.default = moduleWithSystem (
|
||||
perSystem@{ config }: # NOTE: only explicit params will be in perSystem
|
||||
nixos@{ ... }:
|
||||
{
|
||||
services.foo.package = perSystem.config.packages.foo;
|
||||
imports = [ ./nixos-foo.nix ];
|
||||
}
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
- `withSystem`: enter the scope of a system. Worked example:
|
||||
|
||||
```nix
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
./modules/devShells.nix
|
||||
./modules/flake.nix
|
||||
./modules/legacyPackages.nix
|
||||
./modules/moduleWithSystem.nix
|
||||
./modules/nixosConfigurations.nix
|
||||
./modules/nixosModules.nix
|
||||
./modules/nixpkgs.nix
|
||||
|
|
32
modules/moduleWithSystem.nix
Normal file
32
modules/moduleWithSystem.nix
Normal file
|
@ -0,0 +1,32 @@
|
|||
{ config, lib, withSystem, ... }:
|
||||
{
|
||||
config = {
|
||||
_module.args = {
|
||||
moduleWithSystem =
|
||||
module:
|
||||
|
||||
{ config, ... }:
|
||||
let
|
||||
system =
|
||||
config._module.args.system or
|
||||
config._module.args.pkgs.stdenv.hostPlatform.system or
|
||||
(throw "moduleWithSystem: Could not determine the configuration's system parameter for this module system application.");
|
||||
|
||||
allArgs = withSystem system (args: args);
|
||||
|
||||
lazyArgsPerParameter = f: builtins.mapAttrs
|
||||
(k: v: allArgs.${k} or (throw "moduleWithSystem: module argument `${k}` does not exist."))
|
||||
(builtins.functionArgs f);
|
||||
|
||||
# Use reflection to make the call lazy in the argument.
|
||||
# Restricts args to the ones declared.
|
||||
callLazily = f: a: f (lazyArgsPerParameter f);
|
||||
in
|
||||
{
|
||||
imports = [
|
||||
(callLazily module allArgs)
|
||||
];
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
Loading…
Add table
Reference in a new issue