#!/usr/bin/env bash # Download the latest NixOS installer ISO into iso/. # # 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 .sha256 sidecar. A stable # symlink latest-nixos--.iso always points at the newest one. # # Downloads go to .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}" CURL_COMMON=(--fail --location --show-error --connect-timeout 20 --retry 3 --retry-delay 2) [[ "$BASE_URL" == https://* ]] && CURL_COMMON+=(--proto '=https' --proto-redir '=https') # Files we may need to clean up on exit. TMP_SUM="" cleanup() { [[ -z "$TMP_SUM" ]] || rm -f -- "$TMP_SUM"; } trap cleanup EXIT 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 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 } # 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 # Format: "<64 hex> ", 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#\*}" [[ "$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 .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 fetch_expected_hash "${BASE_URL}/${CHANNEL}/${LATEST_NAME}.sha256" local iso="${DEST_DIR}/${REAL_NAME}" local part="${iso}.part" # 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 # 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 "$@"