blob: 2244d1574b24c607535bb295263a8ea4f869ba58 (
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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
{ config, lib, ... }:
let
inherit (lib)
attrValues
filter
flip
match
mkOption
recursiveUpdate
removeAttrs
removePrefix
types
;
inherit (lib.filesystem) listFilesRecursive;
cfg = config;
toRelativePath = filePath: removePrefix (toString cfg.rootDirectory + "/") (toString filePath);
handleSecretRegex =
secret:
let
secretRegexMatches = str: match secret.regex str != null;
matched = filter (filePath: secretRegexMatches (toRelativePath filePath)) secretFiles;
in
map (
path:
recursiveUpdate secret {
path = toRelativePath path;
}
) matched;
secretFiles = listFilesRecursive cfg.rootDirectory;
failedAssertions = map (x: x.message) (filter (x: !x.assertion) cfg.assertions);
assertionsMessage = "\nFailed assertions:\n${lib.concatLines (map (x: "- " + x) failedAssertions)}";
agenixSecretSubmodule = {
freeformType = lib.types.attrsOf types.anything;
options = {
publicKeys = mkOption {
type = types.listOf types.str;
defaultText = "config.recipients.default";
description = "List of public keys a given secret is encrypted for.";
};
};
};
recipientsSubmodule = {
freeformType = types.attrsOf types.str;
options = {
default = mkOption {
type = types.listOf types.str;
default = [ ];
description = "Recipetents added to secrets by default.";
};
};
};
recipientsOptionSubmodule = {
options = {
recipients = mkOption {
type = types.submodule recipientsSubmodule;
default = { };
description = "Recipetents that files will be encrypted for.";
};
};
};
secretSettingsSubmodule =
{ config, ... }:
{
imports = [ recipientsOptionSubmodule ];
options = {
recipients = mkOption {
# We only use this in the toplevel `config.recipients`
apply = flip removeAttrs [ "default" ];
};
useDefault = lib.mkEnableOption "the default recipients" // {
default = true;
};
settings = mkOption {
type = types.submodule agenixSecretSubmodule;
default = { };
description = ''
Settings for a given secret.
Loosely documented in the agenix [tutorial](https://github.com/ryantm/agenix#tutorial).
'';
};
};
# Dogfood `settings` to apply global and per-secret recipients
# Use `mkForce` to override
config = lib.mkMerge [
{
settings.publicKeys = attrValues config.recipients;
}
(lib.mkIf config.useDefault {
settings.publicKeys = cfg.recipients.default;
})
];
};
secretPathSubmodule = {
imports = [ secretSettingsSubmodule ];
options = {
path = mkOption {
type = types.nullOr types.str;
default = null;
description = "Relative path (to `config.rootDirectory`) of a secret";
};
regex = mkOption {
type = types.nullOr types.str;
default = null;
description = "A regex for a given file path relative to `config.rootDirectory`.";
};
};
};
# TODO: Re-implement when can `types.either (types.submodule ...) (types.submodule ...)` works
# It would be good to avoid the `nullOr` above and assertions below
/*
secretRegexSubmodule = {
imports = [ secretSettingsSubmodule ];
options = {
regex = mkOption {
type = types.str;
description = "A regex for a given file path relative to `config.rootDirectory`.";
};
};
};
*/
buildSubmodule = {
options = {
rules = mkOption {
type = types.attrsOf (types.submodule agenixSecretSubmodule);
readOnly = true;
description = "Final rules passed to the agenix CLi.";
};
};
};
in
{
imports = [
recipientsOptionSubmodule
<nixpkgs/nixos/modules/misc/assertions.nix>
];
options = {
rootDirectory = mkOption {
type = types.path;
description = "Root directory containing agenix secrets.";
};
secrets = mkOption {
# TODO: Use `types.listOf (types.either ...)`
type = types.listOf (types.submodule secretPathSubmodule);
default = { };
description = "Submodule describing agenix secrets.";
};
# Outputs
build = mkOption {
type = types.submodule buildSubmodule;
default = { };
apply = build: if failedAssertions != [ ] then throw assertionsMessage else build;
internal = true;
};
};
config = {
assertions = map (secret: {
assertion = secret.path != null || secret.regex != null;
message = "One of `path` or `regex` must be set";
}) cfg.secrets;
build = {
# TODO: Harvest all secrets
rules = lib.listToAttrs (
lib.concatMap (
secret:
let
normalized = if secret.regex != null then handleSecretRegex secret else [ secret ];
in
map (secret': lib.nameValuePair secret'.path secret'.settings) normalized
) cfg.secrets
);
};
};
}
|