services: postgresql: Use "/tmp" host directory.
[jackhill/guix/guix.git] / gnu / services / certbot.scm
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>
8 ;;;
9 ;;; This file is part of GNU Guix.
10 ;;;
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.
15 ;;;
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.
20 ;;;
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/>.
23
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
38 certbot-configuration
39 certbot-configuration?
40 certificate-configuration))
41
42 ;;; Commentary:
43 ;;;
44 ;;; Automatically obtaining TLS certificates from Let's Encrypt.
45 ;;;
46 ;;; Code:
47
48 \f
49 (define-record-type* <certificate-configuration>
50 certificate-configuration make-certificate-configuration
51 certificate-configuration?
52 (name certificate-configuration-name
53 (default #f))
54 (domains certificate-configuration-domains
55 (default '()))
56 (challenge certificate-configuration-challenge
57 (default #f))
58 (authentication-hook certificate-authentication-hook
59 (default #f))
60 (cleanup-hook certificate-cleanup-hook
61 (default #f))
62 (deploy-hook certificate-configuration-deploy-hook
63 (default #f)))
64
65 (define-record-type* <certbot-configuration>
66 certbot-configuration make-certbot-configuration
67 certbot-configuration?
68 (package certbot-configuration-package
69 (default certbot))
70 (webroot certbot-configuration-webroot
71 (default "/var/www"))
72 (certificates certbot-configuration-certificates
73 (default '()))
74 (email certbot-configuration-email
75 (default #f))
76 (server certbot-configuration-server
77 (default #f))
78 (rsa-key-size certbot-configuration-rsa-key-size
79 (default #f))
80 (default-location certbot-configuration-default-location
81 (default
82 (nginx-location-configuration
83 (uri "/")
84 (body
85 (list "return 301 https://$host$request_uri;"))))))
86
87 (define certbot-command
88 (match-lambda
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)))
93 (commands
94 (map
95 (match-lambda
96 (($ <certificate-configuration> custom-name domains challenge
97 authentication-hook cleanup-hook
98 deploy-hook)
99 (let ((name (or custom-name (car domains))))
100 (if challenge
101 (append
102 (list name certbot "certonly" "-n" "--agree-tos"
103 "--manual"
104 (string-append "--preferred-challenges=" challenge)
105 "--cert-name" name
106 "--manual-public-ip-logging-ok"
107 "-d" (string-join domains ","))
108 (if email
109 `("--email" ,email)
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)
115 '())
116 (if cleanup-hook `("--manual-cleanup-hook" ,cleanup-hook) '())
117 (if deploy-hook `("--deploy-hook" ,deploy-hook) '()))
118 (append
119 (list name certbot "certonly" "-n" "--agree-tos"
120 "--webroot" "-w" webroot
121 "--cert-name" name
122 "-d" (string-join domains ","))
123 (if email
124 `("--email" ,email)
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) '()))))))
129 certificates)))
130 (program-file
131 "certbot-command"
132 #~(begin
133 (use-modules (ice-9 match))
134 (let ((code 0))
135 (for-each
136 (match-lambda
137 ((name . command)
138 (begin
139 (format #t "Acquiring or renewing certificate: ~a~%" name)
140 (set! code (or (apply system* command) code)))))
141 '#$commands) code)))))))
142
143 (define (certbot-renewal-jobs config)
144 (list
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))))
149
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)))
154 (match config
155 (($ <certbot-configuration> package webroot certificates email
156 server rsa-key-size default-location)
157 (with-imported-modules '((guix build utils))
158 #~(begin
159 (use-modules (guix build utils))
160 (mkdir-p #$webroot)
161 (mkdir-p #$certbot-directory)
162 (copy-file #$(certbot-command config) #$script)
163 (display #$message)))))))
164
165 (define certbot-nginx-server-configurations
166 (match-lambda
167 (($ <certbot-configuration> package webroot certificates email
168 server rsa-key-size default-location)
169 (list
170 (nginx-server-configuration
171 (listen '("80" "[::]:80"))
172 (ssl-certificate #f)
173 (ssl-certificate-key #f)
174 (server-name
175 (apply append (map certificate-configuration-domains certificates)))
176 (locations
177 (filter identity
178 (list
179 (nginx-location-configuration
180 (uri "/.well-known")
181 (body (list (list "root " webroot ";"))))
182 default-location))))))))
183
184 (define certbot-service-type
185 (service-type (name 'certbot)
186 (extensions
187 (list (service-extension nginx-service-type
188 certbot-nginx-server-configurations)
189 (service-extension activation-service-type
190 certbot-activation)
191 (service-extension mcron-service-type
192 certbot-renewal-jobs)))
193 (compose concatenate)
194 (extend (lambda (config additional-certificates)
195 (certbot-configuration
196 (inherit config)
197 (certificates
198 (append
199 (certbot-configuration-certificates config)
200 additional-certificates)))))
201 (description
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}.")))