dynix/modules/dynamicism/default.nix

139 lines
3.8 KiB
Nix
Raw Normal View History

{ pkgs, lib, config, options, ... }:
let
inherit (lib.options)
mkOption
showOption
;
t = lib.types;
2026-02-06 17:12:02 +01:00
inherit (import ./lib.nix { inherit lib; })
typeCheck
convenientAttrPath
concatFoldl
recUpdateFoldl
recUpdateFoldlAttrs
;
evalNixos = import (pkgs.path + "/nixos");
2026-02-03 18:37:16 +01:00
opts = options.dynamicism;
2026-02-06 17:12:02 +01:00
2026-02-03 18:37:16 +01:00
subOpts = lib.mapAttrs (_: metaAttr: metaAttr.configuration.options) options.dynamicism.for.valueMeta.attrs;
2026-02-03 22:31:32 +01:00
settingsFormat = pkgs.formats.yaml { };
2026-02-06 17:12:02 +01:00
finalSettingsFor = { ... }@submod: recUpdateFoldl (optPath:
lib.setAttrByPath optPath (lib.getAttrFromPath optPath config)
) submod.source-options;
2026-02-03 22:31:32 +01:00
ourAssertions = lib.concatAttrValues {
2026-02-06 17:12:02 +01:00
unitsExist = subOpts
|> lib.attrValues
|> concatFoldl (submod: submod.systemd-services-updated.value
|> lib.map (unit: {
2026-02-03 22:31:32 +01:00
assertion = config.systemd.units.${unit}.enable or false;
message = ''
2026-02-06 17:12:02 +01:00
${showOption submod.systemd-services-updated.loc}' specified non-existent unit '${unit}'
2026-02-03 22:31:32 +01:00
'';
2026-02-06 17:12:02 +01:00
})
|> lib.optionals submod.enable.value
);
2026-02-03 22:31:32 +01:00
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-06 17:12:02 +01:00
doChange = mkOption {
type = t.functionTo t.pathInStore;
readOnly = true;
description = ''
The function to call to Do The Thing.
'';
};
};
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.
#
2026-02-06 17:12:02 +01:00
config.dynamicism.doChange = {
option,
value,
configuration ? builtins.getEnv "NIXOS_CONFIG",
}: let
loc = opts.doChange.loc ++ [ "(function argument)" "value" ];
option' = typeCheck loc convenientAttrPath option;
nixosAfter = evalNixos {
configuration = { config, ... }: {
imports = [
configuration
(lib.setAttrByPath option' (lib.mkOverride (-999) value))
];
environment.systemPackages = [
config.dynamicism.for.gotosocial.activate
];
};
};
allActivations = lib.mapAttrsToList (name: submod: submod.activate) config.dynamicism.for;
allActivationScripts = pkgs.writeShellApplication {
name = "dynamicism-activate";
runtimeInputs = allActivations;
text = nixosAfter.config.dynamicism.for
|> lib.mapAttrsToList (name: submod: ''
echo "Activating dynamicism for ${name}"
${lib.getExe submod.activate}
'')
|> lib.concatStringsSep "\n";
};
in allActivationScripts;
2026-02-03 22:31:32 +01:00
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
'';
};
}