gnu: Add openjdk12.
[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 "-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)
106 '())
107 (if cleanup-hook `("--manual-cleanup-hook" ,cleanup-hook) '())
108 (if deploy-hook `("--deploy-hook" ,deploy-hook) '()))
109 (append
110 (list name certbot "certonly" "-n" "--agree-tos"
111 "-m" email
112 "--webroot" "-w" webroot
113 "--cert-name" name
114 "-d" (string-join domains ","))
115 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())
116 (if deploy-hook `("--deploy-hook" ,deploy-hook) '()))))))
117 certificates)))
118 (program-file
119 "certbot-command"
120 #~(begin
121 (use-modules (ice-9 match))
122 (let ((code 0))
123 (for-each
124 (match-lambda
125 ((name . command)
126 (begin
127 (format #t "Acquiring or renewing certificate: ~a~%" name)
128 (set! code (or (apply system* command) code)))))
129 '#$commands) code)))))))
130
131 (define (certbot-renewal-jobs config)
132 (list
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))))
137
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)))
142 (match config
143 (($ <certbot-configuration> package webroot certificates email
144 rsa-key-size default-location)
145 (with-imported-modules '((guix build utils))
146 #~(begin
147 (use-modules (guix build utils))
148 (mkdir-p #$webroot)
149 (mkdir-p #$certbot-directory)
150 (copy-file #$(certbot-command config) #$script)
151 (display #$message)))))))
152
153 (define certbot-nginx-server-configurations
154 (match-lambda
155 (($ <certbot-configuration> package webroot certificates email
156 rsa-key-size default-location)
157 (list
158 (nginx-server-configuration
159 (listen '("80" "[::]:80"))
160 (ssl-certificate #f)
161 (ssl-certificate-key #f)
162 (server-name
163 (apply append (map certificate-configuration-domains certificates)))
164 (locations
165 (filter identity
166 (list
167 (nginx-location-configuration
168 (uri "/.well-known")
169 (body (list (list "root " webroot ";"))))
170 default-location))))))))
171
172 (define certbot-service-type
173 (service-type (name 'certbot)
174 (extensions
175 (list (service-extension nginx-service-type
176 certbot-nginx-server-configurations)
177 (service-extension activation-service-type
178 certbot-activation)
179 (service-extension mcron-service-type
180 certbot-renewal-jobs)))
181 (compose concatenate)
182 (extend (lambda (config additional-certificates)
183 (certbot-configuration
184 (inherit config)
185 (certificates
186 (append
187 (certbot-configuration-certificates config)
188 additional-certificates)))))
189 (description
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}.")))