1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Nils Gillmann <ng0@n0.is>
3 ;;; Copyright © 2016 Sou Bunnbu <iyzsong@member.fsf.org>
4 ;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (gnu services certbot)
22 #:use-module (gnu services)
23 #:use-module (gnu services base)
24 #:use-module (gnu services shepherd)
25 #:use-module (gnu services mcron)
26 #:use-module (gnu services web)
27 #:use-module (gnu system shadow)
28 #:use-module (gnu packages tls)
29 #:use-module (guix i18n)
30 #:use-module (guix records)
31 #:use-module (guix gexp)
32 #:use-module (srfi srfi-1)
33 #:use-module (ice-9 match)
34 #:export (certbot-service-type
36 certbot-configuration?
37 certificate-configuration))
41 ;;; Automatically obtaining TLS certificates from Let's Encrypt.
46 (define-record-type* <certificate-configuration>
47 certificate-configuration make-certificate-configuration
48 certificate-configuration?
49 (name certificate-configuration-name
51 (domains certificate-configuration-domains
53 (deploy-hook certificate-configuration-deploy-hook
56 (define-record-type* <certbot-configuration>
57 certbot-configuration make-certbot-configuration
58 certbot-configuration?
59 (package certbot-configuration-package
61 (webroot certbot-configuration-webroot
63 (certificates certbot-configuration-certificates
65 (email certbot-configuration-email)
66 (rsa-key-size certbot-configuration-rsa-key-size
68 (default-location certbot-configuration-default-location
70 (nginx-location-configuration
73 (list "return 301 https://$host$request_uri;"))))))
75 (define certbot-command
77 (($ <certbot-configuration> package webroot certificates email
78 rsa-key-size default-location)
79 (let* ((certbot (file-append package "/bin/certbot"))
80 (rsa-key-size (and rsa-key-size (number->string rsa-key-size)))
84 (($ <certificate-configuration> custom-name domains
86 (let ((name (or custom-name (car domains))))
88 (list name certbot "certonly" "-n" "--agree-tos"
90 "--webroot" "-w" webroot
92 "-d" (string-join domains ","))
93 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())
94 (if deploy-hook `("--deploy-hook" ,deploy-hook) '())))))
99 (use-modules (ice-9 match))
105 (format #t "Acquiring or renewing certificate: ~a~%" name)
106 (set! code (or (apply system* command) code)))))
107 '#$commands) code)))))))
109 (define (certbot-renewal-jobs config)
111 ;; Attempt to renew the certificates twice per day, at a random minute
112 ;; within the hour. See https://certbot.eff.org/all-instructions/.
113 #~(job '(next-minute-from (next-hour '(0 12)) (list (random 60)))
114 #$(certbot-command config))))
116 (define (certbot-activation config)
117 (let* ((certbot-directory "/var/lib/certbot")
118 (script (in-vicinity certbot-directory "renew-certificates"))
119 (message (format #f (G_ "~a may need to be run~%") script)))
121 (($ <certbot-configuration> package webroot certificates email
122 rsa-key-size default-location)
123 (with-imported-modules '((guix build utils))
125 (use-modules (guix build utils))
127 (mkdir-p #$certbot-directory)
128 (copy-file #$(certbot-command config) #$script)
129 (display #$message)))))))
131 (define certbot-nginx-server-configurations
133 (($ <certbot-configuration> package webroot certificates email
134 rsa-key-size default-location)
136 (nginx-server-configuration
137 (listen '("80" "[::]:80"))
139 (ssl-certificate-key #f)
141 (apply append (map certificate-configuration-domains certificates)))
145 (nginx-location-configuration
147 (body (list (list "root " webroot ";"))))
148 default-location))))))))
150 (define certbot-service-type
151 (service-type (name 'certbot)
153 (list (service-extension nginx-service-type
154 certbot-nginx-server-configurations)
155 (service-extension activation-service-type
157 (service-extension mcron-service-type
158 certbot-renewal-jobs)))
159 (compose concatenate)
160 (extend (lambda (config additional-certificates)
161 (certbot-configuration
165 (certbot-configuration-certificates config)
166 additional-certificates)))))
168 "Automatically renew @url{https://letsencrypt.org, Let's
169 Encrypt} HTTPS certificates by adjusting the nginx web server configuration
170 and periodically invoking @command{certbot}.")))