Skip to content

Instantly share code, notes, and snippets.

@con-f-use
Last active October 27, 2025 18:20
Show Gist options
  • Save con-f-use/82cc8d926615e778abad44cc99a1e04a to your computer and use it in GitHub Desktop.
Save con-f-use/82cc8d926615e778abad44cc99a1e04a to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
prt:all() {
prt:part "$@"
prt:lvm
prt:fs
prt:mnt
}
prt:part() {
local disc
disc=${1:?Error: need disk device as first arg}
# Note: partition 1 bios_grub is a hack so we can be compatible
# ...with legacy bios AND use GPT partition tables.
parted --align optimal --fix --script "$disc" \
mklabel gpt \
mkpart primary 1MB 3MB \
set 1 bios_grub on \
mkpart eps fat32 3MB 2GB \
name 2 boot \
set 2 boot on \
mkpart primary 2GB 100% \
name 3 system \
set 3 lvm on
}
prt:lvm() {
pvcreate /dev/disk/by-partlabel/system
vgcreate storage /dev/disk/by-partlabel/system
lvcreate -l 100%FREE -n root storage /dev/disk/by-partlabel/system
}
prt:fs() {
mkfs.fat -F 32 /dev/disk/by-partlabel/boot -n BOOT
mkfs.btrfs /dev/storage/root -L system -f
sleep 3; sync || true
mount -t btrfs /dev/disk/by-label/system /mnt/ -o "$btrfsopts"
btrfs subvolume create /mnt/nix
btrfs subvolume create /mnt/nixos
btrfs subvolume create /mnt/home
btrfs subvolume create /mnt/log
btrfs subvolume set-default /mnt/nixos
umount /mnt/
}
prt:mnt() {
mount -t btrfs /dev/disk/by-label/system /mnt/ -o "$btrfsopts"
mkdir -p /mnt/{var/log,nix,home,boot}
chattr +i /mnt/{var/log,nix,home}
mount -t btrfs /dev/disk/by-label/system /mnt/var/log -o "subvol=/log"
mount -t btrfs /dev/disk/by-label/system /mnt/nix -o "subvol=/nix"
mount -t btrfs /dev/disk/by-label/system /mnt/home -o "subvol=/home"
mount /dev/disk/by-label/BOOT /mnt/boot
}
btrfsopts='autodefrag,space_cache=v2,clear_cache,compress=zstd'
if [ "$0" = "${BASH_SOURCE[0]}" ]; then
set -o errexit -o errtrace -o pipefail -o nounset -o xtrace
cmd=${1:?Need cmd}; shift
prt:"$cmd" "$@"
fi
@con-f-use
Copy link
Author

con-f-use commented Oct 27, 2025

Corresponding fileSystems NixOS config:

  fileSystems =
    let
      device = "/dev/mapper/storage-root";
      fsType = "btrfs";
      btrfsopts = [ "autodefrag" "noatime" "space_cache=v2" "clear_cache" "compress=zstd" ];
    in
    {
      "/" = {
        inherit device fsType;
        options = [ "subvol=nixos" ] ++ btrfsopts;
      };

      "/var/log" = {
        inherit device fsType;
        options = [ "subvol=log" ] ++ btrfsopts;
      };

      "/nix" = {
        inherit device fsType;
        options = [ "subvol=nix" ] ++ btrfsopts;
      };

      "/home" = {
        inherit device fsType;
        options = [ "subvol=home" ] ++ btrfsopts;
      };

      "/boot" = {
        device = "/dev/disk/by-label/BOOT";
        fsType = "vfat";
        options = [ "fmask=0022" "dmask=0022" ];
      };
    };
 
  boot.loader.systemd-boot.enable = true;
  boot.loader.efi.canTouchEfiVariables = true;
  boot.supportedFilesystems = [ "btrfs" "reiserfs" "vfat" "f2fs" "xfs" "ntfs" "nfs" "cifs" ];
  hardware.enableAllFirmware = true;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment