From cf4bd07a8ee7465ce1d2b38e63268e394d9ab46f Mon Sep 17 00:00:00 2001 From: Alex Maldonado Date: Sun, 11 Oct 2026 07:22:13 -0400 Subject: [PATCH] feat: improve scripts and write tests --- justfile | 10 +- scripts/capture-hardware.sh | 197 ++++--- scripts/docker-creds.sh | 237 +++++--- scripts/fetch-iso.sh | 248 ++++++-- scripts/gen-keys.sh | 124 +++- scripts/lib/common.sh | 68 +++ scripts/list-hosts.py | 77 +-- scripts/make-usb.sh | 677 +++++++++++++--------- scripts/nixflake.py | 258 +++++++++ scripts/register-host.py | 571 +++++++++++------- scripts/tests/harness.sh | 78 +++ scripts/tests/range_server.py | 96 +++ scripts/tests/run.sh | 38 ++ scripts/tests/shims/blockdev | 6 + scripts/tests/shims/dd | 10 + scripts/tests/shims/dmesg | 2 + scripts/tests/shims/findmnt | 16 + scripts/tests/shims/lsblk | 43 ++ scripts/tests/shims/mountpoint | 5 + scripts/tests/shims/nixos-generate-config | 19 + scripts/tests/shims/pass | 7 + scripts/tests/shims/ssh-keygen | 20 + scripts/tests/shims/sudo | 3 + scripts/tests/shims/umount | 5 + scripts/tests/shims/wipefs | 2 + scripts/tests/test_capture_hardware.sh | 170 ++++++ scripts/tests/test_docker_creds.sh | 186 ++++++ scripts/tests/test_fetch_iso.sh | 217 +++++++ scripts/tests/test_gen_keys.sh | 129 +++++ scripts/tests/test_make_usb.sh | 389 +++++++++++++ scripts/tests/test_python.py | 623 ++++++++++++++++++++ scripts/tests/usb_driver.sh | 9 + 32 files changed, 3817 insertions(+), 723 deletions(-) create mode 100644 scripts/lib/common.sh create mode 100644 scripts/nixflake.py create mode 100644 scripts/tests/harness.sh create mode 100644 scripts/tests/range_server.py create mode 100755 scripts/tests/run.sh create mode 100755 scripts/tests/shims/blockdev create mode 100755 scripts/tests/shims/dd create mode 100755 scripts/tests/shims/dmesg create mode 100755 scripts/tests/shims/findmnt create mode 100755 scripts/tests/shims/lsblk create mode 100755 scripts/tests/shims/mountpoint create mode 100755 scripts/tests/shims/nixos-generate-config create mode 100755 scripts/tests/shims/pass create mode 100755 scripts/tests/shims/ssh-keygen create mode 100755 scripts/tests/shims/sudo create mode 100755 scripts/tests/shims/umount create mode 100755 scripts/tests/shims/wipefs create mode 100755 scripts/tests/test_capture_hardware.sh create mode 100755 scripts/tests/test_docker_creds.sh create mode 100755 scripts/tests/test_fetch_iso.sh create mode 100755 scripts/tests/test_gen_keys.sh create mode 100755 scripts/tests/test_make_usb.sh create mode 100644 scripts/tests/test_python.py create mode 100755 scripts/tests/usb_driver.sh diff --git a/justfile b/justfile index 16d2285..ecfd45d 100644 --- a/justfile +++ b/justfile @@ -62,13 +62,17 @@ make-usb *args: # Scaffold hosts// and add a flake.nix entry. # Example: just register-host blackbear register-host name: - @./scripts/register-host.py --repo-root . "{{name}}" + @./scripts/register-host.py --repo-root . -- "$1" # Capture the current machine's hardware into hosts//hardware.nix. # Run this on the new machine. # Example: just capture-hardware blackbear capture-hardware name: - @./scripts/capture-hardware.sh "{{name}}" + @./scripts/capture-hardware.sh -- "$1" + +# Run the tests for scripts/. +test: + @./scripts/tests/run.sh # Show which hosts are currently registered in flake.nix. hosts: @@ -78,4 +82,4 @@ hosts: # Override store: just docker-creds secretservice # Unprotected key: just docker-creds pass --no-passphrase docker-creds store='pass' flag='': - @./scripts/docker-creds.sh "{{store}}" "{{flag}}" + @./scripts/docker-creds.sh "$1" "$2" diff --git a/scripts/capture-hardware.sh b/scripts/capture-hardware.sh index a6fcef0..bd799e6 100755 --- a/scripts/capture-hardware.sh +++ b/scripts/capture-hardware.sh @@ -1,82 +1,143 @@ #!/usr/bin/env bash -# Generate hardware.nix for a registered host. +# Generate hosts//hardware.nix for a registered host. # -# Run this on the NEW machine (the one whose hardware you want captured), -# inside this repo. Two contexts are auto-detected: +# Run this on the NEW machine (the one whose hardware you want to capture), +# from inside this repo. It works in two situations, detected automatically: +# +# * from the installer, with the target system mounted at /mnt: +# uses `nixos-generate-config --root /mnt` +# * on an already-running NixOS system: describes the running system +# +# The new file is generated into a temporary file and only moved into place +# once it looks valid, so a failure never destroys an existing hardware.nix. set -euo pipefail -NAME="" -REPO_ROOT="." +SCRIPT_DIR="$(cd -- "$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")" && pwd -P)" +# shellcheck source=lib/common.sh +source "$SCRIPT_DIR/lib/common.sh" -while (($#)); do - case "$1" in - --repo-root) REPO_ROOT="${2:?missing path}"; shift 2 ;; - -h|--help) - awk '/^#!/ {next} /^[^#]/ {exit} {sub(/^# ?/, ""); print}' "$0" - exit 0 ;; - -*) echo "[-] unknown flag: $1" >&2; exit 1 ;; - *) - if [[ -z "$NAME" ]]; then - NAME="$1"; shift - else - echo "[-] unexpected positional arg: $1" >&2; exit 1 - fi - ;; - esac -done +usage() { + cat <<'EOF' +Usage: capture-hardware.sh [options] NAME -if [[ -z "$NAME" ]]; then - echo "[-] usage: $0 " >&2 - exit 1 -fi + NAME host to capture (must already exist under hosts/; + create it with `just register-host NAME`) + --repo-root DIR repository root (default: current directory) + --install-root DIR where the installer mounts the target (default: /mnt) + -f, --force overwrite an existing, non-placeholder hardware.nix + without asking + -h, --help show this help -cd "$REPO_ROOT" -target="hosts/${NAME}/hardware.nix" +Exit codes: 0 ok, 1 usage/precondition error, 2 not on NixOS, 3 generation failed. +EOF +} -if [[ ! -d "hosts/${NAME}" ]]; then - echo "[-] hosts/${NAME} does not exist." >&2 - echo " Run \`just register-host ${NAME}\` first." >&2 - exit 1 -fi +# Placeholder files written by register-host.py contain this marker. +PLACEHOLDER_MARKER="PLACEHOLDER hardware.nix" -if ! command -v nixos-generate-config >/dev/null 2>&1; then - echo "[-] nixos-generate-config not found." >&2 - echo " Run this on a NixOS machine (running system or installer ISO)." >&2 - exit 2 -fi +NAME="" REPO_ROOT="." INSTALL_ROOT="/mnt" FORCE=0 +TMP_FILE="" +cleanup() { [[ -z "$TMP_FILE" ]] || rm -f -- "$TMP_FILE"; } +trap cleanup EXIT -# Detect installer-mounted-at-/mnt vs running-system. -extra_args=() -if mountpoint -q /mnt 2>/dev/null && [[ -d /mnt/etc ]]; then - echo "[+] Detected mounted target at /mnt — using --root /mnt" - extra_args+=(--root /mnt) -elif mountpoint -q /mnt 2>/dev/null; then - echo "[!] /mnt is mounted but doesn't look like a NixOS target (no /mnt/etc)." - echo " Continuing as if on a running system. Edit hardware.nix afterwards" - echo " if disks aren't permanent." -fi - -# Confirm before overwriting a non-placeholder hardware.nix. -if [[ -f "$target" ]] && ! grep -q 'PLACEHOLDER hardware.nix' "$target"; then - echo "[!] $target already exists and doesn't look like a placeholder." >&2 - if [[ -t 0 ]]; then - read -r -p "Overwrite? (yes/[no]) " confirm - [[ "$confirm" == "yes" ]] || { echo "aborted."; exit 1; } - else - echo "[-] non-interactive; refusing to overwrite. Delete $target first." >&2 - exit 1 +parse_args() { + while (($#)); do + case "$1" in + --repo-root) need_value "$@"; REPO_ROOT="$2"; shift 2 ;; + --install-root) need_value "$@"; INSTALL_ROOT="$2"; shift 2 ;; + -f|--force) FORCE=1; shift ;; + -h|--help) usage; exit 0 ;; + --) shift; break ;; + -*) die "unknown option: $1 (see --help)" 1 ;; + *) + [[ -z "$NAME" ]] || die "unexpected extra argument: $1" 1 + NAME="$1"; shift ;; + esac + done + # Anything left after `--` is the (single) name. + if (($#)); then + [[ -z "$NAME" && $# -eq 1 ]] || die "unexpected extra argument(s): $*" 1 + NAME="$1" fi -fi + [[ -n "$NAME" ]] || { usage >&2; exit 1; } + valid_hostname "$NAME" \ + || die "invalid hostname '$NAME' (lowercase letters, digits, hyphens; must start with a letter)" 1 +} -echo "[+] Generating hardware config..." -# The redirect runs in the user's shell BEFORE sudo, so the file is owned -# by the invoking user. sudo is needed because nixos-generate-config -# probes /proc, /sys, and may need to read mounted filesystems. -sudo nixos-generate-config --show-hardware-config "${extra_args[@]}" > "$target" -echo "[+] Wrote $target" +main() { + parse_args "$@" -if command -v git >/dev/null && git rev-parse >/dev/null 2>&1; then - echo "[+] git diff:" - git --no-pager diff -- "$target" || true -fi + [[ -d "$REPO_ROOT" ]] || die "repo root not found: $REPO_ROOT" 1 + cd -- "$REPO_ROOT" + [[ -f flake.nix ]] || die "no flake.nix in $PWD; run from the repo root or pass --repo-root" 1 + + local host_dir="hosts/${NAME}" + local target="${host_dir}/hardware.nix" + [[ -d "$host_dir" ]] || die "${host_dir} does not exist. Run \`just register-host ${NAME}\` first." 1 + + command -v nixos-generate-config >/dev/null 2>&1 \ + || die "nixos-generate-config not found. Run this on a NixOS machine (running system or installer ISO)." 2 + + # sudo is only needed when we aren't root already. + local -a as_root=() + if ((EUID != 0)); then + command -v sudo >/dev/null 2>&1 || die "not root and sudo is not installed" 1 + as_root=(sudo) + fi + + # Installer with the target mounted vs. running system. + local -a extra_args=() + if command -v mountpoint >/dev/null 2>&1 && mountpoint -q -- "$INSTALL_ROOT" 2>/dev/null; then + if [[ -d "$INSTALL_ROOT/etc" ]]; then + log "Detected mounted target at $INSTALL_ROOT; using --root $INSTALL_ROOT" + extra_args=(--root "$INSTALL_ROOT") + else + warn "$INSTALL_ROOT is mounted but doesn't look like a NixOS target (no etc/)." + warn "Continuing as if on a running system; check the filesystems in hardware.nix afterwards." + fi + fi + + # Don't silently replace a real, hand-edited hardware.nix. + if [[ -e "$target" ]] && ! grep -q -- "$PLACEHOLDER_MARKER" "$target" 2>/dev/null; then + warn "$target already exists and is not a placeholder." + if ((FORCE)); then + warn "--force given; overwriting." + elif [[ -t 0 ]]; then + local confirm + read -r -p "Overwrite? (yes/[no]) " confirm || confirm="" + [[ "$confirm" == "yes" ]] || die "aborted." 1 + else + die "non-interactive; refusing to overwrite. Pass --force or delete $target first." 1 + fi + fi + + # Generate into a temp file in the same directory (so the final mv is an + # atomic rename), validate it, then move it into place. + TMP_FILE="$(mktemp "${host_dir}/.hardware.nix.XXXXXX")" + + log "Generating hardware config..." + # The redirect is performed by this shell before sudo runs, so the file is + # owned by the invoking user. sudo is needed because nixos-generate-config + # probes /proc, /sys and mounted filesystems. + if ! "${as_root[@]+"${as_root[@]}"}" nixos-generate-config --show-hardware-config \ + "${extra_args[@]+"${extra_args[@]}"}" > "$TMP_FILE"; then + die "nixos-generate-config failed; $target was left untouched." 3 + fi + if [[ ! -s "$TMP_FILE" ]] || ! grep -Eq '^\{' "$TMP_FILE"; then + die "nixos-generate-config produced no usable output; $target was left untouched." 3 + fi + + chmod 644 -- "$TMP_FILE" + mv -f -- "$TMP_FILE" "$target" + TMP_FILE="" + log "Wrote $target" + + if command -v git >/dev/null 2>&1 && git rev-parse --is-inside-work-tree >/dev/null 2>&1; then + log "git diff:" + git --no-pager diff -- "$target" || true + info "Flakes only see git-tracked files: git add $target" + fi +} + +main "$@" diff --git a/scripts/docker-creds.sh b/scripts/docker-creds.sh index 9a90460..ffda5dc 100755 --- a/scripts/docker-creds.sh +++ b/scripts/docker-creds.sh @@ -1,85 +1,186 @@ #!/usr/bin/env bash +# Point Docker at a credential store and, for `pass`, set up GPG + pass. +# +# 1. Sets "credsStore" in ~/.docker/config.json (all other keys preserved). +# 2. For the `pass` store only: creates a dedicated GPG key (if there isn't +# one already) and initializes ~/.password-store with it. +# +# Safe to re-run: every step is skipped when already done. + set -euo pipefail -store="${1:-pass}" -no_passphrase=0 -[ "${2:-}" = "--no-passphrase" ] && no_passphrase=1 +SCRIPT_DIR="$(cd -- "$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")" && pwd -P)" +# shellcheck source=lib/common.sh +source "$SCRIPT_DIR/lib/common.sh" -config_dir="$HOME/.docker" -config_file="$config_dir/config.json" +usage() { + cat <<'EOF' +Usage: docker-creds.sh [STORE] [--no-passphrase] -# Identity used for the dedicated docker-creds GPG key. Stable so re-runs -# are idempotent (we look the key up by this uid rather than guessing). -gpg_name="Docker Credential Store" -gpg_email="${USER}@$(hostname)" -gpg_uid="$gpg_name <$gpg_email>" + STORE credential helper suffix: pass (default), secretservice, + osxkeychain, desktop, ... (uses docker-credential-STORE) + --no-passphrase pass only: create the GPG key WITHOUT a passphrase. + Anyone who can read ~/.gnupg can then decrypt your + registry credentials. + -h, --help show this help -# --- credsStore in config.json (merge, never clobber auths) ------------- -if ! command -v "docker-credential-$store" >/dev/null 2>&1; then - echo "warning: docker-credential-$store not found in PATH" >&2 - echo " is docker-credential-helpers installed and the rebuild applied?" >&2 -fi +Environment: DOCKER_CONFIG (default ~/.docker), PASSWORD_STORE_DIR (default ~/.password-store) + +Exit codes: 0 ok, 1 failure, 2 usage error. +EOF +} + +STORE="pass" +NO_PASSPHRASE=0 + +parse_args() { + local store_set=0 a + for a in "$@"; do + case "$a" in + "") ;; # `just` passes an empty string when a recipe argument is unset + -h|--help) usage; exit 0 ;; + --no-passphrase) NO_PASSPHRASE=1 ;; + -*) die "unknown option: $a (see --help)" 2 ;; + *) + ((store_set == 0)) || die "unexpected extra argument: $a" 2 + STORE="$a"; store_set=1 ;; + esac + done + [[ "$STORE" =~ ^[a-z0-9][a-z0-9._-]*$ ]] || die "invalid credential store name: '$STORE'" 2 +} + +# Merge credsStore into config.json without losing anything else. +# * refuses to touch a file that isn't a JSON object +# * writes atomically (temp file + rename), mode 0600 +# * if the path is a symlink (e.g. managed by home-manager) it is replaced +# by a regular file rather than writing through to a read-only target +set_creds_store() { + local config_file="$1" store="$2" + python3 - "$config_file" "$store" <<'PY' +import json, os, sys, tempfile -mkdir -p "$config_dir" -python3 - "$config_file" "$store" <<'PY' -import json, os, sys path, store = sys.argv[1], sys.argv[2] config = {} -if os.path.exists(path): - with open(path) as f: +if os.path.lexists(path): + try: + with open(path, encoding="utf-8") as f: + text = f.read() + except OSError as e: + sys.exit(f"cannot read {path}: {e}") + if text.strip(): try: - config = json.load(f) - except json.JSONDecodeError: - config = {} + config = json.loads(text) + except json.JSONDecodeError as e: + sys.exit(f"{path} is not valid JSON ({e}); refusing to overwrite it. " + "Fix or move it, then re-run.") + if not isinstance(config, dict): + sys.exit(f"{path} does not contain a JSON object; refusing to overwrite it.") + +if config.get("credsStore") == store and os.path.exists(path): + print(f'credsStore is already "{store}" in {path}') + sys.exit(0) + +if os.path.islink(path): + print(f"note: {path} was a symlink to {os.readlink(path)}; replacing it with a regular file", + file=sys.stderr) + config["credsStore"] = store -with open(path, "w") as f: - json.dump(config, f, indent=2) - f.write("\n") +directory = os.path.dirname(path) or "." +fd, tmp = tempfile.mkstemp(dir=directory, prefix=".config.json.", suffix=".tmp") +try: + with os.fdopen(fd, "w", encoding="utf-8") as f: # mkstemp creates it 0600 + json.dump(config, f, indent=2) + f.write("\n") + f.flush() + os.fsync(f.fileno()) + os.replace(tmp, path) +except BaseException: + try: + os.unlink(tmp) + except FileNotFoundError: + pass + raise +print(f'Set credsStore = "{store}" in {path}') PY -chmod u+w "$config_file" -echo "Set credsStore = \"$store\" in $config_file" +} -# The remaining steps only apply to the pass-backed store. -[ "$store" = "pass" ] || { echo "store is '$store'; skipping GPG/pass setup."; exit 0; } +main() { + parse_args "$@" + need_cmd python3 + # Check every tool up front so a missing one can't leave us half-configured. + [[ "$STORE" != "pass" ]] || need_cmd gpg pass -# --- GPG key (generate only if our uid doesn't already have one) -------- -if gpg --list-secret-keys "$gpg_uid" >/dev/null 2>&1; then - echo "GPG key for '$gpg_uid' already exists; skipping generation." -else - echo "Generating GPG key for '$gpg_uid'..." - protection="# (passphrase-protected; pinentry will prompt)" - if [ "$no_passphrase" -eq 1 ]; then - echo "warning: creating an UNPROTECTED key (--no-passphrase)." >&2 - echo " anyone who can read ~/.gnupg can decrypt your registry creds." >&2 - protection="%no-protection" - fi - gpg --batch --full-generate-key <&2; exit 1; } + # --- credsStore in config.json ------------------------------------------ + if ! command -v "docker-credential-$STORE" >/dev/null 2>&1; then + warn "docker-credential-$STORE not found in PATH" + warn " is docker-credential-helpers installed and the rebuild applied?" + fi + (umask 077 && mkdir -p -- "$config_dir") + set_creds_store "$config_file" "$STORE" || die "could not update $config_file" 1 -# --- pass init (only if the store isn't already initialized) ------------ -store_dir="${PASSWORD_STORE_DIR:-$HOME/.password-store}" -if [ -f "$store_dir/.gpg-id" ]; then - echo "pass already initialized at $store_dir; skipping init." -else - pass init "$fpr" -fi + # The remaining steps only apply to the pass-backed store. + if [[ "$STORE" != "pass" ]]; then + log "store is '$STORE'; skipping GPG/pass setup." + return 0 + fi -echo -echo "Done. Remaining manual step: docker login" + # Let pinentry-curses find the terminal. + if tty -s 2>/dev/null; then GPG_TTY="$(tty)"; export GPG_TTY; fi + + # --- GPG key (generate only if our uid doesn't already have one) -------- + # Stable identity so re-runs find the key instead of creating another. + local gpg_name="Docker Credential Store" user host gpg_email gpg_uid + user="$(id -un)" + host="$(hostname 2>/dev/null || uname -n)" + gpg_email="${user//[^A-Za-z0-9._-]/_}@${host//[^A-Za-z0-9._-]/_}" + gpg_uid="$gpg_name <$gpg_email>" + + if gpg --list-secret-keys "$gpg_uid" >/dev/null 2>&1; then + log "GPG key for '$gpg_uid' already exists; skipping generation." + else + log "Generating GPG key for '$gpg_uid'..." + local -a params=( + "%echo Generating docker-credential key" + "Key-Type: eddsa" + "Key-Curve: ed25519" + "Subkey-Type: ecdh" + "Subkey-Curve: cv25519" + "Name-Real: $gpg_name" + "Name-Email: $gpg_email" + "Expire-Date: 0" + ) + if ((NO_PASSPHRASE)); then + warn "creating an UNPROTECTED key (--no-passphrase)." + warn " anyone who can read ~/.gnupg can decrypt your registry creds." + params+=("%no-protection") + else + info "gpg will ask for a passphrase (via pinentry)." + fi + params+=("%commit" "%echo done") + printf '%s\n' "${params[@]}" | gpg --batch --full-generate-key \ + || die "GPG key generation failed" 1 + fi + + # Pull the fingerprint back out for pass. Don't `exit` early in awk: with + # pipefail that can SIGPIPE gpg and fail the whole pipeline at random. + local fpr + fpr="$(gpg --list-secret-keys --with-colons "$gpg_uid" \ + | awk -F: '/^fpr:/ && !seen { print $10; seen = 1 }')" + [[ "$fpr" =~ ^[0-9A-Fa-f]{40,}$ ]] || die "could not determine the GPG key fingerprint (got '${fpr:0:80}')" 1 + + # --- pass init (only if the store isn't already initialized) ------------ + local store_dir="${PASSWORD_STORE_DIR:-$HOME/.password-store}" + if [[ -f "$store_dir/.gpg-id" ]]; then + log "pass already initialized at $store_dir; skipping init." + else + pass init "$fpr" || die "pass init failed" 1 + fi + + echo >&2 + log "Done. Remaining manual step: docker login" +} + +main "$@" diff --git a/scripts/fetch-iso.sh b/scripts/fetch-iso.sh index ba3643d..9459d21 100755 --- a/scripts/fetch-iso.sh +++ b/scripts/fetch-iso.sh @@ -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--.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 .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}" -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 ` ` -# 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> ", 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 .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 "$@" diff --git a/scripts/gen-keys.sh b/scripts/gen-keys.sh index 3598999..50ae0e2 100755 --- a/scripts/gen-keys.sh +++ b/scripts/gen-keys.sh @@ -1,30 +1,112 @@ #!/usr/bin/env bash - -# Generate Ed25519 SSH keys for the given service names. +# Generate one Ed25519 SSH key per service name in ~/.ssh. +# +# Existing keys are never overwritten or modified. set -euo pipefail -if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then - awk '/^#!/ {next} /^[^#]/ {exit} {sub(/^# ?/, ""); print}' "$0" - exit 0 -fi +SCRIPT_DIR="$(cd -- "$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")" && pwd -P)" +# shellcheck source=lib/common.sh +source "$SCRIPT_DIR/lib/common.sh" -if (( $# == 0 )); then - echo "[+] No services specified; nothing to do." - exit 0 -fi +usage() { + cat <<'EOF' +Usage: gen-keys.sh [--passphrase] [--] SERVICE... -mkdir -p ~/.ssh -chmod 700 ~/.ssh +Creates ~/.ssh/id_ (+ .pub) for each SERVICE, an Ed25519 key with the +comment "@". Services whose key already exists are +skipped. -host_short="$(hostname -s)" + --passphrase prompt for a passphrase for each key (default: no passphrase) + -h, --help show this help -for service in "$@"; do - file="$HOME/.ssh/id_$service" - if [[ -f "$file" ]]; then - echo "[+] skipping: $file already exists" - continue +SERVICE may contain letters, digits, '.', '_' and '-' and must start with a +letter or digit. + +Exit codes: 0 ok, 1 a key could not be created, 2 usage error. +EOF +} + +main() { + local use_passphrase=0 + local -a services=() + + while (($#)); do + case "$1" in + -h|--help) usage; exit 0 ;; + --passphrase) use_passphrase=1; shift ;; + --) shift; services+=("$@"); break ;; + -*) die "unknown option: $1 (see --help)" 2 ;; + *) services+=("$1"); shift ;; + esac + done + + if ((${#services[@]} == 0)); then + log "No services specified; nothing to do." + return 0 fi - echo "[+] generating: $file" - ssh-keygen -t ed25519 -f "$file" -N "" -C "$service@$host_short" -done + + # Validate everything up front so a typo in the last name doesn't leave + # us with half the keys created. + local s + for s in "${services[@]}"; do + [[ "$s" =~ ^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$ ]] \ + || die "invalid service name: '$s' (letters, digits, . _ - only; max 64 chars)" 2 + done + + : "${HOME:?HOME is not set}" + need_cmd ssh-keygen + if ((use_passphrase)) && [[ ! -t 0 ]]; then + die "--passphrase needs an interactive terminal" 2 + fi + + local ssh_dir="$HOME/.ssh" + umask 077 + mkdir -p -- "$ssh_dir" + chmod 700 -- "$ssh_dir" + + local host_short + host_short="$(hostname -s 2>/dev/null || hostname 2>/dev/null || uname -n)" + host_short="${host_short//[^A-Za-z0-9._-]/_}" + + local failed=0 priv pub + local -a seen=() + for s in "${services[@]}"; do + # Skip duplicates on the command line. + if [[ " ${seen[*]:-} " == *" $s "* ]]; then continue; fi + seen+=("$s") + + priv="$ssh_dir/id_$s" + pub="$priv.pub" + + if [[ -e "$priv" || -L "$priv" ]]; then + log "skipping: $priv already exists" + continue + fi + if [[ -e "$pub" || -L "$pub" ]]; then + err "refusing: $pub exists without a matching private key; move it away first" + failed=1 + continue + fi + + log "generating: $priv" + # If interrupted mid-generation, don't leave half a keypair behind. + trap 'rm -f -- "$priv" "$pub"; exit 130' INT + trap 'rm -f -- "$priv" "$pub"; exit 143' TERM + local -a pass_args=(-N "") + ((use_passphrase)) && pass_args=() + if ssh-keygen -q -t ed25519 -f "$priv" -C "$s@$host_short" "${pass_args[@]+"${pass_args[@]}"}"; then + trap - INT TERM + info "public key: $pub" + else + trap - INT TERM + rm -f -- "$priv" "$pub" + err "ssh-keygen failed for '$s'" + failed=1 + fi + done + + return "$failed" +} + +main "$@" diff --git a/scripts/lib/common.sh b/scripts/lib/common.sh new file mode 100644 index 0000000..bc0f9e9 --- /dev/null +++ b/scripts/lib/common.sh @@ -0,0 +1,68 @@ +# shellcheck shell=bash +# Shared helpers for the scripts in this directory. Source it; don't execute it. +# +# SCRIPT_DIR="$(cd -- "$(dirname -- "$(readlink -f -- "${BASH_SOURCE[0]}")")" && pwd -P)" +# source "$SCRIPT_DIR/lib/common.sh" +# +# Conventions: human-readable status goes to stderr, data goes to stdout. +# Colors are disabled when stderr is not a terminal or NO_COLOR is set. + +[[ -n "${_NOTFILES_COMMON_SH:-}" ]] && return 0 +_NOTFILES_COMMON_SH=1 + +if [[ -t 2 && -z "${NO_COLOR:-}" ]]; then + C_GREEN=$'\e[32m' C_YELLOW=$'\e[33m' C_RED=$'\e[31m' C_BLUE=$'\e[34m' + C_BOLD=$'\e[1m' C_RESET=$'\e[0m' +else + C_GREEN="" C_YELLOW="" C_RED="" C_BLUE="" C_BOLD="" C_RESET="" +fi + +log() { printf '%s[+]%s %s\n' "$C_GREEN" "$C_RESET" "$*" >&2; } +info() { printf '%s[i]%s %s\n' "$C_BLUE" "$C_RESET" "$*" >&2; } +warn() { printf '%s[!]%s %s\n' "$C_YELLOW" "$C_RESET" "$*" >&2; } +err() { printf '%s[-]%s %s\n' "$C_RED" "$C_RESET" "$*" >&2; } + +# die MESSAGE [EXIT_CODE] +die() { err "$1"; exit "${2:-1}"; } + +# need_value FLAG "$@"-remaining: fail clearly if an option is missing its value. +# --iso) need_value "$@"; ISO="$2"; shift 2 ;; +need_value() { + (($# >= 2)) || die "option $1 requires a value" 2 +} + +# need_cmd CMD...: fail unless every command is on PATH. +need_cmd() { + local missing=() c + for c in "$@"; do + command -v "$c" >/dev/null 2>&1 || missing+=("$c") + done + ((${#missing[@]} == 0)) || die "missing required command(s): ${missing[*]}" 3 +} + +# Must stay in sync with HOST_RE in register-host.py. +valid_hostname() { + [[ "$1" =~ ^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$ ]] +} + +# sha256_of FILE: print the lowercase hex digest. Works with GNU coreutils, +# macOS (shasum), or OpenSSL. +sha256_of() { + local out + if command -v sha256sum >/dev/null 2>&1; then + out="$(sha256sum -- "$1")" || return 1 + elif command -v shasum >/dev/null 2>&1; then + out="$(shasum -a 256 -- "$1")" || return 1 + elif command -v openssl >/dev/null 2>&1; then + out="$(openssl dgst -sha256 -r -- "$1")" || return 1 + else + err "no sha256 tool found (need sha256sum, shasum, or openssl)" + return 1 + fi + printf '%s\n' "${out%% *}" +} + +# human BYTES: 1073741824 -> 1.0GiB (falls back to raw bytes without numfmt). +human() { + numfmt --to=iec-i --suffix=B "$1" 2>/dev/null || printf '%s B\n' "$1" +} diff --git a/scripts/list-hosts.py b/scripts/list-hosts.py index 6c8cb0e..278b75f 100755 --- a/scripts/list-hosts.py +++ b/scripts/list-hosts.py @@ -1,53 +1,56 @@ #!/usr/bin/env python3 """ -Print the nixosConfigurations defined in flake.nix. +Print the hosts defined under nixosConfigurations in flake.nix, one per line. -Parses flake.nix directly (no `nix eval`, so it works without nix tooling -and is fast). Brace-matches to find the closing `}` of nixosConfigurations -so it handles nested attrsets correctly. +Parses flake.nix textually (no `nix eval`), so it is fast and works without +Nix installed. Comments, strings and ${} interpolations are ignored, so a +brace or a commented-out host can't confuse it. + +Exit codes: 0 ok, 1 flake missing/unreadable/unrecognized, 2 usage. """ -import re +from __future__ import annotations + +import argparse import sys from pathlib import Path - -def list_hosts(flake_path: Path) -> list[str]: - src = flake_path.read_text() - m = re.search(r"nixosConfigurations\s*=\s*\{", src) - if not m: - return [] - - # Brace-match to find the matching closing }. - depth, i, close = 0, m.end() - 1, None - while i < len(src): - c = src[i] - if c == "{": - depth += 1 - elif c == "}": - depth -= 1 - if depth == 0: - close = i - break - i += 1 - - block = src[m.end():close] if close is not None else "" - return re.findall( - r"^[ \t]*([A-Za-z_][\w-]*)\s*=\s*nixpkgs\.lib\.nixosSystem", - block, - re.M, - ) +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import nixflake # noqa: E402 +from nixflake import FlakeError # noqa: E402 -def main() -> int: - flake = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("flake.nix") - if not flake.is_file(): - print(f"[-] not a file: {flake}", file=sys.stderr) +def list_hosts(src: str) -> list[str]: + return [h.name for h in nixflake.find_hosts(src)] + + +def main(argv: list[str] | None = None) -> int: + p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + p.add_argument("flake", nargs="?", default="flake.nix", type=Path, help="path to flake.nix (default: ./flake.nix)") + args = p.parse_args(argv) + + if not args.flake.is_file(): + print(f"[-] not a file: {args.flake}", file=sys.stderr) return 1 - for name in list_hosts(flake): + try: + src = args.flake.read_bytes().decode("utf-8") + hosts = list_hosts(src) + except (OSError, UnicodeDecodeError) as e: + print(f"[-] cannot read {args.flake}: {e}", file=sys.stderr) + return 1 + except FlakeError as e: + print(f"[-] {args.flake}: {e}", file=sys.stderr) + return 1 + + if not hosts: + print(f"[!] no hosts defined in {args.flake}", file=sys.stderr) + for name in hosts: print(name) return 0 if __name__ == "__main__": - sys.exit(main()) + try: + sys.exit(main()) + except BrokenPipeError: # e.g. `list-hosts.py | head -1` + sys.exit(0) diff --git a/scripts/make-usb.sh b/scripts/make-usb.sh index 62faabd..4944cfd 100755 --- a/scripts/make-usb.sh +++ b/scripts/make-usb.sh @@ -1,83 +1,424 @@ #!/usr/bin/env bash # Write a NixOS installer ISO to a USB stick (raw dd). # -# The script self-elevates via sudo if not already running as root, so -# you can run it as your normal user. +# DESTRUCTIVE: everything on the target disk is erased. Safeguards: +# * only whole disks are accepted (never partitions) +# * a disk holding anything but auto-mount locations (/, /boot, /nix, /home, +# swap, ...) is refused outright +# * non-removable, non-USB disks need --allow-internal +# * the ISO must not live on the disk being erased +# * you must retype the device path to confirm (unless --yes) +# * the written image is read back and compared with the ISO # -# After booting the resulting USB on the target machine, fetch this repo -# over the network with `git clone ` from the installer shell. +# The script re-executes itself under sudo if it isn't root. set -euo pipefail +SCRIPT_PATH="$(readlink -f -- "${BASH_SOURCE[0]}")" +SCRIPT_DIR="$(dirname -- "$SCRIPT_PATH")" +# shellcheck source=lib/common.sh +source "$SCRIPT_DIR/lib/common.sh" +usage() { + cat <<'EOF' +Usage: make-usb.sh [options] -if [[ -t 1 ]]; then - GREEN=$'\e[32m'; YELLOW=$'\e[33m'; RED=$'\e[31m'; BLUE=$'\e[34m' - BOLD=$'\e[1m'; RESET=$'\e[0m' -else - GREEN=""; YELLOW=""; RED=""; BLUE=""; BOLD=""; RESET="" -fi -log() { printf '%s[+]%s %s\n' "$GREEN" "$RESET" "$*"; } -warn() { printf '%s[!]%s %s\n' "$YELLOW" "$RESET" "$*" >&2; } -err() { printf '%s[-]%s %s\n' "$RED" "$RESET" "$*" >&2; } -info() { printf '%s[i]%s %s\n' "$BLUE" "$RESET" "$*"; } -die() { err "$1"; exit "${2:-1}"; } - + --iso PATH ISO to write (prompted for if omitted) + --device DEV target disk, e.g. /dev/sdb (chosen from a menu if omitted) + --allow-internal allow disks that are neither removable nor on the USB bus + --no-verify skip reading the written image back for comparison + -y, --yes don't ask for confirmation (requires --iso and --device) + -h, --help show this help +Exit codes: 0 ok, 1 aborted/other, 2 usage, 3 missing tools, 4 unsafe or +invalid device/ISO, 5 checksum or write failure, 6 read-back verification failed. +EOF +} ISO_PATH="" DEVICE="" ALLOW_INTERNAL=0 ASSUME_YES=0 +VERIFY=1 +ORIG_ARGS=() +ISO_BYTES=0 +DEV_BYTES=0 -# Save original args before the parser shifts them away; needed if we -# later re-exec under sudo. -ORIG_ARGS=("$@") +# /proc/swaps path; overridable so tests can fake it. +PROC_SWAPS="${PROC_SWAPS:-/proc/swaps}" -while (($#)); do +parse_args() { + while (($#)); do + case "$1" in + --iso=*) ISO_PATH="${1#*=}"; shift ;; + --device=*|--dev=*) DEVICE="${1#*=}"; shift ;; + --iso) need_value "$@"; ISO_PATH="$2"; shift 2 ;; + --device|--dev) need_value "$@"; DEVICE="$2"; shift 2 ;; + --allow-internal) ALLOW_INTERNAL=1; shift ;; + --no-verify) VERIFY=0; shift ;; + -y|--yes) ASSUME_YES=1; shift ;; + -h|--help) usage; exit 0 ;; + *) die "unknown argument: $1 (see --help)" 2 ;; + esac + done +} + +# -------------------------------------------------------------------------- +# Block-device helpers. Everything below talks to lsblk/findmnt only through +# fixed argument lists and never evals or word-splits device-reported text, +# since model strings and labels are attacker-controlled (any USB device can +# claim any model name). +# -------------------------------------------------------------------------- + +is_block_device() { [[ -b "$1" ]]; } + +# blk_field DEVICE COLUMN: one lsblk column for one device, reduced to +# printable ASCII and trimmed. Empty if unknown. +blk_field() { + local v + v="$(lsblk -dn -o "$2" "$1" 2>/dev/null || true)" + v="${v%%$'\n'*}" + v="$(printf '%s' "$v" | LC_ALL=C tr -cd '[:print:]')" + v="${v#"${v%%[![:space:]]*}"}" + printf '%s' "${v%"${v##*[![:space:]]}"}" +} + +# list_candidate_disks: whole disks only, one path per line. +list_candidate_disks() { + local name type + lsblk -dnpr -o NAME,TYPE 2>/dev/null | while read -r name type; do + [[ "$type" == disk ]] || continue + case "$name" in + /dev/sr*|/dev/loop*|/dev/ram*|/dev/zram*|/dev/dm-*|/dev/nbd*) continue ;; + esac + printf '%s\n' "$name" + done +} + +# device_tree DEVICE: the device plus all partitions and holders (LUKS, LVM...). +device_tree() { lsblk -lnp -o NAME "$1" 2>/dev/null || true; } + +# mounts_of DEVICE: mount points backed by the disk or anything stacked on it. +# Returns nonzero if the mount table can't be read; callers must treat that as +# "unknown", never as "nothing mounted". +mounts_of() { + local tree table src tgt + tree="$(device_tree "$1")" + [[ -n "$tree" ]] || return 0 + table="$(findmnt -rn -o SOURCE,TARGET 2>/dev/null)" || return 1 + while read -r src tgt; do + src="${src%%\[*}" # btrfs/bind: /dev/sda2[/@] -> /dev/sda2 + if grep -qxF -- "$src" <<<"$tree"; then + printf '%b\n' "$tgt" # findmnt -r escapes spaces as \x20 + fi + done <<<"$table" +} + +# has_swap DEVICE: true if swap is active on the disk or anything stacked on +# it. Also true (fail closed) if we can't read the swap table at all. +has_swap() { + local tree s + [[ -r "$PROC_SWAPS" ]] || return 0 + tree="$(device_tree "$1")" + while read -r s _; do + [[ -n "$s" && "$s" != Filename ]] || continue + grep -qxF -- "$s" <<<"$tree" && return 0 + done < "$PROC_SWAPS" + return 1 +} + +# Mount points we are willing to unmount automatically (desktop auto-mounts). +is_automount_point() { case "$1" in - --iso) ISO_PATH="${2:?missing path}"; shift 2 ;; - --device|--dev) DEVICE="${2:?missing path}"; shift 2 ;; - --allow-internal) ALLOW_INTERNAL=1; shift ;; - --yes|-y) ASSUME_YES=1; shift ;; - -h|--help) - awk '/^#!/ {next} /^[^#]/ {exit} {sub(/^# ?/, ""); print}' "$0" - exit 0 ;; - *) die "unknown arg: $1" 2 ;; + /run/media/*|/media/*|/mnt/*|/run/mnt/*) return 0 ;; + *) return 1 ;; esac -done +} - - -# Self-elevate via sudo if not already root. We re-exec rather than tell -# the user to prefix the command, so `just make-usb` and `./scripts/make-usb.sh` -# both Just Work. -if (( EUID != 0 )); then - if ! command -v sudo >/dev/null 2>&1; then - die "must run as root and sudo is not installed" 1 +# protected_mounts DEVICE: print every in-use mount (or [SWAP]) on this disk +# that must NOT be unmounted behind the user's back. Allowlist, not blocklist: +# any mount point we don't positively recognize as an auto-mount protects it. +protected_mounts() { + local mounts mp + if ! mounts="$(mounts_of "$1")"; then + printf '[mount table unreadable]\n' + return 0 fi - # ORIG_ARGS may be empty; ${ORIG_ARGS[@]+"${ORIG_ARGS[@]}"} expands - # safely under set -u even when the array is unset. - exec sudo --preserve-env=PATH -- "$0" ${ORIG_ARGS[@]+"${ORIG_ARGS[@]}"} -fi + while IFS= read -r mp; do + [[ -n "$mp" ]] || continue + is_automount_point "$mp" || printf '%s\n' "$mp" + done <<<"$mounts" + if has_swap "$1"; then printf '[SWAP or swap table unreadable]\n'; fi +} -# Require an interactive tty for any prompt we still need. -need_tty=0 -[[ -z "$DEVICE" || -z "$ISO_PATH" ]] && need_tty=1 -(( ASSUME_YES )) || need_tty=1 -if (( need_tty )) && ! [[ -t 0 ]]; then - die "stdin is not a tty; pass --iso, --device, and --yes for non-interactive use" 2 -fi +is_external() { + [[ "$(blk_field "$1" RM)" == 1 || "$(blk_field "$1" TRAN)" == usb ]] +} -# Required commands. -REQUIRED=(lsblk dd sync wipefs blockdev mount umount awk) -missing=() -for cmd in "${REQUIRED[@]}"; do - command -v "$cmd" >/dev/null 2>&1 || missing+=("$cmd") -done -if ((${#missing[@]})); then - err "missing tools: ${missing[*]}" - cat >&2 <<'EOF' +# unsafe_reason DEVICE: if the device must not be written, print why and +# return 0. Returns 1 (printing nothing) if it is acceptable. +unsafe_reason() { + local dev="$1" type mounts + if ! is_block_device "$dev"; then + printf '%s is not a block device\n' "$dev"; return 0 + fi + if [[ -z "$(device_tree "$dev")" ]]; then + printf 'cannot inspect %s with lsblk; refusing\n' "$dev"; return 0 + fi + type="$(blk_field "$dev" TYPE)" + if [[ "$type" != disk ]]; then + printf '%s is not a whole disk (type: %s); pass the disk, not a partition\n' "$dev" "${type:-unknown}" + return 0 + fi + mounts="$(protected_mounts "$dev")" + if [[ -n "$mounts" ]]; then + printf '%s holds in-use system data (%s); refusing\n' "$dev" "$(tr '\n' ' ' <<<"$mounts" | sed 's/ $//')" + return 0 + fi + if ((! ALLOW_INTERNAL)) && ! is_external "$dev"; then + printf '%s is neither removable nor on the USB bus; pass --allow-internal if you really mean it\n' "$dev" + return 0 + fi + return 1 +} + +# iso_on_device ISO DEVICE: true if the ISO file lives on the disk we're about to wipe. +iso_on_device() { + local src + src="$(findmnt -n -o SOURCE --target "$1" 2>/dev/null || true)" + src="${src%%\[*}" + [[ -n "$src" ]] || return 1 + grep -qxF -- "$src" <<<"$(device_tree "$2")" +} + +# -------------------------------------------------------------------------- +# Interactive pieces +# -------------------------------------------------------------------------- + +select_device() { + local -a cands=() + mapfile -t cands < <(list_candidate_disks) + ((${#cands[@]})) || die "no candidate disks found" 4 + + log "Available whole disks:" + printf ' %-3s %-14s %-9s %-6s %-12s %s\n' Idx Device Size Bus Kind Model >&2 + local i=0 d size bus model kind color + for d in "${cands[@]}"; do + i=$((i + 1)) + size="$(human "$(blockdev --getsize64 "$d" 2>/dev/null || echo 0)")" + bus="$(blk_field "$d" TRAN)" + model="$(blk_field "$d" MODEL)" + if [[ -n "$(protected_mounts "$d")" ]]; then + kind="SYSTEM DISK"; color="$C_RED" + elif is_external "$d"; then + kind="removable"; color="$C_GREEN" + else + kind="internal"; color="$C_YELLOW" + fi + printf ' %-3s %-14s %-9s %-6s %s%-12s%s %s\n' \ + "$i" "$d" "$size" "${bus:-?}" "$color" "$kind" "$C_RESET" "${model:-?}" >&2 + done + echo >&2 + + local idx reason + while :; do + read -r -p "Select device [1-${#cands[@]}, 0 to abort]: " idx || die "unexpected end of input" 1 + [[ "$idx" =~ ^[0-9]{1,4}$ ]] || { warn "not a number"; continue; } + idx=$((10#$idx)) # "08" is a number, not invalid octal + ((idx != 0)) || die "aborted by user" 1 + ((idx <= ${#cands[@]})) || { warn "out of range"; continue; } + if reason="$(unsafe_reason "${cands[idx - 1]}")"; then + err "$reason" + continue + fi + DEVICE="${cands[idx - 1]}" + return 0 + done +} + +# describe_device DEVICE: "/dev/sdb (14.9GiB, SanDisk Ultra)" +describe_device() { + local model; model="$(blk_field "$1" MODEL)" + printf '%s (%s%s)' "$1" "$(human "$DEV_BYTES")" "${model:+, $model}" +} + +resolve_device() { + local reason + if [[ -z "$DEVICE" ]]; then + select_device + else + DEVICE="$(readlink -f -- "$DEVICE")" # /dev/disk/by-id/... -> /dev/sdX + if reason="$(unsafe_reason "$DEVICE")"; then die "$reason" 4; fi + fi + DEV_BYTES="$(blockdev --getsize64 "$DEVICE")" + log "Target: $(describe_device "$DEVICE")" + if ((DEV_BYTES > 512 * 1024 * 1024 * 1024)); then + warn "$DEVICE is larger than 512 GiB; that is unusual for a USB stick." + fi +} + +# The invoking user's home, even under sudo (HOME may be /root there). +invoking_home() { + local h="" + if [[ -n "${SUDO_USER:-}" ]] && command -v getent >/dev/null 2>&1; then + h="$(getent passwd "$SUDO_USER" | cut -d: -f6)" + fi + printf '%s' "${h:-${HOME:-/}}" +} + +resolve_iso() { + if [[ -z "$ISO_PATH" ]]; then + local home; home="$(invoking_home)" + while :; do + read -r -e -p "Path to NixOS ISO: " ISO_PATH || die "unexpected end of input" 1 + ISO_PATH="${ISO_PATH/#\~/$home}" + [[ -f "$ISO_PATH" ]] && break + warn "not a file: $ISO_PATH" + done + fi + [[ -f "$ISO_PATH" ]] || die "ISO not found: $ISO_PATH" 4 + ISO_BYTES="$(stat -c '%s' -- "$ISO_PATH")" + ((ISO_BYTES > 0)) || die "ISO is empty: $ISO_PATH" 4 + log "ISO: $ISO_PATH ($(human "$ISO_BYTES"))" + + if command -v file >/dev/null 2>&1; then + local ftype; ftype="$(file -b -- "$ISO_PATH")" + if ! grep -qiE 'iso 9660|udf' <<<"$ftype"; then + warn "$ftype" + warn "file(1) doesn't recognize this as an ISO 9660 image. Continuing anyway." + fi + fi + + ((ISO_BYTES <= DEV_BYTES)) || die "ISO ($(human "$ISO_BYTES")) is larger than the disk ($(human "$DEV_BYTES"))" 4 + + if iso_on_device "$ISO_PATH" "$DEVICE"; then + die "the ISO is stored on $DEVICE, which is about to be erased; copy it elsewhere first" 4 + fi + + # Verify against a sha256 sidecar if there is one. Resolve symlinks so + # `--iso iso/latest-...iso` finds the sidecar next to the real file. + local real; real="$(readlink -f -- "$ISO_PATH")" + if [[ -f "$real.sha256" ]]; then + log "Verifying $real.sha256..." + ( cd -- "$(dirname -- "$real")" && sha256sum -c -- "$(basename -- "$real").sha256" >&2 ) \ + || die "sha256 verification failed" 5 + else + warn "no $(basename -- "$real").sha256 next to the ISO; skipping checksum verification" + fi +} + +confirm_write() { + cat >&2 </dev/null + + log "Writing ISO with dd (this can take a few minutes)..." + if ! dd if="$ISO_PATH" of="$DEVICE" bs=4M status=progress conv=fsync oflag=direct; then + err "dd failed." + # Surface recent kernel complaints so bad flash ("Medium Error") can be + # told apart from a flaky cable/port ("reset", "Communication failure"). + local dev_short dmesg_out + dev_short="$(basename -- "$DEVICE")" + if dmesg_out="$(dmesg 2>/dev/null | grep -E "(${dev_short}|usb [0-9]+-[0-9]+)" | tail -10)" \ + && [[ -n "$dmesg_out" ]]; then + warn "Recent kernel messages mentioning ${dev_short} or USB events:" + sed 's/^/ /' <<<"$dmesg_out" >&2 + warn "'Medium Error' / 'Hardware Error' => bad flash, replace the stick." + warn "'reset' / 'Communication failure' => cable or port, try another." + else + warn "Run \`sudo dmesg | tail -30\` to see why the kernel rejected the write." + fi + exit 5 + fi + + log "Final sync (flushing kernel buffers; may take a moment)..." + sync + blockdev --flushbufs "$DEVICE" 2>/dev/null || true +} + +verify_write() { + if ((! VERIFY)); then + warn "skipping read-back verification (--no-verify)" + return 0 + fi + if ! command -v cmp >/dev/null 2>&1; then + warn "cmp not found; skipping read-back verification" + return 0 + fi + log "Verifying: reading back $(human "$ISO_BYTES") from $DEVICE..." + # flushbufs above also invalidated the device's page cache, so this reads + # the stick itself rather than our own recent writes. + if ! cmp -s -n "$ISO_BYTES" -- "$ISO_PATH" "$DEVICE"; then + err "read-back verification FAILED: $DEVICE does not match the ISO." + err "The stick is probably faulty or counterfeit (fake capacity). Don't trust it." + exit 6 + fi + log "Read-back verified." +} + +ensure_root() { + ((EUID == 0)) && return 0 + command -v sudo >/dev/null 2>&1 || die "must run as root and sudo is not installed" 1 + # Re-exec rather than ask the user to prefix the command, so both + # `just make-usb` and `./scripts/make-usb.sh` just work. ${arr[@]+...} + # expands safely under `set -u` even when there are no arguments. + exec sudo --preserve-env=PATH -- "$BASH" "$SCRIPT_PATH" "${ORIG_ARGS[@]+"${ORIG_ARGS[@]}"}" +} + +main() { + ORIG_ARGS=("$@") + parse_args "$@" + ensure_root + + # Anything we still need to ask requires a terminal. + if { [[ -z "$DEVICE" || -z "$ISO_PATH" ]] || ((! ASSUME_YES)); } && [[ ! -t 0 ]]; then + die "stdin is not a tty; pass --iso, --device, and --yes for non-interactive use" 2 + fi + + local missing=() cmd + for cmd in lsblk findmnt dd sync wipefs blockdev umount awk stat tr sed grep; do + command -v "$cmd" >/dev/null 2>&1 || missing+=("$cmd") + done + if ((${#missing[@]})); then + err "missing tools: ${missing[*]}" + cat >&2 <<'EOF' On NixOS / with Nix installed, re-run inside a shell that has them: nix shell nixpkgs#util-linux nixpkgs#coreutils -c sudo ./scripts/make-usb.sh @@ -85,211 +426,27 @@ On NixOS / with Nix installed, re-run inside a shell that has them: On Debian/Ubuntu: sudo apt install util-linux coreutils EOF - exit 3 -fi + exit 3 + fi + resolve_device + resolve_iso + confirm_write + unmount_all + write_image + verify_write - -human() { numfmt --to=iec --suffix=B "$1" 2>/dev/null || echo "$1"; } - -is_removable() { - # /sys/block//removable: 1 = removable (USB stick, SD via USB reader), - # 0 = fixed (internal disk, many "USB" enclosures lie though). - local name; name="$(basename "$1")" - [[ "$(cat "/sys/block/$name/removable" 2>/dev/null || echo 0)" == "1" ]] + echo >&2 + log "${C_BOLD}Done.${C_RESET} You can unplug $DEVICE now." + echo >&2 + info "Boot the new machine from this USB. Once at the installer shell:" + info " - Connect to the network (Ethernet, or 'nmtui' on the graphical ISO)." + info " - Clone your repo: git clone /tmp/notfiles" + info " - Partition the target disk and mount it at /mnt, then:" + info " nixos-install --flake /tmp/notfiles#" } -contains_root_or_boot() { - # Refuse if any partition on this disk holds /, /boot, or active swap. - local dev="$1" - lsblk -lnpo MOUNTPOINT,NAME "$dev" | awk -v d="$dev" ' - $1=="/" || $1=="/boot" || $1=="[SWAP]" { found=1 } - END { exit !found } - ' -} - -select_device() { - log "Available whole disks:" - printf ' %-3s %-14s %-8s %-6s %-16s %s\n' "Idx" "Device" "Size" "Bus" "Removable" "Model" - - # Use lsblk's pairs mode for robust parsing (handles spaces in MODEL). - # lsblk -P emits lines of KEY="value" KEY="value" ... we sanity-check - # the line matches that strict shape before eval'ing it. - local -a CANDS=() - local line NAME SIZE MODEL TRAN RM TYPE - while IFS= read -r line; do - [[ "$line" =~ ^([A-Z]+=\"[^\"]*\"[[:space:]]*)+$ ]] || continue - NAME=""; SIZE=""; MODEL=""; TRAN=""; RM=""; TYPE="" - eval "$line" - [[ "${TYPE:-}" == "disk" ]] || continue - # Skip optical, loop, ram, zram, dm. - case "$NAME" in - /dev/sr*|/dev/loop*|/dev/ram*|/dev/zram*|/dev/dm-*) continue ;; - esac - CANDS+=("$NAME"$'\t'"${SIZE:-?}"$'\t'"${TRAN:-?}"$'\t'"${RM:-0}"$'\t'"${MODEL:-?}") - done < <(lsblk -dpP -o NAME,SIZE,MODEL,TRAN,RM,TYPE) - - ((${#CANDS[@]})) || die "no candidate disks found" 4 - - local i=1 - for c in "${CANDS[@]}"; do - IFS=$'\t' read -r n s t r m <<<"$c" - local marker="" - if [[ "$r" == "1" ]]; then marker="${GREEN}yes${RESET}"; else marker="${YELLOW}no${RESET}"; fi - if contains_root_or_boot "$n"; then - marker="${RED}SYSTEM DISK${RESET}" - fi - printf ' %-3s %-14s %-8s %-6s %-16b %s\n' "$i" "$n" "$s" "$t" "$marker" "$m" - ((i++)) - done - echo - echo "Enter 0 to abort." - echo - - local idx - while :; do - read -r -p "Select device [1-${#CANDS[@]}]: " idx - [[ "$idx" =~ ^[0-9]+$ ]] || { warn "not a number"; continue; } - (( idx == 0 )) && die "aborted by user" 1 - (( idx >= 1 && idx <= ${#CANDS[@]} )) || { warn "out of range"; continue; } - IFS=$'\t' read -r n _ _ r _ <<<"${CANDS[idx-1]}" - if contains_root_or_boot "$n"; then - err "$n is the running system disk — refusing"; continue - fi - if [[ "$r" != "1" ]] && (( ! ALLOW_INTERNAL )); then - err "$n is not removable; pass --allow-internal if you really mean it" - continue - fi - DEVICE="$n" - return 0 - done -} - - - -if [[ -z "$DEVICE" ]]; then - select_device -else - [[ -b "$DEVICE" ]] || die "$DEVICE is not a block device" 2 - # Reject partitions. We want whole disks only. - if [[ "$(lsblk -dno TYPE "$DEVICE" 2>/dev/null)" != "disk" ]]; then - die "$DEVICE is not a whole disk (partition or other type)" 4 - fi - if contains_root_or_boot "$DEVICE"; then - die "$DEVICE holds /, /boot, or active swap — refusing" 4 - fi - if ! is_removable "$DEVICE" && (( ! ALLOW_INTERNAL )); then - die "$DEVICE is not removable; pass --allow-internal to override" 4 - fi +# Run main only when executed, not when sourced (the tests source this file). +if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then + main "$@" fi - -DEV_BYTES="$(blockdev --getsize64 "$DEVICE")" -log "Target: $DEVICE ($(human "$DEV_BYTES"))" - - - -if [[ -z "$ISO_PATH" ]]; then - while :; do - read -r -e -p "Path to NixOS ISO: " ISO_PATH - ISO_PATH="${ISO_PATH/#\~/$HOME}" - [[ -f "$ISO_PATH" ]] && break - warn "not a file: $ISO_PATH" - done -fi -[[ -f "$ISO_PATH" ]] || die "ISO not found: $ISO_PATH" 2 - -ISO_BYTES="$(stat -c '%s' "$ISO_PATH")" -log "ISO: $ISO_PATH ($(human "$ISO_BYTES"))" - -# Sanity: file should look like an ISO 9660 image. -if command -v file >/dev/null 2>&1; then - if ! file -b "$ISO_PATH" | grep -qiE 'iso 9660|udf'; then - warn "$(file -b "$ISO_PATH")" - warn "file(1) doesn't recognise this as ISO 9660. Continuing anyway." - fi -fi - -# Sanity: ISO must fit on disk. -if (( ISO_BYTES > DEV_BYTES )); then - die "ISO is larger than the disk" 4 -fi - -# Optional sha256 sidecar verification. -# Resolve through symlinks so `make-usb --iso iso/latest-...iso` finds -# the sidecar that lives next to the real file. -ISO_REAL="$(readlink -f "$ISO_PATH")" -if [[ -f "$ISO_REAL.sha256" ]]; then - log "Verifying $ISO_REAL.sha256..." - ( cd "$(dirname "$ISO_REAL")" && sha256sum -c "$(basename "$ISO_REAL").sha256" ) \ - || die "sha256 verification failed" 5 -elif [[ -f "$ISO_PATH.sha256" ]]; then - log "Verifying $ISO_PATH.sha256..." - ( cd "$(dirname "$ISO_PATH")" && sha256sum -c "$(basename "$ISO_PATH").sha256" ) \ - || die "sha256 verification failed" 5 -fi - - - -cat </dev/null || umount -l "$part" 2>/dev/null || warn "could not umount $part" -done < <(lsblk -lnpo NAME,MOUNTPOINT "$DEVICE" | awk '$2!=""') - - - -log "Wiping signatures..." -wipefs -a "$DEVICE" >/dev/null - -log "Writing ISO with dd (this can take a few minutes)..." -if ! dd if="$ISO_PATH" of="$DEVICE" bs=4M status=progress conv=fsync oflag=direct; then - err "dd failed." - # Surface the most recent kernel complaints about this device so the - # user can distinguish bad-block / hardware failure ("Medium Error", - # "Hardware Error") from cable/port flakiness ("usb ... reset", - # "Communication failure") without having to dig through dmesg. - dev_short="$(basename "$DEVICE")" - if dmesg_out="$(dmesg 2>/dev/null | grep -E "(${dev_short}|usb [0-9]+-[0-9]+)" | tail -10)" \ - && [[ -n "$dmesg_out" ]]; then - warn "Recent kernel messages mentioning ${dev_short} or USB events:" - printf '%s\n' "$dmesg_out" | sed 's/^/ /' >&2 - warn "Look for 'Medium Error' / 'Hardware Error' (bad flash → replace stick)," - warn "or 'reset' / 'Communication failure' (cable/port → try a different one)." - else - warn "Run \`sudo dmesg | tail -30\` to see why the kernel rejected the write." - fi - exit 5 -fi - -log "Final sync (flushing kernel buffers; may take a moment)..." -sync -blockdev --flushbufs "$DEVICE" 2>/dev/null || true - - - -echo -log "${BOLD}Done.${RESET} You can unplug $DEVICE now." -echo -info "Boot the new machine from this USB. Once at the installer shell:" -info " - Connect to the network (Ethernet, or 'sudo systemctl start wpa_supplicant' + 'wpa_cli', or 'nmtui' on the graphical ISO)." -info " - Clone your repo: git clone /tmp/notfiles" -info " - Partition the target disk, mount at /mnt, then:" -info " nixos-install --flake /tmp/notfiles#" -echo diff --git a/scripts/nixflake.py b/scripts/nixflake.py new file mode 100644 index 0000000..5bc42f3 --- /dev/null +++ b/scripts/nixflake.py @@ -0,0 +1,258 @@ +""" +Small, dependency-free helpers for finding hosts inside a ``flake.nix``. + +This is deliberately *not* a Nix parser. It is a lexical scanner that is +just smart enough to answer "where are the real braces?" by ignoring +everything that is not code: + +* ``# line comments`` and ``/* block comments */`` +* ``"double quoted strings"`` (with ``\\`` escapes) +* ``''indented strings''`` (with ``'''``, ``''$`` and ``''\\`` escapes) +* the *text* of strings, while still treating code inside ``${ ... }`` + interpolations as code + +The central trick is :func:`mask_non_code`, which returns a string of the +same length as the input in which all non-code characters are replaced by +spaces. Regexes and brace counting can then run on the masked text without +being fooled by a ``{`` in a comment, and every offset is valid in the +original text too. + +Limitations (all reported as errors rather than silently mishandled): + +* only bare-identifier attribute names are recognized (``willow = ...``, + not ``"willow" = ...``) +* only entries of the form `` = <...>nixosSystem { ... };`` are + recognized as hosts +""" + +from __future__ import annotations + +import re +from dataclasses import dataclass + + +class FlakeError(Exception): + """flake.nix is not shaped the way these tools expect.""" + + +# Nix keywords that can never be used as a bare attribute name. +NIX_KEYWORDS = frozenset( + {"if", "then", "else", "assert", "with", "let", "in", "rec", "inherit", "or"} +) + +# Nix identifier: [a-zA-Z_][a-zA-Z0-9_'-]* +_IDENT = r"[A-Za-z_][A-Za-z0-9_'-]*" + +# ` = [.]nixosSystem {`, at the start of a line or directly after +# a `{` or `;` (so one-line attrsets work too). +_HOST_RE = re.compile( + rf"(?:^|(?<=[{{;]))([ \t]*)({_IDENT})[ \t]*=\s*(?:[A-Za-z_][\w'-]*\.)*nixosSystem\s*\{{", + re.MULTILINE, +) + +_NIXOS_CONFIGS_RE = re.compile(r"(? str: + """Leading whitespace of the line containing offset ``pos``.""" + line_start = src.rfind("\n", 0, pos) + 1 + m = re.compile(r"[ \t]*").match(src, line_start) + return m.group(0) if m else "" + + +def mask_non_code(src: str) -> str: + """Return ``src`` with comments and string contents blanked out. + + Newlines are preserved so line structure (and ``^`` anchors) still work. + The returned string always has ``len(result) == len(src)``. + """ + out = list(src) + n = len(src) + + def blank(a: int, b: int) -> None: + for k in range(a, min(b, n)): + if out[k] != "\n": + out[k] = " " + + # While inside a ``${ ... }`` interpolation we remember which kind of + # string we came from and the brace depth of the enclosing code. + interp: list[tuple[str, int]] = [] + state = "code" # code | dq | ind + depth = 0 + i = 0 + + while i < n: + c = src[i] + + if state == "code": + if c == "#": + j = src.find("\n", i) + j = n if j == -1 else j + blank(i, j) + i = j + elif c == "/" and src.startswith("/*", i): + j = src.find("*/", i + 2) + j = n if j == -1 else j + 2 + blank(i, j) + i = j + elif c == '"': + state = "dq" + i += 1 + elif c == "'" and src.startswith("''", i): + state = "ind" + i += 2 + elif c == "{": + depth += 1 + i += 1 + elif c == "}": + if depth == 0 and interp: + # This brace closes a ${ ... } interpolation. + blank(i, i + 1) + state, depth = interp.pop() + else: + depth -= 1 + i += 1 + else: + i += 1 + + elif state == "dq": + if c == "\\": + blank(i, i + 2) + i += 2 + elif c == '"': + state = "code" + i += 1 + elif c == "$" and src.startswith("${", i): + blank(i, i + 2) + interp.append(("dq", depth)) + state, depth = "code", 0 + i += 2 + else: + blank(i, i + 1) + i += 1 + + else: # state == "ind" + if src.startswith("'''", i) or src.startswith("''$", i): + blank(i, i + 3) + i += 3 + elif src.startswith("''\\", i): + blank(i, i + 4) + i += 4 + elif src.startswith("''", i): + state = "code" + i += 2 + elif c == "$" and src.startswith("${", i): + blank(i, i + 2) + interp.append(("ind", depth)) + state, depth = "code", 0 + i += 2 + else: + blank(i, i + 1) + i += 1 + + return "".join(out) + + +def find_matching_brace(masked: str, open_pos: int) -> int | None: + """Index of the ``}`` matching the ``{`` at ``open_pos``, or ``None``.""" + if open_pos >= len(masked) or masked[open_pos] != "{": + raise ValueError(f"no '{{' at offset {open_pos}") + depth = 0 + for i in range(open_pos, len(masked)): + c = masked[i] + if c == "{": + depth += 1 + elif c == "}": + depth -= 1 + if depth == 0: + return i + return None + + +def braces_balanced(src: str) -> bool: + """True if every code-level ``{`` has a matching ``}``.""" + depth = 0 + for c in mask_non_code(src): + if c == "{": + depth += 1 + elif c == "}": + depth -= 1 + if depth < 0: + return False + return depth == 0 + + +@dataclass(frozen=True) +class HostEntry: + """One `` = ...nixosSystem { ... };`` binding.""" + + name: str + indent: str # leading whitespace of the line the entry starts on + start: int # offset where the entry begins (leading indent included) + end: int # offset just past the terminating ``;`` + + +@dataclass(frozen=True) +class ConfigBlock: + """Location of the ``nixosConfigurations = { ... }`` attrset.""" + + open_pos: int # offset of ``{`` + close_pos: int # offset of matching ``}`` + + +def locate_configurations(src: str, masked: str | None = None) -> ConfigBlock: + """Find the ``nixosConfigurations`` attrset or raise :class:`FlakeError`.""" + masked = mask_non_code(src) if masked is None else masked + m = _NIXOS_CONFIGS_RE.search(masked) + if not m: + raise FlakeError( + "could not find a literal `nixosConfigurations = { ... }` attrset " + "(is it generated by a function such as mapAttrs?)" + ) + open_pos = m.end() - 1 + close_pos = find_matching_brace(masked, open_pos) + if close_pos is None: + raise FlakeError("unbalanced braces: nixosConfigurations is never closed") + return ConfigBlock(open_pos, close_pos) + + +def find_hosts(src: str) -> list[HostEntry]: + """Top-level host entries of ``nixosConfigurations``, in file order. + + Entries nested deeper than the attrset itself (e.g. inside a ``let``) + are ignored. + """ + masked = mask_non_code(src) + block = locate_configurations(src, masked) + hosts: list[HostEntry] = [] + pos = block.open_pos + 1 + depth = 0 # brace depth relative to the nixosConfigurations body + + for m in _HOST_RE.finditer(masked, block.open_pos + 1, block.close_pos): + if m.start() < pos: + continue # nested inside a host entry we already consumed + # Advance the running depth up to the start of this match. + for c in masked[pos : m.start()]: + if c == "{": + depth += 1 + elif c == "}": + depth -= 1 + pos = m.start() + if depth != 0: + continue + + open_brace = m.end() - 1 + close_brace = find_matching_brace(masked, open_brace) + if close_brace is None: + raise FlakeError(f"unbalanced braces in host entry `{m.group(2)}`") + semi = re.compile(r"\s*;").match(masked, close_brace + 1) + if semi is None: + raise FlakeError( + f"host entry `{m.group(2)}` is not terminated by `;` after its closing brace" + ) + hosts.append(HostEntry(m.group(2), line_indent(src, m.start()), m.start(), semi.end())) + # Skip over the entry body so we don't double count nested braces. + pos = semi.end() + # `pos` jumped past the body, whose braces are balanced, so depth is unchanged. + + return hosts diff --git a/scripts/register-host.py b/scripts/register-host.py index 43058a7..389eba2 100755 --- a/scripts/register-host.py +++ b/scripts/register-host.py @@ -1,57 +1,88 @@ #!/usr/bin/env python3 - """ -Scaffold a new NixOS host and add it to flake.nix. +Scaffold a new NixOS host and register it in flake.nix. + +Creates hosts//{default.nix,hardware.nix} and adds a +` = nixpkgs.lib.nixosSystem { ... };` entry to nixosConfigurations, +cloning an existing host's entry when possible so local customizations carry over. + +Everything is computed and validated in memory before the first byte is +written, and the write phase is rolled back if any step fails, so a failed +run never leaves a half-registered host behind. + +Exit codes: 0 ok, 1 error, 2 usage, 130 interrupted. """ from __future__ import annotations import argparse +import os import re +import shutil +import subprocess import sys +import tempfile +from dataclasses import dataclass, field from pathlib import Path from textwrap import dedent +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import nixflake # noqa: E402 (needs the sys.path tweak above) +from nixflake import FlakeError, HostEntry # noqa: E402 + +# -------------------------------------------------------------------------- +# Output helpers +# -------------------------------------------------------------------------- + +_COLOR = sys.stderr.isatty() and "NO_COLOR" not in os.environ -def _supports_color() -> bool: - return sys.stderr.isatty() - - -_C = { - "green": "\033[32m" if _supports_color() else "", - "yellow": "\033[33m" if _supports_color() else "", - "red": "\033[31m" if _supports_color() else "", - "bold": "\033[1m" if _supports_color() else "", - "reset": "\033[0m" if _supports_color() else "", -} +def _c(code: str) -> str: + return f"\033[{code}m" if _COLOR else "" def log(msg: str) -> None: - print(f"{_C['green']}[+]{_C['reset']} {msg}", file=sys.stderr) + print(f"{_c('32')}[+]{_c('0')} {msg}", file=sys.stderr) def warn(msg: str) -> None: - print(f"{_C['yellow']}[!]{_C['reset']} {msg}", file=sys.stderr) + print(f"{_c('33')}[!]{_c('0')} {msg}", file=sys.stderr) -def die(msg: str, code: int = 1) -> None: - print(f"{_C['red']}[-]{_C['reset']} {msg}", file=sys.stderr) - sys.exit(code) +class RegisterError(Exception): + """A user-facing failure; message is printed without a traceback.""" +# -------------------------------------------------------------------------- +# Hostname validation +# -------------------------------------------------------------------------- -# RFC 1123 hostname: 1-63 chars, [a-z0-9-], no leading/trailing dash. -HOST_RE = re.compile(r"^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$") +# RFC 1123 label, restricted further so it is also a legal bare Nix attribute +# name: lowercase, starts with a letter, no trailing dash, at most 63 chars. +HOST_RE = re.compile(r"^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$") -def valid_hostname(name: str) -> bool: - return bool(HOST_RE.match(name)) +def validate_hostname(name: str) -> None: + if not HOST_RE.fullmatch(name): + raise RegisterError( + f"invalid hostname {name!r}: use lowercase letters, digits and hyphens, " + "start with a letter, end with a letter or digit, max 63 characters" + ) + if name in nixflake.NIX_KEYWORDS: + raise RegisterError(f"invalid hostname {name!r}: reserved word in Nix") +# -------------------------------------------------------------------------- +# Templates +# +# These contain Nix code, which is full of braces, so they must NOT go +# through str.format() or f-strings. Placeholders are @UPPER_CASE@ tokens +# replaced by render(), which fails loudly on any leftover token. +# -------------------------------------------------------------------------- DEFAULT_NIX_TEMPLATE = dedent("""\ - {{ pkgs, ... }}: {{ + { pkgs, ... }: + { imports = [ ./hardware.nix ../../modules/core.nix @@ -59,7 +90,7 @@ DEFAULT_NIX_TEMPLATE = dedent("""\ ../../modules/security.nix ]; - networking.hostName = "{name}"; + networking.hostName = "@NAME@"; boot.loader.systemd-boot.enable = true; boot.loader.efi.canTouchEfiVariables = true; @@ -74,11 +105,11 @@ DEFAULT_NIX_TEMPLATE = dedent("""\ zlib ]; - users.users.alex = {{ + users.users.alex = { isNormalUser = true; shell = pkgs.zsh; extraGroups = [ "wheel" "networkmanager" ]; - }}; + }; programs._1password.enable = true; programs._1password-gui = { @@ -87,244 +118,370 @@ DEFAULT_NIX_TEMPLATE = dedent("""\ }; system.stateVersion = "26.05"; - }} + } """) +HARDWARE_PLACEHOLDER_MARKER = "PLACEHOLDER hardware.nix" # capture-hardware.sh greps for this PLACEHOLDER_HARDWARE = dedent("""\ - # PLACEHOLDER hardware.nix for host "{name}". + # PLACEHOLDER hardware.nix for host "@NAME@". # # Replace this file with the output of `nixos-generate-config --show-hardware-config` # run on the target machine. The justfile has a helper for this: # - # just capture-hardware {name} + # just capture-hardware @NAME@ # # Until then, this file intentionally does nothing useful and the host # will not build. - {{ lib, ... }}: {{ - # nixpkgs hostPlatform — change if not x86_64-linux. + { lib, ... }: + { + # nixpkgs hostPlatform; change if not x86_64-linux. nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux"; # Intentionally empty. See note above. - }} + } """) +# Used only when there is no existing host to clone. @I@ is the base indent. +DEFAULT_FLAKE_ENTRY = dedent("""\ + @I@@NAME@ = nixpkgs.lib.nixosSystem { + @I@ inherit system; + @I@ specialArgs = { inherit inputs; }; + @I@ modules = [ + @I@ ./hosts/@NAME@ + @I@ home-manager.nixosModules.home-manager + @I@ { + @I@ home-manager = { + @I@ useGlobalPkgs = true; + @I@ useUserPackages = true; + @I@ users.alex = import ./home/alex; + @I@ extraSpecialArgs = { inherit inputs; }; + @I@ sharedModules = [ inputs.plasma-manager.homeModules.plasma-manager ]; + @I@ }; + @I@ } + @I@ ]; + @I@}; +""") + +_TOKEN_RE = re.compile(r"@[A-Z][A-Z_]*@") +def render(template: str, **values: str) -> str: + """Substitute ``@KEY@`` tokens. Unlike str.format this is brace-safe.""" + out = template + for key, value in values.items(): + out = out.replace(f"@{key.upper()}@", value) + leftover = _TOKEN_RE.search(out) + if leftover: + raise RuntimeError(f"BUG: unreplaced template token {leftover.group(0)}") + return out -def add_to_flake(flake_path: Path, name: str, *, template_host: str | None = None) -> None: + +# -------------------------------------------------------------------------- +# flake.nix editing (pure functions: text in, text out) +# -------------------------------------------------------------------------- + + +@dataclass +class FlakeEdit: + new_src: str + template_used: str | None + stale_template_lines: list[str] = field(default_factory=list) + + +def plan_flake_edit(src: str, name: str, template: str | None) -> FlakeEdit: + """Return ``src`` with a new host entry for ``name`` inserted. + + If ``template`` names an existing host, its entry is cloned (renamed, + with ``./hosts/