summaryrefslogtreecommitdiffstats
path: root/nixos/modules/security/acme/doc.xml
blob: 1439594a5aca6d54ba29017f4c23f8e7dcb0e257 (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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<chapter xmlns="http://docbook.org/ns/docbook"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         xmlns:xi="http://www.w3.org/2001/XInclude"
         version="5.0"
         xml:id="module-security-acme">
 <title>SSL/TLS Certificates with ACME</title>
 <para>
  NixOS supports automatic domain validation &amp; certificate retrieval and
  renewal using the ACME protocol. Any provider can be used, but by default
  NixOS uses Let's Encrypt. The alternative ACME client
  <link xlink:href="https://go-acme.github.io/lego/">lego</link> is used under
  the hood.
 </para>
 <para>
  Automatic cert validation and configuration for Apache and Nginx virtual
  hosts is included in NixOS, however if you would like to generate a wildcard
  cert or you are not using a web server you will have to configure DNS
  based validation.
 </para>
 <section xml:id="module-security-acme-prerequisites">
  <title>Prerequisites</title>

  <para>
   To use the ACME module, you must accept the provider's terms of service
   by setting <literal><xref linkend="opt-security.acme.acceptTerms" /></literal>
   to <literal>true</literal>. The Let's Encrypt ToS can be found
   <link xlink:href="https://letsencrypt.org/repository/">here</link>.
  </para>

  <para>
   You must also set an email address to be used when creating accounts with
   Let's Encrypt. You can set this for all certs with
   <literal><xref linkend="opt-security.acme.defaults.email" /></literal>
   and/or on a per-cert basis with
   <literal><xref linkend="opt-security.acme.certs._name_.email" /></literal>.
   This address is only used for registration and renewal reminders,
   and cannot be used to administer the certificates in any way.
  </para>

  <para>
   Alternatively, you can use a different ACME server by changing the
   <literal><xref linkend="opt-security.acme.defaults.server" /></literal> option
   to a provider of your choosing, or just change the server for one cert with
   <literal><xref linkend="opt-security.acme.certs._name_.server" /></literal>.
  </para>

  <para>
   You will need an HTTP server or DNS server for verification. For HTTP,
   the server must have a webroot defined that can serve
   <filename>.well-known/acme-challenge</filename>. This directory must be
   writeable by the user that will run the ACME client. For DNS, you must
   set up credentials with your provider/server for use with lego.
  </para>
 </section>
 <section xml:id="module-security-acme-nginx">
  <title>Using ACME certificates in Nginx</title>

  <para>
   NixOS supports fetching ACME certificates for you by setting
   <literal><link linkend="opt-services.nginx.virtualHosts._name_.enableACME">enableACME</link>
   = true;</literal> in a virtualHost config. We first create self-signed
   placeholder certificates in place of the real ACME certs. The placeholder
   certs are overwritten when the ACME certs arrive. For
   <literal>foo.example.com</literal> the config would look like this:
  </para>

<programlisting>
<xref linkend="opt-security.acme.acceptTerms" /> = true;
<xref linkend="opt-security.acme.defaults.email" /> = "admin+acme@example.com";
services.nginx = {
  <link linkend="opt-services.nginx.enable">enable</link> = true;
  <link linkend="opt-services.nginx.virtualHosts">virtualHosts</link> = {
    "foo.example.com" = {
      <link linkend="opt-services.nginx.virtualHosts._name_.forceSSL">forceSSL</link> = true;
      <link linkend="opt-services.nginx.virtualHosts._name_.enableACME">enableACME</link> = true;
      # All serverAliases will be added as <link linkend="opt-security.acme.certs._name_.extraDomainNames">extra domain names</link> on the certificate.
      <link linkend="opt-services.nginx.virtualHosts._name_.serverAliases">serverAliases</link> = [ "bar.example.com" ];
      locations."/" = {
        <link linkend="opt-services.nginx.virtualHosts._name_.locations._name_.root">root</link> = "/var/www";
      };
    };

    # We can also add a different vhost and reuse the same certificate
    # but we have to append extraDomainNames manually beforehand:
    # <link linkend="opt-security.acme.certs._name_.extraDomainNames">security.acme.certs."foo.example.com".extraDomainNames</link> = [ "baz.example.com" ];
    "baz.example.com" = {
      <link linkend="opt-services.nginx.virtualHosts._name_.forceSSL">forceSSL</link> = true;
      <link linkend="opt-services.nginx.virtualHosts._name_.useACMEHost">useACMEHost</link> = "foo.example.com";
      locations."/" = {
        <link linkend="opt-services.nginx.virtualHosts._name_.locations._name_.root">root</link> = "/var/www";
      };
    };
  };
}
</programlisting>
 </section>
 <section xml:id="module-security-acme-httpd">
  <title>Using ACME certificates in Apache/httpd</title>

  <para>
   Using ACME certificates with Apache virtual hosts is identical
   to using them with Nginx. The attribute names are all the same, just replace
   "nginx" with "httpd" where appropriate.
  </para>
 </section>
 <section xml:id="module-security-acme-configuring">
  <title>Manual configuration of HTTP-01 validation</title>

  <para>
   First off you will need to set up a virtual host to serve the challenges.
   This example uses a vhost called <literal>certs.example.com</literal>, with
   the intent that you will generate certs for all your vhosts and redirect
   everyone to HTTPS.
  </para>

<programlisting>
<xref linkend="opt-security.acme.acceptTerms" /> = true;
<xref linkend="opt-security.acme.defaults.email" /> = "admin+acme@example.com";

# /var/lib/acme/.challenges must be writable by the ACME user
# and readable by the Nginx user. The easiest way to achieve
# this is to add the Nginx user to the ACME group.
<link linkend="opt-users.users._name_.extraGroups">users.users.nginx.extraGroups</link> = [ "acme" ];

services.nginx = {
  <link linkend="opt-services.nginx.enable">enable</link> = true;
  <link linkend="opt-services.nginx.virtualHosts">virtualHosts</link> = {
    "acmechallenge.example.com" = {
      # Catchall vhost, will redirect users to HTTPS for all vhosts
      <link linkend="opt-services.nginx.virtualHosts._name_.serverAliases">serverAliases</link> = [ "*.example.com" ];
      locations."/.well-known/acme-challenge" = {
        <link linkend="opt-services.nginx.virtualHosts._name_.locations._name_.root">root</link> = "/var/lib/acme/.challenges";
      };
      locations."/" = {
        <link linkend="opt-services.nginx.virtualHosts._name_.locations._name_.return">return</link> = "301 https://$host$request_uri";
      };
    };
  };
}
# Alternative config for Apache
<link linkend="opt-users.users._name_.extraGroups">users.users.wwwrun.extraGroups</link> = [ "acme" ];
services.httpd = {
  <link linkend="opt-services.httpd.enable">enable = true;</link>
  <link linkend="opt-services.httpd.virtualHosts">virtualHosts</link> = {
    "acmechallenge.example.com" = {
      # Catchall vhost, will redirect users to HTTPS for all vhosts
      <link linkend="opt-services.httpd.virtualHosts._name_.serverAliases">serverAliases</link> = [ "*.example.com" ];
      # /var/lib/acme/.challenges must be writable by the ACME user and readable by the Apache user.
      # By default, this is the case.
      <link linkend="opt-services.httpd.virtualHosts._name_.documentRoot">documentRoot</link> = "/var/lib/acme/.challenges";
      <link linkend="opt-services.httpd.virtualHosts._name_.extraConfig">extraConfig</link> = ''
        RewriteEngine On
        RewriteCond %{HTTPS} off
        RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge [NC]
        RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
      '';
    };
  };
}
</programlisting>

  <para>
   Now you need to configure ACME to generate a certificate.
  </para>

<programlisting>
<xref linkend="opt-security.acme.certs"/>."foo.example.com" = {
  <link linkend="opt-security.acme.certs._name_.webroot">webroot</link> = "/var/lib/acme/.challenges";
  <link linkend="opt-security.acme.certs._name_.email">email</link> = "foo@example.com";
  # Ensure that the web server you use can read the generated certs
  # Take a look at the <link linkend="opt-services.nginx.group">group</link> option for the web server you choose.
  <link linkend="opt-security.acme.certs._name_.group">group</link> = "nginx";
  # Since we have a wildcard vhost to handle port 80,
  # we can generate certs for anything!
  # Just make sure your DNS resolves them.
  <link linkend="opt-security.acme.certs._name_.extraDomainNames">extraDomainNames</link> = [ "mail.example.com" ];
};
</programlisting>

  <para>
   The private key <filename>key.pem</filename> and certificate
   <filename>fullchain.pem</filename> will be put into
   <filename>/var/lib/acme/foo.example.com</filename>.
  </para>

  <para>
   Refer to <xref linkend="ch-options" /> for all available configuration
   options for the <link linkend="opt-security.acme.certs">security.acme</link>
   module.
  </para>
 </section>
 <section xml:id="module-security-acme-config-dns">
  <title>Configuring ACME for DNS validation</title>

  <para>
   This is useful if you want to generate a wildcard certificate, since
   ACME servers will only hand out wildcard certs over DNS validation.
   There are a number of supported DNS providers and servers you can utilise,
   see the <link xlink:href="https://go-acme.github.io/lego/dns/">lego docs</link>
   for provider/server specific configuration values. For the sake of these
   docs, we will provide a fully self-hosted example using bind.
  </para>

<programlisting>
services.bind = {
  <link linkend="opt-services.bind.enable">enable</link> = true;
  <link linkend="opt-services.bind.extraConfig">extraConfig</link> = ''
    include "/var/lib/secrets/dnskeys.conf";
  '';
  <link linkend="opt-services.bind.zones">zones</link> = [
    rec {
      name = "example.com";
      file = "/var/db/bind/${name}";
      master = true;
      extraConfig = "allow-update { key rfc2136key.example.com.; };";
    }
  ];
}

# Now we can configure ACME
<xref linkend="opt-security.acme.acceptTerms" /> = true;
<xref linkend="opt-security.acme.defaults.email" /> =