-
Notifications
You must be signed in to change notification settings - Fork 2
/
acme.nix
248 lines (226 loc) · 7.46 KB
/
acme.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
packages: { pkgs, config, lib, ... }:
with lib;
let
cfg = config.security.acme-eon;
# These options can be specified within
# security.acme.defaults or security.acme.certs.<name>
inheritableModule = isDefaults:
{ config, ... }:
let
defaultAndText = name: default: {
default = if isDefaults || !config.inheritDefaults then
default
else
cfg.defaults.${name};
defaultText = if isDefaults then
default
else
literalExpression "config.security.acme.defaults.${name}";
};
in {
options = {
capFile = mkOption {
type = types.str;
inherit (defaultAndText "capFile" null) default defaultText;
description = lib.mdDoc "Capability file path.";
};
email = mkOption {
type = types.nullOr types.str;
inherit (defaultAndText "email" null) default defaultText;
description = lib.mdDoc ''
Email address for account creation and correspondence from the CA.
It is recommended to use the same email for all certs to avoid account
creation limits.
'';
};
group = mkOption {
type = types.str;
inherit (defaultAndText "group" "acme-eon") default defaultText;
description = lib.mdDoc "Group running the client.";
};
reloadServices = mkOption {
type = types.listOf types.str;
inherit (defaultAndText "reloadServices" [ ]) default defaultText;
description = lib.mdDoc ''
The list of systemd services to call `systemctl try-reload-or-restart`
on.
'';
};
};
};
certOpts = { name, ... }: {
options = {
directory = mkOption {
type = types.str;
readOnly = true;
default = "/var/lib/acme-eon/${name}";
description =
lib.mdDoc "Directory where certificate and other state is stored.";
};
domain = mkOption {
type = types.str;
readOnly = true;
default = name;
description =
lib.mdDoc "Domain to fetch certificate for (the entry name).";
};
extraDomainNames = mkOption {
type = types.listOf types.str;
default = [ ];
example = literalExpression ''
[
"example.com"
"mydomain.org"
]
'';
description = lib.mdDoc ''
A list of extra domain names, which are included in the one certificate to be issued.
'';
};
inheritDefaults = mkOption {
default = true;
example = true;
description = lib.mdDoc
"Whether to inherit values set in `security.acme.defaults` or not.";
type = lib.types.bool;
};
};
};
in {
options.security.acme-eon = {
package = lib.mkOption {
type = lib.types.package;
default = packages.${config.nixpkgs.hostPlatform.system}.default;
};
acceptTerms = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc ''
Accept the CA's terms of service. The default provider is Let's Encrypt,
you can find their ToS at <https://letsencrypt.org/repository/>.
'';
};
defaults = mkOption {
type = types.submodule (inheritableModule true);
description = lib.mdDoc ''
Default values inheritable by all configured certs. You can
use this to define options shared by all your certs. These defaults
can also be ignored on a per-cert basis using the
{option}`security.acme.certs.''${cert}.inheritDefaults` option.
'';
};
certs = mkOption {
default = { };
type = with types;
attrsOf (submodule [ (inheritableModule false) certOpts ]);
description = lib.mdDoc ''
Attribute set of certificates to get signed and renewed. Other services can add dependencies
to those units if they rely on the certificates being present,
or trigger restarts of the service if certificates get renewed.
'';
example = literalExpression ''
{
"example.com" = {
email = "[email protected]";
extraDomainNames = [ "www.example.com" "foo.example.com" ];
};
"bar.example.com" = {
email = "[email protected]";
};
}
'';
};
nginxCerts = mkOption {
type = types.listOf types.str;
default = [ ];
description = lib.mdDoc ''
Domain names to configure Nginx certificates for.
'';
};
};
config = let
mkCertService = name: cert: {
description = "Provision ACME certificate for ${cert.domain}";
wantedBy = optionals (!config.boot.isContainer) [ "multi-user.target" ];
path = with pkgs; [ cfg.package ];
wants = [ "network-online.target" "eon.service" ];
after = [ "network-online.target" "eon.service" ];
serviceConfig = {
User = "acme-eon";
Group = cert.group;
UMask = "0022";
StateDirectoryMode = "750";
StateDirectory = [ "acme-eon/${cert.domain}" ];
# Run as root (Prefixed with +)
ExecStartPre = "+" + (pkgs.writeShellScript "acme-prerun" ''
cp ${cert.capFile} domain.cap
chown acme-eon domain.cap
'');
ExecStart = ''
${cfg.package}/bin/capc cert \
domain.cap \
${cert.email} \
-d ${cert.domain} \
${
lib.strings.concatMapStringsSep " " (d: "-d " + d)
cert.extraDomainNames
} \
--cert-dir ${cert.directory} \
--exit-when-renewed
'';
# Run as root (Prefixed with +)
ExecStartPost = "+" + (pkgs.writeShellScript "acme-postrun" ''
cd /var/lib/acme-eon/${escapeShellArg cert.domain}
if [ -e renewed ]; then
rm renewed
${
optionalString (cert.reloadServices != [ ])
"systemctl --no-block try-reload-or-restart ${
escapeShellArgs cert.reloadServices
}"
}
fi
'');
};
};
in {
assertions = [{
assertion = cfg.acceptTerms;
message = ''
You must accept the CA's terms of service before using
the ACME module by setting `security.acme-eon.acceptTerms`
to `true`. For Let's Encrypt's ToS see https://letsencrypt.org/repository/
'';
}];
users.users.acme-eon = {
home = "/var/lib/acme-eon/";
group = "acme-eon";
isSystemUser = true;
};
users.groups.acme-eon = { };
security.acme-eon.certs = builtins.listToAttrs (builtins.map (name: {
inherit name;
value = {
group = "nginx";
reloadServices = [ "nginx" ];
};
}) cfg.nginxCerts);
systemd.services = (lib.attrsets.mapAttrs' (name: cert: {
name = "acme-eon-${name}";
value = mkCertService name cert;
}) cfg.certs) // {
nginx.wants =
builtins.map (name: "acme-eon-${name}.service") cfg.nginxCerts;
nginx-config-reload.after =
builtins.map (name: "acme-eon-${name}.service") cfg.nginxCerts;
};
services.nginx.virtualHosts = builtins.listToAttrs (builtins.map (name: {
name = name;
value = {
sslCertificate = "${cfg.certs.${name}.directory}/fullchain.pem";
sslCertificateKey = "${cfg.certs.${name}.directory}/key.pem";
sslTrustedCertificate = "${cfg.certs.${name}.directory}/chain.pem";
};
}) cfg.nginxCerts);
};
}