From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id cHuUKn4CY2T/twAASxT56A (envelope-from ) for ; Tue, 16 May 2023 06:11:42 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id CM97Kn4CY2TiWgAA9RJhRA (envelope-from ) for ; Tue, 16 May 2023 06:11:42 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 5D89142910 for ; Tue, 16 May 2023 06:11:42 +0200 (CEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pym1i-0008JV-5E; Tue, 16 May 2023 00:11:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pym1g-0008J0-2k for guix-patches@gnu.org; Tue, 16 May 2023 00:11:04 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pym1f-0002pe-I6 for guix-patches@gnu.org; Tue, 16 May 2023 00:11:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pym1f-00023L-DR for guix-patches@gnu.org; Tue, 16 May 2023 00:11:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63402] [PATCH v3 2/3] services: wireguard: Clean-up configuration file serializer. Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Tue, 16 May 2023 04:11:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63402 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 63402@debbugs.gnu.org Cc: Maxim Cournoyer Received: via spool by 63402-submit@debbugs.gnu.org id=B63402.16842102557846 (code B ref 63402); Tue, 16 May 2023 04:11:03 +0000 Received: (at 63402) by debbugs.gnu.org; 16 May 2023 04:10:55 +0000 Received: from localhost ([127.0.0.1]:44874 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pym1W-00022O-7Q for submit@debbugs.gnu.org; Tue, 16 May 2023 00:10:55 -0400 Received: from mail-qk1-f178.google.com ([209.85.222.178]:60717) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pym1T-000227-D8 for 63402@debbugs.gnu.org; Tue, 16 May 2023 00:10:52 -0400 Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-75773a7bd66so1055306585a.1 for <63402@debbugs.gnu.org>; Mon, 15 May 2023 21:10:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684210245; x=1686802245; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jpQwKmoBpnx+YxEEHaVPQfQrfFXNaLnDWAMgJbg8Ry0=; b=VVKeaGT+EzTqaRhzmg7buDIHUYnwJplU/FjJWhv0ccYJH7duR1wmkQpP+r6iB8s29i W4ZmVT0g10skBQNgXkQ/TVxU20ufupu+C1De/30mKLLx/38Hgc8atbPi9jkRiwQLQmd/ TOMKZM/v2Vvx8bvBVQ8MAzSGjSe49uLVdD1/gBT3ryYP4w9+nG9nmyqIJNs78hr4hufG IcFa3q+9f3++ze9jJY7bg8qeax2n0OEu8d3rp9E+xeuSWzpekXv2TnDOl4nyWRnUUoAP antl2faGPfLpxcdWLBSCzxclsP46TQHqSzxLBx3EjIVWtRv/3pnicXuaSR+eAnDkxbVI fPkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684210245; x=1686802245; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jpQwKmoBpnx+YxEEHaVPQfQrfFXNaLnDWAMgJbg8Ry0=; b=i9pEjTBO2df+FcrzQQcGBgAdc9rsRcM2xrAP4mJ/HYlYW2TwoCZQ13Sla9ES6uIzVi fE9aCXI9L3FDERnPMTHGNzxEgofBohqf6ZoNjo6DhL3zGzl7C0UuYY/DdjRB0Ck+STf/ SlmkFa0g17FaSLFulgo5MmJP3a2m39qmBesnsHYFVl8PHRSYzytWLTZavkNsy4buKXv/ 8aqiKAwnk0Xe72hlS1A5F31NPLSD9gELpdIXDE7MWnUWVxsxCQyIrBDlryhTYwCEN5Tl Vnout531rLbvDhFH9pq9FMaxfqkw7gP7N3jrb7wpBN9QMrvD7jiqS8L8kgd+AT7H4eFC h4Ug== X-Gm-Message-State: AC+VfDxOoyQY7i8BW0gnsistQ0IGjQGzhPkFQDZUb/bU2OH3S1orD4yA nvKHR+fErmTicMrUnwW2FxchRpbDexjVxDY1 X-Google-Smtp-Source: ACHHUZ5ZnzZmp2b8+bPrWl5RH2JB9WDWm/fwhzg1z8ljCqmBtHKPi14eNLMm/GIkVXnojSOLLqU4Aw== X-Received: by 2002:ad4:5cce:0:b0:5e8:979f:2e49 with SMTP id iu14-20020ad45cce000000b005e8979f2e49mr54008527qvb.41.1684210245515; Mon, 15 May 2023 21:10:45 -0700 (PDT) Received: from localhost.localdomain (dsl-205-236-230-106.b2b2c.ca. [205.236.230.106]) by smtp.gmail.com with ESMTPSA id f21-20020a0caa95000000b005f2dba7a5b0sm5367347qvb.132.2023.05.15.21.10.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 21:10:45 -0700 (PDT) From: Maxim Cournoyer Date: Tue, 16 May 2023 00:09:07 -0400 Message-Id: X-Mailer: git-send-email 2.39.2 In-Reply-To: <76b34e5229e0e97068cb3bd42152f29630a8dbfc.1684210148.git.maxim.cournoyer@gmail.com> References: <76b34e5229e0e97068cb3bd42152f29630a8dbfc.1684210148.git.maxim.cournoyer@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN ARC-Seal: i=1; s=key1; d=yhetil.org; t=1684210302; a=rsa-sha256; cv=none; b=DhJGuh816AbEaG9o4E1sYlqsIkzHuk5LJd7Vk2DZxIk1Me4aoIBg5VjR4SUFkHI+TGqrnD 9LM8PZjgXhJqf66rxno3FDH9SaMssQOlWXSOBJXTFYvH/saCmWhMPnIUt1mAk6ltIjAdko 8X2R1/7tyPIO8DaRqtoqUVasl+a1DAuJ3EIopA7XgejEuevbW/xPijqEy5NyQLnlfN4wXk P7rvaVbcUqk6r1M88H4CAvWk+wWL9cupJwB4P+owr4g0M7ZWZpuvFoytUd9rH0o+a6cyNp d6o9xI8EZ3mO3T8mZ4ipg92VTzzJ9ex8wPNBmQEcysVT5hYZPYzmCYxWIid2Xw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20221208 header.b=VVKeaGT+; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1684210302; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=jpQwKmoBpnx+YxEEHaVPQfQrfFXNaLnDWAMgJbg8Ry0=; b=onZ7h+sszaX+32kyAQfUAQlLC5I8ZjNymufAq1OyiDr9YSH/V+wbqDYoc8iSJuGCUR3uGw /966sAwEaXm1H6SaJUhiTK4US0BNjmmbw/1LJkXN/ItSnQq2FqiJ+ywNSjtHlz2lYUFX0U Pfq+5JkbayfrOOu902eBDgqH1VOzuoSIihFralfhei+cZKmRcPMYOWjes0V6N/haBFilOi 6WEwnMUMgiqk355TwcWk3AHws7dTE0VCk7sNi0Imy5FiDDWav2rXRROQAvRWVi24NtTI6V p7/f9tUTcBpx3henpspj2IFsrXP8utDFFwyDPpCoJno5MUgIlMhlZ+xDDfN4hg== X-Migadu-Spam-Score: -0.31 X-Spam-Score: -0.31 X-Migadu-Queue-Id: 5D89142910 X-Migadu-Scanner: scn0.migadu.com Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20221208 header.b=VVKeaGT+; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-TUID: JAHBJMq8IoPA Previously, the generated config file would contain arbitrary whitespace that made it look ugly. * gnu/services/vpn.scm () [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 4499a911d6..51c75a7dfc 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 e21f999bc0..3f66db79de 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* (peers wireguard-configuration-peers ;list of (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* (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 + (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