PoC part 2
This commit is contained in:
parent
0580ad02bd
commit
15ed56d8ad
3 changed files with 117 additions and 40 deletions
|
|
@ -13,19 +13,43 @@ let
|
|||
cfg = config.dynamicism;
|
||||
opts = options.dynamicism;
|
||||
subOpts = lib.mapAttrs (_: metaAttr: metaAttr.configuration.options) options.dynamicism.for.valueMeta.attrs;
|
||||
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;
|
||||
|
||||
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
|
||||
{
|
||||
#
|
||||
# Interface.
|
||||
#
|
||||
options.dynamicism = {
|
||||
for = mkOption {
|
||||
type = t.attrsOf (t.submoduleWith {
|
||||
modules = [ ./submodule.nix ];
|
||||
shorthandOnlyDefinesConfig = false;
|
||||
specialArgs = { inherit pkgs; };
|
||||
});
|
||||
default = { };
|
||||
};
|
||||
|
|
@ -40,39 +64,30 @@ in
|
|||
};
|
||||
};
|
||||
|
||||
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);
|
||||
# Assertions.
|
||||
config.assertions = ourAssertions;
|
||||
|
||||
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
|
||||
];
|
||||
#
|
||||
# Generic implementation.
|
||||
#
|
||||
config.dynamicism.finalSettings = lib.asserts.checkAssertWarn ourAssertions [ ] (
|
||||
recUpdateFoldlAttrs (name: { ... }@submod: finalSettingsFor submod) config.dynamicism.for
|
||||
);
|
||||
|
||||
config.dynamicism.for = {
|
||||
gotosocial = {
|
||||
source-options = [
|
||||
"services.gotosocial.setting"
|
||||
];
|
||||
systemd-services-updated = [
|
||||
"gotosocial.service"
|
||||
];
|
||||
};
|
||||
# Implementations.
|
||||
config.dynamicism.for.gotosocial = let
|
||||
cfg = config.dynamicism.for.gotosocial;
|
||||
in {
|
||||
source-options = [
|
||||
"services.gotosocial.settings"
|
||||
];
|
||||
|
||||
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
|
||||
'';
|
||||
};
|
||||
|
||||
config.dynamicism.finalSettings = lib.foldlAttrs (acc: name: { ... }@submod: let
|
||||
next = finalSettingsFor submod;
|
||||
in lib.recursiveUpdate acc next) { } config.dynamicism.for;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue