From d9da723d5dbcd45fb67379e96e9b9d37d12ca227 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Thu, 26 Jun 2025 09:46:26 +0200 Subject: [PATCH] Refactor(build-inventory): remove build-inventory in favor of simple and expressive evalModules --- lib/modules/clan/default.nix | 4 - lib/modules/default.nix | 5 +- lib/modules/eval-docs.nix | 4 +- lib/modules/function-adapter.nix | 3 +- lib/modules/inventory/default.nix | 1 - lib/modules/inventory/flake-module.nix | 2 + lib/modules/inventory/tests/default.nix | 101 +++++++++++++----------- lib/modules/inventoryClass/default.nix | 49 ------------ 8 files changed, 64 insertions(+), 105 deletions(-) delete mode 100644 lib/modules/inventoryClass/default.nix diff --git a/lib/modules/clan/default.nix b/lib/modules/clan/default.nix index 975a584be..1681e19a4 100644 --- a/lib/modules/clan/default.nix +++ b/lib/modules/clan/default.nix @@ -1,8 +1,4 @@ -{ clanLib }: { - _module.args = { - inherit clanLib; - }; imports = [ ./module.nix ./interface.nix diff --git a/lib/modules/default.nix b/lib/modules/default.nix index e27c89022..f3af36ece 100644 --- a/lib/modules/default.nix +++ b/lib/modules/default.nix @@ -8,8 +8,11 @@ }: { flakePartsModule = { + _module.args = { + inherit clanLib; + }; imports = [ - (lib.modules.importApply ./clan/default.nix { inherit clanLib; }) + ./clan/default.nix ]; }; diff --git a/lib/modules/eval-docs.nix b/lib/modules/eval-docs.nix index b1dfc9e1b..5fed5e09c 100644 --- a/lib/modules/eval-docs.nix +++ b/lib/modules/eval-docs.nix @@ -5,9 +5,11 @@ }: let eval = lib.evalModules { + # TODO: Move this into a 'classForMachines' or something + # @enzime why do we need this here? class = "nixos"; modules = [ - (import ./clan/default.nix { inherit clanLib; }) + clanLib.buildClanModule.flakePartsModule ]; }; evalDocs = pkgs.nixosOptionsDoc { diff --git a/lib/modules/function-adapter.nix b/lib/modules/function-adapter.nix index 0f8742091..6380cead3 100644 --- a/lib/modules/function-adapter.nix +++ b/lib/modules/function-adapter.nix @@ -10,6 +10,7 @@ module: (lib.evalModules { specialArgs = { + inherit (clan-core) clanLib; inherit self clan-core @@ -18,7 +19,7 @@ module: ; }; modules = [ - (import ./clan/default.nix { inherit (clan-core) clanLib; }) + ./clan/default.nix module { inherit specialArgs; diff --git a/lib/modules/inventory/default.nix b/lib/modules/inventory/default.nix index b01368955..9fcb8be51 100644 --- a/lib/modules/inventory/default.nix +++ b/lib/modules/inventory/default.nix @@ -4,7 +4,6 @@ let in { inherit (services) evalClanService mapInstances resolveModule; - inherit (import ../inventoryClass { inherit lib clanLib; }) buildInventory; interface = { _file = "clanLib.inventory.interface"; imports = [ diff --git a/lib/modules/inventory/flake-module.nix b/lib/modules/inventory/flake-module.nix index 34b76890a..1c62a0cf0 100644 --- a/lib/modules/inventory/flake-module.nix +++ b/lib/modules/inventory/flake-module.nix @@ -57,6 +57,8 @@ in legacyPackages.evalTests-inventory = import ./tests { inherit lib; clan-core = self; + inherit (self) clanLib; + inherit (self.inputs) nix-darwin; }; checks = { diff --git a/lib/modules/inventory/tests/default.nix b/lib/modules/inventory/tests/default.nix index f950be65e..2c00ff92c 100644 --- a/lib/modules/inventory/tests/default.nix +++ b/lib/modules/inventory/tests/default.nix @@ -1,18 +1,28 @@ -{ clan-core, lib, ... }: +{ + clan-core, + nix-darwin, + lib, + clanLib, +}: let - inventory = ( - import ../../inventoryClass/default.nix { - inherit lib; - clanLib = clan-core.clanLib; - } - ); - inherit (inventory) buildInventory; + # TODO: Unify these tests with clan tests + clan = + m: + lib.evalModules { + specialArgs = { inherit clan-core nix-darwin clanLib; }; + modules = [ + ../../clan/default.nix + { + self = { }; + } + m + ]; + }; in { test_inventory_a = let - compiled = buildInventory { - flakeInputs = { }; + eval = clan { inventory = { machines = { A = { }; @@ -28,10 +38,11 @@ in }; in { + inherit eval; expr = { legacyModule = lib.filterAttrs ( name: _: name == "isClanModule" - ) compiled.machines.A.compiledServices.legacyModule; + ) eval.config.clanInternals.inventoryClass.machines.A.compiledServices.legacyModule; }; expected = { legacyModule = { @@ -41,24 +52,21 @@ in test_inventory_empty = let - compiled = buildInventory { - flakeInputs = { }; + eval = clan { inventory = { }; directory = ./.; }; in { # Empty inventory should return an empty module - expr = compiled.machines; + expr = eval.config.clanInternals.inventoryClass.machines; expected = { }; }; test_inventory_role_resolve = let - compiled = buildInventory { - flakeInputs = { }; + eval = clan { directory = ./.; inventory = { - modules = clan-core.clanModules; services = { borgbackup.instance_1 = { roles.server.machines = [ "backup_server" ]; @@ -78,10 +86,17 @@ in in { expr = { - m1 = (compiled.machines."backup_server").compiledServices.borgbackup.matchedRoles; - m2 = (compiled.machines."client_1_machine").compiledServices.borgbackup.matchedRoles; - m3 = (compiled.machines."client_2_machine").compiledServices.borgbackup.matchedRoles; - inherit ((compiled.machines."client_2_machine").compiledServices.borgbackup) + m1 = + (eval.config.clanInternals.inventoryClass.machines."backup_server") + .compiledServices.borgbackup.matchedRoles; + m2 = + (eval.config.clanInternals.inventoryClass.machines."client_1_machine") + .compiledServices.borgbackup.matchedRoles; + m3 = + (eval.config.clanInternals.inventoryClass.machines."client_2_machine") + .compiledServices.borgbackup.matchedRoles; + inherit + ((eval.config.clanInternals.inventoryClass.machines."client_2_machine").compiledServices.borgbackup) resolvedRolesPerInstance ; }; @@ -113,11 +128,9 @@ in }; test_inventory_tag_resolve = let - configs = buildInventory { - flakeInputs = { }; + eval = clan { directory = ./.; inventory = { - modules = clan-core.clanModules; services = { borgbackup.instance_1 = { roles.client.tags = [ "backup" ]; @@ -136,7 +149,8 @@ in }; in { - expr = configs.machines.client_1_machine.compiledServices.borgbackup.resolvedRolesPerInstance; + expr = + eval.config.clanInternals.inventoryClass.machines.client_1_machine.compiledServices.borgbackup.resolvedRolesPerInstance; expected = { instance_1 = { client = { @@ -154,11 +168,9 @@ in test_inventory_multiple_roles = let - configs = buildInventory { - flakeInputs = { }; + eval = clan { directory = ./.; inventory = { - modules = clan-core.clanModules; services = { borgbackup.instance_1 = { roles.client.machines = [ "machine_1" ]; @@ -172,7 +184,8 @@ in }; in { - expr = configs.machines.machine_1.compiledServices.borgbackup.matchedRoles; + expr = + eval.config.clanInternals.inventoryClass.machines.machine_1.compiledServices.borgbackup.matchedRoles; expected = [ "client" "server" @@ -181,11 +194,9 @@ in test_inventory_module_doesnt_exist = let - configs = buildInventory { - flakeInputs = { }; + eval = clan { directory = ./.; inventory = { - modules = clan-core.clanModules; services = { fanatasy.instance_1 = { roles.default.machines = [ "machine_1" ]; @@ -198,8 +209,8 @@ in }; in { - inherit configs; - expr = configs.machines.machine_1.machineImports; + inherit eval; + expr = eval.config.clanInternals.inventoryClass.machines.machine_1.machineImports; expectedError = { type = "ThrownError"; msg = "ClanModule not found*"; @@ -208,11 +219,9 @@ in test_inventory_role_doesnt_exist = let - configs = buildInventory { - flakeInputs = { }; + eval = clan { directory = ./.; inventory = { - modules = clan-core.clanModules; services = { borgbackup.instance_1 = { roles.roleXYZ.machines = [ "machine_1" ]; @@ -225,8 +234,8 @@ in }; in { - inherit configs; - expr = configs.machines.machine_1.machineImports; + inherit eval; + expr = eval.config.clanInternals.inventoryClass.machines.machine_1.machineImports; expectedError = { type = "ThrownError"; msg = ''Roles \["roleXYZ"\] are not defined in the service borgbackup''; @@ -236,11 +245,9 @@ in # So the lib.warn is turned into abort test_inventory_tag_doesnt_exist = let - configs = buildInventory { - flakeInputs = { }; + eval = clan { directory = ./.; inventory = { - modules = clan-core.clanModules; services = { borgbackup.instance_1 = { roles.client.machines = [ "machine_1" ]; @@ -256,7 +263,7 @@ in }; in { - expr = configs.machines.machine_1.machineImports; + expr = eval.config.clanInternals.inventoryClass.machines.machine_1.machineImports; expectedError = { type = "Error"; # TODO: Add warning matching in nix-unit @@ -265,11 +272,9 @@ in }; test_inventory_disabled_service = let - configs = buildInventory { - flakeInputs = { }; + eval = clan { directory = ./.; inventory = { - modules = clan-core.clanModules; services = { borgbackup.instance_1 = { enabled = false; @@ -285,10 +290,10 @@ in }; in { - inherit configs; + inherit eval; expr = builtins.filter ( v: v != { } && !v.clan.inventory.assertions ? "alive.assertion.inventory" - ) configs.machines.machine_1.machineImports; + ) eval.config.clanInternals.inventoryClass.machines.machine_1.machineImports; expected = [ ]; }; } diff --git a/lib/modules/inventoryClass/default.nix b/lib/modules/inventoryClass/default.nix deleted file mode 100644 index ceeef7711..000000000 --- a/lib/modules/inventoryClass/default.nix +++ /dev/null @@ -1,49 +0,0 @@ -# Generate partial NixOS configurations for every machine in the inventory -# This function is responsible for generating the module configuration for every machine in the inventory. -{ lib, clanLib }: -let - /* - Returns a set with NixOS configuration for every machine in the inventory. - - machinesFromInventory :: Inventory -> { ${machine_name} :: NixOSConfiguration } - */ - buildInventory = - { - inventory, - directory, - flakeInputs, - prefix ? [ ], - localModuleSet ? { }, - }: - (lib.evalModules { - # TODO: move clanLib from specialArgs to options - specialArgs = { - inherit clanLib; - }; - modules = [ - ./builder/default.nix - (lib.modules.importApply ./service-list-from-inputs.nix { - inherit flakeInputs clanLib localModuleSet; - }) - { inherit directory inventory; } - ( - # config.distributedServices.allMachines.${name} or [ ]; - { config, ... }: - { - distributedServices = clanLib.inventory.mapInstances { - inherit (config) inventory; - inherit localModuleSet; - inherit flakeInputs; - prefix = prefix ++ [ "distributedServices" ]; - }; - machines = config.distributedServices.allMachines; - - } - ) - ./inventory-introspection.nix - ]; - }).config; -in -{ - inherit buildInventory; -}