at master 2.9 kB view raw
1#!/usr/bin/env bash 2 3set -euo pipefail 4 5usage() { 6 echo "Usage: $0 <disk> <hostname> [-c <config-url>] [-y]" 7 echo " -h <hostname> : Required hostname for the new system" 8 echo " -c <config-url> : Optional URL to a NixOS configuration (default: https://github.com/RyanGibb/nixos.git)" 9 echo " -y : Run non-interactively (skip all prompts)" 10 exit 1 11} 12 13if [ "$EUID" -ne 0 ] 14 then echo "Please run as root" 15 exit 16fi 17 18URL="https://github.com/RyanGibb/nixos.git" 19AUTO_YES=false 20 21if [ $# -lt 2 ]; then 22 usage 23fi 24 25DISK=$1 26HOST=$2 27shift 2 28 29while getopts ":c:y" opt; do 30 case $opt in 31 c) URL="$OPTARG" ;; 32 y) AUTO_YES=true ;; 33 *) usage ;; 34 esac 35done 36 37PART_SUFFIX="" 38if [[ "$DISK" == *nvme* ]]; then 39 PART_SUFFIX="p" 40fi 41 42echo "Partitioning $DISK..." 43parted "$DISK" -- mklabel gpt 44parted "$DISK" -- mkpart ESP fat32 1MB 512MB 45parted "$DISK" -- mkpart primary 512MiB 100% 46parted "$DISK" -- set 1 esp on 47 48echo "Making file systems..." 49mkfs.fat -F 32 -n boot "${DISK}${PART_SUFFIX}1" 50mkfs.ext4 -L nixos "${DISK}${PART_SUFFIX}2" 51 52echo "Mounting partitions..." 53mount "${DISK}${PART_SUFFIX}2" /mnt 54mkdir -p /mnt/boot 55mount "${DISK}${PART_SUFFIX}1" /mnt/boot 56 57if [ "$AUTO_YES" = false ]; then 58 echo 59 echo "Disk partitioning and mounting complete." 60 echo "The following partitions have been created and mounted on $DISK:" 61 echo " - ${DISK}${PART_SUFFIX}1 (ESP, FAT32, /boot) mounted at /mnt/boot" 62 echo " - ${DISK}${PART_SUFFIX}2 (Primary, ext4, /) mounted at /mnt" 63 echo 64 echo "Do you want to use the NixOS configuration included in this ISO or from a remote git repository?" 65 echo "1) Use local" 66 echo "2) Use remote: $CONFIG_URL" 67 read -r -p "Select an option [1/2] (default is 2): " config_choice 68 config_choice=${config_choice:-2} 69else 70 config_choice=2 71fi 72 73mkdir -p /mnt/etc/nixos/ 74if [[ "$config_choice" == "1" ]]; then 75 echo "Copying local configuration to /mnt/etc/nixos ..." 76 cp -R /home/nixos/nixos /mnt/etc/nixos/ 77else 78 echo "Cloning $URL to /mnt/etc/nixos ..." 79 git clone "$URL" /mnt/etc/nixos 80fi 81 82echo "Generating configuration at /mnt/etc/nixos/hosts/$HOST ..." 83mkdir -p /mnt/etc/nixos/hosts/"$HOST" 84nixos-generate-config --show-hardware-config >> /mnt/etc/nixos/hosts/"$HOST"/hardware-configuration.nix 85(cd /mnt/etc/nixos/hosts/"$HOST"; nix flake init -t /mnt/etc/nixos#host) 86 87if [ "$AUTO_YES" = false ]; then 88 echo 89 read -r -p "Do you want to continue with the NixOS installation? [y/N] " response 90 if [[ ! "$response" =~ ^[Yy]$ ]]; then 91 echo "Installation aborted by user." 92 exit 0 93 fi 94fi 95 96echo "Installing NixOS..." 97nixos-install --root /mnt/ --flake /mnt/etc/nixos#"$HOST" 98 99if [ "$AUTO_YES" = false ]; then 100 echo 101 read -r -p "Installation is complete. Do you want to reboot the system now? [y/N] " response 102 if [[ ! "$response" =~ ^[Yy]$ ]]; then 103 echo "Reboot canceled by user. You may continue using the system." 104 exit 0 105 fi 106fi 107 108reboot