Inventory: refactor build-inventory in more independent parts

This commit is contained in:
Johannes Kirschbauer
2025-02-03 08:34:21 +01:00
parent 9458fdf7bc
commit 46dd52332a
2 changed files with 159 additions and 145 deletions

View File

@@ -76,7 +76,7 @@ let
(machines.${name} or { }) (machines.${name} or { })
# Inherit the inventory assertions ? # Inherit the inventory assertions ?
# { inherit (mergedInventory) assertions; } # { inherit (mergedInventory) assertions; }
{ imports = serviceConfigs.${name} or [ ]; } { imports = serviceConfigs.machines.${name}.machineImports or []; }
( (
{ {
# Settings # Settings

View File

@@ -42,55 +42,30 @@ let
builtins.elem "inventory" builtins.elem "inventory"
(clan-core.lib.modules.getFrontmatter modulepath serviceName).features or [ ]; (clan-core.lib.modules.getFrontmatter modulepath serviceName).features or [ ];
extendMachine = compileMachine =
{ machineConfig, inventory }: { machineConfig }:
[ {
machineImports = [
(lib.optionalAttrs (machineConfig.deploy.targetHost or null != null) { (lib.optionalAttrs (machineConfig.deploy.targetHost or null != null) {
config.clan.core.networking.targetHost = machineConfig.deploy.targetHost; config.clan.core.networking.targetHost = machineConfig.deploy.targetHost;
}) })
{
assertions = lib.foldlAttrs (
acc: serviceName: _serviceConfigs:
acc
++ [
{
assertion = checkService inventory.modules.${serviceName} serviceName;
message = ''
Service ${serviceName} cannot be used in inventory. It does not declare the 'inventory' feature.
To allow it add the following to the beginning of the README.md of the module:
---
...
features = [ "inventory" ]
---
Also make sure to test the module with the 'inventory' feature enabled.
'';
}
]
) [ ] inventory.services;
}
]; ];
assertions = { };
};
mapMachineConfigToNixOSConfig = compileServicesForMachine =
# Returns a NixOS configuration for the machine 'machineName'. # Returns a NixOS configuration for the machine 'machineName'.
# Return Format: { imports = [ ... ]; config = { ... }; options = { ... } } # Return Format: { imports = [ ... ]; config = { ... }; options = { ... } }
{ {
machineName, machineName,
machineConfig,
inventory, inventory,
directory, directory,
}: }:
let
compileServiceModules = serviceName: serviceConfigs: {
# TODO: Add other attributes
machineImports = (
lib.foldlAttrs ( lib.foldlAttrs (
# [ Modules ], String, { ${instance_name} :: ServiceConfig }
initialServiceModules: serviceName: serviceConfigs:
initialServiceModules
# Collect service config
++ (lib.foldlAttrs (
# [ Modules ], String, ServiceConfig # [ Modules ], String, ServiceConfig
acc2: instanceName: serviceConfig: acc2: instanceName: serviceConfig:
@@ -187,30 +162,69 @@ let
] ]
else else
acc2 acc2
) [ ] (serviceConfigs)) ) [ ] (serviceConfigs)
) [ ] inventory.services );
# Global extension for each machine
++ (extendMachine { inherit machineConfig inventory; }); assertions = lib.mapAttrs' (name: value: {
name = "checkservice.${serviceName}.${name}";
value = {
assertion = checkService inventory.modules.${serviceName} serviceName;
message = ''
Service ${serviceName} cannot be used in inventory. It does not declare the 'inventory' feature.
To allow it add the following to the beginning of the README.md of the module:
---
...
features = [ "inventory" ]
---
Also make sure to test the module with the 'inventory' feature enabled.
'';
};
}) inventory.services;
};
in
lib.mapAttrs compileServiceModules inventory.services;
/* /*
Returns a NixOS configuration for every machine in the inventory. Returns a set with NixOS configuration for every machine in the inventory.
machinesFromInventory :: Inventory -> { ${machine_name} :: NixOSConfiguration } machinesFromInventory :: Inventory -> { ${machine_name} :: NixOSConfiguration }
*/ */
buildInventory = buildInventory =
{ inventory, directory }: { inventory, directory }:
# For every machine in the inventory, build a NixOS configuration {
# For each machine generate config, forEach service, if the machine is used. machines = builtins.mapAttrs (
builtins.mapAttrs (
machineName: machineConfig: machineName: machineConfig:
mapMachineConfigToNixOSConfig { let
compiledServices = compileServicesForMachine {
inherit inherit
machineName machineName
machineConfig
inventory inventory
directory directory
; ;
};
compiledMachine = compileMachine {
inherit
machineConfig
;
};
machineImports =
compiledMachine.machineImports
++ builtins.foldl' (acc: service: acc ++ service.machineImports) [ ] (
builtins.attrValues compiledServices
);
in
{
inherit machineImports compiledServices compiledMachine;
} }
) (inventory.machines or { }); ) (inventory.machines or { });
};
in in
{ {
inherit buildInventory; inherit buildInventory;