iTranslated by AI
Secure Boot + LVM on LUKS in NixOS
Foreword
Motivation
This means I want to do the same thing on NixOS.
Q. Isn't the encryption that comes with the graphical installer enough?
A. No.
Reason:
The encryption available in the graphical installer uses LUKS1 (citation needed).
Addendum: As of Nov 24, it seems to have become LUKS2. Thanks for the comment, 34j.
Also, since it's not LVM on LUKS, you can't easily change the partition configuration (the debate on whether you need to change it is out of scope).
Challenges
- The author does not understand or use flakes or home-manager.
- In fact, the author doesn't even properly understand Nix itself.
- It hasn't been verified whether this is a smart way to do things (since n=1).
- systemd-homed cannot be used, so home directory encryption is not implemented.
Deliverables
# Converted to flake
Setup
Partitioning
cgdisk /dev/shX
I recommend using this for editing. It can be operated via a TUI.
Set up the configuration as follows:
| Partition Name | Partition Type | Partition ID | Recommended Size |
|---|---|---|---|
| boot | EFI system | ef00 | 1G |
| system | Linux LVM | 8e00 | All remaining |
The swap area will be created inside the LVM, so it is not needed here.
Allocate plenty of space for the boot partition, as we'll be adjusting it for Secure Boot!
Encrypting the System Partition
Next, we will encrypt the partition named "system" from the previous step.
# Format the partition using LUKS
cryptsetup luksFormat /dev/nvme0n1p2
# Open the encrypted container
cryptsetup open --type luks /dev/nvme0n1p2 cryptolvm
# The decrypted container is available at /dev/mapper/cryptolvm
With this, the system partition is now encrypted with LUKS.
Next, we will create partitions on top of it using LVM.
# Create a physical volume
pvcreate /dev/mapper/cryptolvm
# Create a volume group named MyVol and add the previously created physical volume
vgcreate MyVol /dev/mapper/cryptolvm
# Create logical volumes in the volume group
lvcreate -L 32G MyVol -n swap # swap area
lvcreate -l 100%FREE -n system MyVol # system area
Since I have 16GB of RAM, I adopted the "2x theory" and set the swap to 32GB.
Please use whatever value you prefer.
Formatting
Next, format the partitions we created.
# boot partition: format with fat32
mkfs.vfat -F32 /dev/nvme0n1p1
# root partition: format with ext4
mkfs.ext4 /dev/mapper/MyVol-system
# swap area
mkswap /dev/mapper/MyVol-swap
In this guide, I chose to use ext4, but you can format it with your preferred file system.
File systems like xfs are included in the archiso by default, so no extra installation is required.
Mounting
Mount the formatted partitions.
# Mount the system area to /mnt
mount /dev/mapper/MyVol-system /mnt
# Create boot directory
mkdir /mnt/boot
# Mount the boot partition to the boot directory
mount /dev/nvme0n1p1 /mnt/boot
# Enable the swap area
swapon /dev/mapper/MyVol-swap
Enabling Decryption of the Encrypted Partition at Boot
Generating the Configuration
nixos-generate-config --root /mnt
First, check the UUID of the encrypted partition.
lsblk -f
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
sda
└─sda1 vfat FAT32 NIXOS-MINIM 19EF-375D
nvme0n1
├─nvme0n1p1 vfat FAT32 3699-EB86 924.1M 10% /boot
└─nvme0n1p2 crypto_LUKS 2 9d15756c-f988-446c-ad3e-41af792d5e81
└─cryptlvm LVM2_member LVM2 001 sCNyoX-EzJF-UPQG-3XJ3-RMQT-wTFo-LmHBKG
├─MyVol-swap swap 1 3e35b190-57ae-4844-b6ec-2f22716d4a55 [SWAP]
└─MyVol-system ext4 1.0 fe96fe5c-ffeb-40e4-be64-e093b377b100 410.3G 4% /nix/store
It should look something like this.
In this case, please take note of "9d15756c-f988-446c-ad3e-41af792d5e81".
We will now edit configure.nix so that the bootloader can unlock the storage.
+ boot.initrd.services.lvm.enable = true;
+ boot.initrd.luks.devices = {
+ cryptlvm = {
+ device = "/dev/disk/by-uuid/9d15756c-f988-446c-ad3e-41af792d5e81";
+ };
+ };
Just add this, and you're good to go.
Configure any other settings as you like.
I can't explain the details in depth (as I don't fully understand NixOS yet).
Once configured, build NixOS and reboot.
nixos-install
reboot
After rebooting, register the key to decrypt the storage using TPM2.
systemd-cryptenroll /dev/nvme0n1p2 --wipe-slot=tpm2 --tpm2-device=auto --tpm2-pcrs=7
This completes the LVM on LUKS setup.
Enabling Secure Boot
I will proceed following this project. This is the quick start guide.
Generating Keys
sudo sbctl create-keys
Loading the Repository with niv
cd /etc/nixos/
niv init
niv add nix-community/lanzaboote -r v0.4.1 -v 0.4.1
After that, edit configuration.nix.
Below is the official sample code:
# file: configuration.nix
{ pkgs, lib, ... }:
+let
+ sources = import ./nix/sources.nix;
+ lanzaboote = import sources.lanzaboote;
+in
{
+ imports = [ lanzaboote.nixosModules.lanzaboote ];
environment.systemPackages = [
# For debugging and troubleshooting Secure Boot.
+ pkgs.sbctl
];
# Lanzaboote currently replaces the systemd-boot module.
# This setting is usually set to true in configuration.nix
# generated at installation time. So we force it to false
# for now.
- boot.loader.systemd-boot.enable = true;
+ boot.loader.systemd-boot.enable = lib.mkForce false;
+ boot.lanzaboote = {
+ enable = true;
+ pkiBundle = "/etc/secureboot";
+ };
}
Once you have finished editing configuration.nix:
nixos-rebuild switch
Rebuild it with this command.
Enter UEFI and set to Setup Mode
The method varies by motherboard, so I won't write it in detail.
Please be aware that some motherboards require a UEFI Password to enter Custom Setup Mode (I've been caught by this once).
Registering Keys to the Motherboard
Once NixOS starts up:
sudo sbctl enroll-keys --microsoft
Use this to register the keys to the motherboard.
After that, enter UEFI again and enable Secure Boot.
Secure Boot should now be enabled.
Afterword
This should work.
I really don't understand Nix well, so I'm not sure if this is the best way.
If there are any pointers, I will fix it.
Discussion
24.11で試してみたところ、LUKS2が使用されているっぽいです
コメントありがとうございます。
追記します。