gnu: cmake: Update to 3.12.0.
[jackhill/guix/guix.git] / gnu / services / certbot.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2016 Nils Gillmann <ng0@n0.is>
3 ;;; Copyright © 2016 Sou Bunnbu <iyzsong@member.fsf.org>
4 ;;; Copyright © 2017, 2018 Clément Lassieur <clement@lassieur.org>
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 i18n)
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
36 certbot-configuration?
37 certificate-configuration))
38
39 ;;; Commentary:
40 ;;;
41 ;;; Automatically obtaining TLS certificates from Let's Encrypt.
42 ;;;
43 ;;; Code:
44
45 \f
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
52 (default '()))
53 (deploy-hook certificate-configuration-deploy-hook
54 (default #f)))
55
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"))
63 (certificates certbot-configuration-certificates
64 (default '()))
65 (email certbot-configuration-email)
66 (rsa-key-size certbot-configuration-rsa-key-size
67 (default #f))
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
75 (define certbot-command
76 (match-lambda
77 (($ <certbot-configuration> package webroot certificates email
78 rsa-key-size default-location)
79 (let* ((certbot (file-append package "/bin/certbot"))
80 (rsa-key-size (and rsa-key-size (number->string rsa-key-size)))
81 (commands
82 (map
83 (match-lambda
84 (($ <certificate-configuration> custom-name domains
85 deploy-hook)
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 ","))
93 (if rsa-key-size `("--rsa-key-size" ,rsa-key-size) '())
94 (if deploy-hook `("--deploy-hook" ,deploy-hook) '())))))
95 certificates)))
96 (program-file
97 "certbot-command"
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)))))))
108
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)
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)))))))
130
131 (define certbot-nginx-server-configurations
132 (match-lambda
133 (($ <certbot-configuration> package webroot certificates email
134 rsa-key-size default-location)
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))))))))
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)
160 (extend (lambda (config additional-certificates)
161 (certbot-configuration
162 (inherit config)
163 (certificates
164 (append
165 (certbot-configuration-certificates config)
166 additional-certificates)))))
167 (description
168 "Automatically renew @url{https://letsencrypt.org, Let's
169 Encrypt} HTTPS certificates by adjusting the nginx web server configuration
170 and periodically invoking @command{certbot}.")))