dynix/modules/dynamicism/submodule.nix

103 lines
2.3 KiB
Nix
Raw Permalink Normal View History

# SPDX-FileCopyrightText: 2026 Qyriad <qyriad@qyriad.me>
#
# SPDX-License-Identifier: EUPL-1.1
{
name,
lib,
config,
2026-02-16 18:02:39 +01:00
host,
...
}:
let
inherit (lib.modules)
mkIf
;
inherit (lib.options)
mkOption
mkEnableOption
literalExpression
;
t = lib.types;
2026-02-16 18:02:39 +01:00
inherit (import ./lib.nix { inherit lib; })
2026-02-16 18:02:39 +01:00
convenientAttrPath
executablePathInStore
2026-02-16 18:02:39 +01:00
recUpdateFoldl
;
pkgs = host.pkgs;
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 {
2026-02-16 18:02:39 +01:00
type = t.nullOr t.pathInStore;
2026-02-03 22:31:32 +01:00
internal = true;
2026-02-16 18:02:39 +01:00
default = null;
2026-02-03 22:31:32 +01:00
};
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
};
};
}