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

+7 -3
View File
@@ -62,13 +62,17 @@ make-usb *args:
# Scaffold hosts/<name>/ 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/<name>/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"
+129 -68
View File
@@ -1,82 +1,143 @@
#!/usr/bin/env bash
# Generate hardware.nix for a registered host.
# Generate hosts/<name>/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 <name>" >&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 "$@"
+169 -68
View File
@@ -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 <<EOF
%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
$protection
%commit
%echo done
EOF
fi
local config_dir="${DOCKER_CONFIG:-${HOME:?HOME is not set}/.docker}"
local config_file="$config_dir/config.json"
# Pull the fingerprint back out for pass.
fpr="$(gpg --list-secret-keys --with-colons "$gpg_uid" \
| awk -F: '/^fpr:/ {print $10; exit}')"
[ -n "$fpr" ] || { echo "error: could not determine GPG fingerprint" >&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 "$@"
+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 "$@"
+103 -21
View File
@@ -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_<SERVICE> (+ .pub) for each SERVICE, an Ed25519 key with the
comment "<SERVICE>@<short-hostname>". 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 "$@"
+68
View File
@@ -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"
}
+40 -37
View File
@@ -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)
+417 -260
View File
@@ -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 <url>` 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 <<EOF
${C_BOLD}About to:${C_RESET}
${C_RED}WIPE${C_RESET} $(describe_device "$DEVICE")
Write ISO $ISO_PATH
${C_YELLOW}All data on $DEVICE will be destroyed.${C_RESET}
EOF
if ((! ASSUME_YES)); then
local typed
read -r -p "Type the device path to confirm (e.g. $DEVICE): " typed || die "unexpected end of input" 1
[[ "$typed" == "$DEVICE" ]] || die "confirmation didn't match; aborting" 1
fi
}
# --------------------------------------------------------------------------
# The destructive part
# --------------------------------------------------------------------------
unmount_all() {
local pass mp mounts
local -a mps
log "Unmounting any partitions on $DEVICE..."
for pass in 1 2 3 4; do # stacked mounts need more than one pass
mounts="$(mounts_of "$DEVICE")" || die "could not read the mount table; refusing to write" 4
mapfile -t mps <<<"$mounts"
if [[ -z "$mounts" ]]; then return 0; fi
((pass < 4)) || die "$DEVICE still has mounted filesystems: ${mps[*]}" 4
for mp in "${mps[@]}"; do
warn "umount $mp"
# No lazy (-l) fallback: a lazily "unmounted" filesystem is still
# live in the kernel and we'd be writing underneath it.
umount -- "$mp" || die "could not unmount $mp; close whatever is using it and retry" 4
done
done
}
write_image() {
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 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/<name>/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 <your-repo-url> /tmp/notfiles"
info " - Partition the target disk and mount it at /mnt, then:"
info " nixos-install --flake /tmp/notfiles#<hostname>"
}
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 <<EOF
${BOLD}About to:${RESET}
${RED}WIPE${RESET} $DEVICE ($(human "$DEV_BYTES"))
Write ISO $ISO_PATH
${YELLOW}All data on $DEVICE will be destroyed.${RESET}
EOF
if (( ! ASSUME_YES )); then
read -r -p "Type the device path to confirm (e.g. $DEVICE): " typed
[[ "$typed" == "$DEVICE" ]] || die "confirmation didn't match — aborting" 1
fi
log "Unmounting any partitions on $DEVICE..."
while read -r part mnt; do
[[ -n "$mnt" ]] || continue
warn "umount $part ($mnt)"
umount "$part" 2>/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 <your-repo-url> /tmp/notfiles"
info " - Partition the target disk, mount at /mnt, then:"
info " nixos-install --flake /tmp/notfiles#<hostname>"
echo
+258
View File
@@ -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 ``<name> = <...>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_'-]*"
# `<name> = [<path>.]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"(?<![\w'-])nixosConfigurations\s*=\s*\{")
def line_indent(src: str, pos: int) -> 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 ``<name> = ...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
+364 -207
View File
@@ -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/<name>/{default.nix,hardware.nix} and adds a
`<name> = 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/<template>`` rewritten). Otherwise a default entry is used.
"""
Insert a new `<name> = nixpkgs.lib.nixosSystem { ... };` entry inside
the `nixosConfigurations = { ... };` block of flake.nix.
masked = nixflake.mask_non_code(src)
block = nixflake.locate_configurations(src, masked)
hosts = nixflake.find_hosts(src)
by_name = {h.name: h for h in hosts}
Strategy: locate the literal `nixosConfigurations = {` (allowing inline
or next-line `{`), find the matching closing `}` by brace counting,
then insert the new entry just before it. If `template_host` is set,
clone that host's block; otherwise emit a sensible default.
"""
src = flake_path.read_text()
if name in by_name:
raise RegisterError(f"host `{name}` is already defined in flake.nix")
m = re.search(r"nixosConfigurations\s*=\s*\{", src)
if not m:
die(f"could not find `nixosConfigurations = {{` in {flake_path}")
eol = "\r\n" if "\r\n" in src else "\n"
stale: list[str] = []
tmpl: HostEntry | None = None
open_pos = m.end() - 1 # index of the '{'
# Brace-match to find the closing '}' of this attrset.
depth = 0
close_pos = None
in_string = False
i = open_pos
while i < len(src):
c = src[i]
# Cheap string skipping. flake.nix shouldn't have weird escapes inside
# nixosConfigurations, but be polite about double-quoted strings.
if c == '"' and (i == 0 or src[i - 1] != "\\"):
in_string = not in_string
elif not in_string:
if c == "{":
depth += 1
elif c == "}":
depth -= 1
if depth == 0:
close_pos = i
break
i += 1
if template is not None:
tmpl = by_name.get(template)
if tmpl is None:
known = ", ".join(by_name) or "<none>"
raise RegisterError(f"template host `{template}` not found in flake.nix (known: {known})")
if close_pos is None:
die("could not find the closing `}` of nixosConfigurations")
block = src[open_pos + 1 : close_pos]
# Already present? Bail.
if re.search(rf"(^|\W){re.escape(name)}\s*=\s*nixpkgs\.lib\.nixosSystem", block):
die(f"host `{name}` already exists in {flake_path}")
# If we can clone an existing entry, do it (preserves user customizations).
new_entry: str
cloned = False
if template_host:
# Find `<template_host> = nixpkgs.lib.nixosSystem { ... };` inside block.
tmpl_re = re.compile(
rf"(^[ \t]*){re.escape(template_host)}\s*=\s*nixpkgs\.lib\.nixosSystem\s*\{{",
re.MULTILINE,
if tmpl is not None:
text = src[tmpl.start : tmpl.end]
text = re.sub(
rf"^([ \t]*){re.escape(tmpl.name)}(?![\w'-])",
lambda m: m.group(1) + name,
text,
count=1,
)
tm = tmpl_re.search(block)
if tm:
tmpl_indent = tm.group(1)
tmpl_start = tm.start()
# Brace-match the template entry to find its terminating `};`.
d = 0
j = tm.end() - 1 # index of opening '{'
in_str = False
entry_end = None
while j < len(block):
cc = block[j]
if cc == '"' and (j == 0 or block[j - 1] != "\\"):
in_str = not in_str
elif not in_str:
if cc == "{":
d += 1
elif cc == "}":
d -= 1
if d == 0:
# Expect a trailing ';' after the closing '}'.
semi = block.find(";", j)
if semi != -1 and block[j + 1 : semi].strip() == "":
entry_end = semi + 1
else:
entry_end = j + 1
break
j += 1
if entry_end is None:
die(f"could not parse template host `{template_host}` in flake.nix")
# Rewrite ./hosts/<template> but not ./hosts/<template>-other.
text = re.sub(
rf"(\./hosts/){re.escape(tmpl.name)}(?![\w'-])",
lambda m: m.group(1) + name,
text,
)
stale_re = re.compile(rf"(?<![\w'-]){re.escape(tmpl.name)}(?![\w'-])")
stale = [ln.strip() for ln in text.splitlines() if stale_re.search(ln)]
else:
close_indent = nixflake.line_indent(src, block.close_pos)
indent = hosts[0].indent if hosts else close_indent + " "
text = render(DEFAULT_FLAKE_ENTRY, name=name, i=indent).rstrip("\n")
if eol != "\n":
text = text.replace("\n", eol)
entry_text = block[tmpl_start:entry_end]
# Rename the binding and rewrite ./hosts/<template_host> paths.
new_entry_body = re.sub(
rf"^([ \t]*){re.escape(template_host)}(\s*=\s*nixpkgs\.lib\.nixosSystem)",
rf"\1{name}\2",
entry_text,
count=1,
flags=re.MULTILINE,
)
new_entry_body = new_entry_body.replace(
f"./hosts/{template_host}", f"./hosts/{name}"
)
new_entry = "\n" + new_entry_body
cloned = True
# Insert just before the line holding the closing brace, so we never
# produce `};};` on one line. If other code shares that line (e.g. a
# one-line attrset), split the line instead.
line_start = src.rfind("\n", 0, block.close_pos) + 1
before_brace = src[line_start : block.close_pos]
if before_brace.strip() == "":
head, tail = src[:line_start], src[line_start:]
sep = "" if (head.endswith(eol * 2) or head.endswith("{" + eol)) else eol
new_src = f"{head}{sep}{text}{eol}{tail}"
else:
close_indent = nixflake.line_indent(src, block.close_pos)
head = src[: block.close_pos].rstrip(" \t")
lead = eol if head.endswith("{") else eol * 2
new_src = f"{head}{lead}{text}{eol}{close_indent}{src[block.close_pos:]}"
if not cloned:
# Best-effort default block. Indent two extra spaces if we can guess
# the indentation by looking at existing content; otherwise 6 spaces.
indent_match = re.search(r"^([ \t]+)\S", block, re.MULTILINE)
indent = indent_match.group(1) if indent_match else " "
new_entry = (
f"\n{indent}{name} = nixpkgs.lib.nixosSystem {{\n"
f"{indent} inherit system;\n"
f"{indent} specialArgs = {{ inherit inputs; }};\n"
f"{indent} modules = [\n"
f"{indent} ./hosts/{name}\n"
f"{indent} home-manager.nixosModules.home-manager\n"
f"{indent} {{\n"
f"{indent} home-manager = {{\n"
f"{indent} useGlobalPkgs = true;\n"
f"{indent} useUserPackages = true;\n"
f"{indent} users.alex = import ./home/alex;\n"
f"{indent} extraSpecialArgs = {{ inherit inputs; }};\n"
f"{indent} sharedModules = [ inputs.plasma-manager.homeModules.plasma-manager ];\n"
f"{indent} }};\n"
f"{indent} }}\n"
f"{indent} ];\n"
f"{indent}}};\n"
return FlakeEdit(new_src, tmpl.name if tmpl else None, stale)
def verify_edit(old_src: str, new_src: str, name: str) -> None:
"""Sanity-check the edited flake before it is allowed to touch disk."""
if not nixflake.braces_balanced(new_src):
raise RegisterError("internal check failed: edited flake.nix has unbalanced braces")
before = [h.name for h in nixflake.find_hosts(old_src)]
after = [h.name for h in nixflake.find_hosts(new_src)]
if after != [*before, name]:
raise RegisterError(
f"internal check failed: expected hosts {[*before, name]}, found {after}"
)
# Insert new_entry just before the line containing the closing brace,
# so we never produce `};};` on a single line.
line_start = src.rfind("\n", 0, close_pos)
if line_start == -1:
line_start = 0
if not new_entry.endswith("\n"):
new_entry += "\n"
new_src = src[: line_start + 1] + new_entry + src[line_start + 1 :]
# Write atomically.
tmp = flake_path.with_suffix(flake_path.suffix + ".tmp")
tmp.write_text(new_src)
tmp.replace(flake_path)
def nix_parse_check(src: str) -> None:
"""If nix-instantiate is available, make sure ``src`` still parses.
Skipped silently when Nix isn't installed (e.g. running on macOS without Nix).
"""
exe = shutil.which("nix-instantiate")
if exe is None:
return
with tempfile.TemporaryDirectory() as td:
probe = Path(td) / "flake.nix"
probe.write_text(src, encoding="utf-8", newline="")
try:
proc = subprocess.run(
[exe, "--parse", str(probe)],
capture_output=True,
text=True,
timeout=60,
check=False,
)
except subprocess.TimeoutExpired:
warn("nix-instantiate --parse timed out; skipping syntax check")
return
if proc.returncode != 0:
detail = (proc.stderr or proc.stdout).strip()
raise RegisterError(f"edited flake.nix would not parse; nothing was written:\n{detail}")
def host_in_flake(flake_path: Path, name: str) -> bool:
"""Cheap textual check, does flake.nix already define this host?"""
src = flake_path.read_text()
return bool(
re.search(rf"(^|\W){re.escape(name)}\s*=\s*nixpkgs\.lib\.nixosSystem", src)
)
# --------------------------------------------------------------------------
# Filesystem
# --------------------------------------------------------------------------
def find_template_host(flake_path: Path) -> str | None:
"""Return the name of the first existing nixosConfigurations entry, if any."""
src = flake_path.read_text()
m = re.search(r"nixosConfigurations\s*=\s*\{", src)
if not m:
return None
tail = src[m.end():]
m2 = re.search(r"^[ \t]*([A-Za-z_][A-Za-z0-9_-]*)\s*=\s*nixpkgs\.lib\.nixosSystem",
tail, re.MULTILINE)
return m2.group(1) if m2 else None
def read_text_exact(path: Path) -> str:
"""Read a UTF-8 file without newline translation."""
try:
return path.read_bytes().decode("utf-8")
except UnicodeDecodeError as e:
raise RegisterError(f"{path} is not valid UTF-8: {e}") from e
def atomic_replace(path: Path, data: str) -> None:
"""Replace ``path`` with ``data`` atomically, keeping its mode.
def main() -> int:
Writes through symlinks (the target is replaced, the link is kept).
"""
real = path.resolve()
fd, tmp_name = tempfile.mkstemp(dir=real.parent, prefix=f".{real.name}.", suffix=".tmp")
try:
with os.fdopen(fd, "w", encoding="utf-8", newline="") as f:
f.write(data)
f.flush()
os.fsync(f.fileno())
shutil.copymode(real, tmp_name)
os.replace(tmp_name, real)
except BaseException:
try:
os.unlink(tmp_name)
except FileNotFoundError:
pass
raise
def create_new_file(path: Path, data: str) -> None:
"""Create ``path`` with ``data``; fails if it already exists."""
with path.open("x", encoding="utf-8", newline="") as f:
f.write(data)
f.flush()
os.fsync(f.fileno())
def missing_template_imports(repo: Path, default_nix: str) -> list[str]:
"""Repo-relative files that the generated default.nix imports but which don't exist."""
wanted = re.findall(r"\.\./\.\./([\w./-]+)", default_nix)
return [w for w in wanted if not (repo / w).exists()]
# --------------------------------------------------------------------------
# main
# --------------------------------------------------------------------------
def parse_args(argv: list[str] | None) -> argparse.Namespace:
p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument("name", help="hostname for the new host (lowercase, RFC 1123)")
p.add_argument("name", help="hostname for the new host (lowercase, starts with a letter)")
p.add_argument("--repo-root", default=".", help="path to the repo root (default: cwd)")
p.add_argument("--dry-run", action="store_true", help="describe actions without writing")
args = p.parse_args()
p.add_argument(
"--template",
metavar="HOST",
help="existing host whose flake.nix entry to clone (default: the first host)",
)
p.add_argument(
"--no-template",
action="store_true",
help="don't clone an existing host; emit the built-in default entry",
)
p.add_argument("--dry-run", action="store_true", help="validate and describe, but write nothing")
args = p.parse_args(argv)
if args.template and args.no_template:
p.error("--template and --no-template are mutually exclusive")
return args
if not valid_hostname(args.name):
die(f"invalid hostname: {args.name!r} (must match {HOST_RE.pattern})")
def run(args: argparse.Namespace) -> int:
validate_hostname(args.name)
name: str = args.name
repo = Path(args.repo_root).resolve()
flake = repo / "flake.nix"
hosts_dir = repo / "hosts" / args.name
hosts_dir = repo / "hosts" / name
if not flake.is_file():
die(f"flake.nix not found at {flake}")
raise RegisterError(f"flake.nix not found at {flake}")
# An existing but EMPTY directory is debris from an earlier failed run.
reuse_empty_dir = False
if hosts_dir.exists():
die(f"hosts/{args.name} already exists")
if host_in_flake(flake, args.name):
die(f"host `{args.name}` already defined in {flake} — refusing to touch anything")
if hosts_dir.is_dir() and not any(hosts_dir.iterdir()):
reuse_empty_dir = True
warn(f"hosts/{name} exists but is empty (leftover from a failed run); reusing it")
else:
raise RegisterError(f"hosts/{name} already exists and is not empty; refusing to touch it")
# ---- Phase 1: compute and validate everything in memory. ----
old_src = read_text_exact(flake)
try:
existing = nixflake.find_hosts(old_src)
if args.no_template:
template = None
elif args.template:
template = args.template
else:
template = existing[0].name if existing else None
edit = plan_flake_edit(old_src, name, template)
verify_edit(old_src, edit.new_src, name)
except FlakeError as e:
raise RegisterError(f"cannot edit {flake}: {e}") from e
nix_parse_check(edit.new_src)
default_nix = render(DEFAULT_NIX_TEMPLATE, name=name)
hardware_nix = render(PLACEHOLDER_HARDWARE, name=name)
template_host = find_template_host(flake)
log(f"repo: {repo}")
log(f"new host: {args.name}")
log(f"template host: {template_host or '<none, using default block>'}")
log(f"new host: {name}")
log(f"template host: {edit.template_used or '<none, using built-in default entry>'}")
if existing and not edit.template_used:
warn("flake.nix has hosts but none were cloned; double-check the new entry matches your conventions")
for rel in missing_template_imports(repo, default_nix):
warn(f"generated default.nix imports ../../{rel}, which does not exist in this repo")
if edit.stale_template_lines:
warn(f"cloned entry still mentions `{edit.template_used}` on these lines; review them:")
for ln in edit.stale_template_lines:
print(f" {ln}", file=sys.stderr)
if args.dry_run:
warn("dry-run: nothing written")
return 0
# Scaffold filesystem.
hosts_dir.mkdir(parents=True)
(hosts_dir / "default.nix").write_text(DEFAULT_NIX_TEMPLATE.format(name=args.name))
(hosts_dir / "hardware.nix").write_text(PLACEHOLDER_HARDWARE.format(name=args.name))
log(f"created hosts/{args.name}/default.nix")
log(f"created hosts/{args.name}/hardware.nix (placeholder)")
# ---- Phase 2: write, rolling back on any failure. ----
created_files: list[Path] = []
created_dir = False
try:
if not reuse_empty_dir:
hosts_dir.mkdir(parents=True)
created_dir = True
for fname, body in (("default.nix", default_nix), ("hardware.nix", hardware_nix)):
target = hosts_dir / fname
create_new_file(target, body)
created_files.append(target)
log(f"created hosts/{name}/default.nix")
log(f"created hosts/{name}/hardware.nix (placeholder)")
# Edit flake.nix.
add_to_flake(flake, args.name, template_host=template_host)
log(f"added `{args.name}` entry to flake.nix")
atomic_replace(flake, edit.new_src) # last: the only step that mutates an existing file
log(f"added `{name}` entry to flake.nix")
except BaseException:
for f in reversed(created_files):
f.unlink(missing_ok=True)
if created_dir:
try:
hosts_dir.rmdir()
except OSError:
pass
warn("failed; rolled back all changes")
raise
print()
log(f"{_C['bold']}Next steps:{_C['reset']}")
print(f" 1. On the new machine (booted from installer or already running):")
print(f" just capture-hardware {args.name}")
print(f" This overwrites hosts/{args.name}/hardware.nix with the real config.")
print(f" 2. Review hosts/{args.name}/default.nix — tweak modules, users, etc.")
print(f" 3. git add hosts/{args.name} flake.nix && git commit")
print(f" 4. Build: just rebuild (when running ON the new host)")
print(
dedent(f"""
Next steps:
1. On the new machine (booted from the installer or already running NixOS):
just capture-hardware {name}
This replaces hosts/{name}/hardware.nix with the real config.
2. Review hosts/{name}/default.nix (modules, users, etc.).
3. Flakes only see git-tracked files, so before building:
git add hosts/{name} flake.nix && git commit
4. Build, when running ON the new host:
just rebuild
"""),
file=sys.stderr,
)
return 0
def main(argv: list[str] | None = None) -> int:
args = parse_args(argv)
try:
return run(args)
except RegisterError as e:
print(f"{_c('31')}[-]{_c('0')} {e}", file=sys.stderr)
return 1
except OSError as e:
print(f"{_c('31')}[-]{_c('0')} {e}", file=sys.stderr)
return 1
except KeyboardInterrupt:
print("\ninterrupted", file=sys.stderr)
return 130
if __name__ == "__main__":
sys.exit(main())
+78
View File
@@ -0,0 +1,78 @@
# shellcheck shell=bash
# Minimal test harness. Source from a test file, define test_* functions, call run_all.
# Each test runs in its own subshell with a fresh $T (temp dir) and cwd = $T.
HARNESS_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
SCRIPTS="$(cd -- "$HARNESS_DIR/.." && pwd -P)"
SHIMS="$HARNESS_DIR/shims"
_PASS=0 _FAIL=0 _FAILED=()
_RM="$(command -v rm)"
_MKTEMP="$(command -v mktemp)" # resolved now: tests may shrink PATH later
_TMP_ROOT="$("$_MKTEMP" -d)"
trap '"$_RM" -rf -- "$_TMP_ROOT"' EXIT
_fail() { printf ' FAIL: %s\n' "$*" >&2; exit 1; }
# run CMD...: capture RC, OUT, ERR (stdin is /dev/null)
run() {
local o e
o="$("$_MKTEMP" -p "$_TMP_ROOT")" e="$("$_MKTEMP" -p "$_TMP_ROOT")"
RC=0
"$@" >"$o" 2>"$e" </dev/null || RC=$?
OUT="$(<"$o")" ERR="$(<"$e")"
"$_RM" -f -- "$o" "$e"
}
# run_in FILE CMD...: like run, but stdin comes from FILE.
run_in() {
local in="$1"; shift
local o e
o="$("$_MKTEMP" -p "$_TMP_ROOT")" e="$("$_MKTEMP" -p "$_TMP_ROOT")"
RC=0
"$@" >"$o" 2>"$e" <"$in" || RC=$?
OUT="$(<"$o")" ERR="$(<"$e")"
"$_RM" -f -- "$o" "$e"
}
# launch_like_terminal CMD...: start CMD in the background the way a terminal would:
# its own session/process group (so a signal reaches children too) with SIGINT at
# its default disposition (bash starts async jobs with SIGINT ignored). Sets LAUNCHED_PID.
launch_like_terminal() {
python3 -c 'import os,signal,sys; os.setsid(); signal.signal(signal.SIGINT, signal.SIG_DFL); os.execvp(sys.argv[1], sys.argv[1:])' "$@" >/dev/null 2>&1 &
LAUNCHED_PID=$!
}
# signal_group SIG: send SIG to the whole process group of LAUNCHED_PID, then reap it.
signal_group() { kill "-$1" -- "-$LAUNCHED_PID" 2>/dev/null; wait "$LAUNCHED_PID" 2>/dev/null; return 0; }
assert_rc() { [[ "$RC" == "$1" ]] || _fail "expected exit $1, got $RC${2:+ ($2)}
stdout: ${OUT:0:600}
stderr: ${ERR:0:1200}"; }
assert_eq() { [[ "$1" == "$2" ]] || _fail "${3:-values differ}
expected: $1
actual: $2"; }
assert_err_has() { [[ "$ERR" == *"$1"* ]] || _fail "stderr missing '$1'
stderr: ${ERR:0:1200}"; }
assert_out_has() { [[ "$OUT" == *"$1"* ]] || _fail "stdout missing '$1'
stdout: ${OUT:0:1200}"; }
assert_err_lacks() { [[ "$ERR" != *"$1"* ]] || _fail "stderr unexpectedly contains '$1'
stderr: ${ERR:0:1200}"; }
assert_file() { [[ -f "$1" ]] || _fail "expected file: $1"; }
assert_no_path() { [[ ! -e "$1" && ! -L "$1" ]] || _fail "unexpected path exists: $1"; }
assert_mode() { local m; m="$(stat -c %a -- "$1")"; [[ "$m" == "$2" ]] || _fail "mode of $1 is $m, expected $2"; }
assert_file_eq() { [[ "$(<"$1")" == "$2" ]] || _fail "contents of $1 differ
expected: $2
actual: $(<"$1")"; }
run_all() {
local filter="${1:-}" t
for t in $(declare -F | awk '{print $3}' | grep '^test_' || true); do
[[ -z "$filter" || "$t" == *"$filter"* ]] || continue
T="$("$_MKTEMP" -d -p "$_TMP_ROOT")"
if ( cd "$T" && export T && set +e && "$t" ) ; then
_PASS=$((_PASS + 1)); printf ' ok %s\n' "$t"
else
_FAIL=$((_FAIL + 1)); _FAILED+=("$t"); printf ' FAIL %s\n' "$t"
fi
done
printf '%s: %d passed, %d failed\n' "$(basename "$0")" "$_PASS" "$_FAIL"
((_FAIL == 0))
}
+96
View File
@@ -0,0 +1,96 @@
#!/usr/bin/env python3
"""
Tiny static file server for tests: supports `Range: bytes=N-` and can inject faults.
usage: range_server.py DIR PORT_FILE [FAULT_FILE]
FAULT_FILE holds one fault per line; each GET of a non-.sha256 file consumes the first line:
truncate N send only N body bytes, then drop the connection
status CODE reply with that HTTP status and no body
norange ignore the Range header and send the whole file with 200
Every request is appended to DIR/../requests.log as "<path> <range-header-or-->".
"""
import http.server
import sys
from pathlib import Path
root = Path(sys.argv[1]).resolve()
port_file = Path(sys.argv[2])
fault_file = Path(sys.argv[3]) if len(sys.argv) > 3 else None
log_file = root.parent / "requests.log"
def pop_fault():
if fault_file is None or not fault_file.exists():
return None
lines = [ln for ln in fault_file.read_text().splitlines() if ln.strip()]
if not lines:
return None
fault_file.write_text("\n".join(lines[1:]) + ("\n" if lines[1:] else ""))
return lines[0].split()
class Handler(http.server.BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
def log_message(self, *a):
pass
def do_GET(self):
rng = self.headers.get("Range")
with log_file.open("a") as f:
f.write(f"{self.path} {rng or '-'}\n")
target = (root / self.path.lstrip("/")).resolve()
if root not in target.parents or not target.is_file():
self.send_response(404)
self.send_header("Content-Length", "0")
self.end_headers()
return
data = target.read_bytes()
fault = pop_fault() if not target.name.endswith(".sha256") else None
if fault and fault[0] == "status":
self.send_response(int(fault[1]))
self.send_header("Content-Length", "0")
self.end_headers()
return
start = 0
if rng and not (fault and fault[0] == "norange"):
start = int(rng.split("=")[1].split("-")[0])
if start >= len(data):
self.send_response(416)
self.send_header("Content-Range", f"bytes */{len(data)}")
self.send_header("Content-Length", "0")
self.end_headers()
return
self.send_response(206)
self.send_header(
"Content-Range", f"bytes {start}-{len(data) - 1}/{len(data)}"
)
else:
self.send_response(200)
body = data[start:]
self.send_header("Content-Length", str(len(body)))
self.send_header("Accept-Ranges", "bytes")
self.end_headers()
if fault and fault[0] == "truncate":
self.wfile.write(body[: int(fault[1])])
self.wfile.flush()
self.close_connection = True
self.connection.shutdown(2)
return
self.wfile.write(body)
class QuietServer(http.server.ThreadingHTTPServer):
def handle_error(
self, request, client_address
): # clients drop connections on purpose
pass
srv = QuietServer(("127.0.0.1", 0), Handler)
port_file.write_text(str(srv.server_address[1]))
srv.serve_forever()
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env bash
# Run every test suite for scripts/. No root, network, or real disks needed.
# scripts/tests/run.sh all suites
# scripts/tests/run.sh fetch only suites whose name contains "fetch"
set -u
cd -- "$(dirname -- "$(readlink -f -- "$0")")" || exit 1
filter="${1:-}"
failed=() skipped=() ran=0
need() { # need SUITE CMD...: skip the suite if a tool is missing
local suite="$1" c; shift
for c in "$@"; do
command -v "$c" >/dev/null 2>&1 || { skipped+=("$suite (needs $c)"); return 1; }
done
}
run_suite() {
local name="$1"; shift
[[ -z "$filter" || "$name" == *"$filter"* ]] || return 0
printf '\n== %s\n' "$name"
ran=$((ran + 1))
"$@" || failed+=("$name")
}
need python3 python3 && run_suite python python3 -m unittest -q test_python
need fetch curl python3 flock && run_suite shell/fetch-iso ./test_fetch_iso.sh
run_suite shell/gen-keys ./test_gen_keys.sh
run_suite shell/capture-hardware ./test_capture_hardware.sh
need docker gpg python3 && run_suite shell/docker-creds ./test_docker_creds.sh
need usb script cmp findmnt && run_suite shell/make-usb ./test_make_usb.sh
printf '\n'
((${#skipped[@]})) && printf 'SKIPPED: %s\n' "${skipped[@]}"
if ((${#failed[@]})); then
printf 'FAILED suites: %s\n' "${failed[*]}"
exit 1
fi
printf 'All %d suite(s) passed.\n' "$ran"
+6
View File
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
echo "blockdev $*" >> "${FAKE_OPS_LOG:-/dev/null}"
case "$1" in
--getsize64) awk -F'\t' -v d="$2" '$1==d {print $3; f=1} END {exit !f}' "$FAKE_DISKS" ;;
*) exit 0 ;;
esac
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
# Wrap real dd: drop oflag=direct (unsupported on tmpfs), log, inject faults.
echo "dd $*" >> "${FAKE_OPS_LOG:-/dev/null}"
[[ -z "${FAKE_DD_FAIL:-}" ]] || { echo "dd: error writing: Input/output error" >&2; exit 1; }
args=(); for a in "$@"; do [[ "$a" == oflag=direct ]] || args+=("$a"); done
/usr/bin/dd "${args[@]}" || exit $?
if [[ -n "${FAKE_DD_CORRUPT:-}" ]]; then
for a in "$@"; do [[ "$a" == of=* ]] && of="${a#of=}"; done
printf '\xff' | /usr/bin/dd of="$of" bs=1 seek=1000 conv=notrunc status=none
fi
+2
View File
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
printf '%s\n' "${FAKE_DMESG:-}"
+16
View File
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
# Fake findmnt driven by $FAKE_MOUNTS: lines of "SOURCE TARGET" (raw-escaped).
[[ -z "${FAKE_FINDMNT_FAIL:-}" ]] || { echo "findmnt: simulated failure" >&2; exit 1; }
touch "${FAKE_MOUNTS:?}"
if [[ "$*" == *"--target"* ]]; then
path="${@: -1}"; best="" src=""
while read -r s t; do
t="$(printf '%b' "$t")"
if [[ "$path" == "$t" || "$path" == "$t"/* || "$t" == / ]]; then
((${#t} >= ${#best})) && { best="$t"; src="$s"; }
fi
done < "$FAKE_MOUNTS"
[[ -n "$src" ]] && { echo "$src"; exit 0; }
exit 1
fi
cat "$FAKE_MOUNTS"
+43
View File
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
# Fake lsblk driven by $FAKE_DISKS, a TSV: NAME TYPE SIZE_BYTES MODEL TRAN RM CHILDREN(comma-separated)
# Supports only the invocations make-usb.sh uses.
[[ -z "${FAKE_LSBLK_FAIL:-}" ]] || { echo "lsblk: simulated failure" >&2; exit 1; }
cols="" dev="" flags=""
while (($#)); do
case "$1" in
-o) cols="$2"; shift 2 ;;
/dev/*|/*) dev="$1"; shift ;;
-*) flags+="$1"; shift ;;
*) shift ;;
esac
done
row() { awk -F'\t' -v d="$1" '$1==d' "$FAKE_DISKS"; }
field() { # field ROW COLNAME
local IFS=$'\t' f; read -ra f <<<"$1"
local v
case "$2" in NAME) v="${f[0]}";; TYPE) v="${f[1]}";; SIZE) v="${f[2]}";; MODEL) v="${f[3]}";; TRAN) v="${f[4]}";; RM) v="${f[5]}";; esac
[[ "$v" == "-" ]] && v=""
printf '%s\n' "$v"
}
if [[ -z "$dev" ]]; then
if [[ "$cols" == NAME,TYPE ]]; then
while IFS=$'\t' read -r n t _; do printf '%s %s\n' "$n" "$t"; done < "$FAKE_DISKS"
exit 0
fi
exit 1
fi
r="$(row "$dev")"
if [[ -z "$r" ]]; then
# maybe a child device
if grep -q -F -e "$dev" "$FAKE_DISKS"; then
case "$cols" in NAME) echo "$dev"; exit 0;; TYPE) echo part; exit 0;; *) echo; exit 0;; esac
fi
echo "lsblk: $dev: not a block device" >&2; exit 32
fi
if [[ "$flags" == *l* && "$cols" == NAME ]]; then
echo "$dev"
IFS=',' read -ra kids <<<"$(awk -F'\t' -v d="$dev" '$1==d {print $7}' "$FAKE_DISKS")"
for k in "${kids[@]}"; do [[ -n "$k" ]] && echo "$k"; done
exit 0
fi
field "$r" "$cols"
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
# Fake mountpoint: succeeds only for paths listed (colon-separated) in FAKE_MOUNTPOINTS.
[[ "$1" == "-q" ]] && shift
[[ "$1" == "--" ]] && shift
[[ ":${FAKE_MOUNTPOINTS:-}:" == *":$1:"* ]]
+19
View File
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# Fake nixos-generate-config. Env: FAKE_NGC_MODE = ok (default) | fail | empty | partial-then-fail | garbage
echo "$*" > "${FAKE_NGC_ARGS:-/dev/null}"
case "${FAKE_NGC_MODE:-ok}" in
ok)
cat <<'EOF'
# Do not modify this file! It was generated by nixos-generate-config
{ config, lib, pkgs, modulesPath, ... }:
{
fileSystems."/" = { device = "/dev/disk/by-uuid/1234"; fsType = "ext4"; };
nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux";
}
EOF
;;
fail) echo "boom" >&2; exit 1 ;;
empty) exit 0 ;;
partial-then-fail) echo "{ half = of a file"; exit 1 ;;
garbage) echo "error: something went wrong but exit 0" ;;
esac
+7
View File
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
# Fake `pass`. Supports only: pass init <gpg-id> Env: FAKE_PASS_FAIL=1, FAKE_PASS_LOG
echo "pass $*" >> "${FAKE_PASS_LOG:-/dev/null}"
[[ -z "${FAKE_PASS_FAIL:-}" ]] || { echo "pass: simulated failure" >&2; exit 1; }
[[ "$1" == init ]] || { echo "fake pass: unsupported: $*" >&2; exit 1; }
dir="${PASSWORD_STORE_DIR:-$HOME/.password-store}"
mkdir -p "$dir"; printf '%s\n' "$2" > "$dir/.gpg-id"
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
# Fake ssh-keygen for tests. Env: FAKE_KEYGEN_LOG, FAKE_KEYGEN_FAIL (substring of path), FAKE_KEYGEN_HANG
file="" comment="" pass="<unset>"
while (($#)); do
case "$1" in
-f) file="$2"; shift 2 ;; -C) comment="$2"; shift 2 ;; -N) pass="$2"; shift 2 ;;
-t) shift 2 ;; -q) shift ;; *) shift ;;
esac
done
[[ -n "${FAKE_KEYGEN_LOG:-}" ]] && printf 'file=%s comment=%s pass=%s\n' "$file" "$comment" "$pass" >> "$FAKE_KEYGEN_LOG"
if [[ -n "${FAKE_KEYGEN_FAIL:-}" && "$file" == *"$FAKE_KEYGEN_FAIL"* ]]; then
echo "ssh-keygen: simulated failure" >&2
: > "$file" # leave debris, like a real mid-flight failure could
exit 1
fi
umask 077
printf 'PRIVATE %s\n' "$comment" > "$file"
if [[ -n "${FAKE_KEYGEN_HANG:-}" ]]; then sleep "$FAKE_KEYGEN_HANG"; fi
printf 'ssh-ed25519 AAAAFAKE %s\n' "$comment" > "$file.pub"
chmod 644 "$file.pub"
+3
View File
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
echo "sudo $*" >> "${FAKE_SUDO_LOG:-/dev/null}"
exec "$@"
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
[[ "$1" == "--" ]] && shift
echo "umount $*" >> "${FAKE_OPS_LOG:-/dev/null}"
[[ ":${FAKE_UMOUNT_FAIL:-}:" != *":$1:"* ]] || { echo "umount: $1: target is busy" >&2; exit 32; }
grep -v -F " $1" "$FAKE_MOUNTS" > "$FAKE_MOUNTS.new"; mv "$FAKE_MOUNTS.new" "$FAKE_MOUNTS"
+2
View File
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
echo "wipefs $*" >> "${FAKE_OPS_LOG:-/dev/null}"
+170
View File
@@ -0,0 +1,170 @@
#!/usr/bin/env bash
set -u
# shellcheck source=harness.sh
source "$(dirname "$0")/harness.sh"
CH="$SCRIPTS/capture-hardware.sh"
PLACEHOLDER="# PLACEHOLDER hardware.nix for host \"willow\"."
setup() {
export PATH="$SHIMS:$PATH"
export FAKE_NGC_ARGS="$T/ngc.args"
REPO="$T/repo"; mkdir -p "$REPO/hosts/willow"; echo '{ }' > "$REPO/flake.nix"
echo "$PLACEHOLDER" > "$REPO/hosts/willow/hardware.nix"
cd "$REPO" || exit 1
}
hw() { cat "$REPO/hosts/willow/hardware.nix"; }
no_debris() { [[ -z "$(ls -A "$REPO/hosts/willow" | grep -v '^hardware.nix$' || true)" ]] || _fail "debris in hosts/willow: $(ls -A "$REPO/hosts/willow")"; }
test_replaces_placeholder() {
setup
run "$CH" willow
assert_rc 0
[[ "$(hw)" == *'nixpkgs.hostPlatform'* ]] || _fail "not replaced: $(hw)"
assert_mode "$REPO/hosts/willow/hardware.nix" 644
no_debris
}
test_generator_failure_preserves_existing_file() {
setup
echo "{ real = hand-edited; }" > hosts/willow/hardware.nix
FAKE_NGC_MODE=fail run "$CH" --force willow
assert_rc 3; assert_err_has "left untouched"
assert_eq "{ real = hand-edited; }" "$(hw)" "existing hardware.nix was damaged"
no_debris
}
test_partial_output_then_failure_preserves_file() {
setup
FAKE_NGC_MODE=partial-then-fail run "$CH" willow
assert_rc 3
assert_eq "$PLACEHOLDER" "$(hw)"
no_debris
}
test_empty_or_garbage_output_rejected() {
setup
local mode
for mode in empty garbage; do
FAKE_NGC_MODE=$mode run "$CH" willow
assert_rc 3 "mode $mode"
assert_eq "$PLACEHOLDER" "$(hw)"
done
no_debris
}
test_refuses_to_overwrite_real_file_non_interactively() {
setup
echo "{ real = 1; }" > hosts/willow/hardware.nix
run "$CH" willow
assert_rc 1; assert_err_has "refusing to overwrite"
assert_eq "{ real = 1; }" "$(hw)"
}
test_force_overwrites_real_file() {
setup
echo "{ real = 1; }" > hosts/willow/hardware.nix
run "$CH" -f willow
assert_rc 0
[[ "$(hw)" == *hostPlatform* ]] || _fail "not overwritten"
}
test_interactive_confirmation() {
setup
echo "{ real = 1; }" > hosts/willow/hardware.nix
# `read` needs a tty for the prompt branch; emulate one with script(1) if present.
command -v script >/dev/null 2>&1 || { echo " (skipped: no script(1))"; return 0; }
printf 'no\n' | script -qec "$CH willow" /dev/null >/dev/null 2>&1
assert_eq "{ real = 1; }" "$(hw)" "answering 'no' must keep the file"
printf 'yes\n' | script -qec "$CH willow" /dev/null >/dev/null 2>&1
[[ "$(hw)" == *hostPlatform* ]] || _fail "answering 'yes' should overwrite"
}
test_missing_host_dir_gives_actionable_message() {
setup
run "$CH" other
assert_rc 1; assert_err_has "just register-host other"
}
test_hostname_traversal_rejected() {
setup
local bad
for bad in '../x' 'a/b' 'Willow' '-x' '' 'a b' '..'; do
run "$CH" "$bad"
assert_rc 1 "name '$bad'"
done
assert_no_path "$T/hosts"
}
test_not_in_a_repo() {
setup
run "$CH" --repo-root "$T" willow
assert_rc 1; assert_err_has "no flake.nix"
run "$CH" --repo-root "$T/nope" willow
assert_rc 1; assert_err_has "repo root not found"
}
test_not_on_nixos() {
setup
local bin="$T/bin"; mkdir -p "$bin"
local c; for c in bash dirname readlink env grep mktemp rm mv chmod cat git; do ln -s "$(command -v $c)" "$bin/$c"; done
PATH="$bin" run "$CH" willow
assert_rc 2; assert_err_has "nixos-generate-config not found"
assert_eq "$PLACEHOLDER" "$(hw)"
}
test_installer_root_detected_only_when_target_has_etc() {
setup
mkdir -p "$T/mnt/etc"
FAKE_MOUNTPOINTS="$T/mnt" run "$CH" --install-root "$T/mnt" willow
assert_rc 0; assert_err_has "using --root $T/mnt"
[[ "$(<"$FAKE_NGC_ARGS")" == *"--root $T/mnt"* ]] || _fail "args: $(<"$FAKE_NGC_ARGS")"
rmdir "$T/mnt/etc"
echo "$PLACEHOLDER" > hosts/willow/hardware.nix
FAKE_MOUNTPOINTS="$T/mnt" run "$CH" --install-root "$T/mnt" willow
assert_rc 0; assert_err_has "doesn't look like a NixOS target"
[[ "$(<"$FAKE_NGC_ARGS")" != *"--root"* ]] || _fail "should not pass --root"
}
test_not_mounted_means_running_system() {
setup
run "$CH" --install-root "$T/mnt" willow
assert_rc 0
[[ "$(<"$FAKE_NGC_ARGS")" != *"--root"* ]] || _fail "should not pass --root"
}
test_uses_sudo_only_when_not_root() {
setup
export FAKE_SUDO_LOG="$T/sudo.log"
run "$CH" willow; assert_rc 0
if ((EUID == 0)); then
[[ ! -e "$FAKE_SUDO_LOG" ]] || _fail "sudo used even though we are root"
else
grep -q "nixos-generate-config" "$FAKE_SUDO_LOG" || _fail "sudo not used for a non-root user"
fi
}
test_relative_repo_root_and_git_diff() {
setup
git init -q . && git add -A && git -c user.email=a@b -c user.name=n commit -qm init
cd "$T" || exit 1
run "$CH" --repo-root repo willow
assert_rc 0; assert_err_has "git diff"; assert_err_has "git-tracked"
assert_out_has "hostPlatform" # the diff body
}
test_help_and_unknown_flags() {
setup
run "$CH" -h; assert_rc 0; assert_out_has "Usage:"
run "$CH" --nope willow; assert_rc 1
run "$CH" willow extra; assert_rc 1
run "$CH" ; assert_rc 1
run "$CH" --repo-root; assert_rc 2
}
test_leading_dash_name_via_double_dash_is_validated() {
setup
run "$CH" -- -rf
assert_rc 1; assert_err_has "invalid hostname"
}
run_all "${1:-}"
+186
View File
@@ -0,0 +1,186 @@
#!/usr/bin/env bash
set -u
# shellcheck source=harness.sh
source "$(dirname "$0")/harness.sh"
DC="$SCRIPTS/docker-creds.sh"
setup() {
export HOME="$T/home"; mkdir -p "$HOME"
export GNUPGHOME="$T/gnupg"; mkdir -p "$GNUPGHOME"; chmod 700 "$GNUPGHOME"
export PATH="$SHIMS:$PATH"
export FAKE_PASS_LOG="$T/pass.log"
unset DOCKER_CONFIG PASSWORD_STORE_DIR
CFG="$HOME/.docker/config.json"
}
jq_get() { python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); print(json.dumps(d.get(sys.argv[2]), sort_keys=True))' "$CFG" "$1"; }
secret_key_count() { gpg --list-secret-keys --with-colons 2>/dev/null | grep -c '^sec:' || true; }
test_non_pass_store_only_edits_config() {
setup
run "$DC" secretservice
assert_rc 0
assert_eq '"secretservice"' "$(jq_get credsStore)"
assert_mode "$CFG" 600
assert_eq 0 "$(secret_key_count)" "must not create GPG keys for non-pass stores"
[[ ! -e "$FAKE_PASS_LOG" ]] || _fail "pass should not be invoked"
assert_err_has "skipping GPG/pass setup"
}
test_preserves_existing_config_keys() {
setup
mkdir -p "$HOME/.docker"
cat > "$CFG" <<'EOF'
{"auths": {"ghcr.io": {"auth": "c2VjcmV0"}}, "credHelpers": {"gcr.io": "gcloud"}, "currentContext": "colima", "credsStore": "old"}
EOF
run "$DC" secretservice
assert_rc 0
assert_eq '{"ghcr.io": {"auth": "c2VjcmV0"}}' "$(jq_get auths)"
assert_eq '{"gcr.io": "gcloud"}' "$(jq_get credHelpers)"
assert_eq '"colima"' "$(jq_get currentContext)"
assert_eq '"secretservice"' "$(jq_get credsStore)"
}
test_invalid_json_is_never_overwritten() {
setup
mkdir -p "$HOME/.docker"
printf '{"auths": {"ghcr.io": {"auth": "c2VjcmV0"},}}' > "$CFG" # trailing comma
local before; before="$(<"$CFG")"
run "$DC" secretservice
assert_rc 1; assert_err_has "not valid JSON"
assert_eq "$before" "$(<"$CFG")" "credentials file was clobbered"
no_temp_files
}
test_non_object_json_is_refused() {
setup
mkdir -p "$HOME/.docker"; echo '["not","an","object"]' > "$CFG"
run "$DC" secretservice
assert_rc 1; assert_err_has "JSON object"
assert_file_eq "$CFG" '["not","an","object"]'
}
test_empty_file_treated_as_empty_config() {
setup
mkdir -p "$HOME/.docker"; : > "$CFG"
run "$DC" secretservice
assert_rc 0
assert_eq '"secretservice"' "$(jq_get credsStore)"
}
no_temp_files() { [[ -z "$(find "$HOME/.docker" -name '*.tmp' 2>/dev/null)" ]] || _fail "temp files left behind"; }
test_already_set_does_not_rewrite() {
setup
mkdir -p "$HOME/.docker"; printf '{\n "credsStore": "secretservice"\n}' > "$CFG" # distinctive formatting
local before; before="$(<"$CFG")"
run "$DC" secretservice
assert_rc 0; assert_out_has "already"
assert_eq "$before" "$(<"$CFG")"
}
test_readonly_config_file_is_handled() {
setup
mkdir -p "$HOME/.docker"; echo '{"auths":{}}' > "$CFG"; chmod 444 "$CFG"
run "$DC" secretservice
assert_rc 0
assert_eq '"secretservice"' "$(jq_get credsStore)"
assert_mode "$CFG" 600
}
test_symlinked_config_is_replaced_not_written_through() {
setup
mkdir -p "$HOME/.docker" "$T/store"
echo '{"auths":{"x":{}}}' > "$T/store/config.json"; chmod 444 "$T/store/config.json"
ln -s "$T/store/config.json" "$CFG"
run "$DC" secretservice
assert_rc 0; assert_err_has "was a symlink"
[[ ! -L "$CFG" ]] || _fail "still a symlink"
assert_eq '"secretservice"' "$(jq_get credsStore)"
assert_eq '{"x": {}}' "$(jq_get auths)"
assert_file_eq "$T/store/config.json" '{"auths":{"x":{}}}' # original target untouched
}
test_docker_config_env_is_honored() {
setup
DOCKER_CONFIG="$T/alt" run "$DC" secretservice
assert_rc 0; assert_file "$T/alt/config.json"; assert_no_path "$HOME/.docker"
}
test_pass_flow_end_to_end_and_idempotent() {
setup
run "$DC" pass --no-passphrase
assert_rc 0
assert_err_has "UNPROTECTED"
assert_eq 1 "$(secret_key_count)"
local fpr; fpr="$(<"$HOME/.password-store/.gpg-id")"
[[ "$fpr" =~ ^[0-9A-F]{40}$ ]] || _fail "bad fingerprint in .gpg-id: $fpr"
gpg --list-secret-keys "$fpr" >/dev/null 2>&1 || _fail ".gpg-id does not name a real secret key"
assert_eq '"pass"' "$(jq_get credsStore)"
# second run: nothing new
run "$DC" pass --no-passphrase
assert_rc 0
assert_err_has "already exists"; assert_err_has "already initialized"
assert_eq 1 "$(secret_key_count)" "re-run created a second key"
assert_eq 1 "$(grep -c 'pass init' "$FAKE_PASS_LOG")" "pass init ran twice"
}
test_uid_is_stable_and_findable() {
setup
run "$DC" pass --no-passphrase; assert_rc 0
gpg --list-secret-keys --with-colons | grep '^uid:' | grep -q "Docker Credential Store <$(id -un)@" \
|| _fail "unexpected uid: $(gpg --list-secret-keys --with-colons | grep '^uid:')"
}
test_works_when_USER_is_unset() {
setup
env -u USER "$DC" secretservice 2>"$T/err" >/dev/null || _fail "failed without USER: $(<"$T/err")"
}
test_missing_tools_checked_before_any_change() {
setup
local bin="$T/bin"; mkdir -p "$bin"
local c; for c in bash dirname readlink env python3 mkdir id hostname uname tty cat; do ln -s "$(command -v $c)" "$bin/$c"; done
PATH="$bin" run "$DC" pass --no-passphrase
assert_rc 3; assert_err_has "gpg"
assert_no_path "$CFG" # nothing was modified
}
test_pass_init_failure_is_reported() {
setup
FAKE_PASS_FAIL=1 run "$DC" pass --no-passphrase
assert_rc 1; assert_err_has "pass init failed"
}
test_passphrase_mode_without_pinentry_fails_cleanly_not_hangs() {
setup
# No agent/pinentry/tty here: gpg must fail promptly and we must report it.
GNUPGHOME="$GNUPGHOME" timeout 60 "$DC" pass >"$T/o" 2>"$T/e" </dev/null; RC=$?
ERR="$(<"$T/e")"
[[ $RC == 124 ]] && _fail "hung waiting for a passphrase"
if ((RC != 0)); then
assert_err_has "GPG key generation failed"
assert_no_path "$HOME/.password-store/.gpg-id"
fi
}
test_argument_validation() {
setup
run "$DC" --bogus; assert_rc 2
run "$DC" pass extra; assert_rc 2
run "$DC" '../x'; assert_rc 2
run "$DC" 'a b'; assert_rc 2
run "$DC" -h; assert_rc 0; assert_out_has "Usage:"
assert_no_path "$CFG"
}
test_empty_string_args_from_just_are_ignored() {
setup
run "$DC" secretservice "" # `just docker-creds secretservice` passes flag=""
assert_rc 0
run "$DC" "" "" # defaults -> pass; needs gpg/pass -> use shims
# (defaults to store=pass: runs real gpg flow without --no-passphrase; just assert it parsed)
[[ "$ERR" != *"invalid credential store"* ]] || _fail "empty arg rejected"
}
run_all "${1:-}"
+217
View File
@@ -0,0 +1,217 @@
#!/usr/bin/env bash
set -u
# shellcheck source=harness.sh
source "$(dirname "$0")/harness.sh"
FI="$SCRIPTS/fetch-iso.sh"
REAL="nixos-graphical-26.05.1234.abcdef0-x86_64-linux.iso"
LATEST="latest-nixos-graphical-x86_64-linux.iso"
# Start the server in $T/www and point the script at it.
serve() {
mkdir -p "$T/www/nixos-26.05"
head -c 3000000 /dev/urandom > "$T/www/nixos-26.05/$LATEST"
local h; h="$(sha256sum "$T/www/nixos-26.05/$LATEST" | cut -d' ' -f1)"
echo "$h" > "$T/hash"
printf '%s %s\n' "$h" "$REAL" > "$T/www/nixos-26.05/$LATEST.sha256"
: > "$T/faults"
python3 "$HARNESS_DIR/range_server.py" "$T/www" "$T/port" "$T/faults" &
SERVER_PID=$!
local i; for i in {1..50}; do [[ -s "$T/port" ]] && break; sleep 0.1; done
export NIXOS_CHANNELS_URL="http://127.0.0.1:$(<"$T/port")"
trap 'kill $SERVER_PID 2>/dev/null' EXIT
}
iso_gets() { grep -c "^/nixos-26.05/$LATEST " "$T/requests.log" 2>/dev/null || true; }
test_fresh_download() {
serve
run "$FI" --dest "$T/iso"
assert_rc 0
assert_file "$T/iso/$REAL"
assert_eq "$(<"$T/hash")" "$(sha256sum "$T/iso/$REAL" | cut -d' ' -f1)"
assert_file_eq "$T/iso/$REAL.sha256" "$(<"$T/hash") $REAL"
assert_eq "$REAL" "$(readlink "$T/iso/$LATEST")"
assert_no_path "$T/iso/$REAL.part"
(cd "$T/iso" && sha256sum -c --status "$REAL.sha256") || _fail "sidecar doesn't verify"
assert_eq "" "$(ls -A "$T/iso" | grep '^\.' | grep -v lock || true)" "stray temp files left in dest"
}
test_second_run_is_a_noop_without_redownload() {
serve
run "$FI" --dest "$T/iso"; assert_rc 0
run "$FI" --dest "$T/iso"; assert_rc 0
assert_err_has "already present and verified"
assert_eq 1 "$(iso_gets)" "ISO should have been fetched exactly once"
}
test_interrupted_download_resumes_instead_of_restarting() {
serve
echo "truncate 1000000" > "$T/faults"
run "$FI" --dest "$T/iso"
assert_rc 2; assert_err_has "re-run to resume"
assert_eq 1000000 "$(stat -c %s "$T/iso/$REAL.part")"
assert_no_path "$T/iso/$REAL"
run "$FI" --dest "$T/iso"
assert_rc 0; assert_err_has "Resuming"
grep -q "bytes=1000000-" "$T/requests.log" || _fail "second run did not send a Range header: $(<"$T/requests.log")"
assert_eq "$(<"$T/hash")" "$(sha256sum "$T/iso/$REAL" | cut -d' ' -f1)"
assert_no_path "$T/iso/$REAL.part"
}
test_corrupt_final_iso_is_replaced() {
serve
mkdir -p "$T/iso"; echo "garbage" > "$T/iso/$REAL"
run "$FI" --dest "$T/iso"
assert_rc 0; assert_err_has "doesn't match upstream"
assert_eq "$(<"$T/hash")" "$(sha256sum "$T/iso/$REAL" | cut -d' ' -f1)"
}
test_complete_part_file_is_finalized_without_network_download() {
serve
mkdir -p "$T/iso"; cp "$T/www/nixos-26.05/$LATEST" "$T/iso/$REAL.part"
run "$FI" --dest "$T/iso"
assert_rc 0; assert_err_has "complete partial download"
assert_eq 0 "$(iso_gets)"
assert_file "$T/iso/$REAL"; assert_no_path "$T/iso/$REAL.part"
}
test_oversized_part_is_discarded_and_download_restarts() {
serve
mkdir -p "$T/iso"; head -c 4000000 /dev/urandom > "$T/iso/$REAL.part" # bigger than the 3 MB remote
run "$FI" --dest "$T/iso"
assert_rc 0; assert_err_has "starting over"
assert_eq "$(<"$T/hash")" "$(sha256sum "$T/iso/$REAL" | cut -d' ' -f1)"
assert_no_path "$T/iso/$REAL.part"
}
test_garbage_prefix_part_is_detected_and_redownloaded_automatically() {
serve
mkdir -p "$T/iso"; head -c 1000 /dev/urandom > "$T/iso/$REAL.part" # resume would yield garbage+tail
run "$FI" --dest "$T/iso"
assert_rc 0; assert_err_has "resumed download failed verification"
assert_eq "$(<"$T/hash")" "$(sha256sum "$T/iso/$REAL" | cut -d' ' -f1)"
}
test_persistently_bad_download_gives_up_after_one_retry() {
serve
printf '%064d %s\n' 0 "$REAL" > "$T/www/nixos-26.05/$LATEST.sha256" # nothing can ever verify
mkdir -p "$T/iso"; head -c 1000 /dev/urandom > "$T/iso/$REAL.part"
run "$FI" --dest "$T/iso"
assert_rc 3; assert_err_has "SHA-256 verification failed"
assert_no_path "$T/iso/$REAL"; assert_no_path "$T/iso/$REAL.part"; assert_no_path "$T/iso/$LATEST"
assert_eq 2 "$(iso_gets)" "exactly one retry expected"
}
test_server_that_ignores_range_restarts_from_scratch() {
serve
mkdir -p "$T/iso"; head -c 500000 "$T/www/nixos-26.05/$LATEST" > "$T/iso/$REAL.part"
echo "norange" > "$T/faults"
run "$FI" --dest "$T/iso"
assert_rc 0; assert_err_has "starting over"
assert_eq "$(<"$T/hash")" "$(sha256sum "$T/iso/$REAL" | cut -d' ' -f1)"
}
test_transient_server_error_keeps_partial_file() {
serve
mkdir -p "$T/iso"; head -c 500000 "$T/www/nixos-26.05/$LATEST" > "$T/iso/$REAL.part"
printf 'status 404\n' > "$T/faults"
run "$FI" --dest "$T/iso"
assert_rc 2
assert_eq 500000 "$(stat -c %s "$T/iso/$REAL.part")" "a 404 must not delete the user's partial download"
}
test_wrong_hash_on_fresh_download_is_rejected() {
serve
printf '%064d %s\n' 0 "$REAL" > "$T/www/nixos-26.05/$LATEST.sha256"
run "$FI" --dest "$T/iso"
assert_rc 3
assert_no_path "$T/iso/$REAL"; assert_no_path "$T/iso/$REAL.part"; assert_no_path "$T/iso/$LATEST"
}
test_hostile_upstream_checksum_files() {
serve
local h bad; h="$(<"$T/hash")"
local -a cases=(
"$h ../../evil.iso"
"$h /etc/passwd.iso"
"$h sub/dir.iso"
"$h notaniso.txt"
"$h name with spaces.iso"
"$h ..iso"
"$h a..b.iso"
"nothex $REAL"
"${h:0:63} $REAL"
"$h"
""
"<html>404</html>"
)
for bad in "${cases[@]}"; do
printf '%s\n' "$bad" > "$T/www/nixos-26.05/$LATEST.sha256"
run "$FI" --dest "$T/iso"
assert_rc 2 "case: $bad"
assert_no_path "$T/evil.iso"; assert_no_path "$T/../evil.iso"
[[ -z "$(find "$T/iso" -name '*.iso*' 2>/dev/null)" ]] || _fail "created files for hostile input '$bad'"
done
}
test_binary_marker_and_uppercase_hash_accepted() {
serve
local h; h="$(<"$T/hash")"
printf '%s *%s\n' "${h^^}" "$REAL" > "$T/www/nixos-26.05/$LATEST.sha256"
run "$FI" --dest "$T/iso"
assert_rc 0
assert_file_eq "$T/iso/$REAL.sha256" "$h $REAL" # normalized
}
test_sha_file_404() {
serve
rm "$T/www/nixos-26.05/$LATEST.sha256"
run "$FI" --dest "$T/iso"
assert_rc 2; assert_err_has "sha256 fetch failed"
}
test_unreachable_server() {
export NIXOS_CHANNELS_URL="http://127.0.0.1:1"
run "$FI" --dest "$T/iso"
assert_rc 2
}
test_newer_iso_updates_symlink_and_keeps_old() {
serve
run "$FI" --dest "$T/iso"; assert_rc 0
head -c 2000000 /dev/urandom > "$T/www/nixos-26.05/$LATEST"
local h2; h2="$(sha256sum "$T/www/nixos-26.05/$LATEST" | cut -d' ' -f1)"
local new="nixos-graphical-26.05.9999.ffffff0-x86_64-linux.iso"
printf '%s %s\n' "$h2" "$new" > "$T/www/nixos-26.05/$LATEST.sha256"
run "$FI" --dest "$T/iso"; assert_rc 0
assert_eq "$new" "$(readlink "$T/iso/$LATEST")"
assert_file "$T/iso/$REAL"; assert_file "$T/iso/$new"
}
test_option_validation() {
local bad
for bad in "--channel ../x" "--channel a/b" "--channel -x" "--edition ''" "--arch x;y" "--channel" "--dest" "--bogus"; do
# shellcheck disable=SC2086
run "$FI" $bad
[[ "$RC" == 1 || "$RC" == 2 ]] || _fail "'$bad' exited $RC"
done
run "$FI" --channel=; assert_rc 1
run "$FI" -h; assert_rc 0; assert_out_has "Usage:"
}
test_option_equals_form() {
serve
run "$FI" --dest="$T/iso" --channel=nixos-26.05 --edition=graphical --arch=x86_64-linux
assert_rc 0; assert_file "$T/iso/$REAL"
}
test_concurrent_run_is_refused() {
serve
mkdir -p "$T/iso"
flock "$T/iso/.fetch-iso.lock" sleep 5 &
local lp=$!; sleep 0.3
run "$FI" --dest "$T/iso"
kill "$lp" 2>/dev/null
assert_rc 1; assert_err_has "already running"
}
run_all "${1:-}"
+129
View File
@@ -0,0 +1,129 @@
#!/usr/bin/env bash
set -u
# shellcheck source=harness.sh
source "$(dirname "$0")/harness.sh"
GK="$SCRIPTS/gen-keys.sh"
setup() { export HOME="$T/home"; mkdir -p "$HOME"; export PATH="$SHIMS:$PATH"; export FAKE_KEYGEN_LOG="$T/keygen.log"; }
test_creates_keys_with_secure_perms() {
setup
run "$GK" github gitlab
assert_rc 0
assert_file "$HOME/.ssh/id_github"; assert_file "$HOME/.ssh/id_github.pub"; assert_file "$HOME/.ssh/id_gitlab"
assert_mode "$HOME/.ssh" 700
assert_mode "$HOME/.ssh/id_github" 600
assert_err_has "generating: $HOME/.ssh/id_github"
}
test_comment_and_empty_passphrase() {
setup
run "$GK" github
local line; line="$(<"$FAKE_KEYGEN_LOG")"
[[ "$line" == *"comment=github@"* ]] || _fail "bad comment: $line"
[[ "$line" == *"pass="* && "$line" != *"pass=<unset>"* ]] || _fail "expected -N \"\": $line"
}
test_is_idempotent_and_never_overwrites() {
setup
mkdir -p "$HOME/.ssh"; echo "MY PRECIOUS KEY" > "$HOME/.ssh/id_github"
run "$GK" github
assert_rc 0; assert_err_has "already exists"
assert_file_eq "$HOME/.ssh/id_github" "MY PRECIOUS KEY"
assert_no_path "$HOME/.ssh/id_github.pub"
[[ ! -e "$FAKE_KEYGEN_LOG" ]] || _fail "ssh-keygen should not have run"
}
test_dangling_symlink_counts_as_existing() {
setup
mkdir -p "$HOME/.ssh"; ln -s /nonexistent "$HOME/.ssh/id_github"
run "$GK" github
assert_rc 0; assert_err_has "already exists"
[[ ! -e "$FAKE_KEYGEN_LOG" ]] || _fail "ssh-keygen should not have run"
}
test_orphan_public_key_is_an_error_not_a_hang() {
setup
mkdir -p "$HOME/.ssh"; echo pub > "$HOME/.ssh/id_github.pub"
run "$GK" github gitlab
assert_rc 1; assert_err_has "without a matching private key"
assert_file "$HOME/.ssh/id_gitlab" # other services still processed
assert_file_eq "$HOME/.ssh/id_github.pub" "pub"
}
test_rejects_bad_names_before_creating_anything() {
setup
local bad
for bad in '../escape' 'a/b' '-rf' '.hidden' 'a b' '' 'x;rm' "$(printf 'a%.0s' {1..65})"; do
run "$GK" good "$bad"
assert_rc 2 "name '$bad'"
assert_no_path "$HOME/.ssh/id_good"
done
assert_no_path "$T/escape"
}
test_duplicates_on_cmdline_generate_once() {
setup
run "$GK" github github github
assert_rc 0
assert_eq 1 "$(wc -l < "$FAKE_KEYGEN_LOG")"
}
test_no_args_is_a_noop() {
setup
run "$GK"
assert_rc 0; assert_no_path "$HOME/.ssh"
}
test_keygen_failure_cleans_debris_and_continues() {
setup
FAKE_KEYGEN_FAIL=bad run "$GK" bad ok
assert_rc 1; assert_err_has "failed for 'bad'"
assert_no_path "$HOME/.ssh/id_bad"
assert_file "$HOME/.ssh/id_ok"
}
_interrupt_test() { # _interrupt_test SIGNAL
setup
FAKE_KEYGEN_HANG=30 launch_like_terminal "$GK" first second
local i; for i in {1..80}; do [[ -e "$HOME/.ssh/id_first" ]] && break; sleep 0.1; done
assert_file "$HOME/.ssh/id_first"
signal_group "$1"
assert_no_path "$HOME/.ssh/id_first"
assert_no_path "$HOME/.ssh/id_first.pub"
assert_no_path "$HOME/.ssh/id_second" # did not carry on to the next service
}
test_sigint_removes_half_written_pair_and_stops() { _interrupt_test INT; }
test_sigterm_removes_half_written_pair_and_stops() { _interrupt_test TERM; }
test_passphrase_flag_requires_tty() {
setup
run "$GK" --passphrase github
assert_rc 2; assert_err_has "interactive terminal"
}
test_unknown_option_and_help() {
setup
run "$GK" --bogus; assert_rc 2
run "$GK" -h; assert_rc 0; assert_out_has "Usage:"
run "$GK" -- -weird; assert_rc 2 # still validated after --
}
test_missing_ssh_keygen() {
setup
# a PATH with the basics but no ssh-keygen
local bin="$T/bin"; mkdir -p "$bin"
local c; for c in bash dirname readlink env mkdir chmod hostname uname rm cat; do ln -s "$(command -v $c)" "$bin/$c"; done
PATH="$bin" run "$GK" github
assert_rc 3; assert_err_has "ssh-keygen"
}
test_hostname_is_sanitized_in_comment() {
setup
local bin="$T/bin"; mkdir -p "$bin"
printf '#!/bin/sh\necho "evil host;name"\n' > "$bin/hostname"; chmod +x "$bin/hostname"
PATH="$bin:$PATH" run "$GK" github
[[ "$(<"$FAKE_KEYGEN_LOG")" == *"comment=github@evil_host_name"* ]] || _fail "$(<"$FAKE_KEYGEN_LOG")"
}
run_all "${1:-}"
+389
View File
@@ -0,0 +1,389 @@
#!/usr/bin/env bash
set -u
# shellcheck source=harness.sh
source "$(dirname "$0")/harness.sh"
export SCRIPTS
MU="$SCRIPTS/make-usb.sh"
DRIVER="$HARNESS_DIR/usb_driver.sh"
# ---- fixtures --------------------------------------------------------------
TAB=$'\t'
row() { printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$@" >> "$FAKE_DISKS"; }
mkfix() {
export PATH="$SHIMS:$PATH"
export FAKE_DISKS="$T/disks.tsv" FAKE_MOUNTS="$T/mounts" FAKE_OPS_LOG="$T/ops.log" PROC_SWAPS="$T/swaps"
mkdir -p "$T/dev" "$T/iso"
: > "$FAKE_DISKS"; : > "$FAKE_MOUNTS"; : > "$FAKE_OPS_LOG"
printf 'Filename\tType\tSize\tUsed\tPriority\n' > "$PROC_SWAPS"
SDA="$T/dev/sda" SDB="$T/dev/sdb" SDC="$T/dev/sdc" SDD="$T/dev/sdd"
touch "$SDA" "$SDC" "$SDD"; truncate -s 16M "$SDB"
# name type size model tran rm children
row "$SDA" disk 500107862016 "Samsung SSD 860" sata 0 "${SDA}1,${SDA}2,${SDA}3"
row "$SDB" disk 16008609792 "SanDisk Ultra" usb 1 "${SDB}1"
row "$SDC" disk 1000204886016 "WD Elements" usb 0 "${SDC}1"
row "$SDD" disk 250059350016 "Data SSD" sata 0 "${SDD}1"
row "$T/dev/sr0" rom 1073741312 "DVD" sata 1 -
row "$T/dev/loop0" loop 1000000 - - 0 -
printf '%s /\n%s /boot/efi\n%s /run/media/alex/NIXOS\n' "${SDA}2" "${SDA}1" "${SDB}1" > "$FAKE_MOUNTS"
ISO="$T/iso/nixos.iso"; head -c 3145728 /dev/urandom > "$ISO"
(cd "$T/iso" && sha256sum nixos.iso > nixos.iso.sha256)
}
ops() { cat "$FAKE_OPS_LOG"; }
did() { grep -q -- "$1" "$FAKE_OPS_LOG"; }
usb() { run bash "$DRIVER" "$@"; } # non-interactive end-to-end
unit() { run bash -c 'source "$SCRIPTS/make-usb.sh"; is_block_device() { [[ -e "$1" ]]; }; set +e; '"$1"; }
# pty(): run the driver on a pseudo-terminal feeding $1 as keyboard input.
pty() { local input="$1"; shift; local cmd; cmd="$(printf '%q ' bash "$DRIVER" "$@")"
local o="$T/pty.out"; printf '%b' "$input" | script -qec "$cmd" /dev/null > "$o" 2>&1; RC=$?; OUT="$(<"$o")"; ERR="$OUT"; }
# ---- unit: device inspection ----------------------------------------------
test_model_strings_cannot_inject_commands() {
mkfix
# The old script ran `eval` on lsblk output, so a USB stick whose model is
# "$(touch PWNED)" executed arbitrary commands as root.
row "$T/dev/sde" disk 16008609792 "\$(touch $T/PWNED) \`touch $T/PWNED2\` \"; touch $T/PWNED3; \"" usb 1 -
touch "$T/dev/sde"
pty "0\n" ; unit 'select_device <<<"0" 2>&1; echo done'
pty "0\n"
assert_no_path "$T/PWNED"; assert_no_path "$T/PWNED2"; assert_no_path "$T/PWNED3"
[[ "$OUT" == *'touch'* ]] || _fail "malicious model should be displayed as inert text: $OUT"
}
test_original_script_was_exploitable_via_model_string() {
# Documents the vulnerability being fixed: this is the original parsing logic.
local line='NAME="/dev/sdx" MODEL="$(touch '"$T"'/PWNED)" TYPE="disk"'
[[ "$line" =~ ^([A-Z]+=\"[^\"]*\"[[:space:]]*)+$ ]] || _fail "original validation regex would have rejected it"
eval "$line"
assert_file "$T/PWNED"
}
test_blk_field_strips_control_characters() {
mkfix
row "$T/dev/sdf" disk 1 $'\e[31mEVIL\e[0m\a\r ' usb 1 -
unit "blk_field '$T/dev/sdf' MODEL | cat -v"
assert_eq '[31mEVIL[0m' "$OUT"
unit "blk_field '$SDB' MODEL"; assert_eq "SanDisk Ultra" "$OUT"
}
test_list_candidates_skips_optical_and_loop() {
mkfix
unit 'list_candidate_disks'
assert_eq "$SDA
$SDB
$SDC
$SDD" "$OUT"
}
test_protected_mounts_allowlist_not_blocklist() {
mkfix
# The old check only looked for exactly / and /boot, so these slipped through:
printf '%s /nix/store\n%s /home\n%s /var/lib/docker\n%s /boot/efi\n' "${SDD}1" "${SDD}1" "${SDD}1" "${SDD}1" > "$FAKE_MOUNTS"
unit "protected_mounts '$SDD'"
assert_eq "/nix/store
/home
/var/lib/docker
/boot/efi" "$OUT"
}
test_automount_points_are_not_protected() {
mkfix
printf '%s /run/media/alex/A B\n%s /media/usb\n%s /mnt/x\n' "${SDB}1" "${SDB}1" "${SDB}1" > "$FAKE_MOUNTS"
unit "protected_mounts '$SDB'; echo rc=\$?"
assert_eq "rc=0" "$OUT"
}
test_mount_point_with_spaces_decoded() {
mkfix
printf '%s /run/media/alex/My\\x20Stick\n' "${SDB}1" > "$FAKE_MOUNTS"
unit "mounts_of '$SDB'"
assert_eq "/run/media/alex/My Stick" "$OUT"
}
test_btrfs_subvolume_suffix_and_luks_holder() {
mkfix
row "$T/dev/sdg" disk 1000000000000 "NVMe" nvme 0 "$T/dev/sdg1,$T/dev/mapper/cryptroot"
printf '%s[/@] /\n%s /home\n' "$T/dev/mapper/cryptroot" "$T/dev/mapper/cryptroot" > "$FAKE_MOUNTS"
touch "$T/dev/sdg"
unit "protected_mounts '$T/dev/sdg'"
assert_eq "/
/home" "$OUT"
}
test_swap_protects_disk() {
mkfix
printf '%s\tpartition\t100\t0\t-2\n' "${SDD}1" >> "$PROC_SWAPS"
unit "protected_mounts '$SDD'"
assert_out_has "SWAP"
}
test_unreadable_swap_table_fails_closed() {
mkfix
PROC_SWAPS="$T/does-not-exist" unit "protected_mounts '$SDB'"
assert_out_has "SWAP"
}
test_unreadable_mount_table_fails_closed() {
mkfix
FAKE_FINDMNT_FAIL=1 unit "protected_mounts '$SDB'"
assert_out_has "mount table unreadable"
FAKE_FINDMNT_FAIL=1 unit "unsafe_reason '$SDB'; echo rc=\$?"
assert_out_has "rc=0" # 0 means "unsafe"
}
test_lsblk_failure_fails_closed() {
mkfix
FAKE_LSBLK_FAIL=1 unit "unsafe_reason '$SDB'; echo rc=\$?"
assert_out_has "cannot inspect"; assert_out_has "rc=0"
}
test_unsafe_reason_matrix() {
mkfix
local d expect
# device : expected verdict ("safe" or substring of the reason)
for pair in "$SDB:safe" "$SDC:safe" "$SDA:in-use system data" "$SDD:neither removable" \
"${SDB}1:not a whole disk" "$T/dev/nonexistent:not a block device"; do
d="${pair%%:*}"; expect="${pair#*:}"
# `${SDB}1` is a child and is not a node in the fixture: create it so is_block_device passes.
touch "$d" 2>/dev/null
unit "unsafe_reason '$d'; echo rc=\$?" ; [[ "$d" == *nonexistent ]] && rm -f "$d" && unit "unsafe_reason '$d'; echo rc=\$?"
if [[ "$expect" == safe ]]; then
assert_eq "rc=1" "$OUT" "$d should be acceptable"
else
[[ "$OUT" == *"$expect"* && "$OUT" == *rc=0 ]] || _fail "$d: wanted '$expect', got: $OUT"
fi
done
}
test_usb_hdd_reporting_removable_0_is_accepted_via_transport() {
mkfix
printf '' > "$FAKE_MOUNTS"
row "$T/dev/sdh" disk 1000204886016 "Passport" usb 0 -
touch "$T/dev/sdh"
unit "unsafe_reason '$T/dev/sdh'; echo rc=\$?"; assert_eq "rc=1" "$OUT"
}
test_allow_internal_does_not_override_system_disk() {
mkfix
unit "ALLOW_INTERNAL=1; unsafe_reason '$SDD'; echo rc=\$?"; assert_eq "rc=1" "$OUT" # data disk: now allowed
unit "ALLOW_INTERNAL=1; unsafe_reason '$SDA'; echo rc=\$?"; assert_out_has "in-use system data"
}
test_iso_on_target_device_detected() {
mkfix
printf '%s /run/media/alex/NIXOS\n' "${SDB}1" > "$FAKE_MOUNTS"
unit "iso_on_device /run/media/alex/NIXOS/nixos.iso '$SDB'; echo rc=\$?"; assert_eq "rc=0" "$OUT"
unit "iso_on_device /run/media/alex/NIXOS/nixos.iso '$SDD'; echo rc=\$?"; assert_eq "rc=1" "$OUT"
printf '%s /\n' "${SDA}2" > "$FAKE_MOUNTS"
unit "iso_on_device /home/alex/nixos.iso '$SDB'; echo rc=\$?"; assert_eq "rc=1" "$OUT"
}
# ---- end to end -------------------------------------------------------------
test_e2e_happy_path() {
mkfix
usb --iso "$ISO" --device "$SDB" --yes
assert_rc 0
assert_err_has "Read-back verified"
cmp -n 3145728 "$ISO" "$SDB" || _fail "device content differs from ISO"
did "umount /run/media/alex/NIXOS" || _fail "stick was not unmounted: $(ops)"
did "wipefs -a $SDB" || _fail "no wipefs"
# order matters: unmount -> wipe -> dd
local order; order="$(grep -o '^\(umount\|wipefs\|dd\)' "$FAKE_OPS_LOG" | tr '\n' ' ')"
assert_eq "umount wipefs dd " "$order"
}
test_e2e_resolves_symlinked_device_path() {
mkfix
mkdir -p "$T/dev/disk/by-id"; ln -s "$SDB" "$T/dev/disk/by-id/usb-SanDisk"
usb --iso "$ISO" --device "$T/dev/disk/by-id/usb-SanDisk" --yes
assert_rc 0
cmp -n 3145728 "$ISO" "$SDB" || _fail "wrote to wrong target"
}
test_e2e_refuses_system_disk_and_writes_nothing() {
mkfix
usb --iso "$ISO" --device "$SDA" --yes --allow-internal
assert_rc 4; assert_err_has "in-use system data"
! did "dd " && ! did wipefs && ! did umount || _fail "touched the system disk: $(ops)"
}
test_e2e_refuses_partition() {
mkfix; touch "${SDB}1"
usb --iso "$ISO" --device "${SDB}1" --yes
assert_rc 4
! did "dd " || _fail "wrote to a partition"
}
test_e2e_refuses_internal_without_flag() {
mkfix
usb --iso "$ISO" --device "$SDD" --yes
assert_rc 4; assert_err_has "--allow-internal"
usb --iso "$ISO" --device "$SDD" --yes --allow-internal
assert_rc 0
}
test_e2e_refuses_when_iso_is_on_target() {
mkfix
# A realistic desktop auto-mount: the stick is mounted under /run/media and
# the ISO sits on it. Wiping the stick would destroy the very file we are
# about to write.
local mnt="/run/media/mkusb-test-$$/NIXOS"
mkdir -p "$mnt" 2>/dev/null || { echo " (skipped: cannot create /run/media; needs root)"; return 0; }
cp "$ISO" "$mnt/nixos.iso"
printf '%s %s\n%s /\n' "${SDB}1" "$mnt" "${SDA}2" > "$FAKE_MOUNTS"
usb --iso "$mnt/nixos.iso" --device "$SDB" --yes
local rc=$RC err="$ERR"; rm -rf "/run/media/mkusb-test-$$"
RC=$rc ERR="$err"
assert_rc 4; assert_err_has "stored on"
! did "dd " && ! did wipefs && ! did umount || _fail "touched the disk holding the ISO: $(ops)"
}
test_e2e_iso_bigger_than_disk() {
mkfix
row "$T/dev/sdt" disk 1000 "Tiny" usb 1 -; touch "$T/dev/sdt"
usb --iso "$ISO" --device "$T/dev/sdt" --yes
assert_rc 4; assert_err_has "larger than the disk"
}
test_e2e_empty_or_missing_iso() {
mkfix
: > "$T/empty.iso"
usb --iso "$T/empty.iso" --device "$SDB" --yes; assert_rc 4; assert_err_has "empty"
usb --iso "$T/nope.iso" --device "$SDB" --yes; assert_rc 4; assert_err_has "not found"
! did "dd " || _fail "dd ran"
}
test_e2e_checksum_mismatch_blocks_write() {
mkfix
echo "$(printf '%064d' 0) nixos.iso" > "$T/iso/nixos.iso.sha256"
usb --iso "$ISO" --device "$SDB" --yes
assert_rc 5; assert_err_has "sha256 verification failed"
! did "dd " && ! did wipefs || _fail "wrote despite bad checksum"
}
test_e2e_checksum_sidecar_found_via_symlink() {
mkfix
ln -s "$ISO" "$T/iso/latest.iso"
echo "$(printf '%064d' 0) nixos.iso" > "$T/iso/nixos.iso.sha256"
usb --iso "$T/iso/latest.iso" --device "$SDB" --yes
assert_rc 5 # mismatch detected through the symlink
}
test_e2e_missing_sidecar_warns_but_proceeds() {
mkfix; rm "$T/iso/nixos.iso.sha256"
usb --iso "$ISO" --device "$SDB" --yes
assert_rc 0; assert_err_has "skipping checksum"
}
test_e2e_corrupted_write_detected() {
mkfix
FAKE_DD_CORRUPT=1 usb --iso "$ISO" --device "$SDB" --yes
assert_rc 6; assert_err_has "verification FAILED"
}
test_e2e_no_verify_flag_skips_readback() {
mkfix
FAKE_DD_CORRUPT=1 usb --iso "$ISO" --device "$SDB" --yes --no-verify
assert_rc 0; assert_err_has "skipping read-back"
}
test_e2e_dd_failure_reports_kernel_messages() {
mkfix
FAKE_DD_FAIL=1 FAKE_DMESG="[ 12.3] sdb: Medium Error, sector 42" usb --iso "$ISO" --device "$SDB" --yes
assert_rc 5; assert_err_has "dd failed"; assert_err_has "Medium Error"
assert_err_lacks "Read-back verified"
}
test_e2e_dd_failure_without_kernel_messages_hints_dmesg() {
mkfix
FAKE_DD_FAIL=1 usb --iso "$ISO" --device "$SDB" --yes
assert_rc 5; assert_err_has "dmesg"
}
test_e2e_unmount_failure_aborts_before_any_write() {
mkfix
FAKE_UMOUNT_FAIL="/run/media/alex/NIXOS" usb --iso "$ISO" --device "$SDB" --yes
assert_rc 4; assert_err_has "could not unmount"
! did "dd " && ! did wipefs || _fail "wrote to a busy device: $(ops)"
! did "umount -l" || _fail "used lazy unmount"
}
test_e2e_noninteractive_requires_all_args() {
mkfix
usb --iso "$ISO" --device "$SDB" # no --yes
assert_rc 2; assert_err_has "not a tty"
usb --device "$SDB" --yes
assert_rc 2
! did "dd " || _fail "dd ran"
}
test_e2e_argument_errors() {
mkfix
usb --bogus; assert_rc 2
usb --iso; assert_rc 2; assert_err_has "requires a value"
usb --device; assert_rc 2
usb -h; assert_rc 0; assert_out_has "Usage:"
}
test_e2e_equals_form() {
mkfix
usb --iso="$ISO" --device="$SDB" --yes
assert_rc 0
}
test_e2e_missing_tools() {
mkfix
local bin="$T/bin"; mkdir -p "$bin"
local c; for c in bash dirname readlink env cat sed grep tr awk stat dd sync blockdev umount wipefs findmnt; do ln -s "$(command -v "$c")" "$bin/$c" 2>/dev/null; done
# no lsblk
PATH="$bin" run bash "$DRIVER" --iso "$ISO" --device "$SDB" --yes
assert_rc 3; assert_err_has "lsblk"
}
# ---- interactive (pty) ------------------------------------------------------
test_pty_menu_marks_system_disk_and_rejects_bad_input() {
mkfix
# abc -> not a number; 99 -> out of range; 08 -> must not crash on octal; 1 (=sda, system) -> refused; 0 -> abort
pty 'abc\n99\n08\n1\n0\n' --iso "$ISO" --yes
assert_rc 1
[[ "$OUT" == *"not a number"* ]] || _fail "no 'not a number': $OUT"
[[ "$OUT" == *"out of range"* ]] || _fail "no 'out of range'"
[[ "$OUT" == *"value too great for base"* ]] && _fail "octal crash on '08'"
[[ "$OUT" == *"SYSTEM DISK"* ]] || _fail "system disk not flagged"
[[ "$OUT" == *"in-use system data"* ]] || _fail "selecting the system disk was not refused"
! did "dd " || _fail "dd ran"
}
test_pty_eof_in_menu_is_a_clean_error() {
mkfix
pty '' --iso "$ISO" --yes
assert_rc 1
}
test_pty_select_and_typed_confirmation() {
mkfix
printf '' > "$FAKE_MOUNTS" # nothing mounted: only the stick matters
# menu order: sda sdb sdc sdd -> 2 is the SanDisk
pty "2\n$SDB\n" --iso "$ISO"
assert_rc 0
cmp -n 3145728 "$ISO" "$SDB" || _fail "not written"
}
test_pty_wrong_confirmation_aborts_without_writing() {
mkfix
pty "2\n$SDC\n" --iso "$ISO"
assert_rc 1
[[ "$OUT" == *"confirmation didn't match"* ]] || _fail "$OUT"
! did "dd " && ! did wipefs && ! did umount || _fail "acted despite wrong confirmation"
}
test_pty_prompts_for_iso_and_expands_tilde() {
mkfix
printf '' > "$FAKE_MOUNTS"
export HOME="$T/home"; mkdir -p "$HOME"; cp "$ISO" "$HOME/my.iso"
pty "~/my.iso\n" --device "$SDB" --yes
assert_rc 0
cmp -n 3145728 "$ISO" "$SDB" || _fail "wrong ISO written"
}
run_all "${1:-}"
+623
View File
@@ -0,0 +1,623 @@
#!/usr/bin/env python3
"""Tests for nixflake.py, register-host.py and list-hosts.py. Run: python3 -m unittest -v"""
from __future__ import annotations
import importlib.util
import io
import os
import random
import re
import stat
import sys
import tempfile
import textwrap
import unittest
from contextlib import redirect_stderr, redirect_stdout
from pathlib import Path
from unittest import mock
SCRIPTS = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(SCRIPTS))
import nixflake # noqa: E402
def load(filename: str, modname: str):
spec = importlib.util.spec_from_file_location(modname, SCRIPTS / filename)
mod = importlib.util.module_from_spec(spec)
sys.modules[modname] = mod # dataclasses (with deferred annotations) look themselves up here
spec.loader.exec_module(mod)
return mod
reg = load("register-host.py", "register_host")
lh = load("list-hosts.py", "list_hosts")
BASIC = textwrap.dedent("""\
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = { self, nixpkgs, ... }@inputs:
let system = "x86_64-linux"; in {
nixosConfigurations = {
alpha = nixpkgs.lib.nixosSystem {
inherit system;
modules = [ ./hosts/alpha ];
};
beta = nixpkgs.lib.nixosSystem {
inherit system;
modules = [ ./hosts/beta ./hosts/beta-extras ];
};
};
};
}
""")
def names(src: str) -> list[str]:
return [h.name for h in nixflake.find_hosts(src)]
class MaskTests(unittest.TestCase):
def test_length_and_newlines_preserved(self):
src = 'a = "x\n{";\n# c {\n/* {\n} */ b = \'\'\n${y}\n\'\';\n'
m = nixflake.mask_non_code(src)
self.assertEqual(len(m), len(src))
self.assertEqual([i for i, c in enumerate(src) if c == "\n"], [i for i, c in enumerate(m) if c == "\n"])
def test_braces_in_non_code_ignored(self):
for src in (
'{ a = "}"; }',
"{ a = 1; # }\n}",
"{ a = 1; /* } */ }",
"{ a = ''}''; }",
"{ a = ''x'''}''; }",
'{ a = "\\"}"; }',
"{ a = ''x''${}''; }",
):
self.assertTrue(nixflake.braces_balanced(src), src)
m = nixflake.mask_non_code(src)
self.assertEqual(nixflake.find_matching_brace(m, 0), len(src) - 1, src)
def test_interpolation_is_code(self):
src = '{ a = "x ${ { b = 1; }.b } y"; c = "${"nested ${ "}" }"}"; }'
self.assertTrue(nixflake.braces_balanced(src))
m = nixflake.mask_non_code(src)
self.assertEqual(nixflake.find_matching_brace(m, 0), len(src) - 1)
def test_indented_string_escapes(self):
# ''$ and ''' and ''\n are escapes, not terminators.
src = "{ a = ''x ''${notinterp} ''' still ''\\n in ''; b = 1; }"
m = nixflake.mask_non_code(src)
self.assertEqual(nixflake.find_matching_brace(m, 0), len(src) - 1)
self.assertIn("b = 1", m)
def test_unterminated_constructs_do_not_crash(self):
for src in ('{ a = "oops', "{ a = ''oops", "{ /* oops", "{ # oops", '{ a = "${ x', "\\", "''", '"', "${"):
nixflake.mask_non_code(src) # must not raise
def test_fuzz_never_crashes_and_preserves_length(self):
rng = random.Random(1234)
alphabet = ['"', "'", "''", "$", "${", "}", "{", "#", "/*", "*/", "\\", "\n", " ", "a", "=", ";"]
for _ in range(3000):
src = "".join(rng.choice(alphabet) for _ in range(rng.randint(0, 40)))
m = nixflake.mask_non_code(src)
self.assertEqual(len(m), len(src), repr(src))
nixflake.braces_balanced(src)
class FindHostsTests(unittest.TestCase):
def test_basic(self):
self.assertEqual(names(BASIC), ["alpha", "beta"])
def test_ignores_commented_and_stringified_hosts(self):
src = textwrap.dedent("""\
{ outputs = { nixosConfigurations = {
# ghost = nixpkgs.lib.nixosSystem { };
/* ghost2 = nixpkgs.lib.nixosSystem { }; */
doc = "ghost3 = nixpkgs.lib.nixosSystem { };";
real = nixpkgs.lib.nixosSystem { };
}; }; }
""")
self.assertEqual(names(src), ["real"])
def test_brace_in_comment_does_not_end_block(self):
src = textwrap.dedent("""\
{ nixosConfigurations = {
a = nixpkgs.lib.nixosSystem { x = 1; }; # } closes nothing
b = nixpkgs.lib.nixosSystem { y = "}"; };
}; }
""")
self.assertEqual(names(src), ["a", "b"])
def test_nested_nixossystem_not_a_host(self):
src = textwrap.dedent("""\
{ nixosConfigurations = {
outer = nixpkgs.lib.nixosSystem {
modules = [ { inner = nixpkgs.lib.nixosSystem { }; } ];
other = let
deep = nixpkgs.lib.nixosSystem { };
in deep;
};
}; }
""")
self.assertEqual(names(src), ["outer"])
def test_function_path_variants(self):
src = textwrap.dedent("""\
{ nixosConfigurations = {
a = nixpkgs.lib.nixosSystem { };
b = inputs.nixpkgs.lib.nixosSystem { };
c = lib.nixosSystem { };
d = nixosSystem { };
e-f_g' = nixpkgs.lib.nixosSystem
{
};
not-a-host = somethingElse { };
}; }
""")
self.assertEqual(names(src), ["a", "b", "c", "d", "e-f_g'"])
def test_errors(self):
with self.assertRaises(nixflake.FlakeError):
names("{ outputs = { }; }")
with self.assertRaises(nixflake.FlakeError):
names("{ nixosConfigurations = { a = nixpkgs.lib.nixosSystem { };")
with self.assertRaises(nixflake.FlakeError): # entry missing its `;`
names("{ nixosConfigurations = { a = nixpkgs.lib.nixosSystem { } }; }")
def test_empty_block(self):
self.assertEqual(names("{ nixosConfigurations = { }; }"), [])
self.assertEqual(names("{ nixosConfigurations = {}; }"), [])
def test_crlf(self):
self.assertEqual(names(BASIC.replace("\n", "\r\n")), ["alpha", "beta"])
def test_dotted_prefix_still_found(self):
self.assertEqual(names("{ outputs.nixosConfigurations = { a = nixpkgs.lib.nixosSystem { }; }; }"), ["a"])
# but a lookalike attribute is not nixosConfigurations
with self.assertRaises(nixflake.FlakeError):
names("{ fakenixosConfigurations = { a = nixpkgs.lib.nixosSystem { }; }; }")
class PlanEditTests(unittest.TestCase):
def test_clone_renames_and_rewrites_paths(self):
e = reg.plan_flake_edit(BASIC, "gamma", "beta")
self.assertEqual(names(e.new_src), ["alpha", "beta", "gamma"])
self.assertIn("gamma = nixpkgs.lib.nixosSystem {", e.new_src)
self.assertIn("./hosts/gamma ./hosts/beta-extras", e.new_src.split("gamma = ")[1]) # -extras untouched
self.assertTrue(nixflake.braces_balanced(e.new_src))
self.assertEqual(e.template_used, "beta")
self.assertTrue(any("beta-extras" in ln for ln in e.stale_template_lines) is False)
def test_stale_mentions_reported(self):
src = BASIC.replace("modules = [ ./hosts/beta ", 'networking.hostName = "beta"; modules = [ ./hosts/beta ')
self.assertIn('hostName = "beta"', src)
e = reg.plan_flake_edit(src, "gamma", "beta")
self.assertTrue(any('"beta"' in ln for ln in e.stale_template_lines), e.stale_template_lines)
def test_default_entry_empty_blocks(self):
for src in (
"{ outputs = { nixosConfigurations = { }; }; }",
"{ outputs = { nixosConfigurations = {}; }; }",
"{\n nixosConfigurations = {\n };\n}\n",
"{\n nixosConfigurations = {\n };\n}",
):
e = reg.plan_flake_edit(src, "gamma", None)
self.assertEqual(names(e.new_src), ["gamma"], src)
self.assertTrue(nixflake.braces_balanced(e.new_src), src)
self.assertIn("./hosts/gamma", e.new_src)
def test_one_line_attrset_with_existing_host(self):
src = "{ nixosConfigurations = { a = nixpkgs.lib.nixosSystem { }; }; }"
e = reg.plan_flake_edit(src, "b", "a")
self.assertEqual(names(e.new_src), ["a", "b"])
self.assertTrue(nixflake.braces_balanced(e.new_src))
def test_crlf_is_preserved_everywhere(self):
src = BASIC.replace("\n", "\r\n")
for template in ("alpha", None):
e = reg.plan_flake_edit(src, "gamma", template)
self.assertNotRegex(e.new_src, r"(?<!\r)\n", f"bare LF introduced (template={template})")
self.assertEqual(names(e.new_src), ["alpha", "beta", "gamma"])
def test_tabs(self):
src = BASIC.replace(" ", "\t")
e = reg.plan_flake_edit(src, "gamma", "alpha")
self.assertEqual(names(e.new_src), ["alpha", "beta", "gamma"])
self.assertIn("\n\t\t\t\tgamma = ", e.new_src)
def test_duplicate_and_unknown_template(self):
with self.assertRaises(reg.RegisterError):
reg.plan_flake_edit(BASIC, "alpha", None)
with self.assertRaises(reg.RegisterError):
reg.plan_flake_edit(BASIC, "gamma", "nope")
def test_substring_name_is_not_a_duplicate(self):
# `alp` must not collide with `alpha`, nor `alpha-2`.
e = reg.plan_flake_edit(BASIC, "alp", "alpha")
self.assertEqual(names(e.new_src), ["alpha", "beta", "alp"])
def test_template_with_hostile_contents(self):
src = textwrap.dedent("""\
{ nixosConfigurations = {
a = nixpkgs.lib.nixosSystem {
# comment with } brace and a = nixpkgs.lib.nixosSystem {
modules = [ { x = "}${"{"}"; y = ''
} ${ "}" } ''${ ''' '';
} ];
};
}; }
""")
e = reg.plan_flake_edit(src, "b", "a")
self.assertEqual(names(e.new_src), ["a", "b"])
self.assertTrue(nixflake.braces_balanced(e.new_src))
def test_pure_insertion_invariant(self):
e = reg.plan_flake_edit(BASIC, "gamma", "alpha")
self.assertTrue(is_pure_insertion(BASIC, e.new_src))
def is_pure_insertion(old: str, new: str) -> bool:
"""True if `new` equals `old` with one contiguous chunk inserted."""
if len(new) < len(old):
return False
p = 0
while p < len(old) and old[p] == new[p]:
p += 1
s = 0
while s < len(old) - p and old[-1 - s] == new[-1 - s]:
s += 1
return p + s >= len(old)
def random_flake(rng: random.Random) -> tuple[str, list[str]]:
junk = [
'# } { comment\n', '/* } { */', '"}{"', "''}${\"{\"}{''", '"${ "}" }"', "x = \"a\\\"}\";",
"# x = nixpkgs.lib.nixosSystem { };\n",
]
shape = rng.choice([0, 1, 2])
if shape == 1: # newlines get collapsed below, so line comments would swallow code
junk = [j for j in junk if not j.startswith("#")]
nm = [f"h{i}" for i in range(rng.randint(0, 4))]
entries = []
for n in nm:
body = " ".join(rng.choice(junk) for _ in range(rng.randint(0, 3)))
entries.append(f" {n} = nixpkgs.lib.nixosSystem {{ modules = [ ./hosts/{n} ]; {body}\n }};\n")
sep = rng.choice(["\n", "\n\n", "\r\n"])
inner = sep.join(entries)
if shape == 0:
src = f"{{\n nixosConfigurations = {{\n{inner} }};\n}}\n"
elif shape == 1:
src = f"{{ nixosConfigurations = {{ {re.sub(chr(13) + '?' + chr(10), ' ', inner)} }}; }}"
else:
src = f"# {{\n{{ a = 1; outputs = {{ nixosConfigurations = {{\n{inner}}}; }}; }}"
return src, nm
class FuzzEditTests(unittest.TestCase):
def test_random_flakes(self):
rng = random.Random(42)
for i in range(800):
src, expected = random_flake(rng)
if nixflake.find_hosts(src) != [] or expected:
self.assertEqual(names(src), expected, repr(src))
template = rng.choice([None, *expected]) if expected else None
e = reg.plan_flake_edit(src, "newhost", template)
reg.verify_edit(src, e.new_src, "newhost")
ok = is_pure_insertion(src, e.new_src)
if not ok and "\n" not in src:
# One-line attrsets: the blank gap may be replaced by the new entry.
def squash(t):
return re.sub(r"\s+", "", t)
ok = is_pure_insertion(squash(src), squash(e.new_src))
self.assertTrue(ok, f"case {i}: {src!r} -> {e.new_src!r}")
class HostnameTests(unittest.TestCase):
def test_valid(self):
for n in ("a", "willow", "white-grizzly", "h1", "a" * 63):
reg.validate_hostname(n)
def test_invalid(self):
for n in ("", "-a", "a-", "1abc", "Willow", "a_b", "a.b", "../x", "a/b", "a b", "a" * 64, "in", "let", "rec", "é", "a\n", "--help"):
with self.assertRaises(reg.RegisterError, msg=repr(n)):
reg.validate_hostname(n)
class RenderTests(unittest.TestCase):
def test_braces_survive(self):
out = reg.render("@NAME@ = { a = [ \"@NAME@\" ]; };", name="x")
self.assertEqual(out, 'x = { a = [ "x" ]; };')
def test_leftover_token_is_a_bug(self):
with self.assertRaises(RuntimeError):
reg.render("@NAME@ @OTHER@", name="x")
def test_real_templates_render_and_are_brace_balanced(self):
# This is the exact regression for the reported KeyError.
for t in (reg.DEFAULT_NIX_TEMPLATE, reg.PLACEHOLDER_HARDWARE, reg.DEFAULT_FLAKE_ENTRY):
out = reg.render(t, name="willow", i=" ")
self.assertTrue(nixflake.braces_balanced(out))
self.assertNotRegex(out, r"@[A-Z_]+@")
self.assertIn('polkitPolicyOwners = [ "alex" ];', reg.render(reg.DEFAULT_NIX_TEMPLATE, name="willow"))
def test_hardware_placeholder_marker_matches_capture_script(self):
cap = (SCRIPTS / "capture-hardware.sh").read_text()
self.assertIn(reg.HARDWARE_PLACEHOLDER_MARKER, cap)
self.assertIn(reg.HARDWARE_PLACEHOLDER_MARKER, reg.render(reg.PLACEHOLDER_HARDWARE, name="x"))
class RepoCase(unittest.TestCase):
"""Runs register-host.main() against a throwaway repo."""
def setUp(self):
self._td = tempfile.TemporaryDirectory()
self.addCleanup(self._td.cleanup)
self.repo = Path(self._td.name)
(self.repo / "flake.nix").write_text(BASIC)
for m in ("core", "plasma", "security"):
(self.repo / "modules").mkdir(exist_ok=True)
(self.repo / "modules" / f"{m}.nix").write_text("{ }")
def run_reg(self, *argv: str) -> tuple[int, str]:
err = io.StringIO()
with redirect_stderr(err), redirect_stdout(io.StringIO()):
try:
code = reg.main([*argv, "--repo-root", str(self.repo)] if "--" not in argv else list(argv))
except SystemExit as e: # argparse
code = int(e.code or 0)
return code, err.getvalue()
@property
def flake(self) -> str:
return (self.repo / "flake.nix").read_text()
class RegisterEndToEnd(RepoCase):
def test_success(self):
code, err = self.run_reg("willow")
self.assertEqual(code, 0, err)
self.assertEqual(names(self.flake), ["alpha", "beta", "willow"])
d = self.repo / "hosts" / "willow"
self.assertIn('networking.hostName = "willow";', (d / "default.nix").read_text())
self.assertIn("PLACEHOLDER hardware.nix", (d / "hardware.nix").read_text())
self.assertNotIn("does not exist", err) # modules/ present in this fixture
def test_second_registration_of_same_host_refused_and_changes_nothing(self):
self.assertEqual(self.run_reg("willow")[0], 0)
before = self.flake
code, err = self.run_reg("willow")
self.assertEqual(code, 1)
self.assertEqual(self.flake, before)
def test_dry_run_writes_nothing(self):
before = self.flake
code, err = self.run_reg("willow", "--dry-run")
self.assertEqual(code, 0)
self.assertEqual(self.flake, before)
self.assertFalse((self.repo / "hosts").exists())
self.assertIn("dry-run", err)
def test_dry_run_still_validates(self):
code, err = self.run_reg("alpha", "--dry-run")
self.assertEqual(code, 1)
self.assertIn("already defined", err)
def test_invalid_names_rejected_without_touching_anything(self):
before = self.flake
for bad in ("../evil", "Willow", "1x", "a b", "if"):
code, err = self.run_reg(bad)
self.assertEqual(code, 1, bad)
self.assertEqual(self.flake, before)
self.assertFalse((self.repo / "hosts").exists())
self.assertFalse((self.repo.parent / "evil").exists())
def test_leading_dash_name_after_double_dash(self):
err = io.StringIO()
with redirect_stderr(err):
code = reg.main(["--repo-root", str(self.repo), "--", "-rf"])
self.assertEqual(code, 1)
self.assertIn("invalid hostname '-rf'", err.getvalue())
def test_reuses_empty_dir_but_refuses_nonempty(self):
(self.repo / "hosts" / "willow").mkdir(parents=True)
self.assertEqual(self.run_reg("willow")[0], 0)
(self.repo / "hosts" / "other").mkdir()
(self.repo / "hosts" / "other" / "keep.txt").write_text("precious")
before = self.flake
code, err = self.run_reg("other")
self.assertEqual(code, 1)
self.assertIn("not empty", err)
self.assertEqual(self.flake, before)
self.assertEqual((self.repo / "hosts" / "other" / "keep.txt").read_text(), "precious")
def test_hosts_path_is_a_file(self):
(self.repo / "hosts").mkdir()
(self.repo / "hosts" / "willow").write_text("i am a file")
self.assertEqual(self.run_reg("willow")[0], 1)
self.assertEqual((self.repo / "hosts" / "willow").read_text(), "i am a file")
def test_missing_flake(self):
(self.repo / "flake.nix").unlink()
code, err = self.run_reg("willow")
self.assertEqual(code, 1)
self.assertIn("flake.nix not found", err)
def test_flake_without_configurations(self):
(self.repo / "flake.nix").write_text("{ outputs = { }; }")
code, err = self.run_reg("willow")
self.assertEqual(code, 1)
self.assertIn("nixosConfigurations", err)
self.assertFalse((self.repo / "hosts").exists())
def test_invalid_utf8_flake(self):
(self.repo / "flake.nix").write_bytes(b"{ a = \xff\xfe; }")
code, err = self.run_reg("willow")
self.assertEqual(code, 1)
self.assertIn("UTF-8", err)
def test_template_options(self):
self.assertEqual(self.run_reg("g1", "--template", "beta")[0], 0)
self.assertEqual(self.run_reg("g2", "--no-template")[0], 0)
self.assertEqual(self.run_reg("g3", "--template", "nope")[0], 1)
self.assertEqual(self.run_reg("g4", "--template", "alpha", "--no-template")[0], 2)
self.assertEqual(names(self.flake), ["alpha", "beta", "g1", "g2"])
def test_crlf_flake_stays_crlf(self):
(self.repo / "flake.nix").write_bytes(BASIC.replace("\n", "\r\n").encode())
self.assertEqual(self.run_reg("willow")[0], 0)
raw = (self.repo / "flake.nix").read_bytes()
self.assertNotRegex(raw, rb"(?<!\r)\n")
def test_preserves_mode_and_symlink(self):
real = self.repo / "real-flake.nix"
(self.repo / "flake.nix").rename(real)
real.chmod(0o640)
(self.repo / "flake.nix").symlink_to(real)
self.assertEqual(self.run_reg("willow")[0], 0)
self.assertTrue((self.repo / "flake.nix").is_symlink(), "symlink was replaced by a regular file")
self.assertEqual(stat.S_IMODE(real.stat().st_mode), 0o640)
self.assertIn("willow", real.read_text())
def test_no_temp_files_left_behind(self):
self.run_reg("willow")
self.assertEqual([p.name for p in self.repo.iterdir() if p.name.endswith(".tmp")], [])
class RollbackTests(RepoCase):
def test_flake_write_failure_rolls_everything_back(self):
before = self.flake
with mock.patch.object(reg, "atomic_replace", side_effect=OSError(28, "No space left on device")):
code, err = self.run_reg("willow")
self.assertEqual(code, 1)
self.assertIn("No space left", err)
self.assertEqual(self.flake, before)
self.assertFalse((self.repo / "hosts" / "willow").exists(), "scaffold dir not cleaned up")
self.assertIn("rolled back", err)
def test_rollback_keeps_preexisting_empty_dir(self):
d = self.repo / "hosts" / "willow"
d.mkdir(parents=True)
with mock.patch.object(reg, "atomic_replace", side_effect=OSError("boom")):
self.assertEqual(self.run_reg("willow")[0], 1)
self.assertTrue(d.is_dir())
self.assertEqual(list(d.iterdir()), [])
def test_second_file_failure_removes_first(self):
real_create = reg.create_new_file
calls = {"n": 0}
def flaky(path, data):
calls["n"] += 1
if calls["n"] == 2:
raise OSError("disk on fire")
real_create(path, data)
before = self.flake
with mock.patch.object(reg, "create_new_file", side_effect=flaky):
self.assertEqual(self.run_reg("willow")[0], 1)
self.assertFalse((self.repo / "hosts" / "willow").exists())
self.assertEqual(self.flake, before)
def test_keyboard_interrupt_rolls_back_and_exits_130(self):
before = self.flake
with mock.patch.object(reg, "atomic_replace", side_effect=KeyboardInterrupt):
code, _ = self.run_reg("willow")
self.assertEqual(code, 130)
self.assertFalse((self.repo / "hosts" / "willow").exists())
self.assertEqual(self.flake, before)
def test_atomic_replace_cleans_its_temp_file_on_failure(self):
target = self.repo / "x.nix"
target.write_text("old")
with mock.patch.object(os, "replace", side_effect=OSError("nope")):
with self.assertRaises(OSError):
reg.atomic_replace(target, "new")
self.assertEqual(target.read_text(), "old")
self.assertEqual([p.name for p in self.repo.glob(".x.nix.*")], [])
class NixParseCheckTests(RepoCase):
def _fake_nix(self, exit_code: int, msg: str) -> str:
bindir = self.repo / "fakebin"
bindir.mkdir(exist_ok=True)
exe = bindir / "nix-instantiate"
exe.write_text(f"#!/bin/sh\necho '{msg}' >&2\nexit {exit_code}\n")
exe.chmod(0o755)
return str(bindir)
def test_parse_failure_blocks_all_writes(self):
before = self.flake
with mock.patch.dict(os.environ, {"PATH": self._fake_nix(1, "error: syntax error, unexpected '}'") + os.pathsep + os.environ["PATH"]}):
code, err = self.run_reg("willow")
self.assertEqual(code, 1)
self.assertIn("would not parse", err)
self.assertIn("syntax error", err)
self.assertEqual(self.flake, before)
self.assertFalse((self.repo / "hosts").exists())
def test_parse_success_allows_write(self):
with mock.patch.dict(os.environ, {"PATH": self._fake_nix(0, "") + os.pathsep + os.environ["PATH"]}):
self.assertEqual(self.run_reg("willow")[0], 0)
def test_absent_nix_is_fine(self):
with mock.patch.dict(os.environ, {"PATH": "/nonexistent"}):
reg.nix_parse_check("{ }") # must not raise
class ListHostsTests(RepoCase):
def test_lists_in_order(self):
out = io.StringIO()
with redirect_stdout(out):
self.assertEqual(lh.main([str(self.repo / "flake.nix")]), 0)
self.assertEqual(out.getvalue().split(), ["alpha", "beta"])
def test_errors(self):
with redirect_stderr(io.StringIO()) as err:
self.assertEqual(lh.main([str(self.repo / "nope.nix")]), 1)
(self.repo / "bad.nix").write_text("{ }")
self.assertEqual(lh.main([str(self.repo / "bad.nix")]), 1)
(self.repo / "bin.nix").write_bytes(b"\xff\xff")
self.assertEqual(lh.main([str(self.repo / "bin.nix")]), 1)
self.assertIn("nixosConfigurations", err.getvalue())
def test_empty_block_warns_but_succeeds(self):
(self.repo / "e.nix").write_text("{ nixosConfigurations = { }; }")
with redirect_stderr(io.StringIO()) as err, redirect_stdout(io.StringIO()) as out:
self.assertEqual(lh.main([str(self.repo / "e.nix")]), 0)
self.assertEqual(out.getvalue(), "")
self.assertIn("no hosts", err.getvalue())
class CliSubprocess(RepoCase):
"""Exercise the real executables the way `just` does."""
def test_executable_bits_and_shebangs(self):
for f in ("register-host.py", "list-hosts.py"):
p = SCRIPTS / f
self.assertTrue(os.access(p, os.X_OK), f)
self.assertEqual(p.read_text().splitlines()[0], "#!/usr/bin/env python3")
def test_run_from_another_cwd_via_symlink(self):
import subprocess
link = self.repo / "linked-list-hosts"
link.symlink_to(SCRIPTS / "list-hosts.py")
r = subprocess.run([str(link), str(self.repo / "flake.nix")], capture_output=True, text=True, cwd="/")
self.assertEqual(r.returncode, 0, r.stderr)
self.assertEqual(r.stdout.split(), ["alpha", "beta"])
def test_register_via_subprocess_reproduces_just_invocation(self):
import subprocess
r = subprocess.run(
[str(SCRIPTS / "register-host.py"), "--repo-root", str(self.repo), "--", "willow"],
capture_output=True, text=True,
)
self.assertEqual(r.returncode, 0, r.stderr)
self.assertNotIn("Traceback", r.stderr)
if __name__ == "__main__":
unittest.main(verbosity=2)
+9
View File
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
# Test driver: load make-usb.sh as a library (its main is guarded), swap the
# two functions that need real privileges / a real /dev node, then run main
# exactly as a user would.
# shellcheck source=../make-usb.sh
source "${SCRIPTS:?}/make-usb.sh"
is_block_device() { [[ -e "$1" ]]; }
ensure_root() { :; } # tests never need (or want) a real sudo
main "$@"