diff options
Diffstat (limited to 'krops.nix')
| -rw-r--r-- | krops.nix | 159 |
1 files changed, 111 insertions, 48 deletions
@@ -29,6 +29,11 @@ # directory ~/sync/src/NAME) or NAME=PATH (an absolute path), # e.g. mobile-nixos to try another kernel # +# TARGET BUILDS +# A host without builders gets all sources, its secrets among them, and +# its enstate submissions, and evaluates, builds and activates its system +# itself. +# # REMOTE BUILDS # A host whose spec.nix lists builders (targets like populate's, # [USER@]HOST[:PORT]/PATH, e.g. [ "tv@zu.r/home/tv/tmp/cache/hrm/roots" ]) @@ -43,7 +48,8 @@ # (or, with copyVia = "deployer", the deploying host copies it from the # builder). # The target gets only its secrets and enstate submissions, and -# activates. A record of each such +# activates. The builder gets only derivations and what they need to +# build: never secrets, nor enstate submissions. 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. @@ -77,10 +83,12 @@ 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. + # hrm, as this host has it: its package set, the host's configuration, + # and its enstate source lists, as evaluated here (none for a minimal + # host, which has no enstate options). local = import ./. {}; - sourceLists = local.legacy.nixosConfigurations.${name}.config.enstate.sourceLists; + hostConfig = local.legacy.nixosConfigurations.${name}.config; + sourceLists = hostConfig.enstate.sourceLists or {}; # A source pinned in sources.json, fetched with git. gitSource = source: let @@ -173,46 +181,77 @@ rec { # Delivering the host's enstate submissions: each channel's, from the # host's enstate.sourceLists as this host evaluates them, gets packed - # here, goes to the target's memory, next to enstate's views, and from - # there to enstate's incoming directory. What went last stays there, so - # rsync sends only what changed. + # here, goes to enstate's directory for providers on the target, in + # memory that is never written to swap, and from there to enstate's + # incoming directory. What went last stays there, so populate sends only + # what changed. enstateSteps = let target' = lib.mkTarget target; - run = "/run/enstate/krops"; + run = "${hostConfig.enstate.providersDir}/krops"; pending = "${run}/pending"; stateDir = "/var/lib/enstate"; + # Named importers the host's slots use. + importers.secrets.command = [ "secrets" name "$(var:key)" ]; in { - # In a subshell, so its trap leaves the caller's alone. By channel - # name, so the same state packs the same. - packAndCopy = /* sh */ '' - ( - submissions=$( - TMPDIR=''${XDG_RUNTIME_DIR:-''${TMPDIR:-/tmp}} \ - mktemp -d --tmpdir ${name}-enstate.XXXXXXXX - ) - trap 'rm -fR "$submissions"' EXIT - ${lib.concatStrings (lib.mapAttrsToList (channel: sourceList: /* sh */ '' - ${local.pkgs.enstate}/bin/enstate \ - --pack ${pkgs.writers.writeJSON "${name}-enstate-${channel}.json" sourceList} \ - --out "$submissions"/${channel} - '') sourceLists)} - ${pkgs.rsync}/bin/rsync -a --checksum --delete --no-owner --no-group --chmod=D0700,F0600 \ - -e ${lib.escapeShellArg (lib.concatStringsSep " " ([ "ssh" ] ++ lib.mkUserPortSSHOpts target'))} \ - --rsync-path=${lib.escapeShellArg "mkdir -m 0700 -p ${run} ${pending} && rsync"} \ - "$submissions"/ ${lib.escapeShellArg "${target'.host}:${pending}/"} - ) - ''; + # By channel name, so the same state packs the same. + packAndCopy = pkgs.populate { + source = lib.evalSource [{ + pending.pipe = { + directory = true; + command = toString (pkgs.writers.writeDash "${name}-enstate-pack" '' + set -efu + ${lib.concatStrings (lib.mapAttrsToList (channel: sourceList: /* sh */ '' + ${local.pkgs.enstate}/bin/enstate \ + --pack ${pkgs.writers.writeJSON "${name}-enstate-${channel}.json" (lib.recursiveUpdate sourceList { meta.importer = importers; })} \ + --out "$1"/${channel} + '') sourceLists)} + ''); + }; + }]; + target = target' // { path = run; }; + force = true; + }; # Copied under a dot name, which enstate skips, and renamed: from memory # to disk, mv would copy, and enstate could see half a submission. movePending = /* sh */ '' mkdir -p -m 0700 ${stateDir} ${stateDir}/incoming stamp=$(date -u +%Y%m%dT%H%M%SZ)-$$ - find ${pending} -mindepth 1 -maxdepth 1 -printf '%f\n' | - while read -r channel; do + channels=$(find ${pending} -mindepth 1 -maxdepth 1 -printf '%f\n') + for channel in $channels; do cp -a ${pending}/"$channel" ${stateDir}/incoming/."$stamp-$channel" mv ${stateDir}/incoming/."$stamp-$channel" ${stateDir}/incoming/"$stamp-$channel" done ''; + # What movePending moved, removed again, with the new system's enstate. + forgetMoved = /* sh */ '' + for channel in $channels; do + "$system"/sw/bin/enstate --forget "$stamp-$channel" + done + ''; + # enstate's check before a switch, as its module runs it, of what waits. + check = /* sh */ '' + "$system"/sw/bin/enstate --migrate --dry-run \ + --schema "$system"/etc/enstate/schema.json \ + --config "$system"/etc/enstate/config.json + ''; + # Without a switch, nothing takes what waits: take it now, unless the + # system runs only after a reboot (a deploy with boot), which takes it. + takeNow = /* sh */ '' + if [ "$(readlink -f /run/current-system)" = "$(readlink -f "$system")" ]; then + "$system"/sw/bin/enstate --migrate + ${enstateSteps.showStatus ''"$system"''} + else + echo "enstate: what waits, the reboot into $system takes" >&2 + fi + ''; + # What needs attention, a refused submission or a required slot without + # an entry, shown, failing the deploy; quiet otherwise. + showStatus = system: /* sh */ '' + if ! status=$(${system}/sw/bin/enstate --status); then + printf '%s\n' "$status" + exit 1 + fi + ''; }; # Only the enstate submissions, which the running system takes. @@ -224,8 +263,16 @@ rec { ${enstateSteps.packAndCopy} ${pkgs.krops.runShell (lib.mkTarget target) {} /* sh */ '' set -efu + # The running generation isn't the profile's (after a deploy with boot, + # or test): it would plan what's meant for the one the next boot runs, + # and refuse what only that one has a slot for. + if [ "$(readlink -f /nix/var/nix/profiles/system)" != "$(readlink -f /run/current-system)" ]; then + echo "${name}: the running generation isn't the system profile's: reboot, or deploy" >&2 + exit 1 + fi ${enstateSteps.movePending} /run/current-system/sw/bin/enstate --migrate + ${enstateSteps.showStatus "/run/current-system"} ''} ''; @@ -237,10 +284,16 @@ rec { submit = deploy-action == "deploy" && sourceLists != {}; packAndCopy = lib.optionalString submit enstateSteps.packAndCopy; movePending = lib.optionalString submit enstateSteps.movePending; - # Without a switch, nothing takes what waits: take it now. - takeNow = lib.optionalString submit /* sh */ '' - "$system"/sw/bin/enstate --migrate - ''; + # A refused check leaves nothing waiting. + forgetMoved = lib.optionalString submit enstateSteps.forgetMoved; + showStatus = lib.optionalString submit (enstateSteps.showStatus ''"$system"''); + # NixOS' check of switch inhibitors skips boot, but not when run as + # check: for boot, enstate's alone. + preCheck = builtins.getAttr switch { + switch = ''"$system"/bin/switch-to-configuration check''; + boot = if submit then enstateSteps.check else ":"; + }; + takeNow = lib.optionalString submit enstateSteps.takeNow; in pkgs.writers.writeDash name' /* sh */ '' set -efu @@ -274,12 +327,15 @@ rec { old_gen=$(readlink -f /nix/var/nix/profiles/system) new_gen=$(readlink -f "$system") if [ "x$old_gen" != "x$new_gen" ]; then - ${lib.optionalString (switch == "switch") /* sh */ '' - "$system"/bin/switch-to-configuration check - export NIXOS_NO_CHECK=1 - ''} + if ! ( + ${preCheck} + ); then + ${forgetMoved} + exit 1 + fi nix-env -p /nix/var/nix/profiles/system --set "$system" - "$system"/bin/switch-to-configuration ${switch} + NIXOS_NO_CHECK=1 "$system"/bin/switch-to-configuration ${switch} + ${builtins.getAttr switch { switch = showStatus; boot = ""; }} else ${takeNow} echo 'Configuration is already up to date' >&2 @@ -585,23 +641,30 @@ rec { # The submissions wait for the switch, whose check plans them, and # which takes them. submit = sourceLists != {}; + showStatus = lib.optionalString submit (enstateSteps.showStatus ''"$system"''); + # NixOS' check of switch inhibitors skips boot, but not when run as + # check: for boot, enstate's alone. + preCheck = builtins.getAttr switch { + switch = ''"$system"/bin/switch-to-configuration check''; + boot = if submit then enstateSteps.check else ":"; + }; setAndSwitch = /* sh */ '' old_gen=$(readlink -f /nix/var/nix/profiles/system) new_gen=$(readlink -f "$system") if [ "x$old_gen" != "x$new_gen" ]; then - ${lib.optionalString (switch == "switch") /* sh */ '' - "$system"/bin/switch-to-configuration check - export NIXOS_NO_CHECK=1 - ''} + if ! ( + ${preCheck} + ); then + ${lib.optionalString submit enstateSteps.forgetMoved} + exit 1 + fi nix-env -p /nix/var/nix/profiles/system --set "$system" systemd-run --pipe --wait --collect --quiet \ - --setenv=NIXOS_NO_CHECK="''${NIXOS_NO_CHECK-}" \ + --setenv=NIXOS_NO_CHECK=1 \ "$system"/bin/switch-to-configuration ${switch} + ${builtins.getAttr switch { switch = showStatus; boot = ""; }} else - ${lib.optionalString submit /* sh */ '' - # Without a switch, nothing takes what waits: take it now. - "$system"/sw/bin/enstate --migrate - ''} + ${lib.optionalString submit enstateSteps.takeNow} echo 'Configuration is already up to date' >&2 fi ''; |
