From d99dfbcedd085ade0f73347bcc5d2344b1627327 Mon Sep 17 00:00:00 2001 From: Johannes Kirschbauer Date: Mon, 15 Jul 2024 16:38:54 +0200 Subject: [PATCH] Inventory: add global imports --- lib/build-clan/default.nix | 5 ++++- lib/inventory/build-inventory/default.nix | 16 ++++++++++++++-- lib/inventory/build-inventory/interface.nix | 15 ++++++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/lib/build-clan/default.nix b/lib/build-clan/default.nix index 35401dd1c..82c8f9c36 100644 --- a/lib/build-clan/default.nix +++ b/lib/build-clan/default.nix @@ -106,7 +106,10 @@ let # map from machine name to service configuration # { ${machineName} :: Config } - serviceConfigs = buildInventory mergedInventory; + serviceConfigs = buildInventory { + inventory = mergedInventory; + inherit directory; + }; machinesDirs = lib.optionalAttrs (builtins.pathExists "${directory}/machines") ( builtins.readDir (directory + /machines) diff --git a/lib/inventory/build-inventory/default.nix b/lib/inventory/build-inventory/default.nix index c0e9852da..88fe066df 100644 --- a/lib/inventory/build-inventory/default.nix +++ b/lib/inventory/build-inventory/default.nix @@ -1,7 +1,7 @@ # 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, clan-core }: -inventory: +{ inventory, directory }: let machines = machinesFromInventory inventory; @@ -72,6 +72,12 @@ let machineServiceConfig = (serviceConfig.machines.${machineName} or { }).config or { }; globalConfig = serviceConfig.config or { }; + globalImports = serviceConfig.imports or [ ]; + machineImports = serviceConfig.machines.${machineName}.imports or [ ]; + roleServiceImports = builtins.foldl' ( + acc: role: acc ++ serviceConfig.roles.${role}.imports or [ ] + ) [ ] inverseRoles.${machineName} or [ ]; + # TODO: maybe optimize this dont lookup the role in inverse roles. Imports are not lazy roleModules = builtins.map ( role: @@ -87,12 +93,18 @@ let roleServiceConfigs = builtins.map ( role: serviceConfig.roles.${role}.config or { } ) inverseRoles.${machineName} or [ ]; + dbg = v: lib.traceSeq v v; + + customImports = map (s: "${directory}/${s}") ( + globalImports ++ machineImports ++ roleServiceImports + ); in + if isInService then acc2 ++ [ { - imports = [ clan-core.clanModules.${moduleName} ] ++ roleModules; + imports = dbg ([ clan-core.clanModules.${moduleName} ] ++ roleModules ++ customImports); config.clan.${moduleName} = lib.mkMerge ( [ globalConfig diff --git a/lib/inventory/build-inventory/interface.nix b/lib/inventory/build-inventory/interface.nix index 817e380cd..34a3c1e89 100644 --- a/lib/inventory/build-inventory/interface.nix +++ b/lib/inventory/build-inventory/interface.nix @@ -39,6 +39,12 @@ let default = { }; type = t.attrsOf t.anything; }; + + importsOption = lib.mkOption { + default = [ ]; + type = t.listOf t.str; + # apply = map (pathOrString: "${pathOrString}"); + }; in { options = { @@ -76,10 +82,16 @@ in t.attrsOf ( t.submodule { options.meta = metaOptions; + options.imports = importsOption; options.config = moduleConfig; options.machines = lib.mkOption { default = { }; - type = t.attrsOf (t.submodule { options.config = moduleConfig; }); + type = t.attrsOf ( + t.submodule { + options.imports = importsOption; + options.config = moduleConfig; + } + ); }; options.roles = lib.mkOption { default = { }; @@ -95,6 +107,7 @@ in type = t.listOf tagRef; }; options.config = moduleConfig; + options.imports = importsOption; } ); };