From: Maxim Cournoyer <maxim.cournoyer@gmail.com>
To: 63402@debbugs.gnu.org
Cc: Maxim Cournoyer <maxim.cournoyer@gmail.com>
Subject: [bug#63402] [PATCH v4 2/4] services: wireguard: Clean-up configuration file serializer.
Date: Thu, 18 May 2023 13:48:40 -0400 [thread overview]
Message-ID: <235e307060c61947158361f4ef4eb40df72c79de.1684431342.git.maxim.cournoyer@gmail.com> (raw)
In-Reply-To: <cover.1684431342.git.maxim.cournoyer@gmail.com>
Previously, the generated config file would contain arbitrary whitespace that
made it look ugly.
* gnu/services/vpn.scm (<wireguard-configuration>) [dns]: Change default value
from #f to '().
(wireguard-configuration-file): Use match-record. Format each line
individually, assembling the lines at the end to avoid extraneous white space.
* doc/guix.texi (VPN Services): Update doc.
---
doc/guix.texi | 2 +-
gnu/services/vpn.scm | 119 ++++++++++++++++---------------------------
2 files changed, 46 insertions(+), 75 deletions(-)
diff --git a/doc/guix.texi b/doc/guix.texi
index ef96d064ed..b61a2ceb5b 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -32588,7 +32588,7 @@ VPN Services
@item @code{port} (default: @code{51820})
The port on which to listen for incoming connections.
-@item @code{dns} (default: @code{#f})
+@item @code{dns} (default: @code{'())})
The DNS server(s) to announce to VPN clients via DHCP.
@item @code{monitor-ips?} (default: @code{#f})
diff --git a/gnu/services/vpn.scm b/gnu/services/vpn.scm
index c11faed879..a34889a6cc 100644
--- a/gnu/services/vpn.scm
+++ b/gnu/services/vpn.scm
@@ -44,6 +44,7 @@ (define-module (gnu services vpn)
#:use-module (guix i18n)
#:use-module (guix deprecation)
#:use-module (srfi srfi-1)
+ #:use-module (ice-9 format)
#:use-module (ice-9 match)
#:use-module (ice-9 regex)
#:export (openvpn-client-service ; deprecated
@@ -745,7 +746,7 @@ (define-record-type* <wireguard-configuration>
(peers wireguard-configuration-peers ;list of <wiregard-peer>
(default '()))
(dns wireguard-configuration-dns ;list of strings
- (default #f))
+ (default '()))
(monitor-ips? wireguard-configuration-monitor-ips? ;boolean
(default #f))
(monitor-ips-interval wireguard-configuration-monitor-ips-interval
@@ -763,24 +764,15 @@ (define-record-type* <wireguard-configuration>
(define (wireguard-configuration-file config)
(define (peer->config peer)
- (let ((name (wireguard-peer-name peer))
- (public-key (wireguard-peer-public-key peer))
- (endpoint (wireguard-peer-endpoint peer))
- (allowed-ips (wireguard-peer-allowed-ips peer))
- (keep-alive (wireguard-peer-keep-alive peer)))
- (format #f "[Peer] #~a
-PublicKey = ~a
-AllowedIPs = ~a
-~a~a"
- name
- public-key
- (string-join allowed-ips ",")
- (if endpoint
- (format #f "Endpoint = ~a\n" endpoint)
- "")
- (if keep-alive
- (format #f "PersistentKeepalive = ~a\n" keep-alive)
- "\n"))))
+ (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))
@@ -799,65 +791,44 @@ (define (wireguard-configuration-file config)
(computed-file
"wireguard-config"
#~(begin
+ (use-modules (ice-9 format)
+ (srfi srfi-1))
+
+ (define lines
+ (list
+ "[Interface]"
+ #$@(if (null? addresses)
+ '()
+ (list (format #f "Address = ~{~a~^, ~}"
+ addresses)))
+ (format #f "~@[Table = ~a~]" #$table)
+ #$@(if (null? pre-up)
+ '()
+ (list (format #f "~{PreUp = ~a~%~}" pre-up)))
+ (format #f "PostUp = ~a set %i private-key ~a\
+~{ peer ~a preshared-key ~a~}" #$(file-append wireguard "/bin/wg")
+#$private-key '#$peer-keys)
+ #$@(if (null? post-up)
+ '()
+ (list (format #f "~{PostUp = ~a~%~}" post-up)))
+ #$@(if (null? pre-down)
+ '()
+ (list (format #f "~{PreDown = ~a~%~}" pre-down)))
+ #$@(if (null? post-down)
+ '()
+ (list (format #f "~{PostDown = ~a~%~}" post-down)))
+ (format #f "~@[ListenPort = ~a~]" #$port)
+ #$@(if (null? dns)
+ '()
+ (list (format #f "~{DNS = ~{~a~^, ~}" dns)))))
+
(mkdir #$output)
(chdir #$output)
(call-with-output-file #$config-file
(lambda (port)
- (let ((format (@ (ice-9 format) format)))
- (format port "[Interface]
-Address = ~a
-~a
-~a
-PostUp = ~a set %i private-key ~a~{ peer ~a preshared-key ~a~}
-~a
-~a
-~a
-~a
-~a
-~{~a~^~%~}"
- #$(string-join addresses ",")
- #$(if table
- (format #f "Table = ~a" table)
- "")
- #$(if (null? pre-up)
- ""
- (string-join
- (map (lambda (command)
- (format #f "PreUp = ~a" command))
- pre-up)
- "\n"))
- #$(file-append wireguard "/bin/wg")
- #$private-key
- '#$peer-keys
- #$(if (null? post-up)
- ""
- (string-join
- (map (lambda (command)
- (format #f "PostUp = ~a" command))
- post-up)
- "\n"))
- #$(if (null? pre-down)
- ""
- (string-join
- (map (lambda (command)
- (format #f "PreDown = ~a" command))
- pre-down)
- "\n"))
- #$(if (null? post-down)
- ""
- (string-join
- (map (lambda (command)
- (format #f "PostDown = ~a" command))
- post-down)
- "\n"))
- #$(if port
- (format #f "ListenPort = ~a" port)
- "")
- #$(if dns
- (format #f "DNS = ~a"
- (string-join dns ","))
- "")
- (list #$@peers)))))))))
+ (format port "~a~%~%~{~a~%~^~%~}"
+ (string-join (remove string-null? lines) "\n")
+ '#$peers)))))))
(file-append config "/" config-file))))
(define (wireguard-activation config)
--
2.39.2
next prev parent reply other threads:[~2023-05-18 17:50 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-10 1:08 [bug#63402] [PATCH 0/1] Add a dynamic IP monitoring option to Wireguard service Maxim Cournoyer
2023-05-10 1:09 ` [bug#63403] [PATCH 1/1] services: wireguard: Implement a dynamic IP monitoring feature Maxim Cournoyer
2023-05-15 15:57 ` Maxim Cournoyer
2023-05-15 16:13 ` [bug#63402] [PATCH v2] " Maxim Cournoyer
2023-05-16 4:09 ` [bug#63402] [PATCH v3 1/3] " Maxim Cournoyer
2023-05-16 4:09 ` [bug#63402] [PATCH v3 2/3] services: wireguard: Clean-up configuration file serializer Maxim Cournoyer
2023-05-16 4:09 ` [bug#63402] [PATCH v3 3/3] services: wireguard: Workaround keep-alives bug Maxim Cournoyer
2023-05-18 17:48 ` [bug#63402] [PATCH v4 0/4] Implement a dynamic IP monitoring feature Maxim Cournoyer
2023-05-18 17:48 ` [bug#63402] [PATCH v4 1/4] services: wireguard: " Maxim Cournoyer
2023-05-18 17:48 ` Maxim Cournoyer [this message]
2023-05-18 17:48 ` [bug#63402] [PATCH v4 3/4] services: wireguard: Add a 'configuration' action Maxim Cournoyer
2023-05-18 17:48 ` [bug#63402] [PATCH v4 4/4] gnu: linux-libre: Apply wireguard patch fixing keep-alive bug Maxim Cournoyer
2023-05-19 1:59 ` [bug#63402] [PATCH v5 0/5] Implement a dynamic IP monitoring feature Maxim Cournoyer
2023-05-19 1:59 ` [bug#63402] [PATCH v5 1/5] services: herd: Add a new 'current-service' procedure Maxim Cournoyer
2023-05-22 15:00 ` [bug#63403] [PATCH 1/1] services: wireguard: Implement a dynamic IP monitoring feature Ludovic Courtès
2023-05-22 23:22 ` [bug#63402] bug#63403: " Maxim Cournoyer
2023-05-24 14:44 ` [bug#63403] " Ludovic Courtès
2023-07-21 2:15 ` Maxim Cournoyer
2023-05-19 1:59 ` [bug#63402] [PATCH v5 2/5] " Maxim Cournoyer
2023-05-22 15:03 ` [bug#63402] bug#63403: [PATCH 1/1] " Ludovic Courtès
2023-05-22 23:32 ` Maxim Cournoyer
2023-05-24 14:53 ` [bug#63403] " Ludovic Courtès
2023-05-24 22:12 ` Bruno Victal
2023-05-25 15:13 ` Maxim Cournoyer
2023-05-24 17:25 ` [bug#63402] [PATCH v5 2/5] " Bruno Victal
2023-07-21 3:55 ` Maxim Cournoyer
2023-07-21 13:23 ` Bruno Victal
2023-07-21 15:56 ` Maxim Cournoyer
2023-07-21 16:18 ` bug#63402: " Maxim Cournoyer
2023-05-19 1:59 ` [bug#63402] [PATCH v5 3/5] services: wireguard: Clean-up configuration file serializer Maxim Cournoyer
2023-05-19 1:59 ` [bug#63402] [PATCH v5 4/5] services: wireguard: Add a 'configuration' action Maxim Cournoyer
2023-05-19 1:59 ` [bug#63402] [PATCH v5 5/5] gnu: linux-libre: Apply wireguard patch fixing keep-alive bug Maxim Cournoyer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://guix.gnu.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=235e307060c61947158361f4ef4eb40df72c79de.1684431342.git.maxim.cournoyer@gmail.com \
--to=maxim.cournoyer@gmail.com \
--cc=63402@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/guix.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).