-
Notifications
You must be signed in to change notification settings - Fork 0
/
nager.nix
150 lines (134 loc) · 3.86 KB
/
nager.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
{ config, lib, pkgs, ... }:
let
inherit (lib)
attrValues
concatMapStringsSep
isAttrs
mapAttrs
mkEnableOption
mkIf
mkMerge
mkOption
types
;
ageBin = "${pkgs.age}/bin/age";
secretOpts = types.submodule ({ name, ... }: {
options = {
name = mkOption {
type = types.str;
default = name;
description = ''
Name of the decrypted secret in /run/nager. Defaults to the name of
the attribute.
'';
};
file = mkOption {
type = types.path;
description = "The source of the secret, as an age encrypted file.";
};
mode = mkOption {
type = types.str;
default = "0400";
description = "Permissions for the decrypted secret in a format understood by chmod.";
};
owner = mkOption {
type = types.str;
default = "0";
description = "Owner of the decrypted secret.";
};
group = mkOption {
type = types.str;
default = "0";
description = "Owning group of the decrypted secret.";
};
};
});
cfg = config.nager;
decryptCommands = concatMapStringsSep "\n"
(secret:
let
out = "/run/nager.d/$_nager_generation/${secret.name}";
in
''
install -m 600 -o 0 -g 0 /dev/null "${out}"
${ageBin} --decrypt -i "${cfg.keyFile}" "${secret.file}" >"${out}"
''
)
(attrValues cfg.secrets');
aclCommands = concatMapStringsSep "\n"
(secret:
let
out = "/run/nager/${secret.name}";
in
''
chmod "${secret.mode}" "${out}"
chown "${secret.owner}:${secret.group}" "${out}"
''
)
(attrValues cfg.secrets');
in
{
options = {
nager = {
keyFile = mkOption {
type = types.path;
default = "/etc/nager.age";
description = "Private key used to decrypt secrets on activation. Must NOT be password protected.";
};
secrets = mkOption {
type = with types; attrsOf (oneOf [ path secretOpts ]);
default = { };
description = ''
Lists of secrets to be decrypted by nager. Either an attrSet or just
a path to an encrypted file.
'';
};
secrets' = mkOption {
type = types.attrsOf secretOpts;
description = "Like `nager.secrets`, but shorthand is expanded.";
visible = false;
internal = true;
readOnly = true;
};
};
};
config = mkMerge [
{
nager.secrets' = mapAttrs
(_: v:
if isAttrs v
then v
else { file = v; }
)
cfg.secrets;
}
(mkIf (cfg.secrets' != { }) {
system.activationScripts = {
nager = {
text = ''
# First part: set up tmpfs if needed, create new directory for secrets.
_nager_generation="$(basename "$(readlink /run/nager)" || echo 0)"
(( ++_nager_generation ))
install -d -m 0751 -o 0 -g 0 /run/nager.d
grep -q "/run/nager.d tmpfs" /proc/mounts || mount -t tmpfs none "/run/nager.d" -o nodev,nosuid,noexec,mode=0751
install -d -m 0751 -o 0 -g 0 "/run/nager.d/$_nager_generation";
${decryptCommands}
chmod 0400 "/run/nager.d/$_nager_generation/"*
# Finally: replace old generation with new one, delete old generation
ln -sfn "/run/nager.d/$_nager_generation" /run/nager
rm -rf "/run/nager.d/$(( _nager_generation - 1 ))";
'';
deps = [ "specialfs" ];
};
# Set up the secrets (without ACLs) before users are set up. This allows
# having encrypted password files (but hashedPassword is recommended
# instead).
users.deps = [ "nager" ];
nager-acls = {
text = aclCommands;
deps = [ "nager" "users" "groups" ];
};
};
})
];
}