1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Nikita <nikita@n0.is>
3 ;;; Copyright © 2016 Sou Bunnbu <iyzsong@member.fsf.org>
4 ;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org>
5 ;;; Copyright © 2019 Julien Lepiller <julien@lepiller.eu>
6 ;;; Copyright © 2020 Jack Hill <jackhill@jackhill.us>
7 ;;; Copyright © 2020 Tobias Geerinckx-Rice <me@tobias.gr>
8 ;;; Copyright © 2021 Raghav Gururajan <rg@raghavgururajan.name>
10 ;;; This file is part of GNU Guix.
12 ;;; GNU Guix is free software; you can redistribute it and/or modify it
13 ;;; under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 3 of the License, or (at
15 ;;; your option) any later version.
17 ;;; GNU Guix is distributed in the hope that it will be useful, but
18 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
25 (define-module (gnu services certbot)
26 #:use-module (gnu services)
27 #:use-module (gnu services base)
28 #:use-module (gnu services shepherd)
29 #:use-module (gnu services mcron)
30 #:use-module (gnu services web)
31 #:use-module (gnu system shadow)
32 #:use-module (gnu packages tls)
33 #:use-module (guix i18n)
34 #:use-module (guix records)
35 #:use-module (guix gexp)
36 #:use-module (srfi srfi-1)
37 #:use-module (ice-9 match)
38 #:export (certbot-service-type
40 certbot-configuration?
41 certificate-configuration))
45 ;;; Automatically obtaining TLS certificates from Let's Encrypt.
50 (define-record-type* <certificate-configuration>
51 certificate-configuration make-certificate-configuration
52 certificate-configuration?
53 (name certificate-configuration-name
55 (domains certificate-configuration-domains
57 (challenge certificate-configuration-challenge
59 (csr certificate-configuration-csr
61 (authentication-hook certificate-authentication-hook
63 (cleanup-hook certificate-cleanup-hook
65 (deploy-hook certificate-configuration-deploy-hook
68 (define-record-type* <certbot-configuration>
69 certbot-configuration make-certbot-configuration
70 certbot-configuration?
71 (package certbot-configuration-package
73 (webroot certbot-configuration-webroot
75 (certificates certbot-configuration-certificates
77 (email certbot-configuration-email
79 (server certbot-configuration-server
81 (rsa-key-size certbot-configuration-rsa-key-size
83 (default-location certbot-configuration-default-location
85 (nginx-location-configuration
88 (list "return 301 https://$host$request_uri;"))))))
90 (define certbot-command
92 (($ <certbot-configuration> package webroot certificates email
93 server rsa-key-size default-location)
94 (let* ((certbot (file-append package "/bin/certbot"))
95 (rsa-key-size (and rsa-key-size (number->string rsa-key-size)))
99 (($ <certificate-configuration> custom-name domains challenge
100 csr authentication-hook
101 cleanup-hook deploy-hook)
102 (let ((name (or custom-name (car domains))))
105 (list name certbot "certonly" "-n" "--agree-tos"
107 (string-append "--preferred-challenges=" challenge)
109 "--manual-public-ip-logging-ok"
110 "-d" (string-join domains ","))
111 (if csr `("--csr" ,csr) '())
114 '("--register-unsafely-without-email"))
115 (if server `("--server" ,server) '())
116 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())
117 (if authentication-hook
118 `("--manual-auth-hook" ,authentication-hook)
120 (if cleanup-hook `("--manual-cleanup-hook" ,cleanup-hook) '())
121 (if deploy-hook `("--deploy-hook" ,deploy-hook) '()))
123 (list name certbot "certonly" "-n" "--agree-tos"
124 "--webroot" "-w" webroot
126 "-d" (string-join domains ","))
127 (if csr `("--csr" ,csr) '())
130 '("--register-unsafely-without-email"))
131 (if server `("--server" ,server) '())
132 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())
133 (if deploy-hook `("--deploy-hook" ,deploy-hook) '()))))))
138 (use-modules (ice-9 match))
144 (format #t "Acquiring or renewing certificate: ~a~%" name)
145 (set! code (or (apply system* command) code)))))
146 '#$commands) code)))))))
148 (define (certbot-renewal-jobs config)
150 ;; Attempt to renew the certificates twice per day, at a random minute
151 ;; within the hour. See https://certbot.eff.org/all-instructions/.
152 #~(job '(next-minute-from (next-hour '(0 12)) (list (random 60)))
153 #$(certbot-command config))))
155 (define (certbot-activation config)
156 (let* ((certbot-directory "/var/lib/certbot")
157 (script (in-vicinity certbot-directory "renew-certificates"))
158 (message (format #f (G_ "~a may need to be run~%") script)))
160 (($ <certbot-configuration> package webroot certificates email
161 server rsa-key-size default-location)
162 (with-imported-modules '((guix build utils))
164 (use-modules (guix build utils))
166 (mkdir-p #$certbot-directory)
167 (copy-file #$(certbot-command config) #$script)
168 (display #$message)))))))
170 (define certbot-nginx-server-configurations
172 (($ <certbot-configuration> package webroot certificates email
173 server rsa-key-size default-location)
175 (nginx-server-configuration
176 (listen '("80" "[::]:80"))
178 (ssl-certificate-key #f)
180 (apply append (map certificate-configuration-domains certificates)))
184 (nginx-location-configuration
186 (body (list (list "root " webroot ";"))))
187 default-location))))))))
189 (define certbot-service-type
190 (service-type (name 'certbot)
192 (list (service-extension nginx-service-type
193 certbot-nginx-server-configurations)
194 (service-extension activation-service-type
196 (service-extension mcron-service-type
197 certbot-renewal-jobs)))
198 (compose concatenate)
199 (extend (lambda (config additional-certificates)
200 (certbot-configuration
204 (certbot-configuration-certificates config)
205 additional-certificates)))))
207 "Automatically renew @url{https://letsencrypt.org, Let's
208 Encrypt} HTTPS certificates by adjusting the nginx web server configuration
209 and periodically invoking @command{certbot}.")))