1 { config, lib, pkgs, ... }:
3 cfg = config.boot.initrd.clevis;
4 systemd = config.boot.initrd.systemd;
5 supportedFs = [ "zfs" "bcachefs" ];
8 meta.maintainers = with lib.maintainers; [ julienmalka camillemndn ];
9 meta.doc = ./clevis.md;
12 boot.initrd.clevis.enable = lib.mkEnableOption "Clevis in initrd";
15 boot.initrd.clevis.package = lib.mkOption {
16 type = lib.types.package;
17 default = pkgs.clevis;
18 defaultText = "pkgs.clevis";
19 description = "Clevis package";
22 boot.initrd.clevis.devices = lib.mkOption {
23 description = "Encrypted devices that need to be unlocked at boot using Clevis";
25 type = lib.types.attrsOf (lib.types.submodule ({
26 options.secretFile = lib.mkOption {
27 description = "Clevis JWE file used to decrypt the device at boot, in concert with the chosen pin (one of TPM2, Tang server, or SSS).";
28 type = lib.types.path;
33 boot.initrd.clevis.useTang = lib.mkOption {
34 description = "Whether the Clevis JWE file used to decrypt the devices uses a Tang server as a pin.";
36 type = lib.types.bool;
41 config = lib.mkIf cfg.enable {
43 # Implementation of clevis unlocking for the supported filesystems are located directly in the respective modules.
46 assertions = (lib.attrValues (lib.mapAttrs
48 assertion = (lib.any (fs: fs.device == device && (lib.elem fs.fsType supportedFs) || (fs.fsType == "zfs" && lib.hasPrefix "${device}/" fs.device)) config.system.build.fileSystems) || (lib.hasAttr device config.boot.initrd.luks.devices);
50 No filesystem or LUKS device with the name ${device} is declared in your configuration.'';
56 if cfg.useTang && !config.boot.initrd.network.enable && !config.boot.initrd.systemd.network.enable
57 then [ "In order to use a Tang pinned secret you must configure networking in initrd" ]
61 extraUtilsCommands = lib.mkIf (!systemd.enable) ''
62 copy_bin_and_libs ${pkgs.jose}/bin/jose
63 copy_bin_and_libs ${pkgs.curl}/bin/curl
64 copy_bin_and_libs ${pkgs.bash}/bin/bash
66 copy_bin_and_libs ${pkgs.tpm2-tools}/bin/.tpm2-wrapped
67 mv $out/bin/{.tpm2-wrapped,tpm2}
68 cp {${pkgs.tpm2-tss},$out}/lib/libtss2-tcti-device.so.0
70 copy_bin_and_libs ${cfg.package}/bin/.clevis-wrapped
71 mv $out/bin/{.clevis-wrapped,clevis}
73 for BIN in ${cfg.package}/bin/clevis-decrypt*; do
74 copy_bin_and_libs $BIN
77 for BIN in $out/bin/clevis{,-decrypt{,-null,-tang,-tpm2}}; do
78 sed -i $BIN -e 's,${pkgs.bash},,' -e 's,${pkgs.coreutils},,'
81 sed -i $out/bin/clevis-decrypt-tpm2 -e 's,tpm2_,tpm2 ,'
84 secrets = lib.mapAttrs' (name: value: lib.nameValuePair "/etc/clevis/${name}.jwe" value.secretFile) cfg.devices;
87 extraBin = lib.mkIf systemd.enable {
88 clevis = "${cfg.package}/bin/clevis";
89 curl = "${pkgs.curl}/bin/curl";
92 storePaths = lib.mkIf systemd.enable [
94 "${pkgs.jose}/bin/jose"
95 "${pkgs.curl}/bin/curl"
96 "${pkgs.tpm2-tools}/bin/tpm2_createprimary"
97 "${pkgs.tpm2-tools}/bin/tpm2_flushcontext"
98 "${pkgs.tpm2-tools}/bin/tpm2_load"
99 "${pkgs.tpm2-tools}/bin/tpm2_unseal"