dynix/modules/dynamicism/default.nix

94 lines
2.9 KiB
Nix
Raw Normal View History

{ pkgs, lib, config, options, ... }:
let
inherit (lib.modules)
mkIf
;
inherit (lib.options)
mkOption
mkEnableOption
literalExpression
showOption
;
t = lib.types;
cfg = config.dynamicism;
2026-02-03 18:37:16 +01:00
opts = options.dynamicism;
subOpts = lib.mapAttrs (_: metaAttr: metaAttr.configuration.options) options.dynamicism.for.valueMeta.attrs;
2026-02-03 22:31:32 +01:00
settingsFormat = pkgs.formats.yaml { };
concatFoldl = f: list: lib.foldl' (acc: value: acc ++ (f value)) [ ] list;
recUpdateFoldlAttrs = f: attrs: lib.foldlAttrs (acc: name: value: lib.recursiveUpdate acc (f name value)) { } attrs;
finalSettingsFor = { ... }@submod: lib.foldl (acc: optPath: let
next =
assert lib.isList optPath;
lib.setAttrByPath optPath (lib.getAttrFromPath optPath config);
in lib.recursiveUpdate acc next) { } submod.source-options;
2026-02-03 22:31:32 +01:00
ourAssertions = lib.concatAttrValues {
unitsExist = concatFoldl (submod: let
next = lib.map (unit: assert lib.isString unit; {
assertion = config.systemd.units.${unit}.enable or false;
message = ''
'${showOption submod.systemd-services-updated.loc}' specified non-existent unit '${unit}'
'';
}) submod.systemd-services-updated.value;
in lib.optionals submod.enable.value next) (lib.attrValues subOpts);
optsExist = concatFoldl (submod: lib.optionals submod.enable.value (lib.map (optPath: {
assertion = lib.hasAttrByPath optPath options;
message = "'${showOption submod.source-options.loc}' specified non-existent option '${showOption optPath}'";
}) submod.source-options.value)) (lib.attrValues subOpts);
};
in
{
2026-02-03 22:31:32 +01:00
#
# Interface.
#
options.dynamicism = {
for = mkOption {
type = t.attrsOf (t.submoduleWith {
modules = [ ./submodule.nix ];
shorthandOnlyDefinesConfig = false;
2026-02-03 22:31:32 +01:00
specialArgs = { inherit pkgs; };
});
default = { };
};
finalSettings = mkOption {
type = t.attrsOf t.raw;
internal = true;
readOnly = true;
description = ''
Attrset of each `source-options` tree to their actual values.
'';
};
};
2026-02-03 22:31:32 +01:00
# Assertions.
config.assertions = ourAssertions;
2026-02-03 18:37:16 +01:00
2026-02-03 22:31:32 +01:00
#
# Generic implementation.
#
config.dynamicism.finalSettings = lib.asserts.checkAssertWarn ourAssertions [ ] (
recUpdateFoldlAttrs (name: { ... }@submod: finalSettingsFor submod) config.dynamicism.for
);
2026-02-03 22:31:32 +01:00
# Implementations.
config.dynamicism.for.gotosocial = let
cfg = config.dynamicism.for.gotosocial;
in {
source-options = [
"services.gotosocial.settings"
];
2026-02-03 22:31:32 +01:00
configFile = settingsFormat.generate "gotosocial-override.yml" config.services.gotosocial.settings;
unitDropins."gotosocial.service" = pkgs.writeText "gotosocial-override.conf" ''
[Service]
ExecStart=
ExecStart=${lib.getExe' pkgs.gotosocial "gotosocial"} --config-path ${cfg.configFile} start
'';
};
}