2022-06-23 21:13:28 +03:00
|
|
|
{ config, lib, ... }:
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
2024-07-07 01:44:14 +03:00
|
|
|
getHostConfigurations = hostName: svcName: svcConfig: let
|
|
|
|
serviceConfigs =
|
|
|
|
lib.mapAttrsToList (groupName: _: svcConfig.nixos.${groupName})
|
|
|
|
(lib.filterAttrs (_: lib.elem hostName) svcConfig.nodes);
|
2022-06-23 21:13:28 +03:00
|
|
|
|
2024-07-08 20:45:13 +03:00
|
|
|
secretsConfig = let
|
|
|
|
secrets = lib.filterAttrs (_: secret: lib.any (node: node == hostName) secret.nodes) svcConfig.secrets;
|
|
|
|
in {
|
|
|
|
age.secrets = lib.mapAttrs' (secretName: secretConfig: {
|
|
|
|
name = "cluster-${svcName}-${secretName}";
|
|
|
|
value = {
|
|
|
|
inherit (secretConfig) path mode owner group;
|
|
|
|
file = ../secrets/${svcName}-${secretName}${lib.optionalString (!secretConfig.shared) "-${hostName}"}.age;
|
|
|
|
};
|
|
|
|
}) secrets;
|
|
|
|
|
|
|
|
systemd.services = lib.mkMerge (lib.mapAttrsToList (secretName: secretConfig: lib.genAttrs secretConfig.services (systemdServiceName: {
|
|
|
|
restartTriggers = [ "${../secrets/${svcName}-${secretName}${lib.optionalString (!secretConfig.shared) "-${hostName}"}.age}" ];
|
|
|
|
})) secrets);
|
|
|
|
};
|
2024-07-07 01:44:14 +03:00
|
|
|
in serviceConfigs ++ [
|
|
|
|
secretsConfig
|
|
|
|
];
|
2023-08-31 01:55:45 +03:00
|
|
|
|
|
|
|
introspectionModule._module.args.cluster = {
|
|
|
|
inherit (config) vars;
|
|
|
|
inherit config;
|
|
|
|
};
|
2022-06-23 21:13:28 +03:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options.services = mkOption {
|
|
|
|
description = "Cluster services.";
|
2023-08-31 01:55:45 +03:00
|
|
|
type = with types; attrsOf (submodule ./service-module.nix);
|
2022-06-23 21:13:28 +03:00
|
|
|
default = {};
|
|
|
|
};
|
2023-08-31 01:55:45 +03:00
|
|
|
|
2024-07-07 01:44:14 +03:00
|
|
|
config.out.injectNixosConfig = hostName: (lib.flatten (lib.mapAttrsToList (getHostConfigurations hostName) config.services)) ++ [
|
2023-08-31 01:55:45 +03:00
|
|
|
introspectionModule
|
|
|
|
];
|
2022-06-23 21:13:28 +03:00
|
|
|
}
|