Skip to content

Instantly share code, notes, and snippets.

@violetbp
Created November 10, 2025 00:15
Show Gist options
  • Select an option

  • Save violetbp/b32e6b34758d806d86bdc842252f1b44 to your computer and use it in GitHub Desktop.

Select an option

Save violetbp/b32e6b34758d806d86bdc842252f1b44 to your computer and use it in GitHub Desktop.
# destructively formats a computer to be used with nix with full disk encryption
# decided not to have parted not promtp for user input
set -euo pipefail
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" >&2
exit 1
fi
# only for nvme
DISK=`lsblk --nvme --noheadings --output NAME `
read -p "what will the hostname be:" host_name
# DISK="${1:-}"
# if [[ -z "$DISK" ]] || [[ ! -b "$DISK" ]]; then
# echo "Usage: $0 /dev/sdX" >&2
# exit 1
# fi
read -p "ALL DATA ON $DISK WILL BE DESTROYED. Continue? (type uppercase yes): " confirm
if [[ "$confirm" != "YES" ]]; then
echo "Aborting." >&2
exit 1
fi
ESP_SIZE="1024MiB"
BOOT_SIZE="1GiB"
SWAP_SIZE="8GiB"
echo "Creating GPT label and partitions on $DISK …"
parted --align optimal "$DISK" mklabel gpt
# ESP
parted --align optimal "$DISK" mkpart ESP fat32 1MiB "${ESP_SIZE}"
parted "$DISK" set 1 esp on
parted "$DISK" set 1 boot on
# /boot
START=$(parted -s "$DISK" unit MiB print | awk '/^ 1/{getline; print $2}' | tr -d 'MiB')
parted -s -a optimal "$DISK" mkpart boot ext4 "${START}MiB" "${BOOT_SIZE}"
# swap
START=$(parted -s "$DISK" unit MiB print | awk 'END{print $3}' | tr -d 'MiB')
END=$(awk -v s="$START" -v add="$SWAP_SIZE" 'BEGIN{print s+add}')
parted -s -a optimal "$DISK" mkpart swap linux-swap "${START}MiB" "${END}MiB"
# root
START="$END"
END=$(awk -v s="$START" 'BEGIN{print s+50*1024}') # 50 GiB
parted -s -a optimal "$DISK" mkpart root ext4 "${START}MiB" "${END}MiB"
# home (rest of disk)
START="$END"
parted -s -a optimal "$DISK" mkpart home ext4 "${START}MiB" 100%
echo "Formatting partitions …"
ESP_PART="${DISK}1"
BOOT_PART="${DISK}2"
SWAP_PART="${DISK}3"
ROOT_PART="${DISK}4"
HOME_PART="${DISK}5"
mkfs.fat -F 32 -n ESP "$ESP_PART"
mkfs.ext4 -L boot "$BOOT_PART"
mkswap -L swap "$SWAP_PART"
swapon "$SWAP_PART"
mkfs.ext4 -L root "$ROOT_PART"
mkfs.ext4 -L home "$HOME_PART"
echo "Done. Layout:"
parted -s "$DISK" print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment