nicer scripts

This commit is contained in:
randogoth 2026-02-04 15:21:29 +02:00
parent 978a439cfe
commit 7a42926fba
2 changed files with 128 additions and 75 deletions

View file

@ -4,51 +4,84 @@ set -euo pipefail
# Non-interactive Btrfs installer using the flake in this repo.
# Defaults: DISK=/dev/vda TARGET_HOST=nanuqsaurus
DISK=${DISK:-/dev/vda}
TARGET_HOST=${TARGET_HOST:-nanuqsaurus}
DISK="${DISK:-/dev/vda}"
TARGET_HOST="${TARGET_HOST:-nanuqsaurus}"
SCRIPT_DIR=$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd)
REPO_ROOT=$(cd -- "${SCRIPT_DIR}/.." && pwd)
SCRIPT_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd -- "$SCRIPT_DIR/.." && pwd)"
MNT=/mnt
EFI_SIZE=550M
have() { command -v "$1" >/dev/null 2>&1; }
die() {
echo "ERROR: $*" >&2
exit 1
}
confirm_disk() {
echo "About to DESTROY ALL DATA on ${DISK}"
lsblk "$DISK" || die "Disk not found: $DISK"
echo "Continuing in 5 seconds… (Ctrl+C to abort)"
sleep 5
}
clear
confirm_disk
echo "[1/7] Partitioning ${DISK}"
printf "label: gpt\n,550M,U\n,,L\n" | sfdisk "${DISK}"
sfdisk "$DISK" <<EOF
label: gpt
,${EFI_SIZE},U
,,L
EOF
echo "[2/7] Formatting"
mkfs.fat -F 32 "${DISK}1"
echo "[2/7] Formatting filesystems"
mkfs.fat -F32 "${DISK}1"
mkfs.btrfs -f "${DISK}2"
echo "[3/7] Creating Btrfs subvolumes"
mkdir -p /mnt
mount "${DISK}2" /mnt
btrfs subvolume create /mnt/root
btrfs subvolume create /mnt/home
btrfs subvolume create /mnt/nix
umount /mnt
mkdir -p "$MNT"
mount "${DISK}2" "$MNT"
for subvol in root home nix; do
btrfs subvolume create "$MNT/$subvol"
done
umount "$MNT"
echo "[4/7] Mounting subvolumes"
mount -o compress=zstd,subvol=root "${DISK}2" /mnt
mkdir -p /mnt/{home,nix,boot}
mount -o compress=zstd,subvol=home "${DISK}2" /mnt/home
mount -o compress=zstd,subvol=nix "${DISK}2" /mnt/nix
mount "${DISK}1" /mnt/boot
mount -o compress=zstd,subvol=root "${DISK}2" "$MNT"
mkdir -p "$MNT"/{home,nix,boot}
mount -o compress=zstd,subvol=home "${DISK}2" "$MNT/home"
mount -o compress=zstd,subvol=nix "${DISK}2" "$MNT/nix"
mount "${DISK}1" "$MNT/boot"
echo "[5/7] Copying flake to /etc/nixos"
mkdir -p /mnt/etc/nixos
if command -v rsync >/dev/null 2>&1; then
rsync -a --exclude '.git' --exclude 'result' --exclude 'node_modules' "${REPO_ROOT}/" /mnt/etc/nixos/
mkdir -p "$MNT/etc/nixos"
EXCLUDES=(--exclude=.git --exclude=result --exclude=node_modules)
if have rsync; then
rsync -a "${EXCLUDES[@]}" "$REPO_ROOT/" "$MNT/etc/nixos/"
else
(cd "${REPO_ROOT}" && tar -cf - --exclude=.git --exclude=result --exclude=node_modules .) | (cd /mnt/etc/nixos && tar -xf -)
(cd "$REPO_ROOT" && tar -cf - "${EXCLUDES[@]/#/--exclude=}" .) \
| (cd "$MNT/etc/nixos" && tar -xf -)
fi
echo "[6/7] Generating hardware config"
mkdir -p /mnt/etc/nixos/hosts
nixos-generate-config --root /mnt --show-hardware-config > "/mnt/etc/nixos/hosts/local-${TARGET_HOST}-hardware.nix"
HOSTS_DIR="$MNT/etc/nixos/hosts"
mkdir -p "$HOSTS_DIR"
host_file="/mnt/etc/nixos/hosts/local-${TARGET_HOST}.nix"
if [ ! -e "${host_file}" ]; then
cat > "${host_file}" <<EOF
HW_FILE="$HOSTS_DIR/local-${TARGET_HOST}-hardware.nix"
HOST_FILE="$HOSTS_DIR/local-${TARGET_HOST}.nix"
nixos-generate-config --root "$MNT" --show-hardware-config > "$HW_FILE"
if [[ ! -e "$HOST_FILE" ]]; then
cat > "$HOST_FILE" <<EOF
{ inputs, ... }:
{
@ -67,8 +100,12 @@ if [ ! -e "${host_file}" ]; then
EOF
fi
echo "[7/7] Installing ${TARGET_HOST} via flake (local-${TARGET_HOST})"
nixos-install --root /mnt --flake "/mnt/etc/nixos#local-${TARGET_HOST}" --no-root-passwd |& tee /tmp/nixos-install.log
echo "Install log saved to /tmp/nixos-install.log"
echo "[7/7] Installing ${TARGET_HOST} via flake"
LOG=/tmp/nixos-install.log
nixos-install \
--root "$MNT" \
--flake "$MNT/etc/nixos#local-${TARGET_HOST}" \
--no-root-passwd |& tee "$LOG"
echo "Install log saved to $LOG"
echo "Install complete. You can reboot now."