1# LUKS-Encrypted File Systems {#sec-luks-file-systems} 2 3NixOS supports file systems that are encrypted using *LUKS* (Linux 4Unified Key Setup). For example, here is how you create an encrypted 5Ext4 file system on the device 6`/dev/disk/by-uuid/3f6b0024-3a44-4fde-a43a-767b872abe5d`: 7 8```ShellSession 9# cryptsetup luksFormat /dev/disk/by-uuid/3f6b0024-3a44-4fde-a43a-767b872abe5d 10 11WARNING! 12======== 13This will overwrite data on /dev/disk/by-uuid/3f6b0024-3a44-4fde-a43a-767b872abe5d irrevocably. 14 15Are you sure? (Type uppercase yes): YES 16Enter LUKS passphrase: *** 17Verify passphrase: *** 18 19# cryptsetup luksOpen /dev/disk/by-uuid/3f6b0024-3a44-4fde-a43a-767b872abe5d crypted 20Enter passphrase for /dev/disk/by-uuid/3f6b0024-3a44-4fde-a43a-767b872abe5d: *** 21 22# mkfs.ext4 /dev/mapper/crypted 23``` 24 25The LUKS volume should be automatically picked up by 26`nixos-generate-config`, but you might want to verify that your 27`hardware-configuration.nix` looks correct. To manually ensure that the 28system is automatically mounted at boot time as `/`, add the following 29to `configuration.nix`: 30 31```nix 32boot.initrd.luks.devices.crypted.device = "/dev/disk/by-uuid/3f6b0024-3a44-4fde-a43a-767b872abe5d"; 33fileSystems."/".device = "/dev/mapper/crypted"; 34``` 35 36Should grub be used as bootloader, and `/boot` is located on an 37encrypted partition, it is necessary to add the following grub option: 38 39```nix 40boot.loader.grub.enableCryptodisk = true; 41``` 42 43## FIDO2 {#sec-luks-file-systems-fido2} 44 45NixOS also supports unlocking your LUKS-Encrypted file system using a 46FIDO2 compatible token. In the following example, we will create a new 47FIDO2 credential and add it as a new key to our existing device 48`/dev/sda2`: 49 50```ShellSession 51# export FIDO2_LABEL="/dev/sda2 @ $HOSTNAME" 52# fido2luks credential "$FIDO2_LABEL" 53f1d00200108b9d6e849a8b388da457688e3dd653b4e53770012d8f28e5d3b269865038c346802f36f3da7278b13ad6a3bb6a1452e24ebeeaa24ba40eef559b1b287d2a2f80b7 54 55# fido2luks -i add-key /dev/sda2 f1d00200108b9d6e849a8b388da457688e3dd653b4e53770012d8f28e5d3b269865038c346802f36f3da7278b13ad6a3bb6a1452e24ebeeaa24ba40eef559b1b287d2a2f80b7 56Password: 57Password (again): 58Old password: 59Old password (again): 60Added to key to device /dev/sda2, slot: 2 61``` 62 63To ensure that this file system is decrypted using the FIDO2 compatible 64key, add the following to `configuration.nix`: 65 66```nix 67boot.initrd.luks.fido2Support = true; 68boot.initrd.luks.devices."/dev/sda2".fido2.credential = "f1d00200108b9d6e849a8b388da457688e3dd653b4e53770012d8f28e5d3b269865038c346802f36f3da7278b13ad6a3bb6a1452e24ebeeaa24ba40eef559b1b287d2a2f80b7"; 69``` 70 71You can also use the FIDO2 passwordless setup, but for security reasons, 72you might want to enable it only when your device is PIN protected, such 73as [Trezor](https://trezor.io/). 74 75```nix 76boot.initrd.luks.devices."/dev/sda2".fido2.passwordLess = true; 77```