NixOS installation (LUKS and BTRFS setup)

Context

I recently purchased a new computer to save weight during my commute : the Dell XPS 13 Plus, and a new computer equals a new way to procrastinate by installing yet another Linux distribution.

This time it is NixOS, I have used it on my servers and have wanted to use it on my laptop for some time, but I need my current laptop to function so I couldn’t just erase everything and start tinkering with NixOS.

I also decided to do a full-disk encryption on the new machine, and use btrfs as the filesystem for the underlying disk.

Thus, I merged installation details from https://nixos.wiki/wiki/Btrfs and https://gist.github.com/martijnvermaat/76f2e24d0239470dd71050358b4d5134.

Installation

Partitions

The XPS was shipped with Ubuntu already installed, and had three partitions :

  • /dev/nvme0n1p1 : the 815 MiB EFI partition
  • /dev/nvme0n1p2 : an 8 GiB partition for Microsoft (in case you want to install Windows)
  • /dev/nvme0n1p3 : the partition for Ubuntu, taking up the rest of the disk (~458 GiB)

I didn’t change the EFI partition as it is big enough, but it isn’t much more work to recreate everything.

Using gdisk :

$ gdisk /dev/nvme0n1
  • d 3 : delete the Ubuntu partition
  • d 2 : delete the Microsoft partition
  • n : add a new partition, the default sector selection fill out the remaining space and the type is 8300 for a Linux filesystem
  • w : write the new partition table and exits

LUKS & LVM

Create a new LUKS partition and open it :

$ cryptsetup luksFormat /dev/nvme0n1p2
$ cryptsetup open /dev/nvme0n1p2 enc-pv

Initiate the LVM structure :

$ pvcreate /dev/mapper/enc-pv
$ vgcreate vg /dev/mapper/enc-pv

Create a partition for the swap :

$ lvcreate -L 16G -n swap vg

Create the root partition on the remaining space :

$ lvcreate -l '100%FREE' -n root vg

Format the partitions :

$ mkswap -L swap /dev/vg/swap
$ mkfs.btrfs /dev/vg/root

Btrfs

Create the subvolumes for the root partitions :

$ mkdir -p /mnt
$ mount /dev/vg/root /mnt

$ btrfs subvolume create /mnt/root
$ btrfs subvolume create /mnt/home
$ btrfs subvolume create /mnt/nix

$ umount /mnt

NixOS installation

Mount the partitions and subvolumes :

$ mount -o compress=zstd,subvol=root /dev/vg/root /mnt

$ mkdir /mnt/{home,nix}

$ mount -o compress=zstd,subvol=home /dev/vg/root /mnt/home
$ mount -o compress=zstd,noatime,subvol=nix /dev/vg/root /mnt/nix

$ mkdir /mnt/boot

$ mount /dev/nvme0n1p1 /mnt/boot

$ swapon /dev/vg/swap

Generate the configuration :

$ nixos-generate-config --root /mnt

Configuration changes

By default, NixOS doesn’t recognize LUKS devices, so we need to add the following in the configuration :

  boot.initrd.luks.devices = {
    luksroot = {
      device = "/dev/disk/by-uuid/THE_UUID_OF_THE_LUKS_PARTITION";
      preLVM = true;
      allowDiscards = true;
    };
  };

The mount options for the filesystems are not detected automatically by the NixOS installer, so we need to add the following :

  fileSystems = {
    "/".options = [ "compress=zstd" ];
    "/home".options = [ "compress=zstd" ];
    "/nix".options = [ "compress=zstd" "noatime" ];
  };

Installation

The only thing left is to install NixOS and reboot.

$ nixos-install

$ reboot

Final configuration

The final NixOS configuration is located at https://git.hubrecht.ovh/hubrecht/nixos/src/branch/master/machines/dell-xps/_hw-cfg.nix .