services: certbot: Add verbosity.
[jackhill/guix/guix.git] / gnu / services / certbot.scm
CommitLineData
1115f140
AW
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2016 ng0 <ng0@we.make.ritual.n0.is>
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)
29 #:use-module (guix records)
30 #:use-module (guix gexp)
31 #:use-module (srfi srfi-1)
32 #:use-module (ice-9 match)
33 #:export (certbot-service-type
34 certbot-configuration
c3215d2f
CL
35 certbot-configuration?
36 certificate-configuration))
1115f140
AW
37
38;;; Commentary:
39;;;
40;;; Automatically obtaining TLS certificates from Let's Encrypt.
41;;;
42;;; Code:
43
44\f
c3215d2f
CL
45(define-record-type* <certificate-configuration>
46 certificate-configuration make-certificate-configuration
47 certificate-configuration?
48 (name certificate-configuration-name
49 (default #f))
50 (domains certificate-configuration-domains
51 (default '())))
52
1115f140
AW
53(define-record-type* <certbot-configuration>
54 certbot-configuration make-certbot-configuration
55 certbot-configuration?
56 (package certbot-configuration-package
57 (default certbot))
58 (webroot certbot-configuration-webroot
59 (default "/var/www"))
c3215d2f 60 (certificates certbot-configuration-certificates
1115f140 61 (default '()))
65fc1d89 62 (email certbot-configuration-email)
a2cb2bbc
CL
63 (rsa-key-size certbot-configuration-rsa-key-size
64 (default #f))
1115f140
AW
65 (default-location certbot-configuration-default-location
66 (default
67 (nginx-location-configuration
68 (uri "/")
69 (body
70 (list "return 301 https://$host$request_uri;"))))))
71
c1dfcfdf 72(define certbot-command
1115f140 73 (match-lambda
c3215d2f 74 (($ <certbot-configuration> package webroot certificates email
a2cb2bbc 75 rsa-key-size default-location)
c1dfcfdf 76 (let* ((certbot (file-append package "/bin/certbot"))
a2cb2bbc 77 (rsa-key-size (and rsa-key-size (number->string rsa-key-size)))
c1dfcfdf
CL
78 (commands
79 (map
c3215d2f 80 (match-lambda
e216c797
CL
81 (($ <certificate-configuration> custom-name domains)
82 (let ((name (or custom-name (car domains))))
83 (append
84 (list name certbot "certonly" "-n" "--agree-tos"
85 "-m" email
86 "--webroot" "-w" webroot
87 "--cert-name" name
88 "-d" (string-join domains ","))
89 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())))))
c3215d2f 90 certificates)))
c1dfcfdf
CL
91 (program-file
92 "certbot-command"
e216c797
CL
93 #~(begin
94 (use-modules (ice-9 match))
95 (let ((code 0))
96 (for-each
97 (match-lambda
98 ((name . command)
99 (begin
100 (format #t "Acquiring or renewing certificate: ~a~%" name)
101 (set! code (or (apply system* command) code)))))
102 '#$commands) code)))))))
1115f140 103
c1dfcfdf
CL
104(define (certbot-renewal-jobs config)
105 (list
106 ;; Attempt to renew the certificates twice per day, at a random minute
107 ;; within the hour. See https://certbot.eff.org/all-instructions/.
108 #~(job '(next-minute-from (next-hour '(0 12)) (list (random 60)))
109 #$(certbot-command config))))
110
111(define (certbot-activation config)
112 (match config
c3215d2f 113 (($ <certbot-configuration> package webroot certificates email
a2cb2bbc 114 rsa-key-size default-location)
1115f140
AW
115 (with-imported-modules '((guix build utils))
116 #~(begin
30151863
CL
117 (use-modules (guix build utils))
118 (mkdir-p #$webroot)
c1dfcfdf 119 (zero? (system* #$(certbot-command config))))))))
1115f140
AW
120
121(define certbot-nginx-server-configurations
122 (match-lambda
c3215d2f 123 (($ <certbot-configuration> package webroot certificates email
a2cb2bbc 124 rsa-key-size default-location)
c3215d2f
CL
125 (list
126 (nginx-server-configuration
127 (listen '("80" "[::]:80"))
128 (ssl-certificate #f)
129 (ssl-certificate-key #f)
130 (server-name
131 (apply append (map certificate-configuration-domains certificates)))
132 (locations
133 (filter identity
134 (list
135 (nginx-location-configuration
136 (uri "/.well-known")
137 (body (list (list "root " webroot ";"))))
138 default-location))))))))
1115f140
AW
139
140(define certbot-service-type
141 (service-type (name 'certbot)
142 (extensions
143 (list (service-extension nginx-service-type
144 certbot-nginx-server-configurations)
145 (service-extension activation-service-type
146 certbot-activation)
147 (service-extension mcron-service-type
148 certbot-renewal-jobs)))
149 (compose concatenate)
c3215d2f 150 (extend (lambda (config additional-certificates)
1115f140
AW
151 (certbot-configuration
152 (inherit config)
c3215d2f
CL
153 (certificates
154 (append
155 (certbot-configuration-certificates config)
156 additional-certificates)))))
3af03e59
LC
157 (description
158 "Automatically renew @url{https://letsencrypt.org, Let's
159Encrypt} HTTPS certificates by adjusting the nginx web server configuration
160and periodically invoking @command{certbot}.")))