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>
9 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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)
32 #:use-module (guix i18n)
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
39 certbot-configuration?
40 certificate-configuration))
44 ;;; Automatically obtaining TLS certificates from Let's Encrypt.
49 (define-record-type* <certificate-configuration>
50 certificate-configuration make-certificate-configuration
51 certificate-configuration?
52 (name certificate-configuration-name
54 (domains certificate-configuration-domains
56 (challenge certificate-configuration-challenge
58 (authentication-hook certificate-authentication-hook
60 (cleanup-hook certificate-cleanup-hook
62 (deploy-hook certificate-configuration-deploy-hook
65 (define-record-type* <certbot-configuration>
66 certbot-configuration make-certbot-configuration
67 certbot-configuration?
68 (package certbot-configuration-package
70 (webroot certbot-configuration-webroot
72 (certificates certbot-configuration-certificates
74 (email certbot-configuration-email
76 (server certbot-configuration-server
78 (rsa-key-size certbot-configuration-rsa-key-size
80 (default-location certbot-configuration-default-location
82 (nginx-location-configuration
85 (list "return 301 https://$host$request_uri;"))))))
87 (define certbot-command
89 (($ <certbot-configuration> package webroot certificates email
90 server rsa-key-size default-location)
91 (let* ((certbot (file-append package "/bin/certbot"))
92 (rsa-key-size (and rsa-key-size (number->string rsa-key-size)))
96 (($ <certificate-configuration> custom-name domains challenge
97 authentication-hook cleanup-hook
99 (let ((name (or custom-name (car domains))))
102 (list name certbot "certonly" "-n" "--agree-tos"
104 (string-append "--preferred-challenges=" challenge)
106 "--manual-public-ip-logging-ok"
107 "-d" (string-join domains ","))
110 '("--register-unsafely-without-email"))
111 (if server `("--server" ,server) '())
112 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())
113 (if authentication-hook
114 `("--manual-auth-hook" ,authentication-hook)
116 (if cleanup-hook `("--manual-cleanup-hook" ,cleanup-hook) '())
117 (if deploy-hook `("--deploy-hook" ,deploy-hook) '()))
119 (list name certbot "certonly" "-n" "--agree-tos"
120 "--webroot" "-w" webroot
122 "-d" (string-join domains ","))
125 '("--register-unsafely-without-email"))
126 (if server `("--server" ,server) '())
127 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())
128 (if deploy-hook `("--deploy-hook" ,deploy-hook) '()))))))
133 (use-modules (ice-9 match))
139 (format #t "Acquiring or renewing certificate: ~a~%" name)
140 (set! code (or (apply system* command) code)))))
141 '#$commands) code)))))))
143 (define (certbot-renewal-jobs config)
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))))
150 (define (certbot-activation config)
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)))
155 (($ <certbot-configuration> package webroot certificates email
156 server rsa-key-size default-location)
157 (with-imported-modules '((guix build utils))
159 (use-modules (guix build utils))
161 (mkdir-p #$certbot-directory)
162 (copy-file #$(certbot-command config) #$script)
163 (display #$message)))))))
165 (define certbot-nginx-server-configurations
167 (($ <certbot-configuration> package webroot certificates email
168 server rsa-key-size default-location)
170 (nginx-server-configuration
171 (listen '("80" "[::]:80"))
173 (ssl-certificate-key #f)
175 (apply append (map certificate-configuration-domains certificates)))
179 (nginx-location-configuration
181 (body (list (list "root " webroot ";"))))
182 default-location))))))))
184 (define certbot-service-type
185 (service-type (name 'certbot)
187 (list (service-extension nginx-service-type
188 certbot-nginx-server-configurations)
189 (service-extension activation-service-type
191 (service-extension mcron-service-type
192 certbot-renewal-jobs)))
193 (compose concatenate)
194 (extend (lambda (config additional-certificates)
195 (certbot-configuration
199 (certbot-configuration-certificates config)
200 additional-certificates)))))
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}.")))