Inventory: add global imports

This commit is contained in:
Johannes Kirschbauer 2024-07-15 16:38:54 +02:00
parent c80e2538c4
commit 9afed2295d
Signed by: hsjobeki
SSH Key Fingerprint: SHA256:vX3utDqig7Ph5L0JPv87ZTPb/w7cMzREKVZzzLFg9qU
3 changed files with 32 additions and 4 deletions

View File

@ -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)

View File

@ -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

View File

@ -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;
}
);
};