refactor(clan.service): make evalClanService a standalone function to interact with standalone modules

This commit is contained in:
Johannes Kirschbauer
2025-04-29 15:12:44 +02:00
parent 9b5f100ac6
commit a4a1363195
2 changed files with 172 additions and 156 deletions

View File

@@ -1,8 +1,11 @@
{ lib, clanLib }:
let
services = clanLib.callLib ./distributed-service/inventory-adapter.nix { };
in
{
inherit (services) evalClanService mapInstances;
inherit (import ./build-inventory { inherit lib clanLib; }) buildInventory;
interface = ./build-inventory/interface.nix;
mapInstances = clanLib.callLib ./distributed-service/inventory-adapter.nix { };
# Returns the list of machine names
# { ... } -> [ string ]
resolveTags =

View File

@@ -14,13 +14,32 @@
clanLib,
...
}:
let
evalClanService =
{ modules, id }:
(lib.evalModules {
class = "clan.service";
modules = [
./service-module.nix
# feature modules
(lib.modules.importApply ./api-feature.nix {
inherit clanLib;
attrName = id;
})
];
});
in
{
inherit evalClanService;
mapInstances =
{
# This is used to resolve the module imports from 'flake.inputs'
flakeInputs,
# The clan inventory
inventory,
}:
let
}:
let
# machineHasTag = machineName: tagName: lib.elem tagName inventory.machines.${machineName}.tags;
# map the instances into the module
@@ -119,25 +138,17 @@ let
# TODO: Eagerly check the _class of the resolved module
importedModulesEvaluated = lib.mapAttrs (
module_ident: instances:
(lib.evalModules {
class = "clan.service";
evalClanService {
id = module_ident;
modules =
[
./service-module.nix
# Import the resolved module.
(builtins.head instances).instance.resolvedModule
# feature modules
(lib.modules.importApply ./api-feature.nix {
inherit clanLib;
attrName = module_ident;
})
]
# Include all the instances that correlate to the resolved module
] # Include all the instances that correlate to the resolved module
++ (builtins.map (v: {
instances.${v.instanceName}.roles = v.instance.instanceRoles;
}) instances);
})
}
) grouped;
# Group the instances by the module they resolve to
@@ -168,8 +179,8 @@ let
machineName: result: acc.${machineName} or [ ] ++ [ result.nixosModule ]
) eval.config.result.final
) { } importedModulesEvaluated;
in
{
in
{
inherit
importedModuleWithInstances
grouped
@@ -177,4 +188,6 @@ in
allMachines
importedModulesEvaluated
;
};
}