summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-30 21:42:59 +0000
committertv <tv@krebsco.de>2026-12-01 00:22:40 +0000
commit0b9a35c301558f675ca825b2e9a3619471243d45 (patch)
tree1c3043bba87fe877447d4fb44362d2a2e726b027
parent9e69de1edd908efb748e3a51afd157974c5afea0 (diff)
krops.nix: submit enstate state when deploying
The deploying host evaluates the host's enstate.sourceLists, packs each channel's submission with hrm's enstate, in memory, and copies it to the target's memory; once the system is built, it goes to enstate's incoming directory, where the switch's pre-switch check plans it and enstate.service takes it; without a switch, enstate --migrate takes it. Builds on the target only, for now.
-rw-r--r--krops.nix46
1 files changed, 46 insertions, 0 deletions
diff --git a/krops.nix b/krops.nix
index c1e98fd7..e74db131 100644
--- a/krops.nix
+++ b/krops.nix
@@ -74,6 +74,11 @@ let
})
(builtins.filter (item: item != "") (lib.splitString "," checkouts)));
+ # hrm, as this host has it: its package set, and the host's enstate
+ # source lists, as evaluated here.
+ local = import ./. {};
+ sourceLists = local.legacy.nixosConfigurations.${name}.config.enstate.sourceLists;
+
# A source pinned in sources.json, fetched with git.
gitSource = source: let
entry = (lib.importJSON ./sources.json).${source};
@@ -156,6 +161,8 @@ rec {
pkgs.writers.writeDash "${name}-${action}" (remoteBuildScript action);
mkAction = build-method: deploy-action:
+ assert lib.assertMsg (spec.builders == [] || sourceLists == {})
+ "${name}: enstate submissions need a build on the target, for now";
if spec.builders == [] then
mkTargetBuildAction build-method deploy-action
else
@@ -166,12 +173,49 @@ rec {
mkTargetBuildAction = build-method: deploy-action: let
target' = lib.mkTarget target;
name' = "${name}-deploy-${build-method}-${deploy-action}";
+ # The host's enstate.sourceLists, as this host evaluates them: each
+ # channel's submission gets packed here, goes to the target's memory, and
+ # once the system is built, to where it waits for the switch, whose
+ # check plans it, and which takes it.
+ submit = deploy-action == "deploy" && sourceLists != {};
+ pending = "/run/krops-enstate";
+ stateDir = "/var/lib/enstate";
+ packAndCopy = lib.optionalString submit /* sh */ ''
+ submissions=$(
+ TMPDIR=''${XDG_RUNTIME_DIR:-''${TMPDIR:-/tmp}} \
+ mktemp -d --tmpdir ${name}-enstate.XXXXXXXX
+ )
+ trap 'rm -fR "$submissions"' EXIT
+ stamp=$(date -u +%Y%m%dT%H%M%SZ)-$$
+ ${lib.concatStrings (lib.mapAttrsToList (channel: sourceList: /* sh */ ''
+ ${local.pkgs.enstate}/bin/enstate \
+ --pack ${pkgs.writers.writeJSON "${name}-enstate-${channel}.json" sourceList} \
+ --out "$submissions"/"$stamp"-${channel}
+ '') sourceLists)}
+ ${pkgs.rsync}/bin/rsync -a --no-owner --no-group --chmod=D0700,F0600 \
+ -e ${lib.escapeShellArg (lib.concatStringsSep " " ([ "ssh" ] ++ lib.mkUserPortSSHOpts target'))} \
+ --rsync-path=${lib.escapeShellArg "mkdir -m 0700 -p ${pending} && rsync"} \
+ "$submissions"/ ${lib.escapeShellArg "${target'.host}:${pending}/"}
+ '';
+ forgetPending = lib.optionalString submit /* sh */ ''
+ trap 'rm -fR ${pending}' EXIT
+ '';
+ movePending = lib.optionalString submit /* sh */ ''
+ mkdir -p -m 0700 ${stateDir} ${stateDir}/incoming
+ find ${pending} -mindepth 1 -maxdepth 1 -exec mv -t ${stateDir}/incoming {} +
+ '';
+ # Without a switch, nothing takes what waits: take it now.
+ takeNow = lib.optionalString submit /* sh */ ''
+ "$system"/sw/bin/enstate --migrate
+ '';
in
pkgs.writers.writeDash name' /* sh */ ''
set -efu
${pkgs.populate { source = source; target = target'; }}
+ ${packAndCopy}
${pkgs.krops.runShell target' { allocateTTY = true; } /* sh */ ''
set -efu
+ ${forgetPending}
system=$(
export NIX_PATH=${lib.escapeShellArg target'.path}
${builtins.getAttr build-method {
@@ -191,6 +235,7 @@ rec {
'';
}}
)
+ ${movePending}
${builtins.getAttr deploy-action {
deploy = let
setAndSwitch = /* sh */ ''
@@ -204,6 +249,7 @@ rec {
nix-env -p /nix/var/nix/profiles/system --set "$system"
"$system"/bin/switch-to-configuration ${switch}
else
+ ${takeNow}
echo 'Configuration is already up to date' >&2
fi
'';