diff --git a/lib/inventory/build-inventory/default.nix b/lib/inventory/build-inventory/default.nix index b70cea859..855118a3b 100644 --- a/lib/inventory/build-inventory/default.nix +++ b/lib/inventory/build-inventory/default.nix @@ -141,9 +141,9 @@ let throw "Module doesn't have role: '${role}'. Path: ${path} not found." ) inverseRoles.${machineName} or [ ]; - roleServiceConfigs = builtins.map ( - role: serviceConfig.roles.${role}.config or { } - ) inverseRoles.${machineName} or [ ]; + roleServiceConfigs = builtins.filter (m: m != { }) ( + builtins.map (role: serviceConfig.roles.${role}.config or { }) inverseRoles.${machineName} or [ ] + ); customImports = map (s: "${directory}/${s}") ( globalImports ++ machineImports ++ roleServiceImports @@ -155,14 +155,19 @@ let ++ [ { imports = [ clan-core.clanModules.${serviceName} ] ++ roleModules ++ customImports; - config.clan.${serviceName} = lib.mkMerge ( - [ - globalConfig - machineServiceConfig - ] - ++ roleServiceConfigs - ); } + (lib.optionalAttrs (globalConfig != { } || machineServiceConfig != { } || roleServiceConfigs != [ ]) + { + config.clan.${serviceName} = lib.mkMerge ( + [ + globalConfig + machineServiceConfig + ] + ++ roleServiceConfigs + ); + } + ) + { config.clan.inventory.services.${serviceName}.${instanceName} = { roles = resolvedRoles; diff --git a/lib/inventory/build-inventory/interface.nix b/lib/inventory/build-inventory/interface.nix index d4b26338d..dd20a676a 100644 --- a/lib/inventory/build-inventory/interface.nix +++ b/lib/inventory/build-inventory/interface.nix @@ -13,6 +13,20 @@ let type = types.nullOr types.str; }; }; + metaOptionsWith = name: { + name = lib.mkOption { + type = types.str; + default = name; + }; + description = lib.mkOption { + default = null; + type = types.nullOr types.str; + }; + icon = lib.mkOption { + default = null; + type = types.nullOr types.str; + }; + }; moduleConfig = lib.mkOption { default = { }; @@ -64,26 +78,30 @@ in machines = lib.mkOption { default = { }; type = types.attrsOf ( - types.submodule { - options = { - inherit (metaOptions) name description icon; - tags = lib.mkOption { + types.submodule ( + { name, ... }: + { + options = { + inherit (metaOptionsWith name) name description icon; - default = [ ]; - apply = lib.unique; - type = types.listOf types.str; + tags = lib.mkOption { + + default = [ ]; + apply = lib.unique; + type = types.listOf types.str; + }; + system = lib.mkOption { + default = null; + type = types.nullOr types.str; + }; + deploy.targetHost = lib.mkOption { + description = "Configuration for the deployment of the machine"; + default = null; + type = types.nullOr types.str; + }; }; - system = lib.mkOption { - default = null; - type = types.nullOr types.str; - }; - deploy.targetHost = lib.mkOption { - description = "Configuration for the deployment of the machine"; - default = null; - type = types.nullOr types.str; - }; - }; - } + } + ) ); }; @@ -91,38 +109,41 @@ in default = { }; type = types.attrsOf ( types.attrsOf ( - types.submodule { - options.meta = metaOptions; - options.imports = importsOption; - options.config = moduleConfig; - options.machines = lib.mkOption { - default = { }; - type = types.attrsOf ( - types.submodule { - options.imports = importsOption; - options.config = moduleConfig; - } - ); - }; - options.roles = lib.mkOption { - default = { }; - type = types.attrsOf ( - types.submodule { - options.machines = lib.mkOption { - default = [ ]; - type = types.listOf types.str; - }; - options.tags = lib.mkOption { - default = [ ]; - apply = lib.unique; - type = types.listOf types.str; - }; - options.config = moduleConfig; - options.imports = importsOption; - } - ); - }; - } + types.submodule ( + { name, ... }: + { + options.meta = metaOptionsWith name; + options.imports = importsOption; + options.config = moduleConfig; + options.machines = lib.mkOption { + default = { }; + type = types.attrsOf ( + types.submodule { + options.imports = importsOption; + options.config = moduleConfig; + } + ); + }; + options.roles = lib.mkOption { + default = { }; + type = types.attrsOf ( + types.submodule { + options.machines = lib.mkOption { + default = [ ]; + type = types.listOf types.str; + }; + options.tags = lib.mkOption { + default = [ ]; + apply = lib.unique; + type = types.listOf types.str; + }; + options.config = moduleConfig; + options.imports = importsOption; + } + ); + }; + } + ) ) ); }; diff --git a/lib/inventory/tests/default.nix b/lib/inventory/tests/default.nix index 3ab5a5ee2..dbc60448b 100644 --- a/lib/inventory/tests/default.nix +++ b/lib/inventory/tests/default.nix @@ -117,8 +117,8 @@ in not_used_machine = builtins.length configs.not_used_machine; }; expected = { - client_1_machine = 4; - client_2_machine = 4; + client_1_machine = 5; + client_2_machine = 5; not_used_machine = 2; }; };