diff --git a/inventory.json b/inventory.json index 028bc4112..df0e96053 100644 --- a/inventory.json +++ b/inventory.json @@ -26,19 +26,19 @@ }, "roles": { "default": { - "imports": [], + "extraModules": [], "machines": ["test-inventory-machine"], "tags": [] } }, "config": {}, - "imports": [], + "extraModules": [], "machines": { "test-inventory-machine": { "config": { "packages": ["zed-editor"] }, - "imports": [] + "extraModules": [] } } }, @@ -51,19 +51,19 @@ "roles": { "default": { "config": {}, - "imports": [], + "extraModules": [], "machines": ["test-inventory-machine"], "tags": [] } }, "config": {}, - "imports": [], + "extraModules": [], "machines": { "test-inventory-machine": { "config": { "packages": ["chromium"] }, - "imports": [] + "extraModules": [] } } } @@ -78,19 +78,19 @@ "roles": { "default": { "config": {}, - "imports": [], + "extraModules": [], "machines": ["test-inventory-machine"], "tags": [] } }, "config": {}, - "imports": [], + "extraModules": [], "machines": { "test-inventory-machine": { "config": { "device": "/dev/null" }, - "imports": [] + "extraModules": [] } } } diff --git a/lib/build-clan/module.nix b/lib/build-clan/module.nix index 22b154a3b..b05dfdc85 100644 --- a/lib/build-clan/module.nix +++ b/lib/build-clan/module.nix @@ -223,6 +223,14 @@ in imports = [ # Merge the inventory file { inventory = inventoryLoaded; } + # TODO: Figure out why this causes infinite recursion + { + inventory.machines = lib.optionalAttrs (builtins.pathExists "${directory}/machines") ( + builtins.mapAttrs (_n: _v: { }) ( + (lib.filterAttrs (_: t: t == "directory") (builtins.readDir "${directory}/machines")) + ) + ); + } # Merge the meta attributes from the buildClan function { inventory.meta = if config.meta != null then config.meta else { }; } ]; diff --git a/lib/build-clan/tests.nix b/lib/build-clan/tests.nix index eb0250051..5bfe6aab5 100644 --- a/lib/build-clan/tests.nix +++ b/lib/build-clan/tests.nix @@ -111,7 +111,10 @@ in in { expr = builtins.attrNames result.nixosConfigurations; - expected = [ "test-inventory-machine" ]; + expected = [ + "test-backup" + "test-inventory-machine" + ]; }; test_buildClan_all_machines = diff --git a/lib/inventory/build-inventory/default.nix b/lib/inventory/build-inventory/default.nix index 855118a3b..d3d0e3ec0 100644 --- a/lib/inventory/build-inventory/default.nix +++ b/lib/inventory/build-inventory/default.nix @@ -145,7 +145,7 @@ let builtins.map (role: serviceConfig.roles.${role}.config or { }) inverseRoles.${machineName} or [ ] ); - customImports = map (s: "${directory}/${s}") ( + customImports = map (s: if builtins.typeOf s == "string" then "${directory}/${s}" else s) ( globalImports ++ machineImports ++ roleServiceImports ); in diff --git a/lib/inventory/build-inventory/interface.nix b/lib/inventory/build-inventory/interface.nix index dd20a676a..d505c2fc0 100644 --- a/lib/inventory/build-inventory/interface.nix +++ b/lib/inventory/build-inventory/interface.nix @@ -33,13 +33,15 @@ let type = types.attrsOf types.anything; }; - importsOption = lib.mkOption { + extraModulesOption = lib.mkOption { description = '' - List of imported '.nix' files. + List of imported '.nix' expressions. - Each filename must be a string and is interpreted relative to the 'directory' passed to buildClan. + Strings are interpreted relative to the 'directory' passed to buildClan. The import only happens if the machine is part of the service or role. + Other types are passed through to the nixos configuration. + ## Example To import the `special.nix` file @@ -55,13 +57,18 @@ let ```nix { - imports = [ "modules/special.nix" ]; + extraModules = [ "modules/special.nix" ]; } ``` ''; default = [ ]; - type = types.listOf types.str; + type = types.listOf ( + types.oneOf [ + types.str + types.anything + ] + ); }; in { @@ -113,13 +120,13 @@ in { name, ... }: { options.meta = metaOptionsWith name; - options.imports = importsOption; + options.extraModules = extraModulesOption; options.config = moduleConfig; options.machines = lib.mkOption { default = { }; type = types.attrsOf ( types.submodule { - options.imports = importsOption; + options.extraModules = extraModulesOption; options.config = moduleConfig; } ); @@ -138,7 +145,7 @@ in type = types.listOf types.str; }; options.config = moduleConfig; - options.imports = importsOption; + options.extraModules = extraModulesOption; } ); };