From 27fd6fed59ed7194ca01dfe3367f7230c2245483 Mon Sep 17 00:00:00 2001 From: phaer Date: Mon, 20 Jan 2025 13:21:39 +0100 Subject: [PATCH] try to fix perSystem --- docs/folder-structure.md | 31 ++++++++++++++++++++++++++----- lib/default.nix | 18 +++++++++++++++--- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/docs/folder-structure.md b/docs/folder-structure.md index def8189..58be8db 100644 --- a/docs/folder-structure.md +++ b/docs/folder-structure.md @@ -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..`. -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/(.nix|/default.nix)` diff --git a/lib/default.nix b/lib/default.nix index 00dd21b..b142e98 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -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 = @@ -334,7 +334,7 @@ let publisherArgs = { inherit flake inputs; - inherit (flake) perSystem; + perSystem = true; }; expectsPublisherArgs = @@ -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;