nix/docs/install.sh

95 lines
2.5 KiB
Bash
Raw Normal View History

2023-11-12 01:07:00 +01:00
#!/usr/bin/env bash
2023-11-11 23:36:25 +01:00
#set -euo pipefail
TARGET_HOST="${1:-}"
TARGET_USER="${2:-albert}"
if [ "$(id -u)" -eq 0 ]; then
echo "ERROR! $(basename "$0") should be run as a regular user"
exit 1
fi
2024-02-03 01:31:36 +01:00
read -p "Include wallpapers? [y/N]" -n 1 -r
2024-02-05 14:21:09 +01:00
echo ""
2024-02-03 01:31:36 +01:00
if [[ $REPLY =~ ^[Yy]$ ]]; then
if [ ! -d "/tmp/nixos/git/.git" ]; then
git clone --recursive https://git.sysctl.io/albert/nix "/tmp/nixos/git"
else
git -C "/tmp/nixos/git" pull
fi
else
if [ ! -d "/tmp/nixos/git/.git" ]; then
git clone https://git.sysctl.io/albert/nix "/tmp/nixos/git"
else
git -C "/tmp/nixos/git" pull
fi
2023-11-11 23:36:25 +01:00
fi
pushd /tmp/nixos/git
if [[ -z "$TARGET_HOST" ]]; then
echo "ERROR! $(basename "$0") requires a hostname as the first argument"
echo " The following hosts are available"
ls -1 nixos/hosts/*/default.nix | cut -d'/' -f3 | grep -v -E "iso|rpi"
exit 1
fi
if [[ -z "$TARGET_USER" ]]; then
echo "ERROR! $(basename "$0") requires a username as the second argument"
echo " The following users are available"
ls -1 nixos/users/ | grep -v -E "nixos|root"
exit 1
fi
if [ ! -e "nixos/hosts/$TARGET_HOST/disks.nix" ]; then
echo "ERROR! $(basename "$0") could not find the required nixos/$TARGET_HOST/disks.nix"
exit 1
fi
2024-02-01 11:30:17 +01:00
# Create a key for encrypted swap, if needed
2024-02-05 14:21:09 +01:00
sudo mkdir -p /mnt-root/etc/
2024-02-06 00:22:24 +01:00
sudo chmod 777 -R /mnt-root
2024-02-05 14:21:09 +01:00
echo "$(head -c32 /dev/random | base64)" > /mnt-root/etc/swap.key
2023-11-11 23:36:25 +01:00
echo "WARNING! The disks in $TARGET_HOST are about to get wiped"
echo " NixOS will be re-installed"
echo " This is a destructive operation"
echo
read -p "Are you sure? [y/N]" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
sudo true
sudo nix run github:nix-community/disko \
--extra-experimental-features "nix-command flakes" \
--no-write-lock-file \
-- \
--mode disko \
"nixos/hosts/$TARGET_HOST/disks.nix"
2024-07-14 13:35:21 +02:00
echo ""
echo " ---- "
echo " ---- Disk formatting complete "
echo " ---- "
echo ""
echo "Sleeping 5s before proceeding..."
sleep 5
2023-11-11 23:36:25 +01:00
sudo nixos-install --no-root-password --flake ".#$TARGET_HOST"
# Rsync nix-config to the target install.
sudo mkdir -p "/mnt/etc/nixos"
sudo rsync -a --delete "/tmp/nixos/git/" "/mnt/etc/nixos/git/"
pushd "/mnt/etc/nixos/git/"
popd
# If there is a keyfile for a data disk, put copy it to the root partition and
# ensure the permissions are set appropriately.
2024-02-06 09:12:51 +01:00
if [[ -f "/mnt-root/etc/swap.key" ]]; then
2024-02-05 14:21:09 +01:00
sudo cp /mnt-root/etc/swap.key /mnt/etc/swap.key
2024-02-05 11:19:52 +01:00
sudo chmod 0400 /mnt/etc/swap.key
2023-11-11 23:36:25 +01:00
fi
2024-07-14 13:35:21 +02:00
fi