Commit | Line | Data |
---|---|---|
1115f140 | 1 | ;;; GNU Guix --- Functional package management for GNU |
3c986a7d | 2 | ;;; Copyright © 2016 Nikita <nikita@n0.is> |
1115f140 | 3 | ;;; Copyright © 2016 Sou Bunnbu <iyzsong@member.fsf.org> |
70cd2239 | 4 | ;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org> |
b68aff1f | 5 | ;;; Copyright © 2019 Julien Lepiller <julien@lepiller.eu> |
f6713b55 JH |
6 | ;;; Copyright © 2020 Jack Hill <jackhill@jackhill.us> |
7 | ;;; Copyright © 2020 Tobias Geerinckx-Rice <me@tobias.gr> | |
1115f140 AW |
8 | ;;; |
9 | ;;; This file is part of GNU Guix. | |
10 | ;;; | |
11 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
12 | ;;; under the terms of the GNU General Public License as published by | |
13 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
14 | ;;; your option) any later version. | |
15 | ;;; | |
16 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
17 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;;; GNU General Public License for more details. | |
20 | ;;; | |
21 | ;;; You should have received a copy of the GNU General Public License | |
22 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
23 | ||
24 | (define-module (gnu services certbot) | |
25 | #:use-module (gnu services) | |
26 | #:use-module (gnu services base) | |
27 | #:use-module (gnu services shepherd) | |
28 | #:use-module (gnu services mcron) | |
29 | #:use-module (gnu services web) | |
30 | #:use-module (gnu system shadow) | |
31 | #:use-module (gnu packages tls) | |
f7266296 | 32 | #:use-module (guix i18n) |
1115f140 AW |
33 | #:use-module (guix records) |
34 | #:use-module (guix gexp) | |
35 | #:use-module (srfi srfi-1) | |
36 | #:use-module (ice-9 match) | |
37 | #:export (certbot-service-type | |
38 | certbot-configuration | |
c3215d2f CL |
39 | certbot-configuration? |
40 | certificate-configuration)) | |
1115f140 AW |
41 | |
42 | ;;; Commentary: | |
43 | ;;; | |
44 | ;;; Automatically obtaining TLS certificates from Let's Encrypt. | |
45 | ;;; | |
46 | ;;; Code: | |
47 | ||
48 | \f | |
c3215d2f CL |
49 | (define-record-type* <certificate-configuration> |
50 | certificate-configuration make-certificate-configuration | |
51 | certificate-configuration? | |
52 | (name certificate-configuration-name | |
53 | (default #f)) | |
54 | (domains certificate-configuration-domains | |
fece75fe | 55 | (default '())) |
b68aff1f JL |
56 | (challenge certificate-configuration-challenge |
57 | (default #f)) | |
58 | (authentication-hook certificate-authentication-hook | |
59 | (default #f)) | |
60 | (cleanup-hook certificate-cleanup-hook | |
61 | (default #f)) | |
fece75fe CL |
62 | (deploy-hook certificate-configuration-deploy-hook |
63 | (default #f))) | |
c3215d2f | 64 | |
1115f140 AW |
65 | (define-record-type* <certbot-configuration> |
66 | certbot-configuration make-certbot-configuration | |
67 | certbot-configuration? | |
68 | (package certbot-configuration-package | |
69 | (default certbot)) | |
70 | (webroot certbot-configuration-webroot | |
71 | (default "/var/www")) | |
c3215d2f | 72 | (certificates certbot-configuration-certificates |
1115f140 | 73 | (default '())) |
11a962e6 TL |
74 | (email certbot-configuration-email |
75 | (default #f)) | |
f6713b55 JH |
76 | (server certbot-configuration-server |
77 | (default #f)) | |
a2cb2bbc CL |
78 | (rsa-key-size certbot-configuration-rsa-key-size |
79 | (default #f)) | |
1115f140 AW |
80 | (default-location certbot-configuration-default-location |
81 | (default | |
82 | (nginx-location-configuration | |
83 | (uri "/") | |
84 | (body | |
85 | (list "return 301 https://$host$request_uri;")))))) | |
86 | ||
c1dfcfdf | 87 | (define certbot-command |
1115f140 | 88 | (match-lambda |
c3215d2f | 89 | (($ <certbot-configuration> package webroot certificates email |
f6713b55 | 90 | server rsa-key-size default-location) |
c1dfcfdf | 91 | (let* ((certbot (file-append package "/bin/certbot")) |
a2cb2bbc | 92 | (rsa-key-size (and rsa-key-size (number->string rsa-key-size))) |
c1dfcfdf CL |
93 | (commands |
94 | (map | |
c3215d2f | 95 | (match-lambda |
b68aff1f JL |
96 | (($ <certificate-configuration> custom-name domains challenge |
97 | authentication-hook cleanup-hook | |
fece75fe | 98 | deploy-hook) |
e216c797 | 99 | (let ((name (or custom-name (car domains)))) |
b68aff1f JL |
100 | (if challenge |
101 | (append | |
102 | (list name certbot "certonly" "-n" "--agree-tos" | |
b68aff1f JL |
103 | "--manual" |
104 | (string-append "--preferred-challenges=" challenge) | |
105 | "--cert-name" name | |
ec36339d | 106 | "--manual-public-ip-logging-ok" |
b68aff1f | 107 | "-d" (string-join domains ",")) |
11a962e6 TL |
108 | (if email |
109 | `("--email" ,email) | |
110 | '("--register-unsafely-without-email")) | |
f6713b55 | 111 | (if server `("--server" ,server) '()) |
b68aff1f JL |
112 | (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '()) |
113 | (if authentication-hook | |
114 | `("--manual-auth-hook" ,authentication-hook) | |
115 | '()) | |
116 | (if cleanup-hook `("--manual-cleanup-hook" ,cleanup-hook) '()) | |
117 | (if deploy-hook `("--deploy-hook" ,deploy-hook) '())) | |
118 | (append | |
119 | (list name certbot "certonly" "-n" "--agree-tos" | |
b68aff1f JL |
120 | "--webroot" "-w" webroot |
121 | "--cert-name" name | |
122 | "-d" (string-join domains ",")) | |
11a962e6 TL |
123 | (if email |
124 | `("--email" ,email) | |
125 | '("--register-unsafely-without-email")) | |
f6713b55 | 126 | (if server `("--server" ,server) '()) |
b68aff1f JL |
127 | (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '()) |
128 | (if deploy-hook `("--deploy-hook" ,deploy-hook) '())))))) | |
c3215d2f | 129 | certificates))) |
c1dfcfdf CL |
130 | (program-file |
131 | "certbot-command" | |
e216c797 CL |
132 | #~(begin |
133 | (use-modules (ice-9 match)) | |
134 | (let ((code 0)) | |
135 | (for-each | |
136 | (match-lambda | |
137 | ((name . command) | |
138 | (begin | |
139 | (format #t "Acquiring or renewing certificate: ~a~%" name) | |
140 | (set! code (or (apply system* command) code))))) | |
141 | '#$commands) code))))))) | |
1115f140 | 142 | |
c1dfcfdf CL |
143 | (define (certbot-renewal-jobs config) |
144 | (list | |
145 | ;; Attempt to renew the certificates twice per day, at a random minute | |
146 | ;; within the hour. See https://certbot.eff.org/all-instructions/. | |
147 | #~(job '(next-minute-from (next-hour '(0 12)) (list (random 60))) | |
148 | #$(certbot-command config)))) | |
149 | ||
150 | (define (certbot-activation config) | |
f7266296 CL |
151 | (let* ((certbot-directory "/var/lib/certbot") |
152 | (script (in-vicinity certbot-directory "renew-certificates")) | |
153 | (message (format #f (G_ "~a may need to be run~%") script))) | |
154 | (match config | |
155 | (($ <certbot-configuration> package webroot certificates email | |
f6713b55 | 156 | server rsa-key-size default-location) |
f7266296 CL |
157 | (with-imported-modules '((guix build utils)) |
158 | #~(begin | |
159 | (use-modules (guix build utils)) | |
160 | (mkdir-p #$webroot) | |
161 | (mkdir-p #$certbot-directory) | |
162 | (copy-file #$(certbot-command config) #$script) | |
163 | (display #$message))))))) | |
1115f140 AW |
164 | |
165 | (define certbot-nginx-server-configurations | |
166 | (match-lambda | |
c3215d2f | 167 | (($ <certbot-configuration> package webroot certificates email |
f6713b55 | 168 | server rsa-key-size default-location) |
c3215d2f CL |
169 | (list |
170 | (nginx-server-configuration | |
171 | (listen '("80" "[::]:80")) | |
172 | (ssl-certificate #f) | |
173 | (ssl-certificate-key #f) | |
174 | (server-name | |
175 | (apply append (map certificate-configuration-domains certificates))) | |
176 | (locations | |
177 | (filter identity | |
178 | (list | |
179 | (nginx-location-configuration | |
180 | (uri "/.well-known") | |
181 | (body (list (list "root " webroot ";")))) | |
182 | default-location)))))))) | |
1115f140 AW |
183 | |
184 | (define certbot-service-type | |
185 | (service-type (name 'certbot) | |
186 | (extensions | |
187 | (list (service-extension nginx-service-type | |
188 | certbot-nginx-server-configurations) | |
189 | (service-extension activation-service-type | |
190 | certbot-activation) | |
191 | (service-extension mcron-service-type | |
192 | certbot-renewal-jobs))) | |
193 | (compose concatenate) | |
c3215d2f | 194 | (extend (lambda (config additional-certificates) |
1115f140 AW |
195 | (certbot-configuration |
196 | (inherit config) | |
c3215d2f CL |
197 | (certificates |
198 | (append | |
199 | (certbot-configuration-certificates config) | |
200 | additional-certificates))))) | |
3af03e59 LC |
201 | (description |
202 | "Automatically renew @url{https://letsencrypt.org, Let's | |
203 | Encrypt} HTTPS certificates by adjusting the nginx web server configuration | |
204 | and periodically invoking @command{certbot}."))) |