2024-07-04 16:27:00 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# Script to setup a drive for a brand new NixOS installation.
|
2024-09-10 21:14:07 +00:00
|
|
|
# IMPORTANT: Before running this script, you'll need to create a LUKS partition using:
|
|
|
|
# cryptsetup --label=nixos-crypt --type=luks2 luksFormat [partition]
|
2024-07-04 16:27:00 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2024-09-05 20:47:53 +00:00
|
|
|
if [ "$(id -u)" != "0" ]; then
|
2024-12-05 21:58:44 +00:00
|
|
|
echo "This script must be run as root" 1>&2
|
|
|
|
exit 1
|
2024-09-05 20:47:53 +00:00
|
|
|
fi
|
|
|
|
|
2024-07-04 16:27:00 +00:00
|
|
|
# Configuration parameters
|
|
|
|
ask_root_password=false # Whether to prompt for a root user password
|
|
|
|
boot_partition="" # The drive to install the bootloader to
|
|
|
|
luks_partition="" # The drive partition to create the LUKS container on
|
|
|
|
root_partition="/dev/mapper/nixos-crypt" # The partition to install NixOS to
|
|
|
|
|
|
|
|
function usage() {
|
2024-12-05 21:58:44 +00:00
|
|
|
echo "Usage: format-drives.sh [--boot boot-partition-path] [--luks luks-partition-path] [--ask-root-password]"
|
|
|
|
echo "Options:"
|
|
|
|
echo " -h | --help Show this help screen."
|
|
|
|
echo " -b | --boot <path> The path to the boot drive (e.g. /dev/nvme0n1p1)."
|
|
|
|
echo " -l | --luks <path> The path to the partition to create the LUKS container on (e.g. /dev/nvme0n1p2)."
|
|
|
|
echo " -a | --ask-root-password Sets a password for the root user account."
|
|
|
|
exit 2
|
2024-07-04 16:27:00 +00:00
|
|
|
}
|
|
|
|
|
2024-09-05 20:47:53 +00:00
|
|
|
# Argument processing logic shamelessly stolen from https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
|
|
|
|
POSITIONAL_ARGS=()
|
2024-07-04 16:27:00 +00:00
|
|
|
while [[ $# -gt 0 ]]; do
|
|
|
|
case "$1" in
|
2024-12-05 21:58:44 +00:00
|
|
|
--ask-root-password|-a)
|
|
|
|
ask_root_password=true
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--boot|-b)
|
|
|
|
boot_partition="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--luks|-l)
|
|
|
|
luks_partition="$2"
|
|
|
|
shift
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--help|-h)
|
|
|
|
usage
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
POSITIONAL_ARGS+=("$1") # save positional arg
|
|
|
|
shift # past argument
|
|
|
|
;;
|
|
|
|
esac
|
2024-07-04 16:27:00 +00:00
|
|
|
done
|
|
|
|
|
2024-09-05 20:47:53 +00:00
|
|
|
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
|
2024-07-04 16:27:00 +00:00
|
|
|
|
2024-12-06 18:04:15 +00:00
|
|
|
cryptsetup luksOpen "$luks_partition" nixos-crypt
|
|
|
|
mkfs.btrfs -L nixos "$root_partition"
|
2024-07-04 16:27:00 +00:00
|
|
|
mount /dev/mapper/nixos-crypt /mnt
|
|
|
|
btrfs subvolume create /mnt/@
|
|
|
|
btrfs subvolume create /mnt/@home
|
|
|
|
btrfs subvolume create /mnt/@log
|
|
|
|
btrfs subvolume create /mnt/@nix
|
|
|
|
btrfs subvolume create /mnt/@swap
|
|
|
|
umount /mnt
|
|
|
|
|
2024-12-06 18:04:15 +00:00
|
|
|
mount -o subvol=@ "$root_partition" /mnt
|
2024-07-04 16:27:00 +00:00
|
|
|
mkdir -p /mnt/{boot,home,var/log,nix,swap}
|
2024-12-06 18:04:15 +00:00
|
|
|
mount "$boot_partition" /mnt/boot
|
|
|
|
mount -o subvol=@home "$root_partition" /mnt/home
|
|
|
|
mount -o subvol=@log "$root_partition" /mnt/var/log
|
|
|
|
mount -o subvol=@nix "$root_partition" /mnt/nix
|
|
|
|
mount -o subvol=@swap "$root_partition" /mnt/swap
|
2024-07-04 16:27:00 +00:00
|
|
|
echo "Disks partitioned and mounted to /mnt."
|
|
|
|
|
|
|
|
# Generate hardware-configuration.nix
|
|
|
|
nixos-generate-config --no-filesystems --dir /home/nixos
|
|
|
|
echo "Configuration files generated and saved to /home/nixos."
|
|
|
|
|
|
|
|
echo "Setup complete!"
|
|
|
|
echo "To install, set up your system's configuration files under ./hosts/yourHost and add it to flake.nix."
|
|
|
|
echo "Then, run the following command:"
|
|
|
|
echo "nixos-install --verbose --root /mnt --flake [path-to-flake.nix] $( (( ask_root_password == false )) && echo "--no-root-password" )"
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
|