aboutsummaryrefslogtreecommitdiffstats
path: root/nix
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-30 18:05:23 +0000
committertv <tv@krebsco.de>2026-11-30 22:03:11 +0000
commit2df89c5ff9bb32dab96a504116206ccce1045ccf (patch)
tree9c74466dce57a83aad923b8501254c0aadbe6f92 /nix
parentb74359e97643be0f43034c3aeab533a2389a939f (diff)
nix: the switch serves the views when there is something to serve
enstate.service reruns on a switch when the schema changes. A unit of its own, enstate-switch.service, takes what waits otherwise: a submission in incomingDir, or one staged. switch-to-configuration restarts sysinit-reactivation.target on every switch, which starts it, and triggering conditions keep it from running when nothing waits. The check before a switch uses the new generation's enstate, schema and configuration, and runs on a host without enstate, too: its first switch's state can wait in incomingDir. Subtests of the VM test: staged under an unchanged schema, brought by the switch (without the warning the activation-restart-list gives); a waiting submission taken by the switch that needs it; one refused refusing it.
Diffstat (limited to 'nix')
-rw-r--r--nix/module.nix54
-rw-r--r--nix/test.nix49
2 files changed, 92 insertions, 11 deletions
diff --git a/nix/module.nix b/nix/module.nix
index f81bea9..d2f54c8 100644
--- a/nix/module.nix
+++ b/nix/module.nix
@@ -265,13 +265,26 @@ in
description = "Views made by renderers.";
};
+ incomingDir = mkOption {
+ type = types.str;
+ readOnly = true;
+ default = "${cfg.stateDir}/incoming";
+ defaultText = lib.literalExpression ''"''${config.enstate.stateDir}/incoming"'';
+ description = ''
+ Where submissions wait for the next switch, each a directory of its own, taken
+ in the order of their names: planned against the new generation's schema
+ before switching, which a refused one refuses, and applied by the switch.
+ '';
+ };
+
checkBeforeSwitch = mkOption {
type = types.bool;
default = true;
description = ''
Whether a switch to a generation whose required slots the state can't fill,
- even by migrating, is refused before switching. Not checked when the running
- system has no enstate, nor for `boot` and `dry-activate`.
+ even by migrating and with the submissions waiting in `incomingDir`, or for
+ which a waiting submission is refused, is refused before switching. Not
+ checked for `boot` and `dry-activate`.
'';
};
};
@@ -310,7 +323,7 @@ in
environment.etc."enstate/schema.json".source = schemaFile;
environment.etc."enstate/config.json".source = settings;
- # At boot and after a switch that changes the schema: the views of this
+ # At boot, and after a switch that changes the schema: the views of this
# generation, from the snapshots, and the reactions of those that changed.
systemd.services.enstate = {
description = "enstate: the views of this generation";
@@ -324,16 +337,41 @@ in
};
};
+ # After a switch whose schema didn't change, with a submission waiting in
+ # incomingDir or staged (--stage leaves a marker, which --migrate removes):
+ # switch-to-configuration restarts sysinit-reactivation.target on every switch,
+ # which starts this, and the conditions keep it from running otherwise. After a
+ # switch whose schema changed, enstate.service has taken them already.
+ systemd.services.enstate-switch = {
+ description = "enstate: what waits for the switch";
+ wantedBy = [ "sysinit-reactivation.target" ];
+ after = [ "local-fs.target" ];
+ before = [
+ "sysinit-reactivation.target"
+ "shutdown.target"
+ ];
+ conflicts = [ "shutdown.target" ];
+ unitConfig = {
+ DefaultDependencies = false;
+ # Either ("|").
+ ConditionDirectoryNotEmpty = "|${cfg.incomingDir}";
+ ConditionPathExists = "|${cfg.stateDir}/migrate-due";
+ };
+ serviceConfig = {
+ Type = "oneshot";
+ ExecStart = "${enstate} --migrate";
+ };
+ };
+
+ # With the new generation's enstate, schema and configuration; on a host without
+ # enstate yet, too: its first switch needs its state, which can wait in
+ # incomingDir.
system.preSwitchChecks = lib.mkIf cfg.checkBeforeSwitch {
enstate = ''
case "''${2-}" in
boot|dry-activate) exit 0 ;;
esac
- if [ ! -e /etc/enstate/schema.json ]; then
- echo "enstate: the running system has none, not checking"
- exit 0
- fi
- ${enstate} --migrate --dry-run --schema "$1/etc/enstate/schema.json"
+ ${enstate} --migrate --dry-run --schema "$1/etc/enstate/schema.json" --config "$1/etc/enstate/config.json"
'';
};
};
diff --git a/nix/test.nix b/nix/test.nix
index d2f69d9..4a37ff3 100644
--- a/nix/test.nix
+++ b/nix/test.nix
@@ -57,6 +57,11 @@ pkgs.testers.runNixOSTest {
enstate.channels.secrets.slots."extra.key" = key;
};
+ # Adds another; its submission waits in the incoming directory.
+ specialisation.more.configuration = {
+ enstate.channels.secrets.slots."more.key" = key;
+ };
+
specialisation.retyped.configuration = {
enstate.channels.secrets.slots = lib.mkForce {
"tinc.key" = key // {
@@ -67,7 +72,7 @@ pkgs.testers.runNixOSTest {
};
testScript = ''
- def submit(name, key):
+ def submit(name, key, command="--apply"):
machine.succeed(
f"mkdir -p /root/{name}",
f"printf '{key}' > /root/{name}/key",
@@ -76,7 +81,7 @@ pkgs.testers.runNixOSTest {
f"> /root/{name}/sources.json",
f"enstate --pack /root/{name}/sources.json --out /root/{name}/submission",
f"enstate --plan /root/{name}/submission",
- f"enstate --apply /root/{name}/submission",
+ f"enstate {command} /root/{name}/submission",
)
def seen(expected):
@@ -135,10 +140,48 @@ pkgs.testers.runNixOSTest {
machine.succeed("/run/booted-system/bin/switch-to-configuration test")
machine.succeed("enstate --status | grep -q 'kept extra.key'")
+ with subtest("staged under an unchanged schema: the switch brings it"):
+ submit("third", "M", "--stage")
+ seen("tinc.key L")
+ machine.succeed("test -e /var/lib/enstate/migrate-due")
+ out = machine.succeed("/run/current-system/bin/switch-to-configuration test 2>&1")
+ assert "deprecated" not in out, out
+ seen("tinc.key M")
+ machine.fail("test -e /var/lib/enstate/migrate-due")
+
+ with subtest("incoming: a submission waits for the switch that needs it"):
+ new = "/run/booted-system/specialisation/more"
+ machine.fail(f"{new}/bin/switch-to-configuration test")
+ machine.succeed(
+ "mkdir -p /root/more",
+ "printf Y > /root/more/key",
+ """echo '{"channel": "secrets", "items": {"more.key": {"source": {"file": "key"}}}}' """
+ "> /root/more/sources.json",
+ "enstate --pack /root/more/sources.json --out /var/lib/enstate/incoming/1-more",
+ )
+ machine.succeed(f"{new}/bin/switch-to-configuration test")
+ machine.wait_until_succeeds("grep -qx Y /run/enstate/views/secrets.more.key/more.key")
+ machine.succeed("test -z \"$(ls -A /var/lib/enstate/incoming)\"")
+ seen("tinc.key M")
+ machine.succeed("/run/booted-system/bin/switch-to-configuration test")
+
+ with subtest("incoming: one refused refuses the switch, and stays"):
+ machine.succeed(
+ "mkdir -p /var/lib/enstate/incoming/2-bad/channels/secrets",
+ "printf x > /var/lib/enstate/incoming/2-bad/channels/secrets/nope",
+ """echo '{"submission": 1, "channel": "secrets", "items": """
+ """[{"address": "secrets/nope", "op": "set"}]}' """
+ "> /var/lib/enstate/incoming/2-bad/manifest.json",
+ )
+ machine.fail("/run/booted-system/specialisation/more/bin/switch-to-configuration test")
+ machine.succeed("test -d /var/lib/enstate/incoming/2-bad")
+ machine.succeed("rm -r /var/lib/enstate/incoming/2-bad")
+
+
with subtest("after a reboot, the views are there again"):
machine.shutdown()
machine.start()
machine.wait_for_unit("consumer.service")
- seen("tinc.key L")
+ seen("tinc.key M")
'';
}