dynix/modules/dynamicism/default.nix
2026-02-03 18:37:16 +01:00

78 lines
2.3 KiB
Nix

{ pkgs, lib, config, options, ... }:
let
inherit (lib.modules)
mkIf
;
inherit (lib.options)
mkOption
mkEnableOption
literalExpression
showOption
;
t = lib.types;
cfg = config.dynamicism;
opts = options.dynamicism;
subOpts = lib.mapAttrs (_: metaAttr: metaAttr.configuration.options) options.dynamicism.for.valueMeta.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;
in
{
options.dynamicism = {
for = mkOption {
type = t.attrsOf (t.submoduleWith {
modules = [ ./submodule.nix ];
shorthandOnlyDefinesConfig = false;
});
default = { };
};
finalSettings = mkOption {
type = t.attrsOf t.raw;
internal = true;
readOnly = true;
description = ''
Attrset of each `source-options` tree to their actual values.
'';
};
};
config.assertions = let
unitsExist = lib.foldl' (acc: 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 acc ++ lib.optionals submod.enable.value next) [ ] (lib.attrValues subOpts);
optsExist = lib.foldl' (acc: submod: let
next = lib.map (optPath: {
assertion = lib.hasAttrByPath optPath options;
message = "'${showOption submod.source-options.loc}' specified non-existent option '${showOption optPath}'";
}) submod.source-options.value;
in acc ++ lib.optionals submod.enable.value next) [ ] (lib.attrValues subOpts);
in lib.concatLists [
unitsExist
optsExist
];
config.dynamicism.for = {
gotosocial = {
source-options = [
"services.gotosocial.setting"
];
systemd-services-updated = [
"gotosocial.service"
];
};
};
config.dynamicism.finalSettings = lib.foldlAttrs (acc: name: { ... }@submod: let
next = finalSettingsFor submod;
in lib.recursiveUpdate acc next) { } config.dynamicism.for;
}