Skip to content

Instantly share code, notes, and snippets.

@patrl
Last active November 16, 2022 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrl/ac3b91c0e29052669001ddb8077c148f to your computer and use it in GitHub Desktop.
Save patrl/ac3b91c0e29052669001ddb8077c148f to your computer and use it in GitHub Desktop.
#!/bin/sh
# exit as soon as any line in the script fails, print each command that's going to be executed
set -ex
# wipe disk
sgdisk --zap-all ${DISK}
# create GPT partition table
parted ${DISK} -- mklabel gpt
# create primary partition
parted ${DISK} -- mkpart primary 512MB -8GB
# make swap partition
parted ${DISK} -- mkpart primary linux-swap -8GB 100%
# make boot partition
parted ${DISK} -- mkpart ESP fat32 1MB 512MB
# turn on esp
parted ${DISK} -- set 3 esp on
# setup boot filesystem
mkfs.fat -F 32 -n BOOT ${DISK}-part3
# setup swap
mkswap -L swap ${DISK}-part2
swapon ${DISK}-part2
# create the zfs pool
# relevant options
# autotrim: automatic trim on occasion
# listsnapshots: includes snapshots in zfs list
# compression: zstd: use zstandard compression
zpool create -f \
-o ashift=12 \
-o autotrim=on \
-o listsnapshots=on \
-O acltype=posixacl \
-O compression=zstd \
-O dnodesize=auto \
-O normalization=formD \
-O mountpoint=none \
-O relatime=on \
-O xattr=sa \
rpool \ ${DISK}-part1
# create the encrypted root system container. See https://www.reddit.com/r/zfs/comments/bnvdco/zol_080_encryption_dont_encrypt_the_pool_root/
# don't mount
zfs create -p -v \
-o mountpoint=none \
-o encryption=on \
-o keylocation=prompt \
-o keyformat=passphrase \
rpool/nixos
# zfs dataset for nix store
zfs create -p -v \
-o relatime=off \
-o mountpoint=legacy \
-o com.sun:auto-snapshot=false \
rpool/nixos/nix
# zfs dataset for home
zfs create -p -v \
-o mountpoint=legacy \
-o secondarycache=none \
-o com.sun:auto-snapshot=true \
rpool/nixos/home
# zfs dataset for root
zfs create -p -v \
-o mountpoint=legacy \
-o secondarycache=none \
-o com.sun-auto-snapshot=true \
rpool/nixos/root
# It's not very useful to snapshot systemd logs, so that can have its
# own persistent dataset as well.
zfs create -p -v \
-o mountpoint=legacy \
-o secondarycache=none \
-o com.sun:auto-snapshot=false \
rpool/nixos/systemd-logs
# Finally, create an unused, unmounted 2 GB dataset in case the rest
# of the pool runs out of space and is unable to reclaim it (an
# unfortunate side effect of copy-on-write filesystems).
#
# If that happens, this dataset can be deleted, space can be
# reclaimed, and then it can be created again in case something
# similar happens in the future.
zfs create \
-o refreservation=2G \
-o primarycache=none \
-o secondarycache=none \
-o mountpoint=none \
rpool/nixos/reserved
# mount root dataset
mount -t zfs rpool/nixos/root /mnt
# mount efi partition
mkdir -p /mnt/boot
mount -t vfat ${DISK}-part3 /mnt/boot
# mount home dataset
mkdir -p /mnt/home
mount -t zfs rpool/nixos/home /mnt/home
# mount nixos store
mkdir -p /mnt/nix
mount -t zfs rpool/nixos/nix /mnt/nix
# mount log dataset
mkdir -p /mnt/var/log
mount -t zfs rpool/nixos/systemd-logs /mnt/var/log
nixos-generate-config --root /mnt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment