gnu: ocaml@4.02: Remove the package, affected by a CVE, and its dependent
[jackhill/guix/guix.git] / gnu / services / certbot.scm
CommitLineData
1115f140 1;;; GNU Guix --- Functional package management for GNU
47956fa0 2;;; Copyright © 2016 ng0 <ng0@n0.is>
1115f140 3;;; Copyright © 2016 Sou Bunnbu <iyzsong@member.fsf.org>
70cd2239 4;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org>
1115f140
AW
5;;;
6;;; This file is part of GNU Guix.
7;;;
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.
12;;;
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.
17;;;
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/>.
20
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)
f7266296 29 #:use-module (guix i18n)
1115f140
AW
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
35 certbot-configuration
c3215d2f
CL
36 certbot-configuration?
37 certificate-configuration))
1115f140
AW
38
39;;; Commentary:
40;;;
41;;; Automatically obtaining TLS certificates from Let's Encrypt.
42;;;
43;;; Code:
44
45\f
c3215d2f
CL
46(define-record-type* <certificate-configuration>
47 certificate-configuration make-certificate-configuration
48 certificate-configuration?
49 (name certificate-configuration-name
50 (default #f))
51 (domains certificate-configuration-domains
fece75fe
CL
52 (default '()))
53 (deploy-hook certificate-configuration-deploy-hook
54 (default #f)))
c3215d2f 55
1115f140
AW
56(define-record-type* <certbot-configuration>
57 certbot-configuration make-certbot-configuration
58 certbot-configuration?
59 (package certbot-configuration-package
60 (default certbot))
61 (webroot certbot-configuration-webroot
62 (default "/var/www"))
c3215d2f 63 (certificates certbot-configuration-certificates
1115f140 64 (default '()))
65fc1d89 65 (email certbot-configuration-email)
a2cb2bbc
CL
66 (rsa-key-size certbot-configuration-rsa-key-size
67 (default #f))
1115f140
AW
68 (default-location certbot-configuration-default-location
69 (default
70 (nginx-location-configuration
71 (uri "/")
72 (body
73 (list "return 301 https://$host$request_uri;"))))))
74
c1dfcfdf 75(define certbot-command
1115f140 76 (match-lambda
c3215d2f 77 (($ <certbot-configuration> package webroot certificates email
a2cb2bbc 78 rsa-key-size default-location)
c1dfcfdf 79 (let* ((certbot (file-append package "/bin/certbot"))
a2cb2bbc 80 (rsa-key-size (and rsa-key-size (number->string rsa-key-size)))
c1dfcfdf
CL
81 (commands
82 (map
c3215d2f 83 (match-lambda
fece75fe
CL
84 (($ <certificate-configuration> custom-name domains
85 deploy-hook)
e216c797
CL
86 (let ((name (or custom-name (car domains))))
87 (append
88 (list name certbot "certonly" "-n" "--agree-tos"
89 "-m" email
90 "--webroot" "-w" webroot
91 "--cert-name" name
92 "-d" (string-join domains ","))
fece75fe
CL
93 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())
94 (if deploy-hook `("--deploy-hook" ,deploy-hook) '())))))
c3215d2f 95 certificates)))
c1dfcfdf
CL
96 (program-file
97 "certbot-command"
e216c797
CL
98 #~(begin
99 (use-modules (ice-9 match))
100 (let ((code 0))
101 (for-each
102 (match-lambda
103 ((name . command)
104 (begin
105 (format #t "Acquiring or renewing certificate: ~a~%" name)
106 (set! code (or (apply system* command) code)))))
107 '#$commands) code)))))))
1115f140 108
c1dfcfdf
CL
109(define (certbot-renewal-jobs config)
110 (list
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))))
115
116(define (certbot-activation config)
f7266296
CL
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)))
120 (match config
121 (($ <certbot-configuration> package webroot certificates email
122 rsa-key-size default-location)
123 (with-imported-modules '((guix build utils))
124 #~(begin
125 (use-modules (guix build utils))
126 (mkdir-p #$webroot)
127 (mkdir-p #$certbot-directory)
128 (copy-file #$(certbot-command config) #$script)
129 (display #$message)))))))
1115f140
AW
130
131(define certbot-nginx-server-configurations
132 (match-lambda
c3215d2f 133 (($ <certbot-configuration> package webroot certificates email
a2cb2bbc 134 rsa-key-size default-location)
c3215d2f
CL
135 (list
136 (nginx-server-configuration
137 (listen '("80" "[::]:80"))
138 (ssl-certificate #f)
139 (ssl-certificate-key #f)
140 (server-name
141 (apply append (map certificate-configuration-domains certificates)))
142 (locations
143 (filter identity
144 (list
145 (nginx-location-configuration
146 (uri "/.well-known")
147 (body (list (list "root " webroot ";"))))
148 default-location))))))))
1115f140
AW
149
150(define certbot-service-type
151 (service-type (name 'certbot)
152 (extensions
153 (list (service-extension nginx-service-type
154 certbot-nginx-server-configurations)
155 (service-extension activation-service-type
156 certbot-activation)
157 (service-extension mcron-service-type
158 certbot-renewal-jobs)))
159 (compose concatenate)
c3215d2f 160 (extend (lambda (config additional-certificates)
1115f140
AW
161 (certbot-configuration
162 (inherit config)
c3215d2f
CL
163 (certificates
164 (append
165 (certbot-configuration-certificates config)
166 additional-certificates)))))
3af03e59
LC
167 (description
168 "Automatically renew @url{https://letsencrypt.org, Let's
169Encrypt} HTTPS certificates by adjusting the nginx web server configuration
170and periodically invoking @command{certbot}.")))