Skip to content

Instantly share code, notes, and snippets.

@Diti
Last active August 16, 2021 12:11
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 Diti/7114253dfe25d3430e8a8ecc83be20cc to your computer and use it in GitHub Desktop.
Save Diti/7114253dfe25d3430e8a8ecc83be20cc to your computer and use it in GitHub Desktop.
Setup paranoid NixOS on Samsung T7
#!/bin/sh
#
# This script sets ups a LiveCD-like system on my specific Samsung T7 disk.
# It does not make much effort to be idempotent (I wouldn’t use a shell script for that).
# Inspired by https://grahamc.com/blog/erase-your-darlings
DRIVE='/dev/disk/by-id/usb-Samsung_PSSD_T7_S5T4NS0R602432N-0:0'
drive_exists() {
if [ -e "$DRIVE" ]; then
return 0
else
>&2 echo 'Drive not found. Exiting.'
exit $?
fi
}
drive_exists && >&2 echo 'Samsung T7 drive found, all further operations will operate on it.'
printf "Zap all GPT and MBR data structures? [y/N] "
read -r REPLY
if [ "$REPLY" = 'Y' ] || [ "$REPLY" = 'y' ]; then
drive_exists && sgdisk --zap-all "$DRIVE"
else
>&2 echo 'Not touching anything. Exiting.'
exit 0
fi
# Partitions: boot + swap + ZFS pool
drive_exists && sgdisk --new=0:0:+550MiB --typecode=0:ef00 --change-name=0:boot "$DRIVE"
drive_exists && sgdisk --new=0:0:+16GiB --typecode=0:8200 --change-name=0:swap "$DRIVE"
drive_exists && sgdisk --new=0:0:0 --typecode=0:bf01 --change-name=0:zfs "$DRIVE"
DEV_BOOT="${DRIVE}-part1"
DEV_SWAP="${DRIVE}-part2"
DEV_ZFSPOOL="${DRIVE}-part3"
sleep 1 # Give sgdisk time to finish
# ZFS pools
drive_exists && zpool create -f rpool "$DEV_ZFSPOOL"
drive_exists && zfs create -p -o mountpoint=legacy rpool/local/root
drive_exists && zfs create -p -o mountpoint=legacy rpool/local/boot
drive_exists && zfs create -p -o mountpoint=legacy rpool/local/nix
drive_exists && zfs create -p -o mountpoint=legacy rpool/safe/home
drive_exists && zfs create -p -o mountpoint=legacy rpool/safe/persist
drive_exists && zfs snapshot rpool/local/root@blank
# Create filesystems
drive_exists && mkfs.vfat "$DEV_BOOT"
drive_exists && mkswap --label swap "$DEV_SWAP" && swapon "$DEV_SWAP"
sleep 1 # Give swapon time to finish
# Mountable directories
mkdir --parents /mnt/boot
mkdir --parents /mnt/nix
mkdir --parents /mnt/home
mkdir --parents /mnt/persist
# Mounts (EFI needs non-ZFS type, like FAT32, to boot)
test -d /mnt && mount --types zfs rpool/local/root /mnt
test -d /mnt/boot && mount "$DEV_BOOT" /mnt/boot
test -d /mnt/nix && mount --types zfs rpool/local/nix /mnt/nix
test -d /mnt/home && mount --types zfs rpool/safe/home /mnt/home
test -d /mnt/persist && mount --types zfs rpool/safe/persist /mnt/persist
nixos-generate-config --root /mnt
echo "\
Now edit
/mnt/etc/nixos/configuration.nix
and add at least the following (also import lib at top of file):
boot = {
initrd = {
postDeviceCommands = lib.mkAfter ''
zfs rollback -r rpool/local/root@blank
'';
supportedFilesystems = [ \"zfs\" ];
};
kernelParams = [ \"nohibernate\" ]; # https://github.com/openzfs/zfs/issues/260
supportedFilesystems = [ \"zfs\" ];
};
networking.hostId = \"$(head -c 8 /etc/machine-id)\";
networking.networkmanager.enable = true;
services.zfs.trim.enable = true;
"
echo 'And then run:'
echo 'nixos-install'
# zpool export rpool # Needed if zpool was created in the installer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment