aboutsummaryrefslogtreecommitdiff
path: root/gnu/packages/docbook.scm
blob: d114e24ee794be9e885f58839c039d3e72d30dfa (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2014 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2014 Eric Bavier <bavier@member.fsf.org>
;;; Copyright © 2016 Mathieu Lirzin <mthl@gnu.org>
;;; Copyright © 2018 Tobias Geerinckx-Rice <me@tobias.gr>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (gnu packages docbook)
  #:use-module (gnu packages)
  #:use-module (gnu packages compression)
  #:use-module (gnu packages imagemagick)
  #:use-module (gnu packages inkscape)
  #:use-module (gnu packages tex)
  #:use-module (gnu packages python)
  #:use-module (gnu packages base)
  #:use-module (gnu packages xml)
  #:use-module (guix licenses)
  #:use-module (guix packages)
  #:use-module (guix download)
  #:use-module (guix build-system trivial)
  #:use-module (guix build-system python))

(define-public docbook-xml
  (package
    (name "docbook-xml")
    (version "4.5")
    (source (origin
              (method url-fetch)
              (uri (string-append "http://www.docbook.org/xml/" version
                                  "/docbook-xml-" version ".zip"))
              (sha256
               (base32
                "1d671lcjckjri28xfbf6dq7y3xnkppa910w1jin8rjc35dx06kjf"))))
    (build-system trivial-build-system)
    (arguments
     '(#:builder (begin
                   (use-modules (guix build utils))

                   (let* ((unzip
                           (string-append (assoc-ref %build-inputs "unzip")
                                          "/bin/unzip"))
                          (source (assoc-ref %build-inputs "source"))
                          (out    (assoc-ref %outputs "out"))
                          (dtd    (string-append out "/xml/dtd/docbook")))
                     (mkdir-p dtd)
                     (with-directory-excursion dtd
                       (invoke unzip source))
                     (substitute* (string-append out "/xml/dtd/docbook/catalog.xml")
                       (("uri=\"") 
                        (string-append 
                         "uri=\"file://" dtd "/")))
                     #t))
                 #:modules ((guix build utils))))
    (native-inputs `(("unzip" ,unzip)))
    (home-page "http://docbook.org")
    (synopsis "DocBook XML DTDs for document authoring")
    (description
     "DocBook is general purpose XML and SGML document type particularly well
suited to books and papers about computer hardware and software (though it is
by no means limited to these applications.)  This package provides XML DTDs.")
    (license (x11-style "" "See file headers."))))

(define-public docbook-xml-4.4
  (package (inherit docbook-xml)
    (version "4.4")
    (source (origin
              (method url-fetch)
              (uri (string-append "http://www.docbook.org/xml/" version
                                  "/docbook-xml-" version ".zip"))
              (sha256
               (base32
                "141h4zsyc71sfi2zzd89v4bb4qqq9ca1ri9ix2als9f4i3mmkw82"))))))

(define-public docbook-xml-4.3
  (package (inherit docbook-xml)
    (version "4.3")
    (source (origin
              (method url-fetch)
              (uri (string-append "http://www.docbook.org/xml/" version
                                  "/docbook-xml-" version ".zip"))
              (sha256
               (base32
                "0r1l2if1z4wm2v664sqdizm4gak6db1kx9y50jq89m3gxaa8l1i3"))))))

(define-public docbook-xml-4.2
  (package (inherit docbook-xml)
    (version "4.2")
    (source (origin
              (method url-fetch)
              (uri (string-append "http://www.docbook.org/xml/" version
                                  "/docbook-xml-" version ".zip"))
              (sha256
               (base32
                "18hgwvmywh6a5jh38szjmg3hg2r4v5lb6r3ydc3rd8cp9wg61i5c"))))))

(define-public docbook-xml-4.1.2
  (package (inherit docbook-xml)
    (version "4.1.2")
    (source (origin
              (method url-fetch)
              (uri (string-append "http://www.docbook.org/xml/" version
                                  "/docbkx412.zip"))
              (sha256
               (base32
                "0wkp5rvnqj0ghxia0558mnn4c7s3n501j99q2isp3sp0ci069w1h"))))
    (arguments
     '(#:modules ((guix build utils))
       #:builder
       (begin
         (use-modules (guix build utils))
         (let ((source (assoc-ref %build-inputs "source"))
               (unzip  (string-append (assoc-ref %build-inputs "unzip")
                                      "/bin/unzip"))
               (dtd    (string-append (assoc-ref %outputs "out")
                                      "/xml/dtd/docbook")))
           (mkdir-p dtd)
           (invoke unzip source "-d" dtd)))))))

(define-public docbook-xsl
  (package
    (name "docbook-xsl")
    (version "1.79.1")
    (source (origin
              (method url-fetch)
              (uri (string-append "mirror://sourceforge/docbook/docbook-xsl/"
                                  version "/docbook-xsl-" version ".tar.bz2"))
              (patches (search-patches "docbook-xsl-nonrecursive-string-subst.patch"))
              (sha256
               (base32
                "0s59lihif2fr7rznckxr2kfyrvkirv76r1zvidp9b5mj28p4apvj"))
              (modules '((guix build utils)))
              (snippet
               '(begin
                  (for-each delete-file (find-files "." "\\.jar$"))
                  #t))))
    (build-system trivial-build-system)
    (arguments
     `(#:builder (let ((name-version (string-append ,name "-" ,version)))
                   (use-modules (guix build utils))

                   (let* ((bzip2  (assoc-ref %build-inputs "bzip2"))
                          (xz     (assoc-ref %build-inputs "xz"))
                          (tar    (assoc-ref %build-inputs "tar"))
                          (source (assoc-ref %build-inputs "source"))
                          (out    (assoc-ref %outputs "out"))
                          (xsl    (string-append out "/xml/xsl")))
                     (setenv "PATH" (string-append bzip2 "/bin" ":" xz "/bin"))
                     (invoke (string-append tar "/bin/tar") "xvf" source)

                     (mkdir-p xsl)
                     (copy-recursively name-version
                                       (string-append xsl "/" name-version))

                     (substitute* (string-append xsl "/" name-version "/catalog.xml")
                       (("rewritePrefix=\"./")
                        (string-append "rewritePrefix=\"file://" xsl "/"
                                       name-version "/")))
                     #t))
       #:modules ((guix build utils))))
    (native-inputs `(("bzip2" ,bzip2)
                     ("xz" ,xz)         ;needed for repacked tarballs
                     ("tar" ,tar)))
    (home-page "http://docbook.org")
    (synopsis "DocBook XSL style sheets for document authoring")
    (description
     "This package provides XSL style sheets for DocBook.")
    (license (x11-style "" "See 'COPYING' file."))))

(define-public dblatex
  (package
    (name "dblatex")
    (version "0.3.10")
    (source (origin
              (method url-fetch)
              (uri (string-append "mirror://sourceforge/dblatex/dblatex/"
                                  "dblatex-" version "/dblatex-"
                                  version ".tar.bz2"))
              (sha256
               (base32
                "1yicd861rqz78i2khl35j7nvc0ccv4jx4hzqrbhll17082vrdmkg"))))
    (build-system python-build-system)
    ;; TODO: Add xfig/transfig for fig2dev utility
    (inputs
     `(("texlive" ,(texlive-union (list texlive-amsfonts
                                        texlive-latex-anysize
                                        texlive-latex-appendix
                                        texlive-latex-changebar
                                        texlive-latex-colortbl
                                        texlive-latex-eepic
                                        texlive-latex-eso-pic
                                        texlive-latex-fancybox
                                        texlive-latex-fancyhdr
                                        texlive-latex-fancyvrb
                                        texlive-latex-float
                                        texlive-latex-footmisc
                                        texlive-latex-hyperref
                                        texlive-latex-jknapltx
                                        texlive-latex-listings
                                        texlive-latex-multirow
                                        texlive-latex-oberdiek
                                        texlive-latex-overpic
                                        texlive-latex-pdfpages
                                        texlive-latex-subfigure
                                        texlive-latex-titlesec
                                        texlive-latex-url
                                        texlive-latex-wasysym

                                        texlive-fonts-ec
                                        texlive-fonts-rsfs
                                        texlive-fonts-stmaryrd

                                        texlive-generic-ifxetex)))
       ("imagemagick" ,imagemagick)     ;for convert
       ("inkscape" ,inkscape)           ;for svg conversion
       ("docbook" ,docbook-xml)
       ("libxslt" ,libxslt)))           ;for xsltproc
    (arguments
     `(#:python ,python-2               ;'print' syntax
       ;; Using setuptools causes an invalid "package_base" path in
       ;; out/bin/.dblatex-real due to a missing leading '/'.  This is caused
       ;; by dblatex's setup.py stripping the root path when creating the
       ;; script.  (dblatex's setup.py still uses distutils and thus has to
       ;; create the script by itself. The feature for creating scripts is one
       ;; of setuptools' features.)
       ;; See this thread for details:
       ;; https://lists.gnu.org/archive/html/guix-devel/2016-12/msg00030.html
       #:use-setuptools? #f
       #:tests? #f                      ;no 'test' command
       #:phases
       (modify-phases %standard-phases
         (add-after 'wrap 'set-path
           (lambda* (#:key inputs outputs #:allow-other-keys)
             (let ((out (assoc-ref outputs "out")))
               ;; dblatex executes helper programs at runtime.
               (wrap-program (string-append out "/bin/dblatex")
                 `("PATH" ":" prefix
                   ,(map (lambda (input)
                           (string-append (assoc-ref inputs input)
                                          "/bin"))
                         '("libxslt" "texlive"
                           "imagemagick" "inkscape"))))
               #t))))))
    (home-page "http://dblatex.sourceforge.net")
    (synopsis "DocBook to LaTeX Publishing")
    (description
     "DocBook to LaTeX Publishing transforms your SGML/XML DocBook documents
to DVI, PostScript or PDF by translating them in pure LaTeX as a first
process.  MathML 2.0 markups are supported too.  It started as a clone of
DB2LaTeX.")
    ;; lib/contrib/which is under an X11 license
    (license gpl2+)))
(openvpn-shepherd-service 'client)) (service-extension account-service-type (const %openvpn-accounts)) (service-extension activation-service-type (const %openvpn-activation)))) (description "Run the OpenVPN client service, which allows you to connect to an existing @acronym{VPN, virtual private network}.") (default-value (openvpn-client-configuration)))) (define-deprecated (openvpn-client-service #:key (config (openvpn-client-configuration))) openvpn-client-service-type (service openvpn-client-service-type config)) (define-deprecated (openvpn-server-service #:key (config (openvpn-server-configuration))) openvpn-server-service-type (service openvpn-server-service-type config)) (define (generate-openvpn-server-documentation) (generate-documentation `((openvpn-server-configuration ,openvpn-server-configuration-fields (ccd openvpn-ccd-configuration)) (openvpn-ccd-configuration ,openvpn-ccd-configuration-fields)) 'openvpn-server-configuration)) (define (generate-openvpn-client-documentation) (generate-documentation `((openvpn-client-configuration ,openvpn-client-configuration-fields (remote openvpn-remote-configuration)) (openvpn-remote-configuration ,openvpn-remote-configuration-fields)) 'openvpn-client-configuration)) ;;; ;;; Strongswan. ;;; (define-record-type* <strongswan-configuration> strongswan-configuration make-strongswan-configuration strongswan-configuration? (strongswan strongswan-configuration-strongswan ;file-like (default strongswan)) (ipsec-conf strongswan-configuration-ipsec-conf ;string|#f (default #f)) (ipsec-secrets strongswan-configuration-ipsec-secrets ;string|#f (default #f))) ;; In the future, it might be worth implementing a record type to configure ;; all of the plugins, but for *most* basic use cases, simply creating the ;; files will be sufficient. Same is true of charon-plugins. (define strongswand-configuration-files (list "charon" "charon-logging" "pki" "pool" "scepclient" "swanctl" "tnc")) ;; Plugins to load. All of these plugins end up as configuration files in ;; strongswan.d/charon/. (define charon-plugins (list "aes" "aesni" "attr" "attr-sql" "chapoly" "cmac" "constraints" "counters" "curl" "curve25519" "dhcp" "dnskey" "drbg" "eap-aka-3gpp" "eap-aka" "eap-dynamic" "eap-identity" "eap-md5" "eap-mschapv2" "eap-peap" "eap-radius" "eap-simaka-pseudonym" "eap-simaka-reauth" "eap-simaka-sql" "eap-sim" "eap-sim-file" "eap-tls" "eap-tnc" "eap-ttls" "ext-auth" "farp" "fips-prf" "gmp" "ha" "hmac" "kernel-netlink" "led" "md4" "md5" "mgf1" "nonce" "openssl" "pem" "pgp" "pkcs12" "pkcs1" "pkcs7" "pkcs8" "pubkey" "random" "rc2" "resolve" "revocation" "sha1" "sha2" "socket-default" "soup" "sql" "sqlite" "sshkey" "tnc-tnccs" "vici" "x509" "xauth-eap" "xauth-generic" "xauth-noauth" "xauth-pam" "xcbc")) (define (strongswan-configuration-file config) (match-record config <strongswan-configuration> (strongswan ipsec-conf ipsec-secrets) (if (eq? (string? ipsec-conf) (string? ipsec-secrets)) (let* ((strongswan-dir (computed-file "strongswan.d" #~(begin (mkdir #$output) ;; Create all of the configuration files strongswan.d/. (map (lambda (conf-file) (let* ((filename (string-append #$output "/" conf-file ".conf"))) (call-with-output-file filename (lambda (port) (display "# Created by 'strongswan-service'\n" port))))) (list #$@strongswand-configuration-files)) (mkdir (string-append #$output "/charon")) ;; Create all of the plugin configuration files. (map (lambda (plugin) (let* ((filename (string-append #$output "/charon/" plugin ".conf"))) (call-with-output-file filename (lambda (port) (format port "~a { load = yes }" plugin))))) (list #$@charon-plugins)))))) ;; Generate our strongswan.conf to reflect the user configuration. (computed-file "strongswan.conf" #~(begin (call-with-output-file #$output (lambda (port) (display "# Generated by 'strongswan-service'.\n" port) (format port "charon { load_modular = yes plugins { include ~a/charon/*.conf" #$strongswan-dir) (if #$ipsec-conf (format port " stroke { load = yes secrets_file = ~a } } } starter { config_file = ~a } include ~a/*.conf" #$ipsec-secrets #$ipsec-conf #$strongswan-dir) (format port " } } include ~a/*.conf" #$strongswan-dir))))))) (throw 'error (G_ "strongSwan ipsec-conf and ipsec-secrets must both be (un)set"))))) (define (strongswan-shepherd-service config) (let* ((ipsec (file-append strongswan "/sbin/ipsec")) (strongswan-conf-path (strongswan-configuration-file config))) (list (shepherd-service (requirement '(networking)) (provision '(ipsec)) (start #~(make-forkexec-constructor (list #$ipsec "start" "--nofork") #:environment-variables (list (string-append "STRONGSWAN_CONF=" #$strongswan-conf-path)))) (stop #~(make-kill-destructor)) (documentation "strongSwan's charon IKE keying daemon for IPsec VPN."))))) (define strongswan-service-type (service-type (name 'strongswan) (extensions (list (service-extension shepherd-root-service-type strongswan-shepherd-service))) (default-value (strongswan-configuration)) (description "Connect to an IPsec @acronym{VPN, Virtual Private Network} with strongSwan."))) ;;; ;;; Wireguard. ;;; (define-record-type* <wireguard-peer> wireguard-peer make-wireguard-peer wireguard-peer? (name wireguard-peer-name) (endpoint wireguard-peer-endpoint (default #f)) ;string (public-key wireguard-peer-public-key) ;string (preshared-key wireguard-peer-preshared-key (default #f)) ;string (allowed-ips wireguard-peer-allowed-ips) ;list of strings (keep-alive wireguard-peer-keep-alive (default #f))) ;integer (define-record-type* <wireguard-configuration> wireguard-configuration make-wireguard-configuration wireguard-configuration? (wireguard wireguard-configuration-wireguard ;file-like (default wireguard-tools)) (interface wireguard-configuration-interface ;string (default "wg0")) (addresses wireguard-configuration-addresses ;string (default '("10.0.0.1/32"))) (port wireguard-configuration-port ;integer (default 51820)) (private-key wireguard-configuration-private-key ;maybe-string (default "/etc/wireguard/private.key")) (bootstrap-private-key? wireguard-configuration-bootstrap-private-key? ;boolean (default #t)) (peers wireguard-configuration-peers ;list of <wiregard-peer> (default '())) (dns wireguard-configuration-dns ;list of strings (default '())) (monitor-ips? wireguard-configuration-monitor-ips? ;boolean (default #f)) (monitor-ips-interval wireguard-configuration-monitor-ips-interval (default '(next-minute (range 0 60 5)))) ;string | list (pre-up wireguard-configuration-pre-up ;list of strings (default '())) (post-up wireguard-configuration-post-up ;list of strings (default '())) (pre-down wireguard-configuration-pre-down ;list of strings (default '())) (post-down wireguard-configuration-post-down ;list of strings (default '())) (table wireguard-configuration-table ;string (default "auto"))) (define (wireguard-configuration-file config) (define (peer->config peer) (match-record peer <wireguard-peer> (name public-key endpoint allowed-ips keep-alive) (let ((lines (list (format #f "[Peer] #~a" name) (format #f "PublicKey = ~a" public-key) (format #f "AllowedIPs = ~{~a~^, ~}" allowed-ips) (format #f "~@[Endpoint = ~a~]" endpoint) (format #f "~@[PersistentKeepalive = ~a~]" keep-alive)))) (string-join (remove string-null? lines) "\n")))) (define (peers->preshared-keys peer keys) (let ((public-key (wireguard-peer-public-key peer)) (preshared-key (wireguard-peer-preshared-key peer))) (if preshared-key (cons* public-key preshared-key keys) keys))) (match-record config <wireguard-configuration> (wireguard interface addresses port private-key peers dns pre-up post-up pre-down post-down table) (let* ((config-file (string-append interface ".conf")) (peer-keys (fold peers->preshared-keys (list) peers)) (peers (map peer->config peers)) (config (computed-file "wireguard-config" #~(begin (use-modules (ice-9 format) (srfi srfi-1)) (define lines (list "[Interface]" (if (null? '#$addresses) "" (format #f "Address = ~{~a~^, ~}" (list #$@addresses))) (format #f "~@[Table = ~a~]" #$table) (if (null? '#$pre-up) "" (format #f "~{PreUp = ~a~%~}" (list #$@pre-up))) (if #$private-key (format #f "PostUp = ~a set %i private-key ~a\ ~{ peer ~a preshared-key ~a~}" #$(file-append wireguard "/bin/wg") #$private-key (list #$@peer-keys)) "") (if (null? '#$post-up) "" (format #f "~{PostUp = ~a~%~}" (list #$@post-up))) (if (null? '#$pre-down) "" (format #f "~{PreDown = ~a~%~}" (list #$@pre-down))) (if (null? '#$post-down) "" (format #f "~{PostDown = ~a~%~}" (list #$@post-down))) (format #f "~@[ListenPort = ~a~]" #$port) (if (null? '#$dns) "" (format #f "DNS = ~{~a~^, ~}" (list #$@dns))))) (mkdir #$output) (chdir #$output) (call-with-output-file #$config-file (lambda (port) (format port "~a~%~%~{~a~%~^~%~}" (string-join (remove string-null? lines) "\n") '#$peers))))))) (file-append config "/" config-file)))) (define (wireguard-activation config) (match-record config <wireguard-configuration> (private-key bootstrap-private-key? wireguard) #~(begin (use-modules (guix build utils) (ice-9 popen) (ice-9 rdelim)) (when (and #$private-key #$bootstrap-private-key?) (mkdir-p (dirname #$private-key)) (unless (file-exists? #$private-key) (let* ((pipe (open-input-pipe (string-append #$(file-append wireguard "/bin/wg") " genkey"))) (key (read-line pipe))) (call-with-output-file #$private-key (lambda (port) (display key port))) (chmod #$private-key #o400) (close-pipe pipe))))))) ;;; XXX: Copied from (guix scripts pack), changing define to define*. (define-syntax-rule (define-with-source (variable args ...) body body* ...) "Bind VARIABLE to a procedure accepting ARGS defined as BODY, also setting its source property." (begin (define* (variable args ...) body body* ...) (eval-when (load eval) (set-procedure-property! variable 'source '(define* (variable args ...) body body* ...))))) (define (wireguard-service-name interface) "Return the WireGuard service name (a symbol) configured to use INTERFACE." (symbol-append 'wireguard- (string->symbol interface))) (define-with-source (strip-port/maybe endpoint #:key ipv6?) "Strip the colon and port, if present in ENDPOINT, a string." (if ipv6? (if (string-prefix? "[" endpoint) (first (string-split (string-drop endpoint 1) #\])) ;ipv6 endpoint) (first (string-split endpoint #\:)))) ;ipv4 (define* (ipv4-address? address) "Predicate to check whether ADDRESS is a valid IPv4 address." (let ((address (strip-port/maybe address))) (false-if-exception (->bool (getaddrinfo address #f AI_NUMERICHOST AF_INET))))) (define* (ipv6-address? address) "Predicate to check whether ADDRESS is a valid IPv6 address." (let ((address (strip-port/maybe address #:ipv6? #t))) (false-if-exception (->bool (getaddrinfo address #f AI_NUMERICHOST AF_INET6))))) (define (host-name? name) "Predicate to check whether NAME is a host name, i.e. not an IP address." (not (or (ipv6-address? name) (ipv4-address? name)))) (define (endpoint-host-names peers) "Return an association list of endpoint host names keyed by their peer public key, if any." (reverse (fold (lambda (peer host-names) (let ((public-key (wireguard-peer-public-key peer)) (endpoint (wireguard-peer-endpoint peer))) (if (and endpoint (host-name? endpoint)) (cons (cons public-key endpoint) host-names) host-names))) '() peers))) (define (wireguard-shepherd-service config) (match-record config <wireguard-configuration> (wireguard interface) (let ((wg-quick (file-append wireguard "/bin/wg-quick")) (config (wireguard-configuration-file config))) (list (shepherd-service (requirement '(networking)) (provision (list (wireguard-service-name interface))) (start #~(lambda _ (invoke #$wg-quick "up" #$config))) (stop #~(lambda _ (invoke #$wg-quick "down" #$config) #f)) ;stopped! (actions (list (shepherd-configuration-action config))) (documentation "Run the Wireguard VPN tunnel")))))) (define (wireguard-monitoring-jobs config) ;; Loosely based on WireGuard's own 'reresolve-dns.sh' shell script (see: ;; https://raw.githubusercontent.com/WireGuard/wireguard-tools/ ;; master/contrib/reresolve-dns/reresolve-dns.sh). (match-record config <wireguard-configuration> (interface monitor-ips? monitor-ips-interval peers) (let ((host-names (endpoint-host-names peers))) (if monitor-ips? (if (null? host-names) (begin (warn "monitor-ips? is #t but no host name to monitor") '()) ;; The mcron monitor job may be a string or a list; ungexp strips ;; one quote level, which must be added back when a list is ;; provided. (list #~(job (if (string? #$monitor-ips-interval) #$monitor-ips-interval '#$monitor-ips-interval) #$(program-file (format #f "wireguard-~a-monitoring" interface) (with-imported-modules (source-module-closure '((gnu services herd) (guix build utils))) #~(begin (use-modules (gnu services herd) (guix build utils) (ice-9 popen) (ice-9 match) (ice-9 textual-ports) (srfi srfi-1) (srfi srfi-26)) (define (resolve-host name) "Return the IP address resolved from NAME." (let* ((ai (car (getaddrinfo name))) (sa (addrinfo:addr ai))) (inet-ntop (sockaddr:fam sa) (sockaddr:addr sa)))) (define wg #$(file-append wireguard-tools "/bin/wg")) #$(procedure-source strip-port/maybe) (define service-name '#$(wireguard-service-name interface)) (when (live-service-running (current-service service-name)) (let* ((pipe (open-pipe* OPEN_READ wg "show" #$interface "endpoints")) (lines (string-split (get-string-all pipe) #\newline)) ;; IPS is an association list mapping ;; public keys to IP addresses. (ips (map (match-lambda ((public-key ip) (cons public-key (strip-port/maybe ip)))) (map (cut string-split <> #\tab) (remove string-null? lines))))) (close-pipe pipe) (for-each (match-lambda ((key . host-name) (let ((resolved-ip (resolve-host (strip-port/maybe host-name))) (current-ip (assoc-ref ips key))) (unless (string=? resolved-ip current-ip) (format #t "resetting `~a' peer \ endpoint to `~a' due to stale IP (`~a' instead of `~a')~%" key host-name current-ip resolved-ip) (invoke wg "set" #$interface "peer" key "endpoint" host-name))))) '#$host-names))))))))) '())))) ;monitor-ips? is #f (define wireguard-service-type (service-type (name 'wireguard) (extensions (list (service-extension shepherd-root-service-type wireguard-shepherd-service) (service-extension activation-service-type wireguard-activation) (service-extension profile-service-type (compose list wireguard-configuration-wireguard)) (service-extension mcron-service-type wireguard-monitoring-jobs))) (description "Set up Wireguard @acronym{VPN, Virtual Private Network} tunnels.")))