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:
@@ -120,17 +120,9 @@ let
|
||||
}
|
||||
```
|
||||
*/
|
||||
makeExtensibleConfig =
|
||||
f: args:
|
||||
let
|
||||
makeModuleExtensible =
|
||||
eval:
|
||||
eval.config
|
||||
// {
|
||||
__functor = _self: m: makeModuleExtensible (eval.extendModules { modules = lib.toList m; });
|
||||
};
|
||||
in
|
||||
makeModuleExtensible (f args);
|
||||
|
||||
# Extend evalModules result by a module, returns .config.
|
||||
extendEval = eval: m: (eval.extendModules { modules = lib.toList m; }).config;
|
||||
|
||||
/**
|
||||
Apply the settings to the instance
|
||||
@@ -146,20 +138,18 @@ let
|
||||
machines = lib.mapAttrs (machineName: v: {
|
||||
# TODO: evaluate the settings against the interface
|
||||
# settings = (evalMachineSettings { inherit roleName instanceName; inherit (v) settings; }).config;
|
||||
settings = (
|
||||
makeExtensibleConfig evalMachineSettings {
|
||||
settings =
|
||||
(evalMachineSettings {
|
||||
inherit roleName instanceName machineName;
|
||||
inherit (v) settings;
|
||||
}
|
||||
);
|
||||
}).config;
|
||||
}) role.machines;
|
||||
# TODO: evaluate the settings against the interface
|
||||
settings = (
|
||||
makeExtensibleConfig evalMachineSettings {
|
||||
settings =
|
||||
(evalMachineSettings {
|
||||
inherit roleName instanceName;
|
||||
inherit (role) settings;
|
||||
}
|
||||
);
|
||||
}).config;
|
||||
}) instance.roles;
|
||||
in
|
||||
{
|
||||
@@ -327,13 +317,17 @@ in
|
||||
name = machineName;
|
||||
roles = lib.attrNames (lib.filterAttrs (_n: v: v.machines ? ${machineName}) roles);
|
||||
};
|
||||
settings = (
|
||||
makeExtensibleConfig evalMachineSettings {
|
||||
settings =
|
||||
(evalMachineSettings {
|
||||
inherit roleName instanceName machineName;
|
||||
settings =
|
||||
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 ];
|
||||
}).config;
|
||||
@@ -396,6 +390,7 @@ in
|
||||
in
|
||||
uniqueStrings (collectRoles machineScope.instances);
|
||||
};
|
||||
# TODO: instances.<instanceName>.roles should contain all roles, even if nobody has the role
|
||||
inherit (machineScope) instances;
|
||||
|
||||
# There are no machine settings.
|
||||
@@ -508,7 +503,8 @@ in
|
||||
imports = [
|
||||
# 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
|
||||
(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;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -23,16 +23,17 @@ let
|
||||
{
|
||||
instanceName,
|
||||
settings,
|
||||
extendSettings,
|
||||
machine,
|
||||
roles,
|
||||
...
|
||||
}:
|
||||
let
|
||||
settings1 = settings {
|
||||
finalSettings = extendSettings {
|
||||
# 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 10 "config.blah";
|
||||
timeout = lib.mkOverride 10 "config.thing";
|
||||
};
|
||||
in
|
||||
{
|
||||
@@ -46,12 +47,7 @@ let
|
||||
|
||||
# We are double vendoring the settings
|
||||
# To test that we can do it indefinitely
|
||||
vendoredSettings = settings1 {
|
||||
# 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";
|
||||
};
|
||||
vendoredSettings = finalSettings;
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -92,18 +88,6 @@ let
|
||||
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
|
||||
{
|
||||
# settings should evaluate
|
||||
@@ -117,10 +101,12 @@ in
|
||||
# instance = instance_foo
|
||||
# roles = peer
|
||||
# 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 =
|
||||
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 = {
|
||||
instanceName = "instance_foo";
|
||||
@@ -139,25 +125,21 @@ in
|
||||
machines = {
|
||||
jon = {
|
||||
settings = {
|
||||
__functor = "__functor";
|
||||
};
|
||||
};
|
||||
};
|
||||
settings = {
|
||||
__functor = "__functor";
|
||||
};
|
||||
};
|
||||
peer = {
|
||||
machines = {
|
||||
jon = {
|
||||
settings = {
|
||||
__functor = "__functor";
|
||||
timeout = "foo-peer-jon";
|
||||
};
|
||||
};
|
||||
};
|
||||
settings = {
|
||||
__functor = "__functor";
|
||||
timeout = "foo-peer";
|
||||
};
|
||||
};
|
||||
@@ -167,11 +149,9 @@ in
|
||||
|
||||
test_per_instance_settings_vendoring = {
|
||||
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 = {
|
||||
# Returns another override
|
||||
__functor = "__functor";
|
||||
timeout = "config.thing";
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user