From 0af36d0a4dec5171d2b896f075bbceaabbac1c3e Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Thu, 8 May 2025 17:17:46 +0200 Subject: [PATCH 1/2] Refactor(inventory/modules): add support for local clan.modules and migrate all checks --- checks/dummy-inventory-test/default.nix | 52 +++++++++---------- clanServices/hello-world/tests/eval-tests.nix | 2 +- clanServices/hello-world/tests/vm/default.nix | 7 ++- lib/build-clan/module.nix | 3 +- lib/inventory/build-inventory/default.nix | 1 + .../distributed-service/inventory-adapter.nix | 5 +- .../distributed-service/tests/default.nix | 6 ++- 7 files changed, 41 insertions(+), 35 deletions(-) diff --git a/checks/dummy-inventory-test/default.nix b/checks/dummy-inventory-test/default.nix index e77ea70e1..6cebf5096 100644 --- a/checks/dummy-inventory-test/default.nix +++ b/checks/dummy-inventory-test/default.nix @@ -34,33 +34,33 @@ clanLib.test.makeTestClan { modules = { legacy-module = ./legacy-module; - new-service = { - _class = "clan.service"; - manifest.name = "new-service"; - roles.peer = { }; - perMachine = { - nixosModule = { - # This should be generated by: - # nix run .#generate-test-vars -- checks/dummy-inventory-test dummy-inventory-test - clan.core.vars.generators.new-service = { - files.not-a-secret = { - secret = false; - deploy = true; - }; - files.a-secret = { - secret = true; - deploy = true; - owner = "nobody"; - group = "users"; - mode = "0644"; - }; - script = '' - # This is a dummy script that does nothing - echo -n "not-a-secret" > $out/not-a-secret - echo -n "a-secret" > $out/a-secret - ''; - }; + }; + }; + modules.new-service = { + _class = "clan.service"; + manifest.name = "new-service"; + roles.peer = { }; + perMachine = { + nixosModule = { + # This should be generated by: + # nix run .#generate-test-vars -- checks/dummy-inventory-test dummy-inventory-test + clan.core.vars.generators.new-service = { + files.not-a-secret = { + secret = false; + deploy = true; }; + files.a-secret = { + secret = true; + deploy = true; + owner = "nobody"; + group = "users"; + mode = "0644"; + }; + script = '' + # This is a dummy script that does nothing + echo -n "not-a-secret" > $out/not-a-secret + echo -n "a-secret" > $out/a-secret + ''; }; }; }; diff --git a/clanServices/hello-world/tests/eval-tests.nix b/clanServices/hello-world/tests/eval-tests.nix index daed19a5c..cfcc82217 100644 --- a/clanServices/hello-world/tests/eval-tests.nix +++ b/clanServices/hello-world/tests/eval-tests.nix @@ -18,7 +18,7 @@ let }; # Register the module for the test - inventory.modules.hello-world = module; + modules.hello-world = module; # Use the module in the test inventory.instances = { diff --git a/clanServices/hello-world/tests/vm/default.nix b/clanServices/hello-world/tests/vm/default.nix index e5063c871..55d20136c 100644 --- a/clanServices/hello-world/tests/vm/default.nix +++ b/clanServices/hello-world/tests/vm/default.nix @@ -14,6 +14,9 @@ clanLib.test.makeTestClan { clan = { directory = ./.; + modules = { + hello-service = module; + }; inventory = { machines.peer1 = { }; @@ -21,10 +24,6 @@ clanLib.test.makeTestClan { module.name = "hello-service"; roles.peer.machines.peer1 = { }; }; - - modules = { - hello-service = module; - }; }; }; diff --git a/lib/build-clan/module.nix b/lib/build-clan/module.nix index 12b8eda88..19aafbc69 100644 --- a/lib/build-clan/module.nix +++ b/lib/build-clan/module.nix @@ -45,7 +45,8 @@ let inherit inventory directory; flakeInputs = config.self.inputs; prefix = config._prefix ++ [ "inventoryClass" ]; - localModuleSet = config.modules; + # TODO: remove inventory.modules, this is here for backwards compatibility + localModuleSet = inventory.modules // config.modules; } ); diff --git a/lib/inventory/build-inventory/default.nix b/lib/inventory/build-inventory/default.nix index bfb3705f0..8e3cb45ca 100644 --- a/lib/inventory/build-inventory/default.nix +++ b/lib/inventory/build-inventory/default.nix @@ -33,6 +33,7 @@ let distributedServices = clanLib.inventory.mapInstances { inherit (config) inventory; + inherit localModuleSet; inherit flakeInputs; prefix = prefix ++ [ "distributedServices" ]; }; diff --git a/lib/inventory/distributed-service/inventory-adapter.nix b/lib/inventory/distributed-service/inventory-adapter.nix index 65347918e..b2fa2de35 100644 --- a/lib/inventory/distributed-service/inventory-adapter.nix +++ b/lib/inventory/distributed-service/inventory-adapter.nix @@ -54,7 +54,7 @@ let ) } - To import a local module from 'inventory.modules' remove the 'input' attribute from the module definition + To import a local module from 'clan.modules' remove the 'input' attribute from the module definition Remove the following line from the module definition: ... @@ -81,6 +81,7 @@ in flakeInputs, # The clan inventory inventory, + localModuleSet, prefix ? [ ], }: let @@ -92,7 +93,7 @@ in let resolvedModule = resolveModule { moduleSpec = instance.module; - localModuleSet = inventory.modules; + inherit localModuleSet; inherit flakeInputs; }; diff --git a/lib/inventory/distributed-service/tests/default.nix b/lib/inventory/distributed-service/tests/default.nix index 0267dd9c1..8f2b42224 100644 --- a/lib/inventory/distributed-service/tests/default.nix +++ b/lib/inventory/distributed-service/tests/default.nix @@ -41,9 +41,13 @@ let callInventoryAdapter = inventoryModule: + let + inventory = evalInventory inventoryModule; + in clanLib.inventory.mapInstances { flakeInputs = flakeInputsFixture; - inventory = evalInventory inventoryModule; + inherit inventory; + localModuleSet = inventory.modules; }; in { From c5a28e26555fe063f473d6466c73a79c22012ac3 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Thu, 8 May 2025 17:38:12 +0200 Subject: [PATCH 2/2] fix: make sure legacyModules dont end up in localModules for inventory.instances --- lib/build-clan/module.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/build-clan/module.nix b/lib/build-clan/module.nix index 19aafbc69..c6290ed6e 100644 --- a/lib/build-clan/module.nix +++ b/lib/build-clan/module.nix @@ -46,7 +46,8 @@ let flakeInputs = config.self.inputs; prefix = config._prefix ++ [ "inventoryClass" ]; # TODO: remove inventory.modules, this is here for backwards compatibility - localModuleSet = inventory.modules // config.modules; + localModuleSet = + lib.filterAttrs (n: _: !inventory._legacyModules ? ${n}) inventory.modules // config.modules; } );