Text only. pkgs/default.nix named overlays/additions.nix, which is now modules/flake/overlays.nix. The closing hint of install-system.sh named my.boot.secureBoot.enable in hosts/$HOST/default.nix; it now says to swap the secure-boot aspect for systemd-boot in modules/hosts/$HOST/host.nix. The script's config.my reads are unchanged (install-facts.nix still provides them). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JZ16yf4acmaSiHq81vyQ6Z
776 lines
25 KiB
Bash
776 lines
25 KiB
Bash
#!/usr/bin/env bash
|
|
# install-system: install this flake onto the machine, from the NixOS installer.
|
|
#
|
|
# Flow: locate flake -> pick disks -> confirm -> disko -> hardware config ->
|
|
# host SSH key (+ secrets check) -> Secure Boot keys -> nixos-install ->
|
|
# password -> copy the repo into the new home directory.
|
|
set -euo pipefail
|
|
|
|
HOST=desktop
|
|
FLAKE_ARG=
|
|
KEYS_ARG=
|
|
DISKO_MODE='destroy,format,mount'
|
|
ASSUME_YES=0
|
|
FORCE_SELECT=0
|
|
MNT=/mnt
|
|
WORKCOPY=/root/nixos-config
|
|
|
|
if [ -t 1 ]; then
|
|
RED=$'\033[1;31m'
|
|
GRN=$'\033[1;32m'
|
|
YEL=$'\033[1;33m'
|
|
BLU=$'\033[1;34m'
|
|
RST=$'\033[0m'
|
|
else
|
|
RED=
|
|
GRN=
|
|
YEL=
|
|
BLU=
|
|
RST=
|
|
fi
|
|
|
|
info() { printf '%s==>%s %s\n' "$BLU" "$RST" "$*"; }
|
|
ok() { printf '%s ok%s %s\n' "$GRN" "$RST" "$*"; }
|
|
warn() { printf '%swarning:%s %s\n' "$YEL" "$RST" "$*" >&2; }
|
|
die() {
|
|
printf '%serror:%s %s\n' "$RED" "$RST" "$*" >&2
|
|
exit 1
|
|
}
|
|
|
|
usage() {
|
|
cat <<'EOF'
|
|
Usage: install-system [options]
|
|
|
|
Partitions the disks, installs NixOS from this flake and prepares secrets and
|
|
Secure Boot keys. Run it as root from the NixOS installer (UEFI mode).
|
|
|
|
Options:
|
|
--host NAME flake host to install (default: desktop)
|
|
--flake DIR flake directory (default: current directory if it has a
|
|
flake.nix, else /etc/nixos-config on the installer ISO)
|
|
--keys-dir DIR directory with <host>/ssh_host_ed25519_key and
|
|
<host>/sbctl (default: <flake>/keys, then mounted sticks)
|
|
--disko-mode MODE destroy,format,mount (default, WIPES the disks),
|
|
format,mount, or mount (nothing is wiped: re-runs/repairs)
|
|
--select-disks choose the disks again even if disk-ids.json is valid
|
|
-y, --yes do not ask for the typed confirmation
|
|
-h, --help show this help
|
|
EOF
|
|
}
|
|
|
|
need_value() {
|
|
[ "$2" -ge 2 ] || die "option $1 needs a value (see --help)"
|
|
}
|
|
|
|
while [ "$#" -gt 0 ]; do
|
|
case "$1" in
|
|
--host)
|
|
need_value "$1" "$#"
|
|
HOST=$2
|
|
shift 2
|
|
;;
|
|
--flake)
|
|
need_value "$1" "$#"
|
|
FLAKE_ARG=$2
|
|
shift 2
|
|
;;
|
|
--keys-dir)
|
|
need_value "$1" "$#"
|
|
KEYS_ARG=$2
|
|
shift 2
|
|
;;
|
|
--disko-mode)
|
|
need_value "$1" "$#"
|
|
DISKO_MODE=$2
|
|
shift 2
|
|
;;
|
|
--select-disks)
|
|
FORCE_SELECT=1
|
|
shift
|
|
;;
|
|
-y | --yes)
|
|
ASSUME_YES=1
|
|
shift
|
|
;;
|
|
-h | --help)
|
|
usage
|
|
exit 0
|
|
;;
|
|
*) die "unknown argument: $1 (see --help)" ;;
|
|
esac
|
|
done
|
|
|
|
case "$HOST" in
|
|
'' | *[!A-Za-z0-9_-]*) die "invalid host name: '$HOST'" ;;
|
|
esac
|
|
case "$DISKO_MODE" in
|
|
destroy,format,mount | format,mount | mount) ;;
|
|
*) die "--disko-mode must be destroy,format,mount, format,mount or mount" ;;
|
|
esac
|
|
|
|
# Everything that nix runs needs flakes; the flake's own nixConfig (binary
|
|
# caches) is accepted because the flake is our own.
|
|
NIX=(nix --extra-experimental-features 'nix-command flakes')
|
|
export NIX_CONFIG="${NIX_CONFIG:+$NIX_CONFIG$'\n'}experimental-features = nix-command flakes"$'\n'"accept-flake-config = true"
|
|
|
|
umask 022
|
|
WORK=$(mktemp -d)
|
|
trap 'rm -rf "$WORK"' EXIT
|
|
|
|
FLAKE=
|
|
FLAKE_SRC=
|
|
KEYS_DIR=
|
|
KEYS_WRITABLE=0
|
|
IDS_FILE=
|
|
SYS_DISK=
|
|
DATA_DISK=
|
|
USER_NAME=
|
|
USER_UID=
|
|
SB_ENABLE=false
|
|
SECRETS_ENABLE=false
|
|
HOST_PUB=
|
|
NEW_HOST_KEY=0
|
|
DESTRUCTIVE=0
|
|
case "$DISKO_MODE" in
|
|
mount) ;;
|
|
*) DESTRUCTIVE=1 ;;
|
|
esac
|
|
|
|
is_writable_dir() {
|
|
local t
|
|
t=$(mktemp -p "$1" .write-test.XXXXXX 2>/dev/null) || return 1
|
|
rm -f "$t"
|
|
}
|
|
|
|
git_() { git -C "$FLAKE" "$@"; }
|
|
|
|
# Flakes only see files that git tracks, so new files must be added.
|
|
git_add() {
|
|
git_ add -- "$@" || warn "git add $* failed: the flake will not see these files"
|
|
}
|
|
|
|
preflight() {
|
|
[ "$(id -u)" -eq 0 ] || die "run as root: sudo install-system"
|
|
[ -d /sys/firmware/efi ] ||
|
|
die "not booted in UEFI mode (/sys/firmware/efi is missing). Boot the USB stick in UEFI mode: Secure Boot and systemd-boot need it."
|
|
local c
|
|
for c in nix nixos-install nixos-generate-config nixos-enter disko git jq age ssh-keygen lsblk findmnt; do
|
|
command -v "$c" >/dev/null || die "missing command '$c': run this from the NixOS installer"
|
|
done
|
|
}
|
|
|
|
# FLAKE_SRC is where the flake came from, FLAKE the writable git checkout used
|
|
# for the install. They differ when the source is read-only (installer ISO),
|
|
# not a git repository, or owned by another user (git would refuse it).
|
|
locate_flake() {
|
|
local cand top
|
|
if [ -n "$FLAKE_ARG" ]; then
|
|
cand=$FLAKE_ARG
|
|
elif [ -f "$PWD/flake.nix" ]; then
|
|
cand=$PWD
|
|
elif [ -f /etc/nixos-config/flake.nix ]; then
|
|
cand=/etc/nixos-config
|
|
else
|
|
die "no flake found: run from the repository, or pass --flake DIR"
|
|
fi
|
|
[ -f "$cand/flake.nix" ] || die "no flake.nix in $cand"
|
|
FLAKE_SRC=$(readlink -f "$cand")
|
|
|
|
local reason=
|
|
if ! is_writable_dir "$FLAKE_SRC"; then
|
|
reason="it is read-only"
|
|
elif ! top=$(git -C "$FLAKE_SRC" rev-parse --show-toplevel 2>/dev/null) ||
|
|
[ "$(readlink -f "$top")" != "$FLAKE_SRC" ]; then
|
|
reason="it is not a usable git repository root"
|
|
fi
|
|
|
|
if [ -z "$reason" ]; then
|
|
FLAKE=$FLAKE_SRC
|
|
if [ -n "$(git_ ls-files -- keys)" ]; then
|
|
die "keys/ is tracked by git: private keys would be copied to the Nix store. Run: git rm -r --cached keys"
|
|
fi
|
|
ok "flake: $FLAKE"
|
|
return
|
|
fi
|
|
|
|
info "flake source $FLAKE_SRC cannot be used in place ($reason); copying to $WORKCOPY"
|
|
if [ -e "$WORKCOPY" ]; then
|
|
rm -rf "${WORKCOPY:?}.old"
|
|
mv "$WORKCOPY" "$WORKCOPY.old"
|
|
warn "previous working copy moved to $WORKCOPY.old"
|
|
fi
|
|
mkdir -p "$WORKCOPY"
|
|
# keys/ must never end up in a path: flake (it would be copied into the
|
|
# world-readable Nix store), so it is not copied; KEYS_DIR points at the source.
|
|
tar -C "$FLAKE_SRC" --exclude=./keys --exclude=./result --exclude='./result-*' -cf - . |
|
|
tar -C "$WORKCOPY" --no-same-owner -xf -
|
|
chown -R root:root "$WORKCOPY"
|
|
chmod -R u+rwX "$WORKCOPY"
|
|
FLAKE=$WORKCOPY
|
|
if ! git -C "$FLAKE" rev-parse --show-toplevel >/dev/null 2>&1; then
|
|
rm -rf "$FLAKE/.git"
|
|
git -C "$FLAKE" init -q -b main
|
|
git -C "$FLAKE" add -A
|
|
git -C "$FLAKE" -c user.name=install-system -c user.email=install-system@localhost \
|
|
commit -q -m "Import flake for installation"
|
|
fi
|
|
ok "flake: $FLAKE (copy of $FLAKE_SRC)"
|
|
}
|
|
|
|
# Best effort: keep generated files on the stick so a re-run starts from them.
|
|
sync_back() {
|
|
[ "$FLAKE" != "$FLAKE_SRC" ] || return 0
|
|
is_writable_dir "$FLAKE_SRC" || return 0
|
|
local rel
|
|
for rel in "$@"; do
|
|
if [ -f "$FLAKE/$rel" ]; then
|
|
install -D -m 0644 "$FLAKE/$rel" "$FLAKE_SRC/$rel" 2>/dev/null ||
|
|
warn "could not save $rel back to $FLAKE_SRC"
|
|
fi
|
|
done
|
|
}
|
|
|
|
ensure_lock() {
|
|
if [ -f "$FLAKE/flake.lock" ]; then
|
|
# `nix run .#install` may have just created it: make sure git tracks it.
|
|
git_add flake.lock
|
|
return
|
|
fi
|
|
info "no flake.lock yet: locking all flake inputs (needs network, takes a minute)"
|
|
"${NIX[@]}" flake lock "$FLAKE" || die "could not lock the flake inputs (network?)"
|
|
git_add flake.lock
|
|
sync_back flake.lock
|
|
}
|
|
|
|
find_keys_dir() {
|
|
if [ -n "$KEYS_ARG" ]; then
|
|
KEYS_DIR=$(readlink -f "$KEYS_ARG")
|
|
else
|
|
KEYS_DIR="$FLAKE_SRC/keys"
|
|
if [ ! -d "$KEYS_DIR/$HOST" ]; then
|
|
local d
|
|
for d in /run/media/*/*/keys /run/media/*/keys /media/*/keys /media/*/*/keys; do
|
|
if [ -d "$d/$HOST" ]; then
|
|
KEYS_DIR=$d
|
|
break
|
|
fi
|
|
done
|
|
fi
|
|
fi
|
|
if mkdir -p "$KEYS_DIR" 2>/dev/null && is_writable_dir "$KEYS_DIR"; then
|
|
KEYS_WRITABLE=1
|
|
fi
|
|
if [ -f "$KEYS_DIR/$HOST/ssh_host_ed25519_key" ]; then
|
|
ok "keys: $KEYS_DIR/$HOST"
|
|
elif [ "$KEYS_WRITABLE" -eq 1 ]; then
|
|
info "no keys for $HOST in $KEYS_DIR yet: they will be generated and saved there"
|
|
else
|
|
warn "no keys for $HOST found and $KEYS_DIR is not writable: new keys will exist only on the target"
|
|
fi
|
|
}
|
|
|
|
# ---- disks -----------------------------------------------------------------
|
|
|
|
disk_ok() {
|
|
case "$1" in
|
|
'' | *CHANGE-ME*) return 1 ;;
|
|
esac
|
|
[ -e "$1" ] && [ -b "$(readlink -f "$1")" ]
|
|
}
|
|
|
|
disk_desc() {
|
|
local real
|
|
real=$(readlink -f "$1")
|
|
printf '%s (%s)' "$real" "$(lsblk -dno SIZE,MODEL "$real" 2>/dev/null | tr -s ' ')"
|
|
}
|
|
|
|
# Mount points on a disk and everything below it (partitions, mappers).
|
|
disk_mounts() {
|
|
lsblk -J -o NAME,MOUNTPOINTS "$1" 2>/dev/null |
|
|
jq -r '[.blockdevices[] | recurse(.children[]?) | .mountpoints[]? | select(. != null)] | unique | .[]'
|
|
}
|
|
|
|
# Anything mounted outside /mnt means: live USB, or the running system.
|
|
disk_busy_mounts() {
|
|
local m
|
|
while IFS= read -r m; do
|
|
case "$m" in
|
|
'' | '[SWAP]' | "$MNT" | "$MNT"/*) ;;
|
|
*) printf '%s ' "$m" ;;
|
|
esac
|
|
done < <(disk_mounts "$1")
|
|
}
|
|
|
|
# Best stable name for /dev/<name>: shortest by-id link that is not wwn/eui/partition.
|
|
by_id_for() {
|
|
local name=$1 best='' p base
|
|
for p in /dev/disk/by-id/*; do
|
|
[ -e "$p" ] || continue
|
|
[ "$(readlink -f "$p")" = "/dev/$name" ] || continue
|
|
base=${p##*/}
|
|
case "$base" in
|
|
wwn-* | *eui.* | *-part*) continue ;;
|
|
esac
|
|
if [ -z "$best" ] || [ "${#base}" -lt "${#best}" ]; then
|
|
best=$base
|
|
fi
|
|
done
|
|
if [ -n "$best" ]; then
|
|
printf '/dev/disk/by-id/%s' "$best"
|
|
else
|
|
printf '/dev/%s' "$name"
|
|
fi
|
|
}
|
|
|
|
CAND_DEV=()
|
|
CAND_SIZE=()
|
|
CAND_MODEL=()
|
|
CAND_TRAN=()
|
|
CAND_ID=()
|
|
|
|
list_candidates() {
|
|
local name size model tran busy
|
|
CAND_DEV=()
|
|
CAND_SIZE=()
|
|
CAND_MODEL=()
|
|
CAND_TRAN=()
|
|
CAND_ID=()
|
|
while IFS=$'\t' read -r name size model tran; do
|
|
busy=$(disk_busy_mounts "/dev/$name")
|
|
if [ -n "$busy" ]; then
|
|
printf ' skipping %-10s %-8s in use (mounted at: %s)\n' "$name" "$size" "$busy"
|
|
continue
|
|
fi
|
|
CAND_DEV+=("$name")
|
|
CAND_SIZE+=("$size")
|
|
CAND_MODEL+=("$model")
|
|
CAND_TRAN+=("$tran")
|
|
CAND_ID+=("$(by_id_for "$name")")
|
|
done < <(lsblk -d -J -o NAME,SIZE,MODEL,TRAN,TYPE |
|
|
jq -r '.blockdevices[]
|
|
| select(.type == "disk")
|
|
| select(.name | test("^(zram|ram|loop|sr|fd)") | not)
|
|
| [.name, .size, ((.model // "-") | gsub("^\\s+|\\s+$"; "") | if . == "" then "-" else . end), (.tran // "-")]
|
|
| @tsv')
|
|
}
|
|
|
|
pick_disk() {
|
|
local label=$1 exclude=$2 n
|
|
while true; do
|
|
read -r -p "$label [1-${#CAND_DEV[@]}]: " n || die "no input (is this a terminal?)"
|
|
case "$n" in
|
|
'' | *[!0-9]*)
|
|
echo "enter a number from the list"
|
|
continue
|
|
;;
|
|
esac
|
|
if [ "$n" -lt 1 ] || [ "$n" -gt "${#CAND_DEV[@]}" ]; then
|
|
echo "enter a number from the list"
|
|
continue
|
|
fi
|
|
if [ "$((n - 1))" -eq "$exclude" ]; then
|
|
echo "that disk is already used for the other role"
|
|
continue
|
|
fi
|
|
PICKED=$((n - 1))
|
|
return
|
|
done
|
|
}
|
|
|
|
prompt_for_disks() {
|
|
info "available disks (the live USB stick and mounted media are skipped):"
|
|
list_candidates
|
|
[ "${#CAND_DEV[@]}" -ge 2 ] || die "need at least two unused disks (system NVMe + data SSD), found ${#CAND_DEV[@]}"
|
|
local i
|
|
printf '\n %-3s %-12s %-9s %-6s %-30s %s\n' '#' DEVICE SIZE TRAN MODEL 'STABLE NAME'
|
|
for i in "${!CAND_DEV[@]}"; do
|
|
printf ' %-3s %-12s %-9s %-6s %-30s %s\n' "$((i + 1))" "${CAND_DEV[$i]}" "${CAND_SIZE[$i]}" \
|
|
"${CAND_TRAN[$i]}" "${CAND_MODEL[$i]}" "${CAND_ID[$i]}"
|
|
done
|
|
printf '\n'
|
|
PICKED=-1
|
|
pick_disk "SYSTEM disk (NVMe: /, /boot, /nix, swap)" -1
|
|
local sys=$PICKED
|
|
pick_disk "DATA disk (SATA SSD: Games, Downloads, ...)" "$sys"
|
|
SYS_DISK=${CAND_ID[$sys]}
|
|
DATA_DISK=${CAND_ID[$PICKED]}
|
|
for i in "$SYS_DISK" "$DATA_DISK"; do
|
|
case "$i" in
|
|
/dev/disk/by-id/*) ;;
|
|
*) warn "$i has no stable /dev/disk/by-id name: the device name may change between boots" ;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
select_disks() {
|
|
IDS_FILE="$FLAKE/hosts/$HOST/disk-ids.json"
|
|
[ -d "$FLAKE/hosts/$HOST" ] || die "unknown host '$HOST': $FLAKE/hosts/$HOST does not exist"
|
|
if [ -f "$IDS_FILE" ]; then
|
|
SYS_DISK=$(jq -r '.system // ""' "$IDS_FILE" 2>/dev/null) || SYS_DISK=
|
|
DATA_DISK=$(jq -r '.data // ""' "$IDS_FILE" 2>/dev/null) || DATA_DISK=
|
|
fi
|
|
if [ "$FORCE_SELECT" -eq 0 ] && disk_ok "$SYS_DISK" && disk_ok "$DATA_DISK" &&
|
|
[ "$(readlink -f "$SYS_DISK")" != "$(readlink -f "$DATA_DISK")" ]; then
|
|
ok "disks from hosts/$HOST/disk-ids.json"
|
|
return
|
|
fi
|
|
if [ "$FORCE_SELECT" -eq 0 ]; then
|
|
info "hosts/$HOST/disk-ids.json holds placeholders or devices that do not exist here"
|
|
fi
|
|
prompt_for_disks
|
|
jq -n --arg system "$SYS_DISK" --arg data "$DATA_DISK" '{system: $system, data: $data}' >"$WORK/ids.json"
|
|
install -m 0644 "$WORK/ids.json" "$IDS_FILE"
|
|
git_add "hosts/$HOST/disk-ids.json"
|
|
sync_back "hosts/$HOST/disk-ids.json"
|
|
ok "wrote hosts/$HOST/disk-ids.json"
|
|
}
|
|
|
|
# ---- configuration values ----------------------------------------------------
|
|
|
|
eval_config() {
|
|
info "evaluating the configuration of $HOST (the first run downloads inputs)"
|
|
local json
|
|
json=$("${NIX[@]}" eval --json "$FLAKE#nixosConfigurations.$HOST.config.my" --apply '
|
|
my: {
|
|
user = my.user.name;
|
|
uid = my.user.uid;
|
|
secureBoot = my.boot.secureBoot.enable;
|
|
secrets = my.secrets.enable;
|
|
}') || die "could not evaluate nixosConfigurations.$HOST (run: nix eval --show-trace $FLAKE#nixosConfigurations.$HOST.config.my.user.name)"
|
|
USER_NAME=$(jq -r '.user' <<<"$json")
|
|
USER_UID=$(jq -r '.uid' <<<"$json")
|
|
SB_ENABLE=$(jq -r '.secureBoot' <<<"$json")
|
|
SECRETS_ENABLE=$(jq -r '.secrets' <<<"$json")
|
|
case "$USER_NAME" in
|
|
'' | null | *[!A-Za-z0-9_.-]*) die "unexpected user name from the configuration: '$USER_NAME'" ;;
|
|
esac
|
|
case "$USER_UID" in
|
|
'' | *[!0-9]*) die "unexpected uid from the configuration: '$USER_UID'" ;;
|
|
esac
|
|
ok "user $USER_NAME (uid $USER_UID), Secure Boot: $SB_ENABLE, secrets: $SECRETS_ENABLE"
|
|
}
|
|
|
|
# ---- confirmation ---------------------------------------------------------------
|
|
|
|
# Refuse to touch a disk that holds the live system, this flake or the keys.
|
|
check_disks_not_live() {
|
|
local d busy path mp
|
|
for d in "$SYS_DISK" "$DATA_DISK"; do
|
|
busy=$(disk_busy_mounts "$(readlink -f "$d")")
|
|
if [ -n "$busy" ]; then
|
|
die "$d has mounted filesystems outside $MNT: $busy
|
|
It looks like the live USB stick or the running system. Pick other disks (--select-disks)."
|
|
fi
|
|
done
|
|
for path in "$FLAKE_SRC" "$KEYS_DIR"; do
|
|
mp=$(findmnt -n -o TARGET -T "$path" 2>/dev/null || true)
|
|
for d in "$SYS_DISK" "$DATA_DISK"; do
|
|
if [ -n "$mp" ] && disk_mounts "$(readlink -f "$d")" | grep -qxF "$mp"; then
|
|
die "$path lives on $d, which would be wiped"
|
|
fi
|
|
done
|
|
done
|
|
}
|
|
|
|
confirm() {
|
|
if [ "$DESTRUCTIVE" -eq 0 ]; then
|
|
info "mode 'mount': existing partitions are mounted at $MNT, nothing is wiped"
|
|
return
|
|
fi
|
|
check_disks_not_live
|
|
printf '\n%s' "$RED"
|
|
cat <<EOF
|
|
############################################################
|
|
# DESTRUCTIVE: EVERYTHING ON THESE DISKS WILL BE ERASED #
|
|
############################################################
|
|
EOF
|
|
printf '%s\n' "$RST"
|
|
printf ' system disk: %s\n' "$(disk_desc "$SYS_DISK")"
|
|
printf ' %s\n' "$SYS_DISK"
|
|
printf ' data disk: %s\n' "$(disk_desc "$DATA_DISK")"
|
|
printf ' %s\n' "$DATA_DISK"
|
|
printf ' disko mode: %s\n\n' "$DISKO_MODE"
|
|
if [ "$ASSUME_YES" -eq 1 ]; then
|
|
warn "--yes given: not asking for confirmation"
|
|
return
|
|
fi
|
|
local answer
|
|
read -r -p "Type the host name ($HOST) to erase these disks and install: " answer || die "no input"
|
|
[ "$answer" = "$HOST" ] || die "confirmation did not match; nothing was changed"
|
|
}
|
|
|
|
# ---- install steps ---------------------------------------------------------------------
|
|
|
|
run_disko() {
|
|
info "disko ($DISKO_MODE)"
|
|
local args=(--mode "$DISKO_MODE" --flake "$FLAKE#$HOST")
|
|
if [ "$DESTRUCTIVE" -eq 1 ]; then
|
|
# Our typed confirmation replaces disko's own prompt.
|
|
args+=(--yes-wipe-all-disks)
|
|
fi
|
|
disko "${args[@]}"
|
|
mountpoint -q "$MNT" || die "disko finished but nothing is mounted at $MNT"
|
|
}
|
|
|
|
gen_hardware_config() {
|
|
info "hardware configuration"
|
|
local rel="hosts/$HOST/hardware-configuration.nix"
|
|
nixos-generate-config --root "$MNT" --no-filesystems --show-hardware-config >"$WORK/hw.nix"
|
|
grep -q 'boot.initrd.availableKernelModules' "$WORK/hw.nix" ||
|
|
die "nixos-generate-config produced no usable hardware configuration"
|
|
install -m 0644 "$WORK/hw.nix" "$FLAKE/$rel"
|
|
git_add "$rel"
|
|
sync_back "$rel"
|
|
ok "wrote $rel"
|
|
}
|
|
|
|
install_host_key() {
|
|
info "host SSH key (agenix identity)"
|
|
local src="$KEYS_DIR/$HOST/ssh_host_ed25519_key"
|
|
local dst="$MNT/etc/ssh/ssh_host_ed25519_key"
|
|
install -d -m 0755 -o root -g root "$MNT/etc/ssh"
|
|
if [ -f "$src" ]; then
|
|
install -m 0600 -o root -g root "$src" "$dst"
|
|
if [ -f "$src.pub" ]; then
|
|
install -m 0644 -o root -g root "$src.pub" "$dst.pub"
|
|
else
|
|
ssh-keygen -y -f "$dst" >"$WORK/hostkey.pub"
|
|
install -m 0644 -o root -g root "$WORK/hostkey.pub" "$dst.pub"
|
|
fi
|
|
ok "installed host key from $src"
|
|
elif [ -f "$dst" ]; then
|
|
ok "reusing host key already on the target ($dst)"
|
|
[ -f "$dst.pub" ] || ssh-keygen -y -f "$dst" >"$dst.pub"
|
|
else
|
|
ssh-keygen -q -t ed25519 -N '' -C "root@$HOST" -f "$dst"
|
|
NEW_HOST_KEY=1
|
|
warn "generated a NEW host key; secrets must be encrypted for it (see below if the check fails)"
|
|
fi
|
|
chown root:root "$dst" "$dst.pub"
|
|
chmod 0600 "$dst"
|
|
chmod 0644 "$dst.pub"
|
|
HOST_PUB=$(cat "$dst.pub")
|
|
|
|
if [ ! -f "$src" ]; then
|
|
if [ "$KEYS_WRITABLE" -eq 1 ]; then
|
|
install -d -m 0700 "$KEYS_DIR/$HOST"
|
|
install -m 0600 "$dst" "$src"
|
|
install -m 0644 "$dst.pub" "$src.pub"
|
|
ok "saved a copy to $KEYS_DIR/$HOST (keep it: re-installs reuse it)"
|
|
else
|
|
warn "$KEYS_DIR is not writable: back up $dst yourself, or secrets will not decrypt after a re-install"
|
|
fi
|
|
fi
|
|
printf ' host public key: %s\n' "$HOST_PUB"
|
|
}
|
|
|
|
verify_secrets() {
|
|
if [ "$SECRETS_ENABLE" != true ]; then
|
|
info "my.secrets.enable is off: skipping the secrets check"
|
|
return
|
|
fi
|
|
local sdir="$FLAKE/secrets" hostkey="$MNT/etc/ssh/ssh_host_ed25519_key"
|
|
local files=() f
|
|
if [ -d "$sdir" ]; then
|
|
mapfile -t files < <(find "$sdir" -maxdepth 1 -type f -name '*.age' | sort)
|
|
fi
|
|
if [ "${#files[@]}" -eq 0 ]; then
|
|
info "no secrets/*.age files: nothing to check"
|
|
return
|
|
fi
|
|
info "checking that the host key can decrypt secrets/*.age"
|
|
|
|
local hostmat rules='{}'
|
|
hostmat=$(awk '{print $2}' "$hostkey.pub")
|
|
if [ -f "$sdir/secrets.nix" ]; then
|
|
rules=$("${NIX[@]}" eval --json --file "$sdir/secrets.nix" \
|
|
--apply 'r: builtins.mapAttrs (_: v: v.publicKeys) r' 2>/dev/null) || rules='{}'
|
|
fi
|
|
|
|
local base listed failed=() warned=()
|
|
for f in "${files[@]}"; do
|
|
base=${f##*/}
|
|
listed=$(jq -r --arg f "$base" --arg k "$hostmat" '
|
|
(.[$f] // [])
|
|
| map(select(type == "string") | split(" ") as $p | if ($p | length) > 1 then $p[1] else $p[0] end)
|
|
| any(. == $k)' <<<"$rules")
|
|
if [ "$rules" = '{}' ] && [ -f "$sdir/secrets.nix" ] && grep -qF "$hostmat" "$sdir/secrets.nix"; then
|
|
listed=true
|
|
fi
|
|
if age -d -i "$hostkey" "$f" >/dev/null 2>"$WORK/age.err"; then
|
|
ok "$base"
|
|
elif [ "$listed" = true ] || [ "$base" = "$USER_NAME-password.age" ]; then
|
|
failed+=("$base")
|
|
else
|
|
warned+=("$base")
|
|
fi
|
|
done
|
|
|
|
for f in "${warned[@]}"; do
|
|
warn "$f is not encrypted for this host key; the installed system will not get it"
|
|
done
|
|
[ "${#failed[@]}" -eq 0 ] && return
|
|
|
|
cat >&2 <<EOF
|
|
|
|
${RED}Cannot decrypt: ${failed[*]}${RST}
|
|
The installed system would boot without them (without the password secret you
|
|
could not log in). Nothing has been installed yet. Host public key:
|
|
|
|
$HOST_PUB
|
|
|
|
Fix it with ONE of the following, then run the installer again with
|
|
sudo install-system --host $HOST --flake $FLAKE --keys-dir $KEYS_DIR --disko-mode mount
|
|
|
|
A) Encrypt the secrets for this key.
|
|
1. Put the key above into secrets/secrets.nix (the 'host' variable).
|
|
2. Re-key with the personal key that can still decrypt them. 'agenix' is
|
|
installed on the installer ISO; the key can sit on the stick:
|
|
cd $sdir && agenix -r -i /path/to/your/id_ed25519
|
|
(or do this on your own machine and copy secrets/ over $sdir).
|
|
3. git -C $FLAKE add secrets
|
|
B) Drop the password secret and set the password during the install:
|
|
git -C $FLAKE rm -f secrets/$USER_NAME-password.age
|
|
EOF
|
|
if [ "$NEW_HOST_KEY" -eq 1 ] && [ "$KEYS_WRITABLE" -eq 0 ]; then
|
|
printf '\nThe new host key exists only at %s: copy it to <stick>/keys/%s/ or the re-run keeps using it from there.\n' \
|
|
"$hostkey" "$HOST" >&2
|
|
fi
|
|
exit 1
|
|
}
|
|
|
|
install_sbctl_keys() {
|
|
if [ "$SB_ENABLE" != true ]; then
|
|
info "Secure Boot is disabled in the configuration: no sbctl keys needed"
|
|
return
|
|
fi
|
|
info "Secure Boot keys (sbctl)"
|
|
command -v sbctl >/dev/null || die "sbctl is not available"
|
|
local dst="$MNT/var/lib/sbctl" src="$KEYS_DIR/$HOST/sbctl" k
|
|
install -d -m 0700 -o root -g root "$dst"
|
|
if [ -d "$dst/keys" ] && [ -f "$dst/GUID" ]; then
|
|
ok "keys already on the target"
|
|
elif [ -d "$src/keys" ] && [ -f "$src/GUID" ]; then
|
|
cp -r --no-preserve=mode,ownership "$src/keys" "$dst/keys"
|
|
cp --no-preserve=mode,ownership "$src/GUID" "$dst/GUID"
|
|
ok "copied keys from $src"
|
|
else
|
|
# landlock would only allow writes below /var/lib, not below /mnt.
|
|
sbctl --disable-landlock create-keys --export "$dst/keys" --database-path "$dst/GUID"
|
|
fi
|
|
for k in PK KEK db; do
|
|
[ -f "$dst/keys/$k/$k.key" ] && [ -f "$dst/keys/$k/$k.pem" ] ||
|
|
die "sbctl keys incomplete: $k.key or $k.pem missing below $dst/keys/$k"
|
|
done
|
|
chown -R root:root "$dst"
|
|
chmod 0700 "$dst"
|
|
find "$dst/keys" -type d -exec chmod 0700 {} +
|
|
find "$dst/keys" -type f -name '*.key' -exec chmod 0400 {} +
|
|
find "$dst/keys" -type f -name '*.pem' -exec chmod 0644 {} +
|
|
chmod 0644 "$dst/GUID"
|
|
|
|
if [ ! -d "$src/keys" ]; then
|
|
if [ "$KEYS_WRITABLE" -eq 1 ]; then
|
|
install -d -m 0700 "$src"
|
|
cp -r --no-preserve=mode,ownership "$dst/keys" "$src/keys"
|
|
cp --no-preserve=mode,ownership "$dst/GUID" "$src/GUID"
|
|
ok "saved a copy to $src (keep it: re-installs reuse the enrolled keys)"
|
|
else
|
|
warn "$KEYS_DIR is not writable: back up $dst yourself, or you must re-enroll Secure Boot keys after a re-install"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
run_nixos_install() {
|
|
info "nixos-install (long: downloads and builds the system)"
|
|
nixos-install --flake "$FLAKE#$HOST" --no-root-passwd --no-channel-copy
|
|
}
|
|
|
|
set_user_password() {
|
|
if [ "$SECRETS_ENABLE" = true ] && [ -f "$FLAKE/secrets/$USER_NAME-password.age" ]; then
|
|
ok "login password comes from secrets/$USER_NAME-password.age"
|
|
return
|
|
fi
|
|
info "no password secret: set the login password for $USER_NAME"
|
|
local i
|
|
for i in 1 2 3; do
|
|
if nixos-enter --root "$MNT" -c "/nix/var/nix/profiles/system/sw/bin/passwd $(printf '%q' "$USER_NAME")"; then
|
|
ok "password set"
|
|
return
|
|
fi
|
|
warn "passwd failed (attempt $i of 3)"
|
|
done
|
|
warn "no password set: the configuration falls back to its initial password; change it with 'passwd' after the first login"
|
|
}
|
|
|
|
copy_repo_to_home() {
|
|
info "copying the flake to ~/nixos-config"
|
|
local home="$MNT/home/$USER_NAME" dest="$MNT/home/$USER_NAME/nixos-config" m
|
|
[ -d "$home" ] || install -d -m 0700 -o "$USER_UID" -g 100 "$home"
|
|
rm -rf "${dest:?}"
|
|
mkdir -p "$dest"
|
|
tar -C "$FLAKE" --exclude=./keys --exclude=./result --exclude='./result-*' -cf - . |
|
|
tar -C "$dest" --no-same-owner -xf -
|
|
chown -R "$USER_UID:100" "$dest"
|
|
chmod -R u+rwX "$dest"
|
|
# The home directory and the data-disk subvolumes mounted into it are created
|
|
# by root; the user needs to own them (gid 100 is 'users' on NixOS).
|
|
chown "$USER_UID:100" "$home"
|
|
while IFS= read -r m; do
|
|
chown "$USER_UID:100" "$m"
|
|
done < <(findmnt -ln -o TARGET | awk -v h="$home/" 'index($0, h) == 1 && substr($0, length(h) + 1) !~ /\//')
|
|
if [ -d "$home/Games" ]; then
|
|
install -d -m 0755 -o "$USER_UID" -g 100 "$home/Games/SteamLibrary"
|
|
fi
|
|
sync
|
|
ok "copied to /home/$USER_NAME/nixos-config"
|
|
}
|
|
|
|
next_steps() {
|
|
cat <<EOF
|
|
|
|
${GRN}Installation finished.${RST}
|
|
|
|
Next steps
|
|
1. Reboot and remove the USB stick: umount -R $MNT; reboot
|
|
EOF
|
|
if [ "$SB_ENABLE" = true ]; then
|
|
cat <<EOF
|
|
2. Secure Boot (first boot only):
|
|
- Enter the firmware setup (Del/F2) and choose "Reset to Setup Mode" /
|
|
clear the Platform Key (NOT "restore factory keys"). Boot NixOS.
|
|
- sudo sbctl enroll-keys --microsoft
|
|
- Reboot, enable Secure Boot in the firmware, then check:
|
|
bootctl status | grep -i 'secure boot'
|
|
Cannot boot at all? In modules/hosts/$HOST/host.nix swap the secure-boot aspect for systemd-boot.
|
|
EOF
|
|
fi
|
|
cat <<EOF
|
|
3. Steam library: add ~/Games/SteamLibrary (data SSD) in Steam > Settings > Storage.
|
|
4. The flake is in ~/nixos-config. Commit the files created by this install:
|
|
hosts/$HOST/disk-ids.json, hosts/$HOST/hardware-configuration.nix, flake.lock
|
|
(keys/ stays out of git). Rebuild later with: switch-system
|
|
EOF
|
|
}
|
|
|
|
main() {
|
|
preflight
|
|
locate_flake
|
|
find_keys_dir
|
|
ensure_lock
|
|
select_disks
|
|
eval_config
|
|
confirm
|
|
run_disko
|
|
gen_hardware_config
|
|
install_host_key
|
|
verify_secrets
|
|
install_sbctl_keys
|
|
run_nixos_install
|
|
set_user_password
|
|
copy_repo_to_home
|
|
next_steps
|
|
}
|
|
|
|
main
|