diff options
| author | tv <tv@krebsco.de> | 2026-11-27 17:09:15 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-28 03:25:50 +0000 |
| commit | 1ad94ea306a984768a5daeb598e7ed771960657a (patch) | |
| tree | 9d56945788dcc5234f7f56f547e1d66915ae704c /krops.nix | |
| parent | 5b342adf15d04aa775c5b0dd630de61049d88ee8 (diff) | |
krops.nix: add bootimg, fastboot.boot, fastboot.flash
They build a host's boot image on a builder like its system, with GC
roots of its own, and fetch it as a file; the fastboot actions then
start it once, or write it and reboot.
Diffstat (limited to 'krops.nix')
| -rw-r--r-- | krops.nix | 255 |
1 files changed, 157 insertions, 98 deletions
@@ -8,6 +8,13 @@ # deploy build the system on the target and switch to it # diff build the system on the target and show what switching to it # would change +# bootimg build the boot image (spec.nix's bootimg, an attribute of the +# host's nixosConfigurations entry) on a builder, and copy it to +# $HOST_CACHEDIR/results/HOSTNAME.bootimg +# fastboot.boot +# build the boot image, and start it once +# fastboot.flash +# build the boot image, write it to the active slot, and reboot # # ARGUMENTS # name the host to deploy @@ -104,6 +111,17 @@ rec { hrm.flake.deploy = mkAction "flake" "deploy"; hrm.flake.diff = mkAction "flake" "diff"; + bootimg = mkBootimgAction "bootimg"; + fastboot.boot = mkBootimgAction "fastboot.boot"; + fastboot.flash = mkBootimgAction "fastboot.flash"; + + mkBootimgAction = action: + assert lib.assertMsg (spec.builders != []) + "${name}: ${action} needs a builder"; + assert lib.assertMsg (spec ? bootimg) + "${name}: spec.nix doesn't say where the boot image is (bootimg)"; + pkgs.writers.writeDash "${name}-${action}" (remoteBuildScript action); + mkAction = build-method: deploy-action: if spec.builders == [] then mkTargetBuildAction build-method deploy-action @@ -175,9 +193,26 @@ rec { ''} ''; - # The deploy or diff script for a host with builders. Its own attribute, + # The script of an action for a host with builders. Its own attribute, # so its text can be checked without building it. - remoteBuildScript = deploy-action: let + remoteBuildScript = action: let + # What gets built, and what its GC roots are called. + build = let + system = { + attr = "config.system.build.toplevel"; + root = name; + }; + bootimg = { + attr = spec.bootimg; + root = "${name}.bootimg"; + }; + in builtins.getAttr action { + deploy = system; + diff = system; + inherit bootimg; + "fastboot.boot" = bootimg; + "fastboot.flash" = bootimg; + }; target' = lib.mkTarget target; q = lib.escapeShellArg; nix = "nix --extra-experimental-features nix-command"; @@ -298,7 +333,7 @@ rec { # commands ssh passes it. builder=${q b.dest} builder_ssh_opts=${q b.sshOpts} \ builder_roots_quoted=${q (q b.path)} \ - builder_root_quoted=${q (q "${b.path}/${name}")} + builder_root_quoted=${q (q "${b.path}/${build.root}")} else echo "$name: builder "${q b.dest}" doesn't answer" >&2 fi @@ -330,9 +365,9 @@ rec { --setenv NIX_PATH ${q target'.path} \ nix-instantiate \ --show-trace \ - --add-root "$host_cache"/roots/"$name".drv \ + --add-root "$host_cache"/roots/${q build.root}.drv \ --argstr buildSystem "$builder_system" \ - --attr nixosConfigurations."$name".config.system.build.toplevel \ + --attr nixosConfigurations."$name".${q build.attr} \ '<hrm/legacy.nix>') # nix-instantiate prints the GC root, a link to the derivation here. drv=$(readlink -f -- "$drv") @@ -352,102 +387,126 @@ rec { readlink -- $builder_root_quoted") build_end=$(date +%s) - # - # Copy the system to the target. - # - copy_start=$(date +%s) - ${if spec.copyVia == "deployer" then /* sh */ '' - echo "$name: copying from $builder via this host..." >&2 - NIX_SSHOPTS=${q targetSSHOpts} \ - ${nix} copy --no-check-sigs \ - --from ssh-ng://"$builder" --to ${q targetURL} "$system" - '' else /* sh */ '' - if ! check=$(ssh $builder_ssh_opts "$builder" ${q /* sh */ - "ssh -o BatchMode=yes -o ConnectTimeout=10 ${targetSSHOpts} ${q targetDest} true" - } 2>&1) - then - echo "$name: error: $builder can't log in to "${q targetDest}":" >&2 - printf '%s\n' "$check" >&2 - case $check in - *'Host key verification failed'*) - echo "$name: $builder doesn't know the target's host key; add it" \ - "to $builder's known hosts (e.g. programs.ssh.knownHosts)." >&2 - ;; - *'Permission denied'*) - echo "$name: the target doesn't accept $builder's key; add it to" \ - "the target's authorized keys:" >&2 - ssh $builder_ssh_opts "$builder" 'cat ~/.ssh/id_*.pub' >&2 || : - ;; - *) - echo "$name: check the network between $builder and the target." >&2 - ;; - esac - exit 1 - fi - echo "$name: copying from $builder..." >&2 - ssh $builder_ssh_opts "$builder" ${q /* sh */ - "NIX_SSHOPTS=${q targetSSHOpts} ${nix} copy --no-check-sigs --to ${q targetURL} " - }"$system" - ''} - copy_end=$(date +%s) + '' + builtins.getAttr action rec { + diff = deploy; + bootimg = /* sh */ '' + # + # Fetch the boot image, a file, and keep it. Not into this host's + # store, which would want a signature, or a trusted user. + # + echo "$name: copying from $builder to this host..." >&2 + bootimg="$host_cache"/results/${q build.root} + mkdir -p "$host_cache"/results + ssh $builder_ssh_opts "$builder" "cat $system" > "$bootimg".tmp + mv -f "$bootimg".tmp "$bootimg" + echo "$bootimg" + ''; + "fastboot.boot" = bootimg + /* sh */ '' + ${pkgs.android-tools}/bin/fastboot boot "$bootimg" + ''; + "fastboot.flash" = bootimg + /* sh */ '' + ${pkgs.android-tools}/bin/fastboot flash boot "$bootimg" + ${pkgs.android-tools}/bin/fastboot reboot || + echo "$name: flashed, but the reboot failed; reboot the device yourself" >&2 + ''; + deploy = /* sh */ '' + # + # Copy the system to the target. + # + copy_start=$(date +%s) + ${if spec.copyVia == "deployer" then /* sh */ '' + echo "$name: copying from $builder via this host..." >&2 + NIX_SSHOPTS=${q targetSSHOpts} \ + ${nix} copy --no-check-sigs \ + --from ssh-ng://"$builder" --to ${q targetURL} "$system" + '' else /* sh */ '' + if ! check=$(ssh $builder_ssh_opts "$builder" ${q /* sh */ + "ssh -o BatchMode=yes -o ConnectTimeout=10 ${targetSSHOpts} ${q targetDest} true" + } 2>&1) + then + echo "$name: error: $builder can't log in to "${q targetDest}":" >&2 + printf '%s\n' "$check" >&2 + case $check in + *'Host key verification failed'*) + echo "$name: $builder doesn't know the target's host key; add it" \ + "to $builder's known hosts (e.g. programs.ssh.knownHosts)." >&2 + ;; + *'Permission denied'*) + echo "$name: the target doesn't accept $builder's key; add it to" \ + "the target's authorized keys:" >&2 + ssh $builder_ssh_opts "$builder" 'cat ~/.ssh/id_*.pub' >&2 || : + ;; + *) + echo "$name: check the network between $builder and the target." >&2 + ;; + esac + exit 1 + fi + echo "$name: copying from $builder..." >&2 + ssh $builder_ssh_opts "$builder" ${q /* sh */ + "NIX_SSHOPTS=${q targetSSHOpts} ${nix} copy --no-check-sigs --to ${q targetURL} " + }"$system" + ''} + copy_end=$(date +%s) - # - # Note what happened, for choosing builders later. - # - closure_size=$(NIX_SSHOPTS=$builder_ssh_opts \ - ${nix} path-info --store ssh-ng://"$builder" -S "$system" | cut -f2) - ${jq} -nc \ - --arg host "$name" --arg action ${q deploy-action} \ - --arg deployer "$(cat /etc/hostname)" --arg builder "$builder" \ - --arg system "$system" --arg via ${q spec.copyVia} \ - --argjson build_start "$build_start" --argjson build_end "$build_end" \ - --argjson copy_start "$copy_start" --argjson copy_end "$copy_end" \ - --argjson closure_size "$closure_size" \ - --argjson builder_load "$builder_load" --argjson builder_cpus "$builder_cpus" \ - --argjson builder_mem "$builder_mem" --argjson builder_disk "$builder_disk" \ - '$ARGS.named' >> "$portable_cache"/builds.jsonl + # + # Note what happened, for choosing builders later. + # + closure_size=$(NIX_SSHOPTS=$builder_ssh_opts \ + ${nix} path-info --store ssh-ng://"$builder" -S "$system" | cut -f2) + ${jq} -nc \ + --arg host "$name" --arg action ${q action} \ + --arg deployer "$(cat /etc/hostname)" --arg builder "$builder" \ + --arg system "$system" --arg via ${q spec.copyVia} \ + --argjson build_start "$build_start" --argjson build_end "$build_end" \ + --argjson copy_start "$copy_start" --argjson copy_end "$copy_end" \ + --argjson closure_size "$closure_size" \ + --argjson builder_load "$builder_load" --argjson builder_cpus "$builder_cpus" \ + --argjson builder_mem "$builder_mem" --argjson builder_disk "$builder_disk" \ + '$ARGS.named' >> "$portable_cache"/builds.jsonl - ${builtins.getAttr deploy-action { - deploy = let - setAndSwitch = /* sh */ '' - old_gen=$(readlink -f /nix/var/nix/profiles/system) - new_gen=$(readlink -f "$system") - if [ "x$old_gen" != "x$new_gen" ]; then - nix-env -p /nix/var/nix/profiles/system --set "$system" - systemd-run --pipe --wait --collect --quiet \ - "$system"/bin/switch-to-configuration ${switch} + ${builtins.getAttr action { + deploy = let + setAndSwitch = /* sh */ '' + old_gen=$(readlink -f /nix/var/nix/profiles/system) + new_gen=$(readlink -f "$system") + if [ "x$old_gen" != "x$new_gen" ]; then + nix-env -p /nix/var/nix/profiles/system --set "$system" + systemd-run --pipe --wait --collect --quiet \ + "$system"/bin/switch-to-configuration ${switch} + else + 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 { + switch = setAndSwitch; + boot = setAndSwitch; + test = /* sh */ '' + systemd-run --pipe --wait --collect --quiet \ + "$system"/bin/switch-to-configuration test + ''; + })} + ''; + diff = /* sh */ '' + old_drv=$(${onTarget /* sh */ '' + nix-store --query --deriver "$(readlink -f /run/current-system)" + ''}) + if nix-store --check-validity "$old_drv" 2>/dev/null; then + ${pkgs.nix-diff}/bin/nix-diff --environment --skip-already-compared \ + "$old_drv" "$drv" else - echo 'Configuration is already up to date' >&2 + echo "$name: no nix-diff: the running system's derivation isn't" \ + "here ($old_drv)" >&2 fi + ${onTarget /* sh */ '' + ${nix} store diff-closures /run/current-system "$system" + "$system"/bin/switch-to-configuration dry-activate + ''} ''; - in /* sh */ '' - # Without prune: the rest of the target's /var/src stays. - ${pkgs.populate { source = targetSource; target = target'; }} - ${onTarget (builtins.getAttr switch { - switch = setAndSwitch; - boot = setAndSwitch; - test = /* sh */ '' - systemd-run --pipe --wait --collect --quiet \ - "$system"/bin/switch-to-configuration test - ''; - })} - ''; - diff = /* sh */ '' - old_drv=$(${onTarget /* sh */ '' - nix-store --query --deriver "$(readlink -f /run/current-system)" - ''}) - if nix-store --check-validity "$old_drv" 2>/dev/null; then - ${pkgs.nix-diff}/bin/nix-diff --environment --skip-already-compared \ - "$old_drv" "$drv" - else - echo "$name: no nix-diff: the running system's derivation isn't" \ - "here ($old_drv)" >&2 - fi - ${onTarget /* sh */ '' - ${nix} store diff-closures /run/current-system "$system" - "$system"/bin/switch-to-configuration dry-activate - ''} - ''; - }} - ''; + }} + ''; + }; } |
