Skip to content

Instantly share code, notes, and snippets.

@mbbx6spp
Last active September 17, 2021 02:14
Show Gist options
  • Save mbbx6spp/637acfd52369fc80f948466aba1a6941 to your computer and use it in GitHub Desktop.
Save mbbx6spp/637acfd52369fc80f948466aba1a6941 to your computer and use it in GitHub Desktop.
Encrypted ZFS for NixOS

Encrypted ZFS for NixOS

Assumptions

We will have add one partition to a zpool.

You have the following set to appropriate values:

  • disk: the path for your disk device, e.g. /dev/sdb or /dev/nvme0
  • efi: the path for the EFI boot partition’s device, e.g. /dev/sdb1
  • part: the path to the partition of your root filesystem, e.g. /dev/sdb2

Setup your partitions

First you will need to setup your partitions:

gdisk ${disk}

In my case I needed to ensure my EFI boot partition had the right type and I added a Linux partition:

  • /dev/sdb1 - EFI boot partition
  • /dev/sdb2 - Linux filesystem (referrd to as part from here on or interpolated using ${part})

Setup disk encryption

Using LUKS we will setup our block-level encryption:

cryptsetup luksFormat ${part}
cryptsetup luksOpen ${part} nixos

Create the ZFS pool

Find the UUID for the partition’s device:

declare uuid="$(lsblk -lno UUID ${part})"

Now create the ZFS pool:

zpool create \
      -o ashift=12 \
      -O mountpoint=none \
      zroot /dev/disk/by-uuid/${uuid}

zfs create zroot/root -o mountpoint=legacy

NixOS installer mounts

After you boot into the NixOS installer:

mount -t zfs zroot/root /mnt
mkdir /mnt/boot
mount ${efi} /mnt/boot

In case you need to remind yourself of the key you used to encrypt via LUKS at the block level:

dmsetup table --showkey nixos

Install NixOS

First generate the generic installer configs for your NixOS installation:

nixos-generate-config --root /mnt

TODO: add basic configuration.nix to download during installer.

@tomberek
Copy link

zpool create \
      -o ashift=12 \
      -O mountpoint=none \
      zroot /dev/disk/by-uuid/${uuid}

shouldn't that be: /dev/mapper/nixos ?

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