Skip to content

Instantly share code, notes, and snippets.

@giuseppe998e
Last active April 29, 2024 18:15
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save giuseppe998e/629774863b149521e2efa855f7042418 to your computer and use it in GitHub Desktop.
Save giuseppe998e/629774863b149521e2efa855f7042418 to your computer and use it in GitHub Desktop.
Install NixOS with BTRFS and IN-RAM root

Install NixOS with BTRFS and IN-RAM root

1. Format and partition the hard drive

  1. Create the GPT partition table
    • $ parted /dev/sdX mklabel gpt
  2. Create the UEFI FAT32 partition (which will be /dev/sdXY)
    • $ parted /dev/sdX mkpart esp fat32 1MiB 512MiB
    • $ parted /dev/sdX set 1 esp on
    • $ parted /dev/sdX set 1 boot on
    • $ mkfs.fat -F 32 -n UEFI /dev/sdXY
  3. Create the SWAP partition (which will be /dev/sdXW) (optional)
    • $ parted /dev/sdX mkpart swap linux-swap 512MiB 4.5GiB
    • $ mkswap -L SWAP /dev/sdXW
  4. Create the NIXOS BTRFS partition (which will be /dev/sdXZ)
    • $ parted /dev/sdX mkpart nixos btrfs 4.5GiB 100%
    • $ mkfs.btrfs -L NIXOS /dev/sdXZ

P.S. The created partitions can be viewed using $ fdisk -l /dev/sdX.

2. Setup BTRFS subvolumes

  1. Mount the NIXOS partition
    • $ mount -t btrfs /dev/sdXZ /mnt
  2. Create the NIX partition subvolume
    • $ btrfs subvolume create /mnt/@home
  3. Create the HOME partition subvolume
    • $ btrfs subvolume create /mnt/@nix
  4. Unmount the NIXOS partition
    • $ umount /mnt

3. Mount the partitions for installation

  1. Mount the in-ram ROOT partition
    • $ mount -t tmpfs -o noatime,mode=755 none /mnt
  2. Create persistent directories on which to mount partitions
    • $ mkdir /mnt/{boot,nix,home}
  3. Mount the UEFI partition
    • $ mount -t vfat -o defaults,noatime /dev/sdXY /mnt/boot
  4. Mount the NIX partition subvolume
    • $ mount -t btrfs -o noatime,compress=zstd,subvol=@nix /dev/sdXZ /mnt/nix
  5. Mount the HOME partition subvolume
    • $ mount -t btrfs -o noatime,compress=zstd,subvol=@home /dev/sdXZ /mnt/home
  6. Create directories that will contain persistent system files
    • $ mkdir -p /mnt/{,nix/persist/}{etc/nixos,var/log}
  7. Bind system directories to their NIX persistent counterparts
    • $ mount --bind /mnt/nix/persist/etc/nixos /mnt/etc/nixos
    • $ mount --bind /mnt/nix/persist/var/log /mnt/var/log
  8. Mount the SWAP partition (optional)
    • $ swapon /dev/sdXW

4. Generate NixOS configs & install

  1. Let NixOS generate template configurations
    • $ nixos-generate-config --root /mnt
  2. Make sure all mount points in hardware-configuration.nix are identical to the previous section
    • $ vim /mnt/etc/nixos/hardware-configuration.nix
  3. Edit the configuration.nix file as needed
    • $ vim /mnt/etc/nixos/configuration.nix
      • Add machine-id file source link:
        environment.etc."machine-id".source = "/nix/persist/etc/machine-id";
      • Disable users mutability:
        users.mutableUsers = false;
      • Add user (hashed) password:
        (In another console: $ nix-shell --run 'mkpasswd -m SHA-512 -s' -p mkpasswd)
        users.users.*USERNAME*.initialHashedPassword = "*HASHED_PASSWORD*";
  4. Start the installer
    • $ nixos-install --no-root-passwd
    • $ reboot

This file is my personal summary based on these articles:

@FBIGlowie
Copy link

good stuff

@Mati365
Copy link

Mati365 commented Apr 14, 2024

Thank you

@TheMrGeeBee
Copy link

Thank you, but I get this error:

# nixos-generate-config --root /mnt
ERROR: Not a Btrfs subvolume: Invalid argument
ERROR: Not a Btrfs subvolume: Invalid argument
writing /mnt/etc/nixos/hardware-configuration.nix...
writing /mnt/etc/nixos/configuration.nix...
For more hardware-specific settings, see https://github.com/NixOS/nixos-hardware.

@TheMrGeeBee
Copy link

Found the error, need to:

btrfs subvolume create /mnt/@

to actually create the root volume.

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