Skip to content

Instantly share code, notes, and snippets.

@voidzero
Created April 15, 2023 18:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voidzero/fb5d188050db307ca8dc7a9550fad726 to your computer and use it in GitHub Desktop.
Save voidzero/fb5d188050db307ca8dc7a9550fad726 to your computer and use it in GitHub Desktop.
set -x
# Set a hashed root password, run mkpasswd -m SHA-512 and copy the hash here
ROOTPW=''
sgdisk --zap-all /dev/sda
sgdisk -a1 -n1:0:+100K -t1:EF02 -c 1:bootcode0 /dev/sda
sgdisk -n2:1M:+1G -t2:EF00 -c 2:efiboot0 /dev/sda
sgdisk -n3:0:+4G -t3:BE00 -c 3:nixos_zboot0 /dev/sda
sgdisk -n4:0:+2G -t4:8200 -c 4:swap0 /dev/sda
sgdisk -n5:0:+4G -t5:BF07 -c 5:log0 /dev/sda
sgdisk -n6:0:+8G -t6:BF08 -c 6:cache0 /dev/sda
sgdisk -n7:0:0 -t7:BF00 -c 7:nixos_zssd0 /dev/sda
partprobe /dev/sda
sleep 4
mkswap -L swapfs0 /dev/disk/by-partlabel/swap0
swapon /dev/disk/by-partlabel/swap0
sgdisk --zap-all /dev/sdb
sgdisk -a1 -n1:0:+100K -t1:EF02 -c 1:bootcode1 /dev/sdb
sgdisk -n2:1M:+1G -t2:EF00 -c 2:efiboot1 /dev/sdb
sgdisk -n3:0:+4G -t3:BE00 -c 3:nixos_zboot1 /dev/sdb
sgdisk -n4:0:+2G -t4:8200 -c 4:swap1 /dev/sdb
sgdisk -n5:0:+4G -t5:BF07 -c 5:log1 /dev/sdb
sgdisk -n6:0:+8G -t6:BF08 -c 6:cache1 /dev/sdb
sgdisk -n7:0:0 -t7:BF00 -c 7:nixos_zssd1 /dev/sdb
partprobe /dev/sdb
sleep 4
mkswap -L swapfs1 /dev/disk/by-partlabel/swap1
swapon /dev/disk/by-partlabel/swap1
ls -alFh --color /dev/disk/by-partlabel
zpool create -o compatibility=grub2 -o ashift=12 -o autotrim=on -O acltype=posixacl -O compression=lz4 -O devices=off -O normalization=formD -O relatime=on -O xattr=sa -O mountpoint=none -O checksum=sha256 -R /mnt zboot mirror /dev/disk/by-partlabel/nixos_zboot0 /dev/disk/by-partlabel/nixos_zboot1
zpool create -o ashift=12 -o autotrim=on -O acltype=posixacl -O compression=zstd -O dnodesize=auto -O normalization=formD -O relatime=on -O xattr=sa -O mountpoint=none -O checksum=edonr -R /mnt zssd mirror /dev/disk/by-partlabel/nixos_zssd0 /dev/disk/by-partlabel/nixos_zssd1
zpool create -o ashift=12 -o autotrim=on -O acltype=posixacl -O compression=zstd -O dnodesize=auto -O normalization=formD -O relatime=on -O xattr=sa -O mountpoint=none -O checksum=edonr -R /mnt zroot log mirror /dev/sd[ab]5 cache /dev/sd[ab]6 raidz1 /dev/sd[cdef]
zfs create zboot/nixos
zfs create -o mountpoint=/ zroot/nixos
zfs create zroot/nixos/home
zfs create zroot/nixos/keep
zfs create -o atime=off zroot/nixos/nix
zfs create zroot/nixos/root
zfs create zroot/nixos/usr
zfs create zroot/nixos/var
zfs create -o mountpoint=/boot zboot/nixos/boot
mkdir -p /mnt/mnt/tmp{1,2,3}
mkdir -p /mnt/etc/zfs/
rm -f /mnt/etc/zfs/zpool.cache
touch /mnt/etc/zfs/zpool.cache
chmod a-w /mnt/etc/zfs/zpool.cache
chattr +i /mnt/etc/zfs/zpool.cache
zfs snapshot zroot/nixos@SYSINIT
zfs snapshot zroot/nixos/usr@SYSINIT
zfs snapshot zroot/nixos/var@SYSINIT
mkfs.vfat -n EFI /dev/disk/by-partlabel/efiboot0
mkdir -p /mnt/boot/efis/efiboot0
mount -t vfat /dev/disk/by-partlabel/efiboot0 /mnt/boot/efis/efiboot0
mkfs.vfat -n EFI /dev/disk/by-partlabel/efiboot1
mkdir -p /mnt/boot/efis/efiboot1
mount -t vfat /dev/disk/by-partlabel/efiboot1 /mnt/boot/efis/efiboot1
mkdir /mnt/boot/efi
mount -t vfat /dev/disk/by-partlabel/efiboot0 /mnt/boot/efi
nixos-generate-config --root /mnt
DISK=(/dev/sda /dev/sdb)
MAINCFG="/mnt/etc/nixos/configuration.nix"
HWCFG="/mnt/etc/nixos/hardware-configuration.nix"
ZFSCFG="/mnt/etc/nixos/zfs.nix"
ZFS_BOOT="zboot"
ZFS_ROOT="zroot"
ZFS_ROOT_VOL="nixos"
EMPTYSNAP="SYSINIT"
IMPERMANENCE=1
sed -i -e "s|./hardware-configuration.nix|& ./zfs.nix|" ${MAINCFG}
if (( $IMPERMANENCE ))
then
echo '{ config, lib, pkgs, ... }:'
else
echo '{ config, pkgs, ... }:'
fi | tee -a ${ZFSCFG}
tee -a ${ZFSCFG} <<EOF
{
boot.supportedFilesystems = [ "zfs" ];
networking.hostId = "$(head -c 8 /etc/machine-id)";
boot.kernelPackages = config.boot.zfs.package.latestCompatibleLinuxPackages;
boot.zfs.devNodes = "/dev/disk/by-partlabel";
EOF
if (( $IMPERMANENCE ))
then
tee -a ${ZFSCFG} <<EOF
boot.initrd.postDeviceCommands = lib.mkAfter ''
zfs rollback -r ${ZFS_ROOT}/${ZFS_ROOT_VOL}@${EMPTYSNAP}
'';
EOF
fi
# Remove boot.loader stuff, it's to be added to zfs.nix
sed -i '/boot.loader/d' ${MAINCFG}
# Disable xserver. Comment them without a space after the pound sign so we can
# recognize them when we edit the config later
sed -i -e 's;^ \(services.xserver\); #\1;' ${MAINCFG}
tee -a ${ZFSCFG} <<-'EOF'
boot.loader.efi.efiSysMountPoint = "/boot/efi";
boot.loader.efi.canTouchEfiVariables = false;
boot.loader.generationsDir.copyKernels = true;
boot.loader.grub.efiInstallAsRemovable = true;
boot.loader.grub.enable = true;
boot.loader.grub.version = 2;
boot.loader.grub.copyKernels = true;
boot.loader.grub.efiSupport = true;
boot.loader.grub.zfsSupport = true;
boot.loader.grub.extraPrepareConfig = ''
mkdir -p /boot/efis
for i in /boot/efis/*; do mount $i ; done
mkdir -p /boot/efi
mount /boot/efi
'';
boot.loader.grub.extraInstallCommands = ''
ESP_MIRROR=$(mktemp -d)
cp -r /boot/efi/EFI $ESP_MIRROR
for i in /boot/efis/*; do
cp -r $ESP_MIRROR/EFI $i
done
rm -rf $ESP_MIRROR
'';
boot.loader.grub.devices = [
EOF
for d in ${DISK[*]}; do
printf " \"${d}\"\n" >>${ZFSCFG}
done
tee -a ${ZFSCFG} <<EOF
];
EOF
sed -i 's|fsType = "zfs";|fsType = "zfs"; options = [ "zfsutil" "X-mount.mkdir" ];|g' ${HWCFG}
ADDNR=$(awk '/^ fileSystems."\/" =$/ {print NR+3}' ${HWCFG})
sed -i "${ADDNR}i"' \ neededForBoot = true;' ${HWCFG}
ADDNR=$(awk '/^ fileSystems."\/boot" =$/ {print NR+3}' ${HWCFG})
sed -i "${ADDNR}i"' \ neededForBoot = true;' ${HWCFG}
if (( $IMPERMANENCE ))
then
# Of course we want to keep the config files after the initial
# reboot. So, create a bind mount from /keep/etc/nixos -> /etc/nixos
# here, and copy the files and actually mount the bind later
ADDNR=$(awk '/^ swapDevices =/ {print NR-1}' ${HWCFG})
TMPFILE=$(mktemp)
head -n ${ADDNR} ${HWCFG} > ${TMPFILE}
tee -a ${TMPFILE} <<EOF
fileSystems."/etc/nixos" =
{ device = "/keep/etc/nixos";
fsType = "none";
options = [ "bind" ];
};
EOF
ADDNR=$(awk '/^ swapDevices =/ {print NR}' ${HWCFG})
tail -n +${ADDNR} ${HWCFG} >> ${TMPFILE}
cat ${TMPFILE} > ${HWCFG}
rm -f ${TMPFILE}
unset ADDNR TMPFILE
fi
tee -a ${ZFSCFG} <<EOF
users.users.root.initialHashedPassword = "${ROOTPW}";
}
EOF
if (( $IMPERMANENCE ))
then
# This is where we copy the config files and mount the bind
install -d -m 0755 /mnt/keep/etc
cp -a /mnt/etc/nixos /mnt/keep/etc/
mount -o bind /mnt/keep/etc/nixos /mnt/etc/nixos
fi
set +x
echo "Now do this (preferably in another shell, this will put out a lot of text):"
echo "nixos-install -v --show-trace --no-root-passwd --root /mnt"
echo "umount -Rl /mnt"
echo "zpool export -a"
echo "swapoff -a"
echo "reboot"
echo "Make note of these instructions because the nixos-install command will output a lot of text."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment