115 lines
2.3 KiB
Bash
115 lines
2.3 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
hostname=""
|
|
device=""
|
|
no_confirm=""
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
--hostname)
|
|
hostname=$2
|
|
shift
|
|
shift
|
|
;;
|
|
--device)
|
|
device=$2
|
|
shift
|
|
shift
|
|
;;
|
|
--no-confirm)
|
|
no_confirm=1
|
|
shift
|
|
;;
|
|
*)
|
|
echo "unknown option: $1"
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
echo ""
|
|
echo " This is clerie's nixfiles auto install for new hosts"
|
|
echo " It will do dangerous things like format your disk"
|
|
echo " So be careful when using it"
|
|
echo ""
|
|
|
|
if [[ -z $no_confirm ]]; then
|
|
read -e -r -p "Continue?" confirm
|
|
echo "$confirm" > /dev/null
|
|
fi
|
|
|
|
if [[ -z $hostname ]]; then
|
|
fallback_hostname="host${RANDOM}"
|
|
read -e -r -p "Hostname [$fallback_hostname]: " hostname
|
|
if [[ -z $hostname ]]; then
|
|
hostname=$fallback_hostname
|
|
fi
|
|
fi
|
|
|
|
echo "[I] Deploying with hostname ${hostname}"
|
|
|
|
if [[ -z $device ]]; then
|
|
device="/dev/sda"
|
|
while true; do
|
|
read -e -r -p "Disk [$device]: " dev
|
|
if [[ -z $dev ]]; then
|
|
dev=$device
|
|
fi
|
|
|
|
if [[ -b $dev ]]; then
|
|
device=$dev
|
|
break
|
|
else
|
|
echo "[E] Disk $dev does not exist"
|
|
fi
|
|
done
|
|
fi
|
|
|
|
echo "[I] Deploying on disk ${device}"
|
|
|
|
if [[ -z $no_confirm ]]; then
|
|
read -e -r -p "Deploy host?" deploy
|
|
echo "$deploy" > /dev/null
|
|
fi
|
|
|
|
echo "[I] Formatting disk"
|
|
|
|
if [[ ! -b $device ]]; then
|
|
echo "Disk $device does not exist"
|
|
exit 1
|
|
fi
|
|
|
|
echo "[I] Using ${device}"
|
|
|
|
parted --script "$device" mklabel gpt
|
|
parted --script "$device" disk_set pmbr_boot on
|
|
|
|
parted --script "$device" mkpart boot 0% 512M
|
|
parted --script "$device" set 1 bios_grub on
|
|
|
|
parted --script "$device" mkpart root 512M 100%
|
|
|
|
echo "[I] Creating file system"
|
|
|
|
mkfs.ext4 -F "${device}2"
|
|
|
|
echo "[I] Mount file system"
|
|
|
|
mount "${device}2" /mnt
|
|
|
|
echo "[I] Generate NixOS configuration"
|
|
|
|
nixfiles-generate-config --root /mnt --hostname "${hostname}"
|
|
|
|
sed -i "s~# boot\.loader\.grub\.device = \"/dev/sda\";~boot\.loader\.grub\.device = \"${device}\";~g" "/mnt/etc/nixos/hosts/${hostname}/configuration.nix"
|
|
|
|
echo "[I] Install NixOS"
|
|
|
|
NIX_CONFIG=$(printf "extra-experimental-features = flakes nix-command\nextra-substituters = https://nix-cache.clerie.de\nextra-trusted-public-keys = nix-cache.clerie.de:bAt1GJTS9BOTcXFWj3nURrSlcjqikCev9yDvqArMP5g=\n")
|
|
export NIX_CONFIG
|
|
|
|
nixos-install --flake "/mnt/etc/nixos#${hostname}" --root /mnt --no-root-password
|
|
|