1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 ng0 <ng0@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)
75 (server certbot-configuration-server
77 (rsa-key-size certbot-configuration-rsa-key-size
79 (default-location certbot-configuration-default-location
81 (nginx-location-configuration
84 (list "return 301 https://$host$request_uri;"))))))
86 (define certbot-command
88 (($ <certbot-configuration> package webroot certificates email
89 server rsa-key-size default-location)
90 (let* ((certbot (file-append package "/bin/certbot"))
91 (rsa-key-size (and rsa-key-size (number->string rsa-key-size)))
95 (($ <certificate-configuration> custom-name domains challenge
96 authentication-hook cleanup-hook
98 (let ((name (or custom-name (car domains))))
101 (list name certbot "certonly" "-n" "--agree-tos"
104 (string-append "--preferred-challenges=" challenge)
106 "--manual-public-ip-logging-ok"
107 "-d" (string-join domains ","))
108 (if server `("--server" ,server) '())
109 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())
110 (if authentication-hook
111 `("--manual-auth-hook" ,authentication-hook)
113 (if cleanup-hook `("--manual-cleanup-hook" ,cleanup-hook) '())
114 (if deploy-hook `("--deploy-hook" ,deploy-hook) '()))
116 (list name certbot "certonly" "-n" "--agree-tos"
118 "--webroot" "-w" webroot
120 "-d" (string-join domains ","))
121 (if server `("--server" ,server) '())
122 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())
123 (if deploy-hook `("--deploy-hook" ,deploy-hook) '()))))))
128 (use-modules (ice-9 match))
134 (format #t "Acquiring or renewing certificate: ~a~%" name)
135 (set! code (or (apply system* command) code)))))
136 '#$commands) code)))))))
138 (define (certbot-renewal-jobs config)
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))))
145 (define (certbot-activation config)
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)))
150 (($ <certbot-configuration> package webroot certificates email
151 server rsa-key-size default-location)
152 (with-imported-modules '((guix build utils))
154 (use-modules (guix build utils))
156 (mkdir-p #$certbot-directory)
157 (copy-file #$(certbot-command config) #$script)
158 (display #$message)))))))
160 (define certbot-nginx-server-configurations
162 (($ <certbot-configuration> package webroot certificates email
163 server rsa-key-size default-location)
165 (nginx-server-configuration
166 (listen '("80" "[::]:80"))
168 (ssl-certificate-key #f)
170 (apply append (map certificate-configuration-domains certificates)))
174 (nginx-location-configuration
176 (body (list (list "root " webroot ";"))))
177 default-location))))))))
179 (define certbot-service-type
180 (service-type (name 'certbot)
182 (list (service-extension nginx-service-type
183 certbot-nginx-server-configurations)
184 (service-extension activation-service-type
186 (service-extension mcron-service-type
187 certbot-renewal-jobs)))
188 (compose concatenate)
189 (extend (lambda (config additional-certificates)
190 (certbot-configuration
194 (certbot-configuration-certificates config)
195 additional-certificates)))))
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}.")))