gnu: retroarch: Update to 1.7.8.4.
[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>
b68aff1f 5;;; Copyright © 2019 Julien Lepiller <julien@lepiller.eu>
1115f140
AW
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)
f7266296 30 #:use-module (guix i18n)
1115f140
AW
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
c3215d2f
CL
37 certbot-configuration?
38 certificate-configuration))
1115f140
AW
39
40;;; Commentary:
41;;;
42;;; Automatically obtaining TLS certificates from Let's Encrypt.
43;;;
44;;; Code:
45
46\f
c3215d2f
CL
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
fece75fe 53 (default '()))
b68aff1f
JL
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))
fece75fe
CL
60 (deploy-hook certificate-configuration-deploy-hook
61 (default #f)))
c3215d2f 62
1115f140
AW
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"))
c3215d2f 70 (certificates certbot-configuration-certificates
1115f140 71 (default '()))
65fc1d89 72 (email certbot-configuration-email)
a2cb2bbc
CL
73 (rsa-key-size certbot-configuration-rsa-key-size
74 (default #f))
1115f140
AW
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
c1dfcfdf 82(define certbot-command
1115f140 83 (match-lambda
c3215d2f 84 (($ <certbot-configuration> package webroot certificates email
a2cb2bbc 85 rsa-key-size default-location)
c1dfcfdf 86 (let* ((certbot (file-append package "/bin/certbot"))
a2cb2bbc 87 (rsa-key-size (and rsa-key-size (number->string rsa-key-size)))
c1dfcfdf
CL
88 (commands
89 (map
c3215d2f 90 (match-lambda
b68aff1f
JL
91 (($ <certificate-configuration> custom-name domains challenge
92 authentication-hook cleanup-hook
fece75fe 93 deploy-hook)
e216c797 94 (let ((name (or custom-name (car domains))))
b68aff1f
JL
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) '()))))))
c3215d2f 117 certificates)))
c1dfcfdf
CL
118 (program-file
119 "certbot-command"
e216c797
CL
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)))))))
1115f140 130
c1dfcfdf
CL
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)
f7266296
CL
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)))))))
1115f140
AW
152
153(define certbot-nginx-server-configurations
154 (match-lambda
c3215d2f 155 (($ <certbot-configuration> package webroot certificates email
a2cb2bbc 156 rsa-key-size default-location)
c3215d2f
CL
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))))))))
1115f140
AW
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)
c3215d2f 182 (extend (lambda (config additional-certificates)
1115f140
AW
183 (certbot-configuration
184 (inherit config)
c3215d2f
CL
185 (certificates
186 (append
187 (certbot-configuration-certificates config)
188 additional-certificates)))))
3af03e59
LC
189 (description
190 "Automatically renew @url{https://letsencrypt.org, Let's
191Encrypt} HTTPS certificates by adjusting the nginx web server configuration
192and periodically invoking @command{certbot}.")))