From d2c068c4a1e8bcb3e0695926f49625e4dcdc8581 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Tue, 15 Apr 2025 15:02:37 +0200 Subject: [PATCH] fixup(buildClan): split up the inner-module into: {forSystem, forName} I am preparing this such that we can move the forName modules into the defaults for the deferred module outputs and the forSystem modules are added later and only for the 'configsPerSystem' where we actually need the system modules --- lib/build-clan/inner-module.nix | 61 --------------------- lib/build-clan/machineModules/forName.nix | 28 ++++++++++ lib/build-clan/machineModules/forSystem.nix | 26 +++++++++ lib/build-clan/module.nix | 56 ++++++++++++++++--- 4 files changed, 101 insertions(+), 70 deletions(-) delete mode 100644 lib/build-clan/inner-module.nix create mode 100644 lib/build-clan/machineModules/forName.nix create mode 100644 lib/build-clan/machineModules/forSystem.nix diff --git a/lib/build-clan/inner-module.nix b/lib/build-clan/inner-module.nix deleted file mode 100644 index 830e21716..000000000 --- a/lib/build-clan/inner-module.nix +++ /dev/null @@ -1,61 +0,0 @@ -{ - system, - name, - pkgs, - clanConfiguration, -}: -{ - _class, - lib, - ... -}: -let - inherit (clanConfiguration) directory machines pkgsForSystem; - inherit (clanConfiguration.clanInternals) inventoryClass; -in -{ - imports = [ - { - imports = builtins.filter builtins.pathExists ( - [ - "${directory}/machines/${name}/configuration.nix" - ] - ++ lib.optionals (_class == "nixos") [ - "${directory}/machines/${name}/hardware-configuration.nix" - "${directory}/machines/${name}/disko.nix" - ] - ); - } - (lib.optionalAttrs (_class == "nixos") { - imports = (inventoryClass.machines.${name}.machineImports or [ ]); - - config = { - clan.core.settings = { - inherit (clanConfiguration.inventory.meta) name icon; - - inherit directory; - machine = { - inherit name; - }; - }; - # Inherited from clan wide settings - # TODO: remove these - }; - }) - ( - { - networking.hostName = lib.mkDefault name; - - # For vars we need to override the system so we run vars - # generators on the machine that runs `clan vars generate`. If a - # users is using the `pkgsForSystem`, we don't set - # nixpkgs.hostPlatform it would conflict with the `nixpkgs.pkgs` - # option. - nixpkgs.hostPlatform = lib.mkIf (system != null && (pkgsForSystem system) != null) ( - lib.mkForce system - ); - } - // lib.optionalAttrs (pkgs != null) { nixpkgs.pkgs = lib.mkForce pkgs; } - ) - ]; -} diff --git a/lib/build-clan/machineModules/forName.nix b/lib/build-clan/machineModules/forName.nix new file mode 100644 index 000000000..dd533d2e0 --- /dev/null +++ b/lib/build-clan/machineModules/forName.nix @@ -0,0 +1,28 @@ +{ + name, + directory, +}: +{ + _class, + lib, + ... +}: +{ + imports = [ + { + imports = builtins.filter builtins.pathExists ( + [ + "${directory}/machines/${name}/configuration.nix" + ] + ++ lib.optionals (_class == "nixos") [ + "${directory}/machines/${name}/hardware-configuration.nix" + "${directory}/machines/${name}/disko.nix" + ] + ); + } + # TODO: move into nixos modules + ({ + networking.hostName = lib.mkDefault name; + }) + ]; +} diff --git a/lib/build-clan/machineModules/forSystem.nix b/lib/build-clan/machineModules/forSystem.nix new file mode 100644 index 000000000..8b3601e77 --- /dev/null +++ b/lib/build-clan/machineModules/forSystem.nix @@ -0,0 +1,26 @@ +{ + pkgs, + pkgsForSystem, + system, +}: +{ + lib, + ... +}: +{ + imports = [ + ( + { + # For vars we need to override the system so we run vars + # generators on the machine that runs `clan vars generate`. If a + # users is using the `pkgsForSystem`, we don't set + # nixpkgs.hostPlatform it would conflict with the `nixpkgs.pkgs` + # option. + nixpkgs.hostPlatform = lib.mkIf (system != null && (pkgsForSystem system) != null) ( + lib.mkForce system + ); + } + // lib.optionalAttrs (pkgs != null) { nixpkgs.pkgs = lib.mkForce pkgs; } + ) + ]; +} diff --git a/lib/build-clan/module.nix b/lib/build-clan/module.nix index 9102a28a2..f6aabfe6f 100644 --- a/lib/build-clan/module.nix +++ b/lib/build-clan/module.nix @@ -59,17 +59,25 @@ let nixpkgs.lib.nixosSystem { modules = let - innerModule = lib.modules.importApply ./inner-module.nix { + innerModule = lib.modules.importApply ./machineModules/forSystem.nix { inherit - system - name pkgs + pkgsForSystem + system ; - clanConfiguration = config; }; + staticModules = ( + lib.modules.importApply ./machineModules/forName.nix { + inherit + name + directory + ; + } + ); in [ (config.outputs.moduleForMachine.${name} or { }) + staticModules innerModule { config.clan.core.module = innerModule; @@ -90,13 +98,24 @@ let nix-darwin.lib.darwinSystem { modules = [ (config.outputs.moduleForMachine.${name} or { }) - (lib.modules.importApply ./inner-module.nix { + # We split the modules to reduce the number of dependencies + # This module only depends on the machine name + # and the directory + (lib.modules.importApply ./machineModules/forName.nix { inherit - system name - pkgs + directory + ; + }) + # This module depends on the system and pkgs + # It contains optional logic to override 'nixpkgs.pkgs' and 'nixpkgs.hostPlatform' + # and other 'system' related logic + (lib.modules.importApply ./machineModules/forSystem.nix { + inherit + pkgs + pkgsForSystem + system ; - clanConfiguration = config; }) ]; @@ -170,7 +189,26 @@ in # See: 'staticModules' in the moduleForMachine option # This is only necessary because clan.machines doesn't include all machines # There can other sources: i.e. inventory - (lib.mapAttrs (_: _: { }) config.inventory.machines) + (lib.mapAttrs ( + name: v: + ( + { _class, ... }: + { + imports = (v.machineImports or [ ]); + config = lib.optionalAttrs (_class == "nixos") { + clan.core.settings = { + inherit (config.inventory.meta) name icon; + + inherit directory; + machine = { + inherit name; + }; + }; + }; + + } + ) + ) inventoryClass.machines) # The user can define some machine config here # i.e. 'clan.machines.jon = ...'