diff options
| author | Kleidi Bujari <mail@4kb.net> | 2025-04-05 14:58:10 -0400 |
|---|---|---|
| committer | Kleidi Bujari <mail@4kb.net> | 2025-04-05 14:58:10 -0400 |
| commit | 179bf31d5edc90c826dacf0b06b149469d8d14fa (patch) | |
| tree | 28a07fad01cce7627a2f332094e4a5261c8e5430 /packages/partitioner.nix | |
| parent | bf4d4b2a330c03373f75942b9b1723a67f03a709 (diff) | |
| download | depot-179bf31d5edc90c826dacf0b06b149469d8d14fa.tar.gz depot-179bf31d5edc90c826dacf0b06b149469d8d14fa.tar.bz2 depot-179bf31d5edc90c826dacf0b06b149469d8d14fa.zip | |
automate hosts gen and bump nixpkgs
Diffstat (limited to 'packages/partitioner.nix')
| -rw-r--r-- | packages/partitioner.nix | 95 |
1 files changed, 72 insertions, 23 deletions
diff --git a/packages/partitioner.nix b/packages/partitioner.nix index cd36f5d..df9f37d 100644 --- a/packages/partitioner.nix +++ b/packages/partitioner.nix @@ -1,33 +1,82 @@ # Apply standard partition layout to a disk device. { pkgs, ... }: -pkgs.writeShellScriptBin "partitioner" '' - set -e +pkgs.writeShellApplication { + name = "partitioner"; - if [ $# -ne 1 ]; then - echo "Usage: $0 <disk-device>" - exit 1 - fi + runtimeInputs = with pkgs; [ + gptfdisk + dosfstools + zfs + coreutils + util-linux + ]; - DISK=$1 + text = '' + set -euo pipefail - # Clear existing partitions - sgdisk -Z "$DISK" + if [ $# -ne 1 ]; then + echo "Usage: $0 <device>" + echo "Example: $0 /dev/sda or /dev/nvme0n1" + exit 1 + fi - # 1GB EFI partition (FAT32) - sgdisk -n 1:0:+1G -t 1:EF00 -c 1:EFI "$DISK" + DISK="$1" - # Rest for ZFS - sgdisk -n 2:0:0 -t 2:BF01 -c 2:ZFS "$DISK" + if [ ! -b "$DISK" ]; then + echo "Error: $DISK is not a valid block device" + exit 1 + fi - mkfs.fat -F32 -nEFI "" + echo "creating partition table on $DISK" + # create gpt partition table + sgdisk --zap-all "$DISK" - zpool create \ - -o ashift=12 \ - -O compression=on \ - -O acltype=posixacl \ - -O xattr=sa \ - -O relatime=off - -O normalization=formD \ - zroot "$DISK" -'' + echo "creating efi partition" + sgdisk --new=1:0:+1G --typecode=1:EF00 --change-name=1:ESP "$DISK" + + echo "creating zfs partition with remaining space" + sgdisk --new=2:0:0 --typecode=2:BF00 --change-name=2:ZROOT "$DISK" + + if [[ "$DISK" == *"nvme"* ]]; then + EFI_PART="''${DISK}p1" + ZFS_PART="''${DISK}p2" + else + EFI_PART="''${DISK}1" + ZFS_PART="''${DISK}2" + fi + + sleep 1 + + echo "formatting rfi partition ($EFI_PART)" + mkfs.fat -F32 -n ESP "$EFI_PART" + + echo "creating pool on $ZFS_PART" + zpool create -f \ + -o ashift=12 \ + -o autotrim=on \ + -O acltype=posixacl \ + -O canmount=off \ + -O compression=on \ + -O mountpoint=none \ + -O normalization=formD \ + -O relatime=off \ + -O xattr=sa \ + zroot "$ZFS_PART" + + # create ZFS datasets + echo "Creating datasets" + + zfs create \ + -o refreservation=10G \ + -o mountpoint=none \ + zroot/reserved + + zfs create -o mountpoint=legacy zroot/nix + zfs create -o mountpoint=legacy zroot/home + zfs create -o mountpoint=legacy zroot/persist + + echo "---" + echo "done" + ''; +} |
