feat: improve scripts and write tests

This commit is contained in:
alexm committed 2026-10-11 07:22:13 -04:00
1 parent bc3091647e
commit cf4bd07a8e
32 files changed
+3817 -723

No files matched your search

+189 -59
View File
@@ -1,81 +1,211 @@
#!/usr/bin/env bash
#
# Download the latest NixOS installer ISO into iso/.
#
# Saves the ISO under its real upstream snapshot name (e.g.
# nixos-graphical-25.11.10684.8fd9daa3db09-x86_64-linux.iso) so different
# channel heads don't clobber each other. Maintains a stable
# `latest-nixos-<edition>-<arch>.iso` symlink in iso/ pointing at the newest.
# The ISO is saved under its real upstream snapshot name (for example
# nixos-graphical-26.05.1234.abcdef-x86_64-linux.iso) so different channel
# heads don't clobber each other, next to a <name>.sha256 sidecar. A stable
# symlink latest-nixos-<edition>-<arch>.iso always points at the newest one.
#
# Downloads go to <name>.part and resume after an interruption. A file only
# gets its final name after its SHA-256 has been verified.
set -euo pipefail
SCRIPT_DIR="$(cd -- "$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")" && pwd -P)"
# shellcheck source=lib/common.sh
source "$SCRIPT_DIR/lib/common.sh"
usage() {
cat <<'EOF'
Usage: fetch-iso.sh [--channel CH] [--edition ED] [--arch ARCH] [--dest DIR]
--channel CH NixOS channel (default: nixos-26.05)
--edition ED graphical | minimal (default: graphical)
--arch ARCH target architecture (default: x86_64-linux)
--dest DIR output directory (default: iso)
-h, --help show this help
Environment:
NIXOS_CHANNELS_URL base URL override (default: https://channels.nixos.org)
Exit codes: 0 ok, 1 usage error, 2 network/parse failure, 3 checksum mismatch.
EOF
}
CHANNEL="nixos-26.05"
EDITION="graphical"
ARCH="x86_64-linux"
DEST_DIR="iso"
BASE_URL="${NIXOS_CHANNELS_URL:-https://channels.nixos.org}"
while (($#)); do
case "$1" in
--channel) CHANNEL="${2:?missing value}"; shift 2 ;;
--edition) EDITION="${2:?missing value}"; shift 2 ;;
--arch) ARCH="${2:?missing value}"; shift 2 ;;
--dest) DEST_DIR="${2:?missing value}"; shift 2 ;;
-h|--help)
awk '/^#!/ {next} /^[^#]/ {exit} {sub(/^# ?/, ""); print}' "$0"
exit 0 ;;
*) echo "[-] unknown arg: $1" >&2; exit 1 ;;
esac
done
CURL_COMMON=(--fail --location --show-error --connect-timeout 20 --retry 3 --retry-delay 2)
[[ "$BASE_URL" == https://* ]] && CURL_COMMON+=(--proto '=https' --proto-redir '=https')
BASE="https://channels.nixos.org/${CHANNEL}"
LATEST_NAME="latest-nixos-${EDITION}-${ARCH}.iso"
# Files we may need to clean up on exit.
TMP_SUM=""
cleanup() { [[ -z "$TMP_SUM" ]] || rm -f -- "$TMP_SUM"; }
trap cleanup EXIT
mkdir -p "$DEST_DIR"
parse_args() {
while (($#)); do
case "$1" in
--channel=*) CHANNEL="${1#*=}"; shift ;;
--edition=*) EDITION="${1#*=}"; shift ;;
--arch=*) ARCH="${1#*=}"; shift ;;
--dest=*) DEST_DIR="${1#*=}"; shift ;;
--channel) need_value "$@"; CHANNEL="$2"; shift 2 ;;
--edition) need_value "$@"; EDITION="$2"; shift 2 ;;
--arch) need_value "$@"; ARCH="$2"; shift 2 ;;
--dest) need_value "$@"; DEST_DIR="$2"; shift 2 ;;
-h|--help) usage; exit 0 ;;
*) die "unknown argument: $1 (see --help)" 1 ;;
esac
done
# Fetch the small sha256 file first. Its format is `<hash> <real-filename>`
# so we can derive the actual snapshot filename from it.
echo "[+] Fetching ${BASE}/${LATEST_NAME}.sha256"
tmp_sum="$(mktemp)"
trap 'rm -f "$tmp_sum"' EXIT
curl --fail --location --silent --show-error --output "$tmp_sum" \
"${BASE}/${LATEST_NAME}.sha256" \
|| { echo "[-] sha256 fetch failed" >&2; exit 2; }
local name value
for name in CHANNEL EDITION ARCH; do
value="${!name}"
[[ "$value" =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ ]] \
|| die "invalid --${name,,}: '$value'" 1
done
[[ -n "$DEST_DIR" ]] || die "--dest must not be empty" 1
}
real_name="$(awk '{print $2}' "$tmp_sum")"
if [[ -z "$real_name" || "$real_name" != *.iso ]]; then
echo "[-] couldn't parse filename from upstream sha256:" >&2
cat "$tmp_sum" >&2
exit 2
fi
# fetch_expected_hash: sets EXPECTED_HASH and REAL_NAME from the upstream
# .sha256 file, validating both: they come from the network and end up in
# file paths and in a checksum file.
fetch_expected_hash() {
local url="$1" hash name rest line
TMP_SUM="$(mktemp "$DEST_DIR/.sha256.XXXXXX")"
log "Fetching ${url}"
curl "${CURL_COMMON[@]}" --silent --max-time 60 --output "$TMP_SUM" "$url" \
|| die "sha256 fetch failed: $url" 2
iso="${DEST_DIR}/${real_name}"
sum="${DEST_DIR}/${real_name}.sha256"
# Format: "<64 hex> <filename>", where the filename may carry a leading
# '*' (binary-mode marker).
IFS= read -r line < "$TMP_SUM" || true
read -r hash name rest <<<"$line"
name="${name#\*}"
if [[ -f "$iso" && -f "$sum" ]]; then
if ( cd "$DEST_DIR" && sha256sum --status -c "${real_name}.sha256" ); then
echo "[+] ${iso} already present and verified."
ln -sfn "${real_name}" "${DEST_DIR}/${LATEST_NAME}"
exit 0
[[ "$hash" =~ ^[0-9a-fA-F]{64}$ ]] \
|| die "upstream checksum file doesn't start with a SHA-256 digest: '${line:0:120}'" 2
[[ -z "$rest" ]] \
|| die "upstream checksum filename contains spaces; refusing: '${line:0:120}'" 2
[[ "$name" =~ ^[A-Za-z0-9][A-Za-z0-9._+-]*\.iso$ && "$name" != *..* ]] \
|| die "upstream checksum names an unexpected file: '${name:0:120}'" 2
EXPECTED_HASH="${hash,,}"
REAL_NAME="$name"
rm -f -- "$TMP_SUM"
TMP_SUM=""
}
matches_hash() { # matches_hash FILE HASH
local actual
actual="$(sha256_of "$1")" || return 2
[[ "$actual" == "$2" ]]
}
write_sidecar() { # atomic: write HASH NAME to <iso>.sha256
local sum="$1" tmp
tmp="$(mktemp "$DEST_DIR/.sidecar.XXXXXX")"
printf '%s %s\n' "$EXPECTED_HASH" "$REAL_NAME" > "$tmp"
chmod 644 -- "$tmp"
mv -f -- "$tmp" "$sum"
}
publish() { # publish ISO: sidecar + latest symlink, then report
write_sidecar "${DEST_DIR}/${REAL_NAME}.sha256"
ln -sfn -- "$REAL_NAME" "${DEST_DIR}/${LATEST_NAME}"
log "${DEST_DIR}/${REAL_NAME}"
log "symlink: ${DEST_DIR}/${LATEST_NAME} -> ${REAL_NAME}"
}
# download PART_FILE: resumable download. Returns 0 on success, otherwise the
# curl exit code. A stale/unusable partial file is discarded and retried once.
download() {
local part="$1" url="$2" attempt rc code
local -a progress=(--silent)
[[ -t 2 ]] && progress=(--progress-bar)
for attempt in 1 2; do
rc=0
code="$(curl "${CURL_COMMON[@]}" "${progress[@]}" --continue-at - \
--write-out '%{http_code}' --output "$part" "$url")" || rc=$?
((rc == 0)) && return 0
# 33 = server can't resume; HTTP 416 = our partial file is longer
# than (or inconsistent with) the remote file. Both mean the partial
# data is useless, so start over once. Anything else (network drop,
# timeout, ...) keeps the partial file so the next run can resume.
if ((attempt == 1)) && { ((rc == 33)) || { ((rc == 22)) && [[ "$code" == 416 ]]; }; }; then
warn "can't resume the partial download; starting over"
rm -f -- "$part"
continue
fi
return "$rc"
done
}
main() {
parse_args "$@"
need_cmd curl
LATEST_NAME="latest-nixos-${EDITION}-${ARCH}.iso"
mkdir -p -- "$DEST_DIR"
# Two concurrent runs would corrupt each other's .part file.
if command -v flock >/dev/null 2>&1; then
exec 9>"$DEST_DIR/.fetch-iso.lock"
flock -n 9 || die "another fetch-iso is already running for $DEST_DIR" 1
fi
echo "[!] ${iso} present but sha256 mismatch — re-downloading from scratch."
# curl --continue-at would resume from the corrupt bytes; nuke first.
rm -f "$iso"
fi
mv "$tmp_sum" "$sum"
trap - EXIT
fetch_expected_hash "${BASE_URL}/${CHANNEL}/${LATEST_NAME}.sha256"
echo "[+] Downloading ${BASE}/${LATEST_NAME}"
echo " -> ${iso}"
curl --fail --location --progress-bar --continue-at - \
--output "$iso" "${BASE}/${LATEST_NAME}" \
|| { echo "[-] download failed" >&2; exit 2; }
local iso="${DEST_DIR}/${REAL_NAME}"
local part="${iso}.part"
echo "[+] Verifying sha256..."
( cd "$DEST_DIR" && sha256sum -c "${real_name}.sha256" ) \
|| { echo "[-] sha256 verification failed" >&2; exit 3; }
# Fast path: already have this exact ISO.
if [[ -f "$iso" ]]; then
if matches_hash "$iso" "$EXPECTED_HASH"; then
log "${iso} already present and verified."
publish
return 0
fi
warn "${iso} is present but its SHA-256 doesn't match upstream; replacing it."
rm -f -- "$iso"
fi
ln -sfn "${real_name}" "${DEST_DIR}/${LATEST_NAME}"
echo "[+] ${iso}"
echo "[+] symlink: ${DEST_DIR}/${LATEST_NAME} -> ${real_name}"
# A previous run may have finished downloading but died before verifying.
if [[ -f "$part" ]] && matches_hash "$part" "$EXPECTED_HASH"; then
log "Found a complete partial download; finalizing."
else
local pass resumed
for pass in 1 2; do
resumed=0
if [[ -f "$part" ]]; then
resumed=1
log "Resuming ${part} ($(human "$(stat -c %s -- "$part" 2>/dev/null || stat -f %z -- "$part")") so far)"
fi
log "Downloading ${BASE_URL}/${CHANNEL}/${LATEST_NAME}"
log " -> ${iso}"
download "$part" "${BASE_URL}/${CHANNEL}/${LATEST_NAME}" \
|| die "download failed; re-run to resume (partial file kept: $part)" 2
log "Verifying SHA-256..."
matches_hash "$part" "$EXPECTED_HASH" && break
rm -f -- "$part"
# Resuming glued new bytes onto a bad prefix: retry once from scratch.
if ((resumed && pass == 1)); then
warn "resumed download failed verification; discarding it and starting over"
continue
fi
die "SHA-256 verification failed; discarded the download" 3
done
fi
mv -f -- "$part" "$iso"
publish
}
main "$@"