##/bin/bash
# NixOS Installer which:
#  - partitions the drive
#  - creates the pool and datasets
#  - generates the system configuration in /mnt/etc/nixos/configuration.nix

# Always use the by-id aliases for devices, otherwise ZFS can choke on imports.
#DISK=/dev/disk/by-id/ata_
DISK=$1
# Partition 2 will be the boot partition, needed for legacy (BIOS) boot
sudo sgdisk -a1 -n2:34:2047 -t2:EF02 $DISK 
# If you need EFI support, make an EFI partition:
#sgdisk -n3:1M:+512M -t3:EF00 $DISK
# Partition 1 will be the main ZFS partition, using up the remaining space on the drive.
sudo sgdisk -n1:0:0 -t1:BF01 $DISK

# Create the pool. If you want to tweak this a bit and you're feeling adventurous, you
# might try adding one or more of the following additional options:
# To disable writing access times:
#   -O atime=off
# To enable filesystem compression:
#   -O compression=lz4
# To enable normalizing unicode filenames (and implicitly set utf8only=on):
#   -O normalization=formD
# To improve performance of certain extended attributes:
#   -O xattr=sa
# For systemd-journald posixacls are required
#   -O  acltype=posixacl 
# To specify that your drive uses 4K sectors instead of relying on the size reported
# by the hardware (note small 'o'):
#   -o ashift=12
#
# The 'mountpoint=none' option disables ZFS's automount machinery; we'll use the
# normal fstab-based mounting machinery in Linux.
# '-R /mnt' is not a persistent property of the FS, it'll just be used while we're installing.
sudo zpool create -O mountpoint=none -O compression=lz4 -o ashift=12 -R /mnt rpool $DISK-part1

# Create the filesystems. This layout is designed so that /home is separate from the root
# filesystem, as you'll likely want to snapshot it differently for backup purposes. It also
# makes a "nixos" filesystem underneath the root, to support installing multiple OSes if
# that's something you choose to do in future.
sudo zfs create -o mountpoint=none rpool/root
sudo zfs create -o mountpoint=legacy rpool/root/nixos
sudo zfs create -o mountpoint=legacy rpool/home

# Mount the filesystems manually. The nixos installer will detect these mountpoints
# and save them to /mnt/nixos/hardware-configuration.nix during the install process.
sudo mount -t zfs rpool/root/nixos /mnt
sudo mkdir /mnt/home
sudo mount -t zfs rpool/home /mnt/home

# If you need to boot EFI, you'll need to set up /boot as a non-ZFS partition.
#mkfs.vfat $DISK-part3
#mkdir /mnt/boot
#mount $DISK-part3 /mnt/boot

# Generate the NixOS configuration, as per the NixOS manual.
sudo nixos-generate-config --root /mnt
res=$?
echo "Command \'nixos-generate-config --root /mnt\' returned ${res}"
if [ $res == 0 ]
then
  echo "SUCCESS: /mnt/etc/nixos/configuration.nix WRITTEN!"
else
  echo "ERROR: !"
fi
# Edit /mnt/etc/nixos/configuration.nix and add the following line:
## ---8<-------------------------8<---
#  boot.supportedFilesystems = [ "zfs" ];
## ---8<-------------------------8<---

# Also, make sure you set the networking.hostId option, which ZFS requires:
## ---8<-------------------------8<---
#  networking.hostId = "<random 8-digit hex string>"
## ---8<-------------------------8<---
# See https://nixos.org/nixos/manual/options.html#opt-networking.hostId for more.

# Continue with installation!
#nixos-install 
