tests for PoC!
This commit is contained in:
parent
15ed56d8ad
commit
447ae19b3c
13 changed files with 450 additions and 19 deletions
|
|
@ -1,38 +1,42 @@
|
|||
{ pkgs, lib, config, options, ... }:
|
||||
let
|
||||
inherit (lib.modules)
|
||||
mkIf
|
||||
;
|
||||
inherit (lib.options)
|
||||
mkOption
|
||||
mkEnableOption
|
||||
literalExpression
|
||||
showOption
|
||||
;
|
||||
t = lib.types;
|
||||
cfg = config.dynamicism;
|
||||
|
||||
inherit (import ./lib.nix { inherit lib; })
|
||||
typeCheck
|
||||
convenientAttrPath
|
||||
concatFoldl
|
||||
recUpdateFoldl
|
||||
recUpdateFoldlAttrs
|
||||
;
|
||||
|
||||
evalNixos = import (pkgs.path + "/nixos");
|
||||
|
||||
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;
|
||||
finalSettingsFor = { ... }@submod: recUpdateFoldl (optPath:
|
||||
lib.setAttrByPath optPath (lib.getAttrFromPath optPath config)
|
||||
) submod.source-options;
|
||||
|
||||
ourAssertions = lib.concatAttrValues {
|
||||
unitsExist = concatFoldl (submod: let
|
||||
next = lib.map (unit: assert lib.isString unit; {
|
||||
unitsExist = subOpts
|
||||
|> lib.attrValues
|
||||
|> concatFoldl (submod: submod.systemd-services-updated.value
|
||||
|> lib.map (unit: {
|
||||
assertion = config.systemd.units.${unit}.enable or false;
|
||||
message = ''
|
||||
'${showOption submod.systemd-services-updated.loc}' specified non-existent unit '${unit}'
|
||||
${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);
|
||||
})
|
||||
|> lib.optionals submod.enable.value
|
||||
);
|
||||
|
||||
optsExist = concatFoldl (submod: lib.optionals submod.enable.value (lib.map (optPath: {
|
||||
assertion = lib.hasAttrByPath optPath options;
|
||||
|
|
@ -62,6 +66,14 @@ in
|
|||
Attrset of each `source-options` tree to their actual values.
|
||||
'';
|
||||
};
|
||||
|
||||
doChange = mkOption {
|
||||
type = t.functionTo t.pathInStore;
|
||||
readOnly = true;
|
||||
description = ''
|
||||
The function to call to Do The Thing.
|
||||
'';
|
||||
};
|
||||
};
|
||||
|
||||
# Assertions.
|
||||
|
|
@ -70,6 +82,39 @@ in
|
|||
#
|
||||
# Generic implementation.
|
||||
#
|
||||
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;
|
||||
|
||||
config.dynamicism.finalSettings = lib.asserts.checkAssertWarn ourAssertions [ ] (
|
||||
recUpdateFoldlAttrs (name: { ... }@submod: finalSettingsFor submod) config.dynamicism.for
|
||||
);
|
||||
|
|
|
|||
24
modules/dynamicism/lib.nix
Normal file
24
modules/dynamicism/lib.nix
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
lib ? import <nixpkgs/lib>,
|
||||
}: let
|
||||
t = lib.types;
|
||||
in lib.fix (self: {
|
||||
/** Perform module-system type checking and resolving on a single option value. */
|
||||
typeCheck = loc: option: value:
|
||||
assert lib.isOptionType option;
|
||||
assert lib.isList loc;
|
||||
assert lib.all lib.isString loc;
|
||||
let
|
||||
merged = lib.modules.mergeDefinitions loc option [ {
|
||||
inherit value;
|
||||
file = "«inline»";
|
||||
} ];
|
||||
in merged.mergedValue;
|
||||
|
||||
/** Either a list of strings, or a dotted string that will be split. */
|
||||
convenientAttrPath = t.coercedTo t.str (lib.splitString ".") (t.listOf t.str);
|
||||
|
||||
concatFoldl = f: list: lib.foldl' (acc: value: acc ++ (f value)) [ ] list;
|
||||
recUpdateFoldl = f: list: lib.foldl' (acc: value: lib.recursiveUpdate acc (f value)) { } list;
|
||||
recUpdateFoldlAttrs = f: attrs: lib.foldlAttrs (acc: name: value: lib.recursiveUpdate acc (f name value)) { } attrs;
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue