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>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu services certbot)
21 #:use-module (gnu services)
22 #:use-module (gnu services base)
23 #:use-module (gnu services shepherd)
24 #:use-module (gnu services mcron)
25 #:use-module (gnu services web)
26 #:use-module (gnu system shadow)
27 #:use-module (gnu packages tls)
28 #:use-module (guix records)
29 #:use-module (guix gexp)
30 #:use-module (srfi srfi-1)
31 #:use-module (ice-9 match)
32 #:export (certbot-service-type
34 certbot-configuration?))
38 ;;; Automatically obtaining TLS certificates from Let's Encrypt.
43 (define-record-type* <certbot-configuration>
44 certbot-configuration make-certbot-configuration
45 certbot-configuration?
46 (package certbot-configuration-package
48 (webroot certbot-configuration-webroot
50 (hosts certbot-configuration-hosts
52 (default-location certbot-configuration-default-location
54 (nginx-location-configuration
57 (list "return 301 https://$host$request_uri;"))))))
59 (define certbot-renewal-jobs
61 (($ <certbot-configuration> package webroot hosts default-location)
63 ;; Avoid pinging certbot if we have no hosts.
67 ;; Attempt to renew the certificates twice a week.
69 (next-day-from (next-hour-from now '(3))
71 (string-append #$package "/bin/certbot renew"
74 (string-append " -d " host))
77 (define certbot-activation
79 (($ <certbot-configuration> package webroot hosts default-location)
80 (with-imported-modules '((guix build utils))
82 (use-modules (guix build utils))
86 (unless (file-exists? (in-vicinity "/etc/letsencrypt/live" host))
87 (unless (zero? (system*
88 (string-append #$certbot "/bin/certbot")
89 "certonly" "--webroot" "-w" #$webroot
91 (error "failed to acquire cert for host" host))))
94 (define certbot-nginx-server-configurations
96 (($ <certbot-configuration> package webroot hosts default-location)
99 (nginx-server-configuration
103 (ssl-certificate-key #f)
104 (server-name (list host))
108 (nginx-location-configuration
110 (body (list (list "root " webroot ";"))))
111 default-location)))))
114 (define certbot-service-type
115 (service-type (name 'certbot)
117 (list (service-extension nginx-service-type
118 certbot-nginx-server-configurations)
119 (service-extension activation-service-type
121 (service-extension mcron-service-type
122 certbot-renewal-jobs)))
123 (compose concatenate)
124 (extend (lambda (config additional-hosts)
125 (certbot-configuration
127 (hosts (append (certbot-configuration-hosts config)
128 additional-hosts)))))
129 (default-value (certbot-configuration))
131 "Automatically renew @url{https://letsencrypt.org, Let's
132 Encrypt} HTTPS certificates by adjusting the nginx web server configuration
133 and periodically invoking @command{certbot}.")))