Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions lib/run-switch.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
#!/usr/bin/env bash
# Canonical source for ~/run-switch.sh; deploy the laptop wrapper from this file.
#
# Guards (issue #3753, issue #3817):
# - flock: overlapping invocations are refused, naming the running
# switch's log and evaluated commit, so two switches cannot build
# concurrently or clobber each other's logs.
# - Evaluated-commit logging: every switch log records the flake
# source's HEAD commit and tree state (clean/dirty) at eval time.
# - HEAD-moved warning: if the source HEAD moves during the build
# (the 2026-11-01 gen-524 sequence), the end of the log warns
# loudly naming both commits — the activated generation was built
# from the evaluated commit, not the new HEAD.
set -uo pipefail
umask 077

home=${HOME:?HOME must be set}
lock_file=${RUN_SWITCH_LOCK_FILE:-"${home}/.run-switch.lock"}
log_dir=${RUN_SWITCH_LOG_DIR:-"${home}"}
flake=${RUN_SWITCH_FLAKE:-"${home}/code#amd"}
src_dir=${RUN_SWITCH_SRC:-"${home}/code"}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Monitor the repository selected by the flake override

When an existing caller sets RUN_SWITCH_FLAKE to another checkout without also setting the newly introduced RUN_SWITCH_SRC, the rebuild uses that checkout but the metadata and HEAD-movement guard inspect ${HOME}/code. Consequently, a successful switch can report an unrelated commit and miss changes to the actual source. The Nix manual defines direct path references as selecting the flake at that path. Derive the monitored source from the selected flake, or validate that the two settings agree instead of silently defaulting to a different repository.

Useful? React with 👍 / 👎.


if ! mkdir -p "${log_dir}"; then
printf 'run-switch: cannot create log directory: %s\n' "${log_dir}" >&2
Expand All @@ -24,21 +36,56 @@ fi

if ! flock -n "${lock_fd}"; then
holder_log=$(sed -n 's/^log=//p' "${lock_file}" | tail -n 1)
if [[ -n ${holder_log} ]]; then
printf 'run-switch: another switch is already running (active log: %s)\n' "${holder_log}" >&2
holder_commit=$(sed -n 's/^commit=//p' "${lock_file}" | tail -n 1)
refusal='run-switch: another switch is already running'
[[ -n ${holder_log} ]] && refusal+=" (active log: ${holder_log}"
[[ -n ${holder_commit} ]] && refusal+=", evaluated commit: ${holder_commit}"
[[ -n ${holder_log} ]] && refusal+=")"
[[ -z ${holder_log} && -n ${holder_commit} ]] && refusal+=" (evaluated commit: ${holder_commit})"
[[ -z ${holder_log} && -z ${holder_commit} ]] && refusal+=" (lock: ${lock_file})"
printf '%s\n' "${refusal}" >&2
exit 1
fi

# Capture the evaluated source state before the build starts. The flake
# is evaluated from src_dir at invocation time; that commit — not
# whatever HEAD is when the long build finishes — is what lands.
eval_commit=$(git -C "${src_dir}" rev-parse HEAD 2>/dev/null || printf 'unknown')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Bind the recorded commit to the source actually evaluated

If HEAD advances after this capture but before Nix reads the source—for example, while sudo waits for a password—the rebuild can evaluate the newer tree, but EVALUATED_COMMIT and the final warning incorrectly assert that the older commit was activated. Line 76 still passes the mutable flake path; the Nix manual specifies that without ref or rev, “files are fetched directly from the local path”. Snapshot/pin the source used by the rebuild, or obtain its actual evaluated revision rather than treating this pre-build observation as authoritative.

Useful? React with 👍 / 👎.

if [[ ${eval_commit} != unknown ]]; then
if [[ -z $(git -C "${src_dir}" status --porcelain 2>/dev/null) ]]; then
tree_state=clean
else
printf 'run-switch: another switch is already running (lock: %s)\n' "${lock_file}" >&2
tree_state=dirty
fi
exit 1
else
tree_state=unknown
fi

# Store holder details only after acquiring the lock. The file is informational;
# flock on the open descriptor is the authority.
printf 'pid=%s\nlog=%s\n' "$$" "${log_file}" >"${lock_file}"
printf 'pid=%s\nlog=%s\ncommit=%s\n' "$$" "${log_file}" "${eval_commit}" >"${lock_file}"
printf '%s\n' "${log_file}"

{
printf 'EVALUATED_COMMIT=%s\n' "${eval_commit}"
printf 'TREE_STATE=%s\n' "${tree_state}"
printf 'SWITCH_STARTED_AT=%s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
} >"${log_file}"

# shellcheck disable=SC2024 # The log file is user-owned; the shell opens it intentionally.
sudo -S -p '' nixos-rebuild switch --flake "${flake}" >"${log_file}" 2>&1
sudo -S -p '' nixos-rebuild switch --flake "${flake}" >>"${log_file}" 2>&1
switch_status=$?

end_commit=$(git -C "${src_dir}" rev-parse HEAD 2>/dev/null || printf 'unknown')
if [[ ${eval_commit} != unknown && ${end_commit} != unknown && ${end_commit} != "${eval_commit}" ]]; then
{
printf '*** WARNING: HEAD MOVED DURING BUILD ***\n'
printf 'WARNING: switch evaluated commit %s but %s HEAD is now %s; the activated generation was built from %s — re-run run-switch.sh to land %s.\n' \
"${eval_commit}" "${src_dir}" "${end_commit}" "${eval_commit}" "${end_commit}"
} >>"${log_file}"
printf 'run-switch: WARNING: HEAD moved during build (evaluated %s, now %s); see %s\n' \
"${eval_commit}" "${end_commit}" "${log_file}" >&2
fi

printf 'SWITCH_EXIT=%s\n' "${switch_status}" >>"${log_file}"
exit "${switch_status}"
100 changes: 100 additions & 0 deletions lib/run-switch.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,106 @@ if [[ ${call_count} -ne 2 ]] || ! grep -Fq "switch --flake ${RUN_SWITCH_FLAKE}"
fi
ok 'single invocation prints a unique log path and preserves the rebuild exit status'

# --- #3817: evaluated-commit logging + HEAD-moved (gen-524) drill ---
# Oracle: a real temp git repo is the flake source; its rev-parse HEAD
# before/after each run decides what the log must name.
src_repo="${TMP_ROOT}/src"
mkdir -p "${src_repo}"
git -C "${src_repo}" init -q
printf 'base\n' >"${src_repo}/flake.txt"
git -C "${src_repo}" add flake.txt
git -C "${src_repo}" -c user.email=drill@example.invalid -c user.name=drill commit -qm base
eval_sha=$(git -C "${src_repo}" rev-parse HEAD)
export RUN_SWITCH_SRC="${src_repo}"

export BLOCK_SWITCH=0
export SWITCH_EXIT_CODE=0
RUN_NAME=guarded "${SCRIPT}" >"${TMP_ROOT}/guarded.stdout" 2>"${TMP_ROOT}/guarded.stderr"
guarded_status=$?
guarded_log=$(<"${TMP_ROOT}/guarded.stdout")
if [[ ${guarded_status} -ne 0 ]]; then
fail "guarded single invocation exited ${guarded_status} instead of 0"
fi
if ! grep -Fxq "EVALUATED_COMMIT=${eval_sha}" "${guarded_log}"; then
fail 'switch log did not record the evaluated commit'
fi
if ! grep -Fxq 'TREE_STATE=clean' "${guarded_log}"; then
fail 'switch log did not record the clean tree state'
fi
if grep -Fq 'HEAD MOVED DURING BUILD' "${guarded_log}"; then
fail 'HEAD-moved warning fired although HEAD did not move'
fi
ok 'switch log records the evaluated commit and tree state'

# Gen-524 drill: HEAD moves while the (stubbed) build is running.
cat >"${TMP_ROOT}/bin/nixos-rebuild" <<'STUB'
#!/usr/bin/env bash
printf 'pid=%s args=%s\n' "$$" "$*" >>"$CALLS_LOG"
if [[ ${BLOCK_SWITCH:-0} == 1 ]]; then
: >"${SWITCH_STARTED:?}"
while [[ ! -e ${SWITCH_RELEASE:?} ]]; do sleep 0.02; done
fi
if [[ ${MOVE_HEAD:-0} == 1 ]]; then
printf 'drift\n' >>"${RUN_SWITCH_SRC}/flake.txt"
git -C "${RUN_SWITCH_SRC}" add flake.txt
git -C "${RUN_SWITCH_SRC}" -c user.email=drill@example.invalid -c user.name=drill commit -qm drift
fi
printf 'stub output for %s\n' "${RUN_NAME:-unnamed}"
exit "${SWITCH_EXIT_CODE:-0}"
STUB
chmod +x "${TMP_ROOT}/bin/nixos-rebuild"

export MOVE_HEAD=1
RUN_NAME=drift "${SCRIPT}" >"${TMP_ROOT}/drift.stdout" 2>"${TMP_ROOT}/drift.stderr"
drift_status=$?
drift_log=$(<"${TMP_ROOT}/drift.stdout")
moved_sha=$(git -C "${src_repo}" rev-parse HEAD)
if [[ ${drift_status} -ne 0 ]]; then
fail "gen-524 drill invocation exited ${drift_status} instead of 0"
fi
if [[ ${moved_sha} == "${eval_sha}" ]]; then
fail 'gen-524 drill stub did not move HEAD'
fi
if ! grep -Fxq "EVALUATED_COMMIT=${eval_sha}" "${drift_log}"; then
fail 'gen-524 drill log did not record the evaluated (stale) commit'
fi
if ! grep -Fq 'HEAD MOVED DURING BUILD' "${drift_log}"; then
fail 'gen-524 drill log did not warn loudly that HEAD moved during the build'
fi
if ! grep -Fq "${moved_sha}" "${drift_log}"; then
fail 'gen-524 drill warning did not name the new HEAD commit'
fi
ok 'gen-524 drill: a HEAD move during the build is caught loudly in the log, naming both commits'
export MOVE_HEAD=0

# Refusal must name the running switch's evaluated commit (#3817 acceptance).
export BLOCK_SWITCH=1
export SWITCH_STARTED="${TMP_ROOT}/guarded-started"
export SWITCH_RELEASE="${TMP_ROOT}/release-guarded"
RUN_NAME=holder "${SCRIPT}" >"${TMP_ROOT}/holder.stdout" 2>"${TMP_ROOT}/holder.stderr" &
first_pid=$!
if ! wait_for_file "${SWITCH_STARTED}"; then
fail 'holder invocation did not reach the stubbed rebuild command'
exit 1
fi
if timeout 2 env RUN_NAME=contender "${SCRIPT}" >"${TMP_ROOT}/contender.output" 2>&1; then
contender_status=0
else
contender_status=$?
fi
if [[ ${contender_status} -eq 0 ]]; then
fail 'contender was not refused while the holder switch ran'
fi
if ! grep -Fq "${moved_sha}" "${TMP_ROOT}/contender.output"; then
fail 'refusal did not name the running switch evaluated commit'
fi
touch "${SWITCH_RELEASE}"
wait "${first_pid}" || true
first_pid=
export BLOCK_SWITCH=0
ok 'refusal names the running switch evaluated commit'


if [[ ${failures} -ne 0 ]]; then
printf '%s test(s) failed\n' "${failures}" >&2
exit 1
Expand Down
Loading