Skip to content

Commit

Permalink
try to fix perSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
phaer committed Jan 24, 2025
1 parent d9d31d0 commit 27fd6fe
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
31 changes: 26 additions & 5 deletions docs/folder-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,37 @@ For the following folder names, we also map them to the following outputs:

These and other unrecognized types also exposed as `modules.<type>.<name>`.

If a module is wrapped in a function that accepts one (or more) of the following arguments:
#### Wrap modules to refer to the flake where they are defined.

Conventionally nixos module arguments are populated when evaluating a nixos system.
This means, module arguments such as `flake`, `inputs` and others most often refer to the flake
where that module is consumed, which might not be the one where it is defined. This means that
in order to import and use a module from another flake, we would have to manually add all of their
inputs to our own flake.

In order to avoid that, we ship a mechanism which allows modules to refer to the flake in which
they are defined: If a module is wrapped in a function that accepts one (or more) of the following
arguments:

* `flake`
* `inputs`
* `perSystem`

Then that function is called before exposing the module as an output.
This allows modules to refer to the flake where it is defined, while the module arguments
`flake`, `inputs` and `perSystem` refer to the flake where the module is consumed. Those can
be but do not need to be the same flake.
Then that function is called before exposing the module as an output. `flake` and `inputs`
directly contain the current flake and its inputs. Requesting `perSystem` on the other hand
only acts as a flag which changes the module argument `perSystem` to refer to the defining flake.

``` nix
{ inputs, perSystem, ...}:
{ pkgs, perSystem }: {
imports = [ inputs.some-input.nixosModules.some-module ];
config.environment.systemPackages = [
perSystem.some-input.some-package
];
}
```


### `package.nix`, `formatter.nix`, `packages/<pname>(.nix|/default.nix)`

Expand Down
18 changes: 15 additions & 3 deletions lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ let
perSystemModule =
{ pkgs, ... }:
{
_module.args.perSystem = systemArgs.${pkgs.system}.perSystem;
_module.args.perSystem = lib.mkDefault systemArgs.${pkgs.system}.perSystem;
};

home-manager =
Expand Down Expand Up @@ -334,7 +334,7 @@ let

publisherArgs = {
inherit flake inputs;
inherit (flake) perSystem;
perSystem = true;
};

expectsPublisherArgs =
Expand All @@ -354,7 +354,19 @@ let
module = import modulePath;
in
if expectsPublisherArgs module then
lib.setDefaultModuleLocation modulePath (module publisherArgs)
{
_file = modulePath;
imports = [
(module publisherArgs)
(
{ pkgs, ... }:
{
key = "persystem-arg-in-blueprint";
_module.args.perSystem = systemArgs.${pkgs.system}.perSystem;
}
)
];
}
else
modulePath;

Expand Down

0 comments on commit 27fd6fe

Please sign in to comment.