dynix/modules/dynamicism/submodule.nix

115 lines
2.7 KiB
Nix
Raw Normal View History

{
name,
lib,
config,
2026-02-16 18:02:39 +01:00
host,
...
}:
let
inherit (lib.modules)
mkIf
;
inherit (lib.options)
mkOption
mkEnableOption
literalExpression
;
2026-02-03 22:31:32 +01:00
inherit (lib.types)
mkOptionType
;
t = lib.types;
2026-02-16 18:02:39 +01:00
inherit (import ./lib.nix { inherit lib; })
recUpdateFoldl
;
pkgs = host.pkgs;
/** Either a list of strings, or a dotted string that will be split. */
convenientAttrPath = t.coercedTo t.str (lib.splitString ".") (t.listOf t.str);
2026-02-03 22:31:32 +01:00
executablePathInStore = mkOptionType {
name = "exepath";
description = "executable path in the Nix store";
descriptionClass = "noun";
merge = lib.mergeEqualOption;
functor = lib.defaultFunctor "exepath";
check = x: if lib.isDerivation x then (
x.meta.mainProgram or null != null
) else (
lib.pathInStore.check x
);
};
in
{
options = {
2026-02-03 18:37:16 +01:00
enable = mkEnableOption "dynamicism for '${name}'";
source-options = mkOption {
type = t.listOf convenientAttrPath;
description = "A list of attrpaths of the NixOS option the dynamicism for '${name}' uses";
example = literalExpression ''
[ [ "services" "gotosocial" "settings" ] "services.nginx.settings" ]
'';
};
systemd-services-updated = mkOption {
type = t.listOf t.str;
description = ''
A list of systemd unit names (including the suffix, e.g. `.service`) that need to be updated.
'';
example = literalExpression ''
2026-02-03 22:31:32 +01:00
[ "gotosocial.service" ]
'';
2026-02-03 22:31:32 +01:00
default = lib.attrNames config.unitDropins;
};
2026-02-16 18:02:39 +01:00
finalSettings = mkOption {
type = t.attrsOf t.raw;
internal = true;
default = recUpdateFoldl (optPath:
lib.setAttrByPath optPath (lib.getAttrFromPath optPath host.config)
) config.source-options;
};
2026-02-03 22:31:32 +01:00
configFile = mkOption {
type = t.pathInStore;
internal = true;
};
unitDropins = mkOption {
2026-02-10 14:59:44 +01:00
type = t.attrsOf t.package;
2026-02-03 22:31:32 +01:00
internal = true;
};
activate = mkOption {
type = executablePathInStore;
internal = true;
};
};
config = mkIf config.enable {
2026-02-03 22:31:32 +01:00
activate = pkgs.writeShellApplication {
name = "dynamicism-for-${name}-activate";
runtimeInputs = [ pkgs.systemd ];
text = let
doEdits = config.unitDropins
|> lib.mapAttrsToList (service: dropin: ''
2026-02-10 14:59:44 +01:00
cat "${dropin}" | systemctl edit "${service}" --runtime --drop=dynix-${dropin.name} --stdin
2026-02-13 21:12:55 +01:00
'')
|> lib.concatStringsSep "\n";
2026-02-03 22:31:32 +01:00
doReloads = config.unitDropins
2026-02-13 21:12:55 +01:00
|> lib.mapAttrsToList (service: _: ''
systemctl reload-or-restart "${service}"
'')
|> lib.concatStringsSep "\n";
in ''
${doEdits}
${doReloads}
'';
2026-02-03 22:31:32 +01:00
};
};
}