gnu: bootstrap: Memoize 'bootstrap-origin'.
[jackhill/guix/guix.git] / gnu / services / certbot.scm
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 ;;;
7 ;;; This file is part of GNU Guix.
8 ;;;
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.
13 ;;;
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.
18 ;;;
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/>.
21
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
36 certbot-configuration
37 certbot-configuration?
38 certificate-configuration))
39
40 ;;; Commentary:
41 ;;;
42 ;;; Automatically obtaining TLS certificates from Let's Encrypt.
43 ;;;
44 ;;; Code:
45
46 \f
47 (define-record-type* <certificate-configuration>
48 certificate-configuration make-certificate-configuration
49 certificate-configuration?
50 (name certificate-configuration-name
51 (default #f))
52 (domains certificate-configuration-domains
53 (default '()))
54 (challenge certificate-configuration-challenge
55 (default #f))
56 (authentication-hook certificate-authentication-hook
57 (default #f))
58 (cleanup-hook certificate-cleanup-hook
59 (default #f))
60 (deploy-hook certificate-configuration-deploy-hook
61 (default #f)))
62
63 (define-record-type* <certbot-configuration>
64 certbot-configuration make-certbot-configuration
65 certbot-configuration?
66 (package certbot-configuration-package
67 (default certbot))
68 (webroot certbot-configuration-webroot
69 (default "/var/www"))
70 (certificates certbot-configuration-certificates
71 (default '()))
72 (email certbot-configuration-email)
73 (rsa-key-size certbot-configuration-rsa-key-size
74 (default #f))
75 (default-location certbot-configuration-default-location
76 (default
77 (nginx-location-configuration
78 (uri "/")
79 (body
80 (list "return 301 https://$host$request_uri;"))))))
81
82 (define certbot-command
83 (match-lambda
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)))
88 (commands
89 (map
90 (match-lambda
91 (($ <certificate-configuration> custom-name domains challenge
92 authentication-hook cleanup-hook
93 deploy-hook)
94 (let ((name (or custom-name (car domains))))
95 (if challenge
96 (append
97 (list name certbot "certonly" "-n" "--agree-tos"
98 "-m" email
99 "--manual"
100 (string-append "--preferred-challenges=" challenge)
101 "--cert-name" name
102 "--manual-public-ip-logging-ok"
103 "-d" (string-join domains ","))
104 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())
105 (if authentication-hook
106 `("--manual-auth-hook" ,authentication-hook)
107 '())
108 (if cleanup-hook `("--manual-cleanup-hook" ,cleanup-hook) '())
109 (if deploy-hook `("--deploy-hook" ,deploy-hook) '()))
110 (append
111 (list name certbot "certonly" "-n" "--agree-tos"
112 "-m" email
113 "--webroot" "-w" webroot
114 "--cert-name" name
115 "-d" (string-join domains ","))
116 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())
117 (if deploy-hook `("--deploy-hook" ,deploy-hook) '()))))))
118 certificates)))
119 (program-file
120 "certbot-command"
121 #~(begin
122 (use-modules (ice-9 match))
123 (let ((code 0))
124 (for-each
125 (match-lambda
126 ((name . command)
127 (begin
128 (format #t "Acquiring or renewing certificate: ~a~%" name)
129 (set! code (or (apply system* command) code)))))
130 '#$commands) code)))))))
131
132 (define (certbot-renewal-jobs config)
133 (list
134 ;; Attempt to renew the certificates twice per day, at a random minute
135 ;; within the hour. See https://certbot.eff.org/all-instructions/.
136 #~(job '(next-minute-from (next-hour '(0 12)) (list (random 60)))
137 #$(certbot-command config))))
138
139 (define (certbot-activation config)
140 (let* ((certbot-directory "/var/lib/certbot")
141 (script (in-vicinity certbot-directory "renew-certificates"))
142 (message (format #f (G_ "~a may need to be run~%") script)))
143 (match config
144 (($ <certbot-configuration> package webroot certificates email
145 rsa-key-size default-location)
146 (with-imported-modules '((guix build utils))
147 #~(begin
148 (use-modules (guix build utils))
149 (mkdir-p #$webroot)
150 (mkdir-p #$certbot-directory)
151 (copy-file #$(certbot-command config) #$script)
152 (display #$message)))))))
153
154 (define certbot-nginx-server-configurations
155 (match-lambda
156 (($ <certbot-configuration> package webroot certificates email
157 rsa-key-size default-location)
158 (list
159 (nginx-server-configuration
160 (listen '("80" "[::]:80"))
161 (ssl-certificate #f)
162 (ssl-certificate-key #f)
163 (server-name
164 (apply append (map certificate-configuration-domains certificates)))
165 (locations
166 (filter identity
167 (list
168 (nginx-location-configuration
169 (uri "/.well-known")
170 (body (list (list "root " webroot ";"))))
171 default-location))))))))
172
173 (define certbot-service-type
174 (service-type (name 'certbot)
175 (extensions
176 (list (service-extension nginx-service-type
177 certbot-nginx-server-configurations)
178 (service-extension activation-service-type
179 certbot-activation)
180 (service-extension mcron-service-type
181 certbot-renewal-jobs)))
182 (compose concatenate)
183 (extend (lambda (config additional-certificates)
184 (certbot-configuration
185 (inherit config)
186 (certificates
187 (append
188 (certbot-configuration-certificates config)
189 additional-certificates)))))
190 (description
191 "Automatically renew @url{https://letsencrypt.org, Let's
192 Encrypt} HTTPS certificates by adjusting the nginx web server configuration
193 and periodically invoking @command{certbot}.")))