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>
7 ;;; This file is part of GNU Guix.
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
22 (define-module (gnu services certbot)
23 #:use-module (gnu services)
24 #:use-module (gnu services base)
25 #:use-module (gnu services shepherd)
26 #:use-module (gnu services mcron)
27 #:use-module (gnu services web)
28 #:use-module (gnu system shadow)
29 #:use-module (gnu packages tls)
30 #:use-module (guix i18n)
31 #:use-module (guix records)
32 #:use-module (guix gexp)
33 #:use-module (srfi srfi-1)
34 #:use-module (ice-9 match)
35 #:export (certbot-service-type
37 certbot-configuration?
38 certificate-configuration))
42 ;;; Automatically obtaining TLS certificates from Let's Encrypt.
47 (define-record-type* <certificate-configuration>
48 certificate-configuration make-certificate-configuration
49 certificate-configuration?
50 (name certificate-configuration-name
52 (domains certificate-configuration-domains
54 (challenge certificate-configuration-challenge
56 (authentication-hook certificate-authentication-hook
58 (cleanup-hook certificate-cleanup-hook
60 (deploy-hook certificate-configuration-deploy-hook
63 (define-record-type* <certbot-configuration>
64 certbot-configuration make-certbot-configuration
65 certbot-configuration?
66 (package certbot-configuration-package
68 (webroot certbot-configuration-webroot
70 (certificates certbot-configuration-certificates
72 (email certbot-configuration-email)
73 (rsa-key-size certbot-configuration-rsa-key-size
75 (default-location certbot-configuration-default-location
77 (nginx-location-configuration
80 (list "return 301 https://$host$request_uri;"))))))
82 (define certbot-command
84 (($ <certbot-configuration> package webroot certificates email
85 rsa-key-size default-location)
86 (let* ((certbot (file-append package "/bin/certbot"))
87 (rsa-key-size (and rsa-key-size (number->string rsa-key-size)))
91 (($ <certificate-configuration> custom-name domains challenge
92 authentication-hook cleanup-hook
94 (let ((name (or custom-name (car domains))))
97 (list name certbot "certonly" "-n" "--agree-tos"
100 (string-append "--preferred-challenges=" challenge)
102 "-d" (string-join domains ","))
103 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())
104 (if authentication-hook
105 `("--manual-auth-hook" ,authentication-hook)
107 (if cleanup-hook `("--manual-cleanup-hook" ,cleanup-hook) '())
108 (if deploy-hook `("--deploy-hook" ,deploy-hook) '()))
110 (list name certbot "certonly" "-n" "--agree-tos"
112 "--webroot" "-w" webroot
114 "-d" (string-join domains ","))
115 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())
116 (if deploy-hook `("--deploy-hook" ,deploy-hook) '()))))))
121 (use-modules (ice-9 match))
127 (format #t "Acquiring or renewing certificate: ~a~%" name)
128 (set! code (or (apply system* command) code)))))
129 '#$commands) code)))))))
131 (define (certbot-renewal-jobs config)
133 ;; Attempt to renew the certificates twice per day, at a random minute
134 ;; within the hour. See https://certbot.eff.org/all-instructions/.
135 #~(job '(next-minute-from (next-hour '(0 12)) (list (random 60)))
136 #$(certbot-command config))))
138 (define (certbot-activation config)
139 (let* ((certbot-directory "/var/lib/certbot")
140 (script (in-vicinity certbot-directory "renew-certificates"))
141 (message (format #f (G_ "~a may need to be run~%") script)))
143 (($ <certbot-configuration> package webroot certificates email
144 rsa-key-size default-location)
145 (with-imported-modules '((guix build utils))
147 (use-modules (guix build utils))
149 (mkdir-p #$certbot-directory)
150 (copy-file #$(certbot-command config) #$script)
151 (display #$message)))))))
153 (define certbot-nginx-server-configurations
155 (($ <certbot-configuration> package webroot certificates email
156 rsa-key-size default-location)
158 (nginx-server-configuration
159 (listen '("80" "[::]:80"))
161 (ssl-certificate-key #f)
163 (apply append (map certificate-configuration-domains certificates)))
167 (nginx-location-configuration
169 (body (list (list "root " webroot ";"))))
170 default-location))))))))
172 (define certbot-service-type
173 (service-type (name 'certbot)
175 (list (service-extension nginx-service-type
176 certbot-nginx-server-configurations)
177 (service-extension activation-service-type
179 (service-extension mcron-service-type
180 certbot-renewal-jobs)))
181 (compose concatenate)
182 (extend (lambda (config additional-certificates)
183 (certbot-configuration
187 (certbot-configuration-certificates config)
188 additional-certificates)))))
190 "Automatically renew @url{https://letsencrypt.org, Let's
191 Encrypt} HTTPS certificates by adjusting the nginx web server configuration
192 and periodically invoking @command{certbot}.")))