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
32{
33 boot.initrd.luks.devices.crypted.device = "/dev/disk/by-uuid/3f6b0024-3a44-4fde-a43a-767b872abe5d";
34 fileSystems."/".device = "/dev/mapper/crypted";
35}
36```
37
38Should grub be used as bootloader, and `/boot` is located on an
39encrypted partition, it is necessary to add the following grub option:
40
41```nix
42{ boot.loader.grub.enableCryptodisk = true; }
43```
44
45## FIDO2 {#sec-luks-file-systems-fido2}
46
47NixOS also supports unlocking your LUKS-Encrypted file system using a FIDO2
48compatible token.
49
50### Without systemd in initrd {#sec-luks-file-systems-fido2-legacy}
51
52In the following example, we will create a new
53FIDO2 credential and add it as a new key to our existing device
54`/dev/sda2`:
55
56```ShellSession
57# export FIDO2_LABEL="/dev/sda2 @ $HOSTNAME"
58# fido2luks credential "$FIDO2_LABEL"
59f1d00200108b9d6e849a8b388da457688e3dd653b4e53770012d8f28e5d3b269865038c346802f36f3da7278b13ad6a3bb6a1452e24ebeeaa24ba40eef559b1b287d2a2f80b7
60
61# fido2luks -i add-key /dev/sda2 f1d00200108b9d6e849a8b388da457688e3dd653b4e53770012d8f28e5d3b269865038c346802f36f3da7278b13ad6a3bb6a1452e24ebeeaa24ba40eef559b1b287d2a2f80b7
62Password:
63Password (again):
64Old password:
65Old password (again):
66Added to key to device /dev/sda2, slot: 2
67```
68
69To ensure that this file system is decrypted using the FIDO2 compatible
70key, add the following to `configuration.nix`:
71
72```nix
73{
74 boot.initrd.luks.fido2Support = true;
75 boot.initrd.luks.devices."/dev/sda2".fido2.credential =
76 "f1d00200108b9d6e849a8b388da457688e3dd653b4e53770012d8f28e5d3b269865038c346802f36f3da7278b13ad6a3bb6a1452e24ebeeaa24ba40eef559b1b287d2a2f80b7";
77}
78```
79
80You can also use the FIDO2 passwordless setup, but for security reasons,
81you might want to enable it only when your device is PIN protected, such
82as [Trezor](https://trezor.io/).
83
84```nix
85{ boot.initrd.luks.devices."/dev/sda2".fido2.passwordLess = true; }
86```
87
88### systemd Stage 1 {#sec-luks-file-systems-fido2-systemd}
89
90If systemd stage 1 is enabled, it handles unlocking of LUKS-encrypted volumes
91during boot. The following example enables systemd stage1 and adds support for
92unlocking the existing LUKS2 volume `root` using any enrolled FIDO2 compatible
93tokens.
94
95```nix
96{
97 boot.initrd = {
98 luks.devices.root = {
99 crypttabExtraOpts = [ "fido2-device=auto" ];
100 device = "/dev/sda2";
101 };
102 systemd.enable = true;
103 };
104}
105```
106
107All tokens that should be used for unlocking the LUKS2-encrypted volume must
108first be enrolled using [systemd-cryptenroll](https://www.freedesktop.org/software/systemd/man/systemd-cryptenroll.html).
109In the following example, a new key slot for the first discovered token is
110added to the LUKS volume.
111
112```ShellSession
113# systemd-cryptenroll --fido2-device=auto /dev/sda2
114```
115
116Existing key slots are left intact, unless `--wipe-slot=` is specified. It is
117recommended to add a recovery key that should be stored in a secure physical
118location and can be entered wherever a password would be entered.
119
120```ShellSession
121# systemd-cryptenroll --recovery-key /dev/sda2
122```