Merge pull request 'feat(inventory/instances): add extendSettings as argument to perInstance, perMachine' (#3233) from hsjobeki/clan-core:role-settings into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3233
This commit is contained in:
hsjobeki
2025-04-08 12:46:44 +00:00
2 changed files with 30 additions and 54 deletions

View File

@@ -120,17 +120,9 @@ let
} }
``` ```
*/ */
makeExtensibleConfig =
f: args: # Extend evalModules result by a module, returns .config.
let extendEval = eval: m: (eval.extendModules { modules = lib.toList m; }).config;
makeModuleExtensible =
eval:
eval.config
// {
__functor = _self: m: makeModuleExtensible (eval.extendModules { modules = lib.toList m; });
};
in
makeModuleExtensible (f args);
/** /**
Apply the settings to the instance Apply the settings to the instance
@@ -146,20 +138,18 @@ let
machines = lib.mapAttrs (machineName: v: { machines = lib.mapAttrs (machineName: v: {
# TODO: evaluate the settings against the interface # TODO: evaluate the settings against the interface
# settings = (evalMachineSettings { inherit roleName instanceName; inherit (v) settings; }).config; # settings = (evalMachineSettings { inherit roleName instanceName; inherit (v) settings; }).config;
settings = ( settings =
makeExtensibleConfig evalMachineSettings { (evalMachineSettings {
inherit roleName instanceName machineName; inherit roleName instanceName machineName;
inherit (v) settings; inherit (v) settings;
} }).config;
);
}) role.machines; }) role.machines;
# TODO: evaluate the settings against the interface # TODO: evaluate the settings against the interface
settings = ( settings =
makeExtensibleConfig evalMachineSettings { (evalMachineSettings {
inherit roleName instanceName; inherit roleName instanceName;
inherit (role) settings; inherit (role) settings;
} }).config;
);
}) instance.roles; }) instance.roles;
in in
{ {
@@ -327,13 +317,17 @@ in
name = machineName; name = machineName;
roles = lib.attrNames (lib.filterAttrs (_n: v: v.machines ? ${machineName}) roles); roles = lib.attrNames (lib.filterAttrs (_n: v: v.machines ? ${machineName}) roles);
}; };
settings = ( settings =
makeExtensibleConfig evalMachineSettings { (evalMachineSettings {
inherit roleName instanceName machineName; inherit roleName instanceName machineName;
settings = settings =
config.instances.${instanceName}.roles.${roleName}.machines.${machineName}.settings or { }; config.instances.${instanceName}.roles.${roleName}.machines.${machineName}.settings or { };
} }).config;
); extendSettings = extendEval (evalMachineSettings {
inherit roleName instanceName machineName;
settings =
config.instances.${instanceName}.roles.${roleName}.machines.${machineName}.settings or { };
});
}; };
modules = [ v ]; modules = [ v ];
}).config; }).config;
@@ -396,6 +390,7 @@ in
in in
uniqueStrings (collectRoles machineScope.instances); uniqueStrings (collectRoles machineScope.instances);
}; };
# TODO: instances.<instanceName>.roles should contain all roles, even if nobody has the role
inherit (machineScope) instances; inherit (machineScope) instances;
# There are no machine settings. # There are no machine settings.
@@ -508,7 +503,8 @@ in
imports = [ imports = [
# For error backtracing. This module was produced by the 'perMachine' function # For error backtracing. This module was produced by the 'perMachine' function
# TODO: check if we need this or if it leads to better errors if we pass the underlying module locations # TODO: check if we need this or if it leads to better errors if we pass the underlying module locations
(lib.setDefaultModuleLocation "clan.service: ${config.manifest.name} - via perMachine" machineResult.nixosModule) # (lib.setDefaultModuleLocation "clan.service: ${config.manifest.name} - via perMachine" machineResult.nixosModule)
(machineResult.nixosModule)
] ++ instanceResults; ] ++ instanceResults;
}; };
} }

View File

@@ -23,16 +23,17 @@ let
{ {
instanceName, instanceName,
settings, settings,
extendSettings,
machine, machine,
roles, roles,
... ...
}: }:
let let
settings1 = settings { finalSettings = extendSettings {
# Sometimes we want to create a default settings set depending on the machine config. # Sometimes we want to create a default settings set depending on the machine config.
# Note: Other machines cannot depend on this settings. We must assign a new name to the settings. # Note: Other machines cannot depend on this settings. We must assign a new name to the settings.
# And thus the new value is not accessible by other machines. # And thus the new value is not accessible by other machines.
timeout = lib.mkOverride 10 "config.blah"; timeout = lib.mkOverride 10 "config.thing";
}; };
in in
{ {
@@ -46,12 +47,7 @@ let
# We are double vendoring the settings # We are double vendoring the settings
# To test that we can do it indefinitely # To test that we can do it indefinitely
vendoredSettings = settings1 { vendoredSettings = finalSettings;
# Sometimes we want to create a default settings set depending on the machine config.
# Note: Other machines cannot depend on this settings. We must assign a new name to the settings.
# And thus the new value is not accessible by other machines.
timeout = lib.mkOverride 5 "config.thing";
};
}; };
}; };
}; };
@@ -92,18 +88,6 @@ let
roles.peer.tags.all = { }; roles.peer.tags.all = { };
}; };
}; };
filterInternals = lib.filterAttrs (n: _v: !lib.hasPrefix "_" n);
# Replace internal attributes ('_' prefix)
# So we catch their presence but don't check the value
mapInternalsRecursive = lib.mapAttrsRecursive (
path: v:
let
name = lib.last path;
in
if !lib.hasPrefix "_" name then v else name
);
in in
{ {
# settings should evaluate # settings should evaluate
@@ -117,10 +101,12 @@ in
# instance = instance_foo # instance = instance_foo
# roles = peer # roles = peer
# machines = jon # machines = jon
settings = filterInternals res.importedModulesEvaluated.self-A.config.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.nixosModule.settings; settings =
res.importedModulesEvaluated.self-A.config.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.nixosModule.settings;
machine = machine =
res.importedModulesEvaluated.self-A.config.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.nixosModule.machine; res.importedModulesEvaluated.self-A.config.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.nixosModule.machine;
roles = mapInternalsRecursive res.importedModulesEvaluated.self-A.config.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.nixosModule.roles; roles =
res.importedModulesEvaluated.self-A.config.result.allRoles.peer.allInstances.instance_foo.allMachines.jon.nixosModule.roles;
}; };
expected = { expected = {
instanceName = "instance_foo"; instanceName = "instance_foo";
@@ -139,25 +125,21 @@ in
machines = { machines = {
jon = { jon = {
settings = { settings = {
__functor = "__functor";
}; };
}; };
}; };
settings = { settings = {
__functor = "__functor";
}; };
}; };
peer = { peer = {
machines = { machines = {
jon = { jon = {
settings = { settings = {
__functor = "__functor";
timeout = "foo-peer-jon"; timeout = "foo-peer-jon";
}; };
}; };
}; };
settings = { settings = {
__functor = "__functor";
timeout = "foo-peer"; timeout = "foo-peer";
}; };
}; };
@@ -167,11 +149,9 @@ in
test_per_instance_settings_vendoring = { test_per_instance_settings_vendoring = {
expr = expr =
mapInternalsRecursive
res.importedModulesEvaluated.self-A.config.result.allRoles.peer.allInstances."instance_foo".allMachines.jon.nixosModule.vendoredSettings; res.importedModulesEvaluated.self-A.config.result.allRoles.peer.allInstances."instance_foo".allMachines.jon.nixosModule.vendoredSettings;
expected = { expected = {
# Returns another override
__functor = "__functor";
timeout = "config.thing"; timeout = "config.thing";
}; };
}; };