#!/bin/usr/env bash # https://nixos.org/manual/nixos/stable/#sec-installation-manual # https://wizardzines.com/comics/bash-errors/bash-errors.png set -euo pipefail exec >> >(tee -i /tmp/install.log) exec 2>&1 clear # Friendly introduction. echo "0. WELCOME" echo "Welcome to the NixOS installation script!" echo "This script will ERASE ALL DATA on the partition you will choose next!" echo read -p "Do you want to continue? Type [Y]es or [N]o. " -n 1 -r echo if [[ ! $REPLY =~ ^[Yy]$ ]]; then exit 1 fi clear # Selecting disk. echo "1. PARTITIONING" echo "Please select a disk to partition:" echo # Don't separate values by space. IFS=$'\n' # Set variable containing name and size of all disks. declare -a dsks=( $(lsblk -d | tail -n+2 | awk '{print $1" "$4}') ) # Select value on array. select dev in "${dsks[@]}" do break done echo # Separate values by spaces. IFS=' ' # Create new variable of selection. array=("${dev}") dev="${array[0]}" clear echo "2. ENCRYPTION" echo # Setting encryption password. echo "Choose a strong password for encrypting the primary partition:" pwcr="" while [[ -z "${pwcr}" ]]; do echo "Please enter a password: " read -rs pwfr read -rs -p "Retype a password: " pwsc if [[ "${pwfr}" == "${pwsc}" ]]; then pwcr="${pwfr}" echo echo "Both passwords are the same. Continuing.." break else echo echo "You have entered different passwords. Try again.." echo fi done clear echo "4. USER" echo # Setting username echo "Please enter a username:" read -r user echo echo "5. INSTALLING SYSTEM.." echo # Starting partitioning. echo "Partitioning /dev/${dev}.." echo # Clearing partition table of selected disk. echo "Clearing existing partitioning table." sudo parted "/dev/${dev}" -- mklabel gpt echo # Creating boot partition. echo "Creating boot partition of 512 MB." sudo parted "/dev/${dev}" -- mkpart ESP 1MB 512MB echo # Setting type for EFI. echo "Setting partition type." sudo parted "/dev/${dev}" -- set 1 esp on echo # Creating system partition. echo "Creating system partition." sudo parted "/dev/${dev}" -- mkpart 512MB 100% echo # Print partitions. echo "This is your new partition table:" lsblk | grep "${dev}" echo # Get new variable. if [[ "${dev}" = "nvme0n1" ]]; then main="${dev}p2" else main="${dev}2" fi # Encrypting partition. echo "Encrypting system partition. This might take a while." echo echo -en "${pwcr}\n${pwcr}" | sudo cryptsetup -c aes-xts-plain -s 512 luksFormat /dev/$main echo "Partition successfully encrypted." echo # Opening encrypted partition and mounting at /dev/mapper/main. echo "Decrypting.. This also might take a while." echo echo -en "${pwcr}\n${pwcr}" | sudo cryptsetup open "/dev/${main}" main echo "Partition successfully opened." echo lsblk | grep "${dev}" echo echo "Creating the filesystem." if [ "${dev}" = "nvme0n1" ]; then boot="${dev}p1" else boot="${dev}1" fi sudo mkfs.fat -F 32 -n UEFI "/dev/${boot}" echo "Filesystem for boot successfully created." echo # Creating btrfs partition. sudo mkfs.btrfs "/dev/mapper/main" sudo mount "/dev/mapper/main" "/mnt" sudo btrfs subvolume create "/mnt/root" sudo btrfs subvolume create "/mnt/home" sudo umount "/mnt" echo echo "Mounting.." sudo mount -o autodefrag,compress=zstd:3,subvol=root "/dev/mapper/main" "/mnt" sudo mkdir -p "/mnt/home" sudo mount -o autodefrag,compress=zstd:3,subvol=home "/dev/mapper/main" "/mnt/home" sudo mkdir -p "/mnt/boot" sudo mount -o umask=077 "/dev/${boot}" "/mnt/boot" echo "Mounting complete." echo lsblk -a echo # Copy existing config sudo nixos-generate-config --root /mnt sudo mv /mnt/etc/nixos/configuration.nix /mnt/etc/nixos/configuration.nix.bak cp configuration.nix /mnt/etc/nixos # Replace username in config sudo sed -i "s/username/${user}/g" /mnt/etc/nixos/configuration.nix # Install sudo nixos-install # Set user password sudo nixos-enter --root /mnt -c 'passwd $user'