From f1cd643abeb470065dd7b862617db71aef3b0dfc Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 1 Dec 2026 00:48:34 +0000 Subject: krops.nix: submit enstate state with remote builds, too As with builds on the target: packed here once the system is copied, moved to the incoming directory on the target before activating it. The packing runs in a subshell, as the remote build's own trap keeps the builder's GC root until the end. --- krops.nix | 55 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 22 deletions(-) (limited to 'krops.nix') diff --git a/krops.nix b/krops.nix index ba5a1d51..88f8eee8 100644 --- a/krops.nix +++ b/krops.nix @@ -42,7 +42,8 @@ # as, not necessarily root) and copies the system to the target # (or, with copyVia = "deployer", the deploying host copies it from the # builder). -# The target gets only its secrets, and activates. A record of each such +# The target gets only its secrets and enstate submissions, and +# activates. A record of each such # build goes to $PORTABLE_CACHEDIR/builds.jsonl (default: # ~/shared-tmp/cache/hrm), the evaluated derivation's GC root to # $HOST_CACHEDIR/roots/HOSTNAME.drv. @@ -163,8 +164,6 @@ 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 @@ -181,22 +180,25 @@ rec { pending = "/run/krops-enstate"; stateDir = "/var/lib/enstate"; in { + # In a subshell, so its trap leaves the caller's alone. packAndCopy = /* sh */ '' - submissions=$( - TMPDIR=''${XDG_RUNTIME_DIR:-''${TMPDIR:-/tmp}} \ - mktemp -d --tmpdir ${name}-enstate.XXXXXXXX + ( + 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}/"} ) - 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 = /* sh */ '' trap 'rm -fR ${pending}' EXIT @@ -577,6 +579,9 @@ rec { ${builtins.getAttr action { deploy = let + # The submissions wait for the switch, whose check plans them, and + # which takes them. + submit = sourceLists != {}; setAndSwitch = /* sh */ '' old_gen=$(readlink -f /nix/var/nix/profiles/system) new_gen=$(readlink -f "$system") @@ -590,20 +595,26 @@ rec { --setenv=NIXOS_NO_CHECK="''${NIXOS_NO_CHECK-}" \ "$system"/bin/switch-to-configuration ${switch} else + ${lib.optionalString submit /* sh */ '' + # Without a switch, nothing takes what waits: take it now. + "$system"/sw/bin/enstate --migrate + ''} echo 'Configuration is already up to date' >&2 fi ''; - in /* sh */ '' - # Without prune: the rest of the target's /var/src stays. - ${pkgs.populate { source = targetSource; target = target'; }} - ${onTarget (builtins.getAttr switch { + activate = builtins.getAttr switch { switch = setAndSwitch; boot = setAndSwitch; test = /* sh */ '' systemd-run --pipe --wait --collect --quiet \ "$system"/bin/switch-to-configuration test ''; - })} + }; + in /* sh */ '' + # Without prune: the rest of the target's /var/src stays. + ${pkgs.populate { source = targetSource; target = target'; }} + ${lib.optionalString submit enstateSteps.packAndCopy} + ${onTarget (lib.optionalString submit (enstateSteps.forgetPending + enstateSteps.movePending) + activate)} ''; diff = /* sh */ '' old_drv=$(${onTarget /* sh */ '' -- cgit v1.3.1