Files

137 lines
4.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# this will one day be an interactive install bootstrap script for my nix config!
# right now this shall be treated as psudocode but also just a list of steps for installing
# without further adoo:
#
# 1. boot target device into nixos installer iso
# 2. `sudo -i``
# 3. `git clone https://git.blakedheld.xyz/blake/nix.git bootstrap_nix`
# 4. if new host use `ls -l /dev/disk/by-id/` to get the id of the target disk and update the related line in the disko config for that host (or partition the target disk manually however you wish)
# 5. run the disko install command `nix run github:nix-community/disko -- --mode destroy,format,mount --flake bootstrap_nix#hostname` to just format the disk(s)
# 6. generate hardware config `nixos-generate-config --root /mnt`
# 7. create directories for keys `mkdir -p /mnt/etc/sops` `mkdir -p /mnt/home/blake/.config/sops/age`
# 8. move nix config to home dir `mv /bootstrap_nix /mnt/home/blake/.nix`
# 9. copy age keys.txt to previously made dirs `echo "<key>" | tee /mnt/etc/sops/keys.txt /mnt/home/blake/.config/sops/age/keys.txt > dev/null`
# 10. install the os (let it fail, fix it, repeat, etc) `nixos-install --flake /mnt/home/blake/.nix#hostname`
# 11. set the root password
# 12. reboot and dont forget to `sudo chown -R blake:blake /home/blake`
# 13. change nix repo upstream to ssh `cd ~/.nix && git remote remove origin && git remote add origin ssh://gitea@git.blakedheld.xyz:7567/blake/nix.git`
#!/usr/bin/env bash
set -euo pipefail
AGE_KEY_FILE="/keys.txt"
echo "nixos bootstrap installer =="
# --- input ---
read -rp "hostname: " HOSTNAME
#read -rp "age key (will not be echoed): " AGE_KEY
if [[ ! -f "$AGE_KEY_FILE" ]]; then
echo "cannot find age keyfile"
exit 1
fi
AGE_KEY="$(cat "$AGE_KEY_FILE")"
echo
echo
echo "install mode:"
echo " 1) disko (auto partition/format/mount)"
echo " 2) manual (you already mounted disks)"
read -rp "choice [1/2]: " MODE
echo
if [[ -d /bootstrap_nix ]]; then
echo "repo already exists, skipping clone"
else
echo "cloning nix config..."
git clone https://git.blakedheld.xyz/blake/nix.git /bootstrap_nix
fi
cd /bootstrap_nix
# fix origin
git remote remove origin
git remote add origin ssh://gitea@git.blakedheld.xyz:7567/blake/nix.git
# --- disk setup ---
if [[ "$MODE" == "1" ]]; then
echo
echo "running disko (THIS WILL DESTROY DISKS)..."
echo "make sure /dev/disk/by-id is correct in your host config!"
# nix run github:nix-community/disko --extra-experimental-features "nix-command flakes" -- \
# --mode destroy,format,mount \
# --flake .#"${HOSTNAME}"
nix run github:nix-community/disko --extra-experimental-features "nix-command flakes" -- \
--mode destroy,format,mount \
--yes-wipe-all-disks \
/bootstrap_nix/hosts/nixos/$HOSTNAME/disko.nix
elif [[ "$MODE" == "2" ]]; then
echo
echo "MANUAL MODE SELECTED"
echo "WARNING: you must have already partitioned + mounted disks to /mnt"
read -rp "press ENTER to continue once disks are mounted..."
else
echo "invalid selection"
exit 1
fi
# --- generate hardware config ---
echo
echo "generating hardware config..."
nixos-generate-config --root /mnt
# --- setup directories ---
echo "creating sops/age directories..."
mkdir -p /mnt/etc/sops
mkdir -p /mnt/home/blake/.config/sops/age
# --- move config ---
echo "moving nix config into /mnt/home/blake/.nix ..."
cp -r /bootstrap_nix /mnt/home/blake/.nix
echo "moving hardware into /mnt/home/blake/.nix/hosts/nixos/$HOSTNAME ..."
cp /mnt/etc/nixos/hardware-configuration.nix /mnt/home/blake/.nix/hosts/nixos/$HOSTNAME
cd /mnt/home/blake/.nix
git add /mnt/home/blake/.nix/hosts/nixos/$HOSTNAME
# --- write age key ---
echo "writing age key..."
echo "$AGE_KEY" | tee \
/mnt/etc/sops/keys.txt \
/mnt/home/blake/.config/sops/age/keys.txt \
> /dev/null
# --- install ---
echo
echo "running nixos-install..."
set +e
nixos-install --root /mnt --flake /mnt/home/blake/.nix#"${HOSTNAME}"
INSTALL_EXIT=$?
set -e
if [[ $INSTALL_EXIT -ne 0 ]]; then
echo
echo "install failed. fix issues and re-run nixos-install manually if needed."
exit $INSTALL_EXIT
fi
# --- post install ---
echo
echo "installation complete!"
echo "set root password if needed, then reboot."
echo "after reboot: sudo chown -R blake:blake /home/blake"
nixos-enter --root /mnt -- run --login bash <<'EOF'
set -e
chown -R blake:blake /home/blake
nixos-rebuild switch --flake /home/blake/.nix
systemd-cryptenroll --tpm2-device=auto /dev/nvme0n1p2
EOF
echo "fin."