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 '())) |
65fc1d89 | 74 | (email certbot-configuration-email) |
f6713b55 JH |
75 | (server certbot-configuration-server |
76 | (default #f)) | |
a2cb2bbc CL |
77 | (rsa-key-size certbot-configuration-rsa-key-size |
78 | (default #f)) | |
1115f140 AW |
79 | (default-location certbot-configuration-default-location |
80 | (default | |
81 | (nginx-location-configuration | |
82 | (uri "/") | |
83 | (body | |
84 | (list "return 301 https://$host$request_uri;")))))) | |
85 | ||
c1dfcfdf | 86 | (define certbot-command |
1115f140 | 87 | (match-lambda |
c3215d2f | 88 | (($ <certbot-configuration> package webroot certificates email |
f6713b55 | 89 | server rsa-key-size default-location) |
c1dfcfdf | 90 | (let* ((certbot (file-append package "/bin/certbot")) |
a2cb2bbc | 91 | (rsa-key-size (and rsa-key-size (number->string rsa-key-size))) |
c1dfcfdf CL |
92 | (commands |
93 | (map | |
c3215d2f | 94 | (match-lambda |
b68aff1f JL |
95 | (($ <certificate-configuration> custom-name domains challenge |
96 | authentication-hook cleanup-hook | |
fece75fe | 97 | deploy-hook) |
e216c797 | 98 | (let ((name (or custom-name (car domains)))) |
b68aff1f JL |
99 | (if challenge |
100 | (append | |
101 | (list name certbot "certonly" "-n" "--agree-tos" | |
102 | "-m" email | |
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 ",")) |
f6713b55 | 108 | (if server `("--server" ,server) '()) |
b68aff1f JL |
109 | (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '()) |
110 | (if authentication-hook | |
111 | `("--manual-auth-hook" ,authentication-hook) | |
112 | '()) | |
113 | (if cleanup-hook `("--manual-cleanup-hook" ,cleanup-hook) '()) | |
114 | (if deploy-hook `("--deploy-hook" ,deploy-hook) '())) | |
115 | (append | |
116 | (list name certbot "certonly" "-n" "--agree-tos" | |
117 | "-m" email | |
118 | "--webroot" "-w" webroot | |
119 | "--cert-name" name | |
120 | "-d" (string-join domains ",")) | |
f6713b55 | 121 | (if server `("--server" ,server) '()) |
b68aff1f JL |
122 | (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '()) |
123 | (if deploy-hook `("--deploy-hook" ,deploy-hook) '())))))) | |
c3215d2f | 124 | certificates))) |
c1dfcfdf CL |
125 | (program-file |
126 | "certbot-command" | |
e216c797 CL |
127 | #~(begin |
128 | (use-modules (ice-9 match)) | |
129 | (let ((code 0)) | |
130 | (for-each | |
131 | (match-lambda | |
132 | ((name . command) | |
133 | (begin | |
134 | (format #t "Acquiring or renewing certificate: ~a~%" name) | |
135 | (set! code (or (apply system* command) code))))) | |
136 | '#$commands) code))))))) | |
1115f140 | 137 | |
c1dfcfdf CL |
138 | (define (certbot-renewal-jobs config) |
139 | (list | |
140 | ;; Attempt to renew the certificates twice per day, at a random minute | |
141 | ;; within the hour. See https://certbot.eff.org/all-instructions/. | |
142 | #~(job '(next-minute-from (next-hour '(0 12)) (list (random 60))) | |
143 | #$(certbot-command config)))) | |
144 | ||
145 | (define (certbot-activation config) | |
f7266296 CL |
146 | (let* ((certbot-directory "/var/lib/certbot") |
147 | (script (in-vicinity certbot-directory "renew-certificates")) | |
148 | (message (format #f (G_ "~a may need to be run~%") script))) | |
149 | (match config | |
150 | (($ <certbot-configuration> package webroot certificates email | |
f6713b55 | 151 | server rsa-key-size default-location) |
f7266296 CL |
152 | (with-imported-modules '((guix build utils)) |
153 | #~(begin | |
154 | (use-modules (guix build utils)) | |
155 | (mkdir-p #$webroot) | |
156 | (mkdir-p #$certbot-directory) | |
157 | (copy-file #$(certbot-command config) #$script) | |
158 | (display #$message))))))) | |
1115f140 AW |
159 | |
160 | (define certbot-nginx-server-configurations | |
161 | (match-lambda | |
c3215d2f | 162 | (($ <certbot-configuration> package webroot certificates email |
f6713b55 | 163 | server rsa-key-size default-location) |
c3215d2f CL |
164 | (list |
165 | (nginx-server-configuration | |
166 | (listen '("80" "[::]:80")) | |
167 | (ssl-certificate #f) | |
168 | (ssl-certificate-key #f) | |
169 | (server-name | |
170 | (apply append (map certificate-configuration-domains certificates))) | |
171 | (locations | |
172 | (filter identity | |
173 | (list | |
174 | (nginx-location-configuration | |
175 | (uri "/.well-known") | |
176 | (body (list (list "root " webroot ";")))) | |
177 | default-location)))))))) | |
1115f140 AW |
178 | |
179 | (define certbot-service-type | |
180 | (service-type (name 'certbot) | |
181 | (extensions | |
182 | (list (service-extension nginx-service-type | |
183 | certbot-nginx-server-configurations) | |
184 | (service-extension activation-service-type | |
185 | certbot-activation) | |
186 | (service-extension mcron-service-type | |
187 | certbot-renewal-jobs))) | |
188 | (compose concatenate) | |
c3215d2f | 189 | (extend (lambda (config additional-certificates) |
1115f140 AW |
190 | (certbot-configuration |
191 | (inherit config) | |
c3215d2f CL |
192 | (certificates |
193 | (append | |
194 | (certbot-configuration-certificates config) | |
195 | additional-certificates))))) | |
3af03e59 LC |
196 | (description |
197 | "Automatically renew @url{https://letsencrypt.org, Let's | |
198 | Encrypt} HTTPS certificates by adjusting the nginx web server configuration | |
199 | and periodically invoking @command{certbot}."))) |