aboutsummaryrefslogtreecommitdiff
path: root/nixos/modules/autounlock.nix
blob: 244dcb42fcfb2bfd2178a21aa91351af74128b56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{
  config,
  lib,
  pkgs,
  ...
}:
with lib; let
  cnf = config.cynerd.autounlock;
in {
  options = {
    cynerd.autounlock = mkOption {
      type = with types; attrsOf str;
      default = {};
      description = "Devices to be auto-unlocked.";
    };
  };

  config = mkIf (cnf != {}) {
    environment.systemPackages = [pkgs.luks-hw-password];
    boot.initrd = {
      extraFiles."/luks-hw-password".source = pkgs.luks-hw-password;
      luks.devices =
        mapAttrs (name: value: {
          device = value;
          keyFile = "/keys/${name}.key";
          fallbackToPassword = true;
          preOpenCommands = ''
            mkdir -p /keys
            /luks-hw-password/bin/luks-hw-password > /keys/${name}.key
          '';
          postOpenCommands = ''
            rm -rf /keys
          '';
        })
        cnf;
    };
  };
}