certbot.scm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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>
  4. ;;; Copyright © 2017 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. (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
  33. certbot-configuration
  34. certbot-configuration?))
  35. ;;; Commentary:
  36. ;;;
  37. ;;; Automatically obtaining TLS certificates from Let's Encrypt.
  38. ;;;
  39. ;;; Code:
  40. (define-record-type* <certbot-configuration>
  41. certbot-configuration make-certbot-configuration
  42. certbot-configuration?
  43. (package certbot-configuration-package
  44. (default certbot))
  45. (webroot certbot-configuration-webroot
  46. (default "/var/www"))
  47. (hosts certbot-configuration-hosts
  48. (default '()))
  49. (default-location certbot-configuration-default-location
  50. (default
  51. (nginx-location-configuration
  52. (uri "/")
  53. (body
  54. (list "return 301 https://$host$request_uri;"))))))
  55. (define certbot-renewal-jobs
  56. (match-lambda
  57. (($ <certbot-configuration> package webroot hosts default-location)
  58. (match hosts
  59. ;; Avoid pinging certbot if we have no hosts.
  60. (() '())
  61. (_
  62. (list
  63. ;; Attempt to renew the certificates twice a week.
  64. #~(job (lambda (now)
  65. (next-day-from (next-hour-from now '(3))
  66. '(2 5)))
  67. (string-append #$package "/bin/certbot renew"
  68. (string-concatenate
  69. (map (lambda (host)
  70. (string-append " -d " host))
  71. '#$hosts))))))))))
  72. (define certbot-activation
  73. (match-lambda
  74. (($ <certbot-configuration> package webroot hosts default-location)
  75. (with-imported-modules '((guix build utils))
  76. #~(begin
  77. (use-modules (guix build utils))
  78. (mkdir-p #$webroot)
  79. (for-each
  80. (lambda (host)
  81. (unless (file-exists? (in-vicinity "/etc/letsencrypt/live" host))
  82. (unless (zero? (system*
  83. (string-append #$certbot "/bin/certbot")
  84. "certonly" "--webroot" "-w" #$webroot
  85. "-d" host))
  86. (error "failed to acquire cert for host" host))))
  87. '#$hosts))))))
  88. (define certbot-nginx-server-configurations
  89. (match-lambda
  90. (($ <certbot-configuration> package webroot hosts default-location)
  91. (map
  92. (lambda (host)
  93. (nginx-server-configuration
  94. (listen '("80"))
  95. (ssl-certificate #f)
  96. (ssl-certificate-key #f)
  97. (server-name (list host))
  98. (locations
  99. (filter identity
  100. (list
  101. (nginx-location-configuration
  102. (uri "/.well-known")
  103. (body (list (list "root " webroot ";"))))
  104. default-location)))))
  105. hosts))))
  106. (define certbot-service-type
  107. (service-type (name 'certbot)
  108. (extensions
  109. (list (service-extension nginx-service-type
  110. certbot-nginx-server-configurations)
  111. (service-extension activation-service-type
  112. certbot-activation)
  113. (service-extension mcron-service-type
  114. certbot-renewal-jobs)))
  115. (compose concatenate)
  116. (extend (lambda (config additional-hosts)
  117. (certbot-configuration
  118. (inherit config)
  119. (hosts (append (certbot-configuration-hosts config)
  120. additional-hosts)))))
  121. (default-value (certbot-configuration))
  122. (description
  123. "Automatically renew @url{https://letsencrypt.org, Let's
  124. Encrypt} HTTPS certificates by adjusting the nginx web server configuration
  125. and periodically invoking @command{certbot}.")))