WIP: home-manager modules and install tooling (in progress, unreviewed)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01J1cugeLkfFLZMpiztQ8TyN
This commit is contained in:
30 files changed
+2619
No files matched your search
@@ -0,0 +1,137 @@
|
||||
#!/usr/bin/env bash
|
||||
# create-keys: generate the machine secrets that live only on the install USB
|
||||
# stick, in keys/<host>/ (gitignored): the SSH host key (agenix identity) and
|
||||
# the Secure Boot keys (sbctl). Existing keys are never overwritten.
|
||||
set -euo pipefail
|
||||
|
||||
HOST=desktop
|
||||
FLAKE_ARG=
|
||||
|
||||
if [ -t 1 ]; then
|
||||
RED=$'\033[1;31m'
|
||||
GRN=$'\033[1;32m'
|
||||
YEL=$'\033[1;33m'
|
||||
RST=$'\033[0m'
|
||||
else
|
||||
RED=
|
||||
GRN=
|
||||
YEL=
|
||||
RST=
|
||||
fi
|
||||
|
||||
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: create-keys [--host NAME] [--flake DIR]
|
||||
|
||||
Creates keys/<host>/ssh_host_ed25519_key(.pub) and keys/<host>/sbctl/{keys,GUID}
|
||||
inside the flake directory (default: the repository you are in) if they do not
|
||||
exist yet, then prints the public key to put into secrets/secrets.nix.
|
||||
Copy keys/ to the install USB stick together with the repository.
|
||||
EOF
|
||||
}
|
||||
|
||||
while [ "$#" -gt 0 ]; do
|
||||
case "$1" in
|
||||
--host)
|
||||
[ "$#" -ge 2 ] || die "--host needs a value"
|
||||
HOST=$2
|
||||
shift 2
|
||||
;;
|
||||
--flake)
|
||||
[ "$#" -ge 2 ] || die "--flake needs a value"
|
||||
FLAKE_ARG=$2
|
||||
shift 2
|
||||
;;
|
||||
-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
|
||||
|
||||
find_flake() {
|
||||
local d
|
||||
if [ -n "$FLAKE_ARG" ]; then
|
||||
d=$FLAKE_ARG
|
||||
elif d=$(git rev-parse --show-toplevel 2>/dev/null) && [ -f "$d/flake.nix" ]; then
|
||||
:
|
||||
elif [ -f "$PWD/flake.nix" ]; then
|
||||
d=$PWD
|
||||
else
|
||||
die "no flake found: run inside the repository or pass --flake DIR"
|
||||
fi
|
||||
[ -f "$d/flake.nix" ] || die "no flake.nix in $d"
|
||||
readlink -f "$d"
|
||||
}
|
||||
|
||||
FLAKE=$(find_flake)
|
||||
KEYS="$FLAKE/keys/$HOST"
|
||||
HOSTKEY="$KEYS/ssh_host_ed25519_key"
|
||||
SBCTL="$KEYS/sbctl"
|
||||
|
||||
# Private keys must never reach git (and through it, the world-readable Nix store).
|
||||
if git -C "$FLAKE" rev-parse --is-inside-work-tree >/dev/null 2>&1 &&
|
||||
! git -C "$FLAKE" check-ignore -q "keys/$HOST/ssh_host_ed25519_key"; then
|
||||
die "keys/ is not ignored by git: add 'keys/' to $FLAKE/.gitignore first"
|
||||
fi
|
||||
|
||||
umask 077
|
||||
mkdir -p "$KEYS"
|
||||
|
||||
if [ -e "$HOSTKEY" ]; then
|
||||
ok "host key exists, left untouched: $HOSTKEY"
|
||||
else
|
||||
ssh-keygen -q -t ed25519 -N '' -C "root@$HOST" -f "$HOSTKEY"
|
||||
ok "created host key: $HOSTKEY"
|
||||
fi
|
||||
if [ ! -e "$HOSTKEY.pub" ]; then
|
||||
ssh-keygen -y -f "$HOSTKEY" >"$HOSTKEY.pub"
|
||||
chmod 0644 "$HOSTKEY.pub"
|
||||
fi
|
||||
|
||||
if [ -d "$SBCTL/keys" ] && [ -f "$SBCTL/GUID" ]; then
|
||||
ok "Secure Boot keys exist, left untouched: $SBCTL"
|
||||
elif [ -e "$SBCTL/keys" ] || [ -e "$SBCTL/GUID" ]; then
|
||||
die "$SBCTL is incomplete (needs both keys/ and GUID): inspect it and remove the leftovers yourself"
|
||||
else
|
||||
# landlock only allows writes below /var/lib, but we write into the repository.
|
||||
sbctl --disable-landlock create-keys --export "$SBCTL/keys" --database-path "$SBCTL/GUID"
|
||||
ok "created Secure Boot keys: $SBCTL"
|
||||
fi
|
||||
|
||||
# Run through sudo: hand the files back to the person who called us.
|
||||
if [ "$(id -u)" -eq 0 ] && [ -n "${SUDO_UID:-}" ] && [ -n "${SUDO_GID:-}" ]; then
|
||||
chown -R "$SUDO_UID:$SUDO_GID" "$FLAKE/keys"
|
||||
fi
|
||||
|
||||
PUB=$(cat "$HOSTKEY.pub")
|
||||
cat <<EOF
|
||||
|
||||
Host public key (not secret):
|
||||
$PUB
|
||||
|
||||
Put it into secrets/secrets.nix and make sure it is a recipient of every secret:
|
||||
|
||||
host = "$PUB";
|
||||
|
||||
recipients = [ host yourPersonalKey ];
|
||||
|
||||
Then (re-)encrypt with your personal key: set-password, secret-from-file, or
|
||||
'cd secrets && agenix -r' for already encrypted files.
|
||||
|
||||
keys/$HOST/ is gitignored and holds PRIVATE keys: copy it to the install USB
|
||||
stick next to the repository and keep a backup. The installer puts the host
|
||||
key into /etc/ssh and the Secure Boot keys into /var/lib/sbctl on the target.
|
||||
EOF
|
||||
@@ -0,0 +1,24 @@
|
||||
# Generates keys/<host>/: SSH host key (agenix identity) and Secure Boot keys.
|
||||
{
|
||||
lib,
|
||||
writeShellApplication,
|
||||
coreutils,
|
||||
git,
|
||||
openssh,
|
||||
sbctl,
|
||||
}:
|
||||
writeShellApplication {
|
||||
name = "create-keys";
|
||||
runtimeInputs = [
|
||||
coreutils
|
||||
git
|
||||
openssh
|
||||
sbctl
|
||||
];
|
||||
text = builtins.readFile ./create-keys.sh;
|
||||
meta = {
|
||||
description = "Create the SSH host key and Secure Boot keys for a host (keys/<host>/)";
|
||||
mainProgram = "create-keys";
|
||||
platforms = lib.platforms.linux;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
# Our own packages: install and key helpers. Imported by overlays/additions.nix,
|
||||
# so they show up as pkgs.<name> on every host and on the installer ISO.
|
||||
{ pkgs, inputs }:
|
||||
let
|
||||
inherit (pkgs.stdenv.hostPlatform) system;
|
||||
|
||||
secret-from-file = pkgs.callPackage ./secret-from-file { };
|
||||
in
|
||||
{
|
||||
inherit secret-from-file;
|
||||
|
||||
install-system = pkgs.callPackage ./install-system {
|
||||
disko = inputs.disko.packages.${system}.disko;
|
||||
};
|
||||
create-keys = pkgs.callPackage ./create-keys { };
|
||||
set-password = pkgs.callPackage ./set-password { inherit secret-from-file; };
|
||||
switch-system = pkgs.callPackage ./switch-system { };
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
# Installer for this flake: disks (disko), keys, secrets check, nixos-install.
|
||||
# nixos-install, nixos-generate-config and nixos-enter come from the NixOS
|
||||
# installer environment, not from here.
|
||||
{
|
||||
lib,
|
||||
writeShellApplication,
|
||||
coreutils,
|
||||
findutils,
|
||||
gawk,
|
||||
gnugrep,
|
||||
gnutar,
|
||||
util-linux,
|
||||
jq,
|
||||
git,
|
||||
age,
|
||||
openssh,
|
||||
sbctl,
|
||||
disko,
|
||||
}:
|
||||
writeShellApplication {
|
||||
name = "install-system";
|
||||
runtimeInputs = [
|
||||
coreutils
|
||||
findutils
|
||||
gawk
|
||||
gnugrep
|
||||
gnutar
|
||||
util-linux
|
||||
jq
|
||||
git
|
||||
age
|
||||
openssh
|
||||
sbctl
|
||||
disko
|
||||
];
|
||||
text = builtins.readFile ./install-system.sh;
|
||||
meta = {
|
||||
description = "Partition, install and key this NixOS flake from the NixOS installer";
|
||||
mainProgram = "install-system";
|
||||
platforms = lib.platforms.linux;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,773 @@
|
||||
#!/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
|
||||
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 --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 "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 -rn -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? Set my.boot.secureBoot.enable = false in hosts/$HOST/default.nix.
|
||||
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
|
||||
@@ -0,0 +1,27 @@
|
||||
# Encrypts a file as secrets/<name>.age for the recipients in secrets/secrets.nix.
|
||||
# `nix` (to read secrets.nix) is taken from the system PATH.
|
||||
{
|
||||
lib,
|
||||
writeShellApplication,
|
||||
coreutils,
|
||||
diffutils,
|
||||
git,
|
||||
jq,
|
||||
age,
|
||||
}:
|
||||
writeShellApplication {
|
||||
name = "secret-from-file";
|
||||
runtimeInputs = [
|
||||
coreutils
|
||||
diffutils
|
||||
git
|
||||
jq
|
||||
age
|
||||
];
|
||||
text = builtins.readFile ./secret-from-file.sh;
|
||||
meta = {
|
||||
description = "Encrypt an existing file as secrets/<name>.age using the agenix recipient rules";
|
||||
mainProgram = "secret-from-file";
|
||||
platforms = lib.platforms.linux;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
#!/usr/bin/env bash
|
||||
# secret-from-file: encrypt an existing file (or stdin) as secrets/<name>.age
|
||||
# for the recipients listed in secrets/secrets.nix. Refuses to overwrite
|
||||
# without --force.
|
||||
set -euo pipefail
|
||||
|
||||
FLAKE_ARG=
|
||||
FORCE=0
|
||||
IDENTITY=
|
||||
NAME=
|
||||
SOURCE=
|
||||
|
||||
if [ -t 2 ]; then
|
||||
RED=$'\033[1;31m'
|
||||
GRN=$'\033[1;32m'
|
||||
RST=$'\033[0m'
|
||||
else
|
||||
RED=
|
||||
GRN=
|
||||
RST=
|
||||
fi
|
||||
|
||||
ok() { printf '%s ok%s %s\n' "$GRN" "$RST" "$*" >&2; }
|
||||
die() {
|
||||
printf '%serror:%s %s\n' "$RED" "$RST" "$*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage: secret-from-file NAME.age SOURCE|- [options]
|
||||
|
||||
Encrypts SOURCE (a file, or - for stdin) to secrets/NAME.age for the public
|
||||
keys that secrets/secrets.nix lists under "NAME.age".publicKeys.
|
||||
|
||||
-i, --identity FILE after encrypting, check that FILE (a private key that
|
||||
is a recipient) can decrypt the result
|
||||
--flake DIR flake directory (default: the repository you are in)
|
||||
-f, --force replace an existing secrets/NAME.age
|
||||
-h, --help show this help
|
||||
|
||||
Examples:
|
||||
secret-from-file ssh-id_ed25519.age ~/.ssh/id_ed25519 -i ~/.ssh/id_ed25519
|
||||
cp ~/.ssh/id_ed25519.pub secrets/ssh-id_ed25519.pub
|
||||
EOF
|
||||
}
|
||||
|
||||
while [ "$#" -gt 0 ]; do
|
||||
case "$1" in
|
||||
-i | --identity)
|
||||
[ "$#" -ge 2 ] || die "$1 needs a value"
|
||||
IDENTITY=$2
|
||||
shift 2
|
||||
;;
|
||||
--flake)
|
||||
[ "$#" -ge 2 ] || die "--flake needs a value"
|
||||
FLAKE_ARG=$2
|
||||
shift 2
|
||||
;;
|
||||
-f | --force)
|
||||
FORCE=1
|
||||
shift
|
||||
;;
|
||||
-h | --help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
-)
|
||||
if [ -z "$SOURCE" ]; then SOURCE=-; else die "unexpected argument: -"; fi
|
||||
shift
|
||||
;;
|
||||
-*) die "unknown option: $1 (see --help)" ;;
|
||||
*)
|
||||
if [ -z "$NAME" ]; then
|
||||
NAME=$1
|
||||
elif [ -z "$SOURCE" ]; then
|
||||
SOURCE=$1
|
||||
else
|
||||
die "unexpected argument: $1"
|
||||
fi
|
||||
shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
[ -n "$NAME" ] && [ -n "$SOURCE" ] || {
|
||||
usage >&2
|
||||
exit 2
|
||||
}
|
||||
case "$NAME" in
|
||||
*.age) ;;
|
||||
*) NAME=$NAME.age ;;
|
||||
esac
|
||||
# The name ends up inside a Nix expression: keep it boring.
|
||||
case "$NAME" in
|
||||
*[!A-Za-z0-9._-]* | .*) die "secret name may only contain letters, digits, '.', '_' and '-': $NAME" ;;
|
||||
esac
|
||||
if [ "$SOURCE" != - ] && [ ! -f "$SOURCE" ]; then
|
||||
die "no such file: $SOURCE"
|
||||
fi
|
||||
if [ -n "$IDENTITY" ] && [ ! -f "$IDENTITY" ]; then
|
||||
die "identity file not found: $IDENTITY"
|
||||
fi
|
||||
|
||||
find_flake() {
|
||||
local d
|
||||
if [ -n "$FLAKE_ARG" ]; then
|
||||
d=$FLAKE_ARG
|
||||
elif d=$(git rev-parse --show-toplevel 2>/dev/null) && [ -f "$d/flake.nix" ]; then
|
||||
:
|
||||
elif [ -f "$PWD/flake.nix" ]; then
|
||||
d=$PWD
|
||||
else
|
||||
die "no flake found: run inside the repository or pass --flake DIR"
|
||||
fi
|
||||
[ -f "$d/flake.nix" ] || die "no flake.nix in $d"
|
||||
readlink -f "$d"
|
||||
}
|
||||
|
||||
FLAKE=$(find_flake)
|
||||
SECRETS="$FLAKE/secrets"
|
||||
RULES="$SECRETS/secrets.nix"
|
||||
OUT="$SECRETS/$NAME"
|
||||
[ -f "$RULES" ] || die "$RULES not found"
|
||||
if [ -e "$OUT" ] && [ "$FORCE" -eq 0 ]; then
|
||||
die "$OUT already exists (use --force to replace it)"
|
||||
fi
|
||||
|
||||
umask 077
|
||||
WORK=$(mktemp -d)
|
||||
trap 'rm -rf "$WORK"' EXIT
|
||||
|
||||
if ! KEYS_JSON=$(nix --extra-experimental-features 'nix-command flakes' eval --json \
|
||||
--file "$RULES" --apply "r: r.\"$NAME\".publicKeys" 2>"$WORK/eval.err"); then
|
||||
cat "$WORK/eval.err" >&2
|
||||
die "no rule for \"$NAME\" in $RULES. Add:
|
||||
\"$NAME\".publicKeys = recipients;"
|
||||
fi
|
||||
if jq -e 'any(.[]; test("PLACEHOLDER"))' <<<"$KEYS_JSON" >/dev/null; then
|
||||
die "$RULES still contains PLACEHOLDER keys: replace them first (create-keys prints the host key)"
|
||||
fi
|
||||
jq -r '.[]' <<<"$KEYS_JSON" >"$WORK/recipients"
|
||||
[ -s "$WORK/recipients" ] || die "\"$NAME\" has no recipients in $RULES"
|
||||
|
||||
if [ "$SOURCE" = - ]; then
|
||||
cat >"$WORK/plain"
|
||||
SOURCE=$WORK/plain
|
||||
fi
|
||||
|
||||
age --encrypt --armor -R "$WORK/recipients" -o "$WORK/out.age" "$SOURCE" ||
|
||||
die "encryption failed: check that every key in $RULES is valid"
|
||||
|
||||
if [ -n "$IDENTITY" ]; then
|
||||
age --decrypt -i "$IDENTITY" "$WORK/out.age" | cmp -s - "$SOURCE" ||
|
||||
die "$IDENTITY cannot decrypt the result: it is not one of the recipients; nothing was written"
|
||||
ok "verified with $IDENTITY"
|
||||
fi
|
||||
|
||||
install -m 0644 "$WORK/out.age" "$OUT"
|
||||
ok "wrote $OUT"
|
||||
|
||||
if git -C "$FLAKE" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||
if git -C "$FLAKE" add -- "secrets/$NAME"; then
|
||||
ok "git add secrets/$NAME (flakes only see tracked files)"
|
||||
fi
|
||||
fi
|
||||
@@ -0,0 +1,20 @@
|
||||
# Hashes a login password (yescrypt) and stores it via secret-from-file.
|
||||
{
|
||||
lib,
|
||||
writeShellApplication,
|
||||
mkpasswd,
|
||||
secret-from-file,
|
||||
}:
|
||||
writeShellApplication {
|
||||
name = "set-password";
|
||||
runtimeInputs = [
|
||||
mkpasswd
|
||||
secret-from-file
|
||||
];
|
||||
text = builtins.readFile ./set-password.sh;
|
||||
meta = {
|
||||
description = "Set the login password of a user as an agenix secret (secrets/<user>-password.age)";
|
||||
mainProgram = "set-password";
|
||||
platforms = lib.platforms.linux;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env bash
|
||||
# set-password: hash a login password (yescrypt) and store it, encrypted with
|
||||
# agenix recipients, as secrets/<user>-password.age.
|
||||
set -euo pipefail
|
||||
|
||||
USER_NAME=faumaray
|
||||
FLAKE_ARG=
|
||||
IDENTITY=
|
||||
FORCE=0
|
||||
|
||||
die() {
|
||||
printf 'error: %s\n' "$*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage: set-password [--user NAME] [--flake DIR] [-i IDENTITY] [--force]
|
||||
|
||||
Asks for a password, hashes it with 'mkpasswd -m yescrypt' and encrypts the hash
|
||||
to secrets/NAME-password.age (recipients: secrets/secrets.nix).
|
||||
|
||||
--user NAME login name (default: faumaray)
|
||||
--flake DIR flake directory (default: the repository you are in)
|
||||
-i, --identity FILE verify the result decrypts with this private key
|
||||
-f, --force replace an existing secrets/NAME-password.age
|
||||
EOF
|
||||
}
|
||||
|
||||
while [ "$#" -gt 0 ]; do
|
||||
case "$1" in
|
||||
--user)
|
||||
[ "$#" -ge 2 ] || die "--user needs a value"
|
||||
USER_NAME=$2
|
||||
shift 2
|
||||
;;
|
||||
--flake)
|
||||
[ "$#" -ge 2 ] || die "--flake needs a value"
|
||||
FLAKE_ARG=$2
|
||||
shift 2
|
||||
;;
|
||||
-i | --identity)
|
||||
[ "$#" -ge 2 ] || die "$1 needs a value"
|
||||
IDENTITY=$2
|
||||
shift 2
|
||||
;;
|
||||
-f | --force)
|
||||
FORCE=1
|
||||
shift
|
||||
;;
|
||||
-h | --help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
*) die "unknown argument: $1 (see --help)" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
case "$USER_NAME" in
|
||||
'' | *[!A-Za-z0-9_-]*) die "invalid user name: '$USER_NAME'" ;;
|
||||
esac
|
||||
|
||||
args=("$USER_NAME-password.age" -)
|
||||
if [ -n "$FLAKE_ARG" ]; then args+=(--flake "$FLAKE_ARG"); fi
|
||||
if [ -n "$IDENTITY" ]; then args+=(--identity "$IDENTITY"); fi
|
||||
if [ "$FORCE" -eq 1 ]; then args+=(--force); fi
|
||||
|
||||
[ -t 0 ] || die "run this in a terminal: the password is read interactively"
|
||||
read -r -s -p "Password for $USER_NAME: " pw1
|
||||
echo
|
||||
read -r -s -p "Repeat password: " pw2
|
||||
echo
|
||||
[ -n "$pw1" ] || die "empty password"
|
||||
[ "$pw1" = "$pw2" ] || die "passwords differ"
|
||||
|
||||
# The password goes through stdin only, never through argv.
|
||||
hash=$(printf '%s' "$pw1" | mkpasswd -m yescrypt -s)
|
||||
unset pw1 pw2
|
||||
printf '%s\n' "$hash" | secret-from-file "${args[@]}"
|
||||
|
||||
echo
|
||||
echo "Rebuild (switch-system) or reinstall to use the new password."
|
||||
@@ -0,0 +1,21 @@
|
||||
# Rebuild this machine from the flake (nh when present, else nixos-rebuild).
|
||||
# nh and nixos-rebuild are taken from the system PATH on purpose.
|
||||
{
|
||||
lib,
|
||||
writeShellApplication,
|
||||
coreutils,
|
||||
git,
|
||||
}:
|
||||
writeShellApplication {
|
||||
name = "switch-system";
|
||||
runtimeInputs = [
|
||||
coreutils
|
||||
git
|
||||
];
|
||||
text = builtins.readFile ./switch-system.sh;
|
||||
meta = {
|
||||
description = "Rebuild and switch this NixOS machine from its flake (nh os / nixos-rebuild)";
|
||||
mainProgram = "switch-system";
|
||||
platforms = lib.platforms.linux;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env bash
|
||||
# switch-system: rebuild and activate this machine from the flake.
|
||||
# Uses `nh os` when available, else `sudo nixos-rebuild`.
|
||||
set -euo pipefail
|
||||
|
||||
HOST=
|
||||
FLAKE_ARG=
|
||||
ACTION=switch
|
||||
|
||||
die() {
|
||||
printf 'error: %s\n' "$*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage: switch-system [--host NAME] [--flake DIR] [boot|switch|test]
|
||||
|
||||
--host NAME flake host (default: this machine's host name)
|
||||
--flake DIR flake directory (default: the repository you are in, else
|
||||
~/nixos-config)
|
||||
switch build, activate now and make it the boot default (default)
|
||||
boot build and activate on next boot
|
||||
test build and activate now without touching the boot entry
|
||||
EOF
|
||||
}
|
||||
|
||||
while [ "$#" -gt 0 ]; do
|
||||
case "$1" in
|
||||
--host)
|
||||
[ "$#" -ge 2 ] || die "--host needs a value"
|
||||
HOST=$2
|
||||
shift 2
|
||||
;;
|
||||
--flake)
|
||||
[ "$#" -ge 2 ] || die "--flake needs a value"
|
||||
FLAKE_ARG=$2
|
||||
shift 2
|
||||
;;
|
||||
boot | switch | test)
|
||||
ACTION=$1
|
||||
shift
|
||||
;;
|
||||
-h | --help)
|
||||
usage
|
||||
exit 0
|
||||
;;
|
||||
*) die "unknown argument: $1 (see --help)" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ -z "$HOST" ]; then
|
||||
HOST=$(cat /proc/sys/kernel/hostname)
|
||||
fi
|
||||
|
||||
# After `sudo`, $HOME can be root's: prefer the home of the invoking user.
|
||||
home=$HOME
|
||||
if [ -n "${SUDO_USER:-}" ]; then
|
||||
home=$(getent passwd "$SUDO_USER" | cut -d: -f6)
|
||||
fi
|
||||
|
||||
if [ -n "$FLAKE_ARG" ]; then
|
||||
DIR=$FLAKE_ARG
|
||||
elif top=$(git rev-parse --show-toplevel 2>/dev/null) && [ -f "$top/flake.nix" ]; then
|
||||
DIR=$top
|
||||
elif [ -f "$home/nixos-config/flake.nix" ]; then
|
||||
DIR=$home/nixos-config
|
||||
else
|
||||
die "no flake found: run inside the repository or pass --flake DIR"
|
||||
fi
|
||||
[ -f "$DIR/flake.nix" ] || die "no flake.nix in $DIR"
|
||||
DIR=$(readlink -f "$DIR")
|
||||
|
||||
# Flakes only see files tracked by git: warn about new, untracked files.
|
||||
if git -C "$DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
||||
untracked=$(git -C "$DIR" ls-files --others --exclude-standard)
|
||||
if [ -n "$untracked" ]; then
|
||||
printf 'warning: untracked files are invisible to the flake (git add them):\n%s\n' "$untracked" >&2
|
||||
fi
|
||||
fi
|
||||
|
||||
if command -v nh >/dev/null 2>&1; then
|
||||
exec nh os "$ACTION" --hostname "$HOST" "$DIR"
|
||||
fi
|
||||
exec sudo nixos-rebuild "$ACTION" --flake "$DIR#$HOST"
|
||||
Reference in new issue
Block a user