-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsecrets.nix
106 lines (91 loc) · 2.96 KB
/
secrets.nix
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
{ util, pkgs, config, lib, ... }:
with lib;
with util;
let
cfg = config.cookie.secrets;
secret = types.submodule ({ config, ... }: {
options = {
source = mkOption {
type = types.str;
description = "local secret path relative to the repo";
};
dest = mkOption {
type = types.str;
description = "where to write the decrypted secret to";
default = "/run/keys/${config._module.args.name}";
};
owner = mkOption {
type = types.str;
description = "who should own the secret";
default = "root";
};
group = mkOption {
type = types.str;
description = "what group should own the secret";
default = "root";
};
permissions = mkOption {
type = types.str;
description = "permissions expressed as octal";
default = "0000"; # lock it down if the user is being silly
example = "0400";
};
wantedBy = mkOption {
type = types.nullOr types.str;
description = "a systemd object that depends on this secret";
default = null;
};
runtime = mkOption {
# The encryption mechanism will not work on some unregistered files
# that can't be encrypted so we register them anyway
type = types.bool;
description = "whether this secret should be available at runtime";
default = true;
};
generateCommand = mkOption {
type = types.nullOr types.str;
description =
"a shell command to generate the secret if it does not already exist";
default = null;
};
};
});
mkService = name:
{ source, dest, owner, group, permissions, wantedBy, ... }: {
description = "decrypt secret for ${name}";
wantedBy = [ "multi-user.target" ]
++ optional (wantedBy != null) wantedBy;
serviceConfig.Type = "simple";
# This needs to be in preStart so if anyone depends on us
# we'll actually be done by the time systemd thinks we're "active".
preStart = with pkgs; ''
rm -f ${dest}
"${rage}"/bin/rage -d -i /etc/ssh/ssh_host_ed25519_key -o '${dest}' '${
./.. + "/encrypted/${config.networking.hostName}/${baseNameOf source}"
}'
chown '${owner}':'${group}' '${dest}'
chmod '${permissions}' '${dest}'
'';
script = ''
${optionalString (wantedBy != null) ''
# TODO: inspect the start reason for *this* -key unit to see whether this is appropiate
systemctl try-reload-or-restart ${escapeShellArg wantedBy}
''}
sleep infinity
'';
};
in {
options.cookie.secrets = mkOption {
type = types.attrsOf secret;
description = "secret configuration";
default = { };
};
config = {
systemd.services = let
units = mapAttrs' (name: info: {
name = "${name}-key";
value = (mkService name info);
}) (filterAttrs (_: secret: secret.runtime) cfg);
in units;
};
}