#!/usr/bin/env bash # create-keys: generate the machine secrets that live only on the install USB # stick, in keys// (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//ssh_host_ed25519_key(.pub) and keys//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" fi chmod 0644 "$HOSTKEY.pub" 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 <