From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id qD35FmNlZmQsTgEASxT56A (envelope-from ) for ; Thu, 18 May 2023 19:50:27 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id YGugFmNlZmR0ewEAauVa8A (envelope-from ) for ; Thu, 18 May 2023 19:50:27 +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 25C7B15967 for ; Thu, 18 May 2023 19:50:27 +0200 (CEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pzhlO-0001IN-1E; Thu, 18 May 2023 13:50: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 1pzhlL-0001GW-Dg for guix-patches@gnu.org; Thu, 18 May 2023 13:50:03 -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 1pzhlL-0001DL-4Z for guix-patches@gnu.org; Thu, 18 May 2023 13:50:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pzhlL-0001hs-17 for guix-patches@gnu.org; Thu, 18 May 2023 13:50:03 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63402] [PATCH v4 2/4] services: wireguard: Clean-up configuration file serializer. Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 18 May 2023 17:50:02 +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.16844321446453 (code B ref 63402); Thu, 18 May 2023 17:50:02 +0000 Received: (at 63402) by debbugs.gnu.org; 18 May 2023 17:49:04 +0000 Received: from localhost ([127.0.0.1]:54403 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pzhkN-0001fu-LX for submit@debbugs.gnu.org; Thu, 18 May 2023 13:49:04 -0400 Received: from mail-qk1-f178.google.com ([209.85.222.178]:54517) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pzhkK-0001eg-37 for 63402@debbugs.gnu.org; Thu, 18 May 2023 13:49:00 -0400 Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-75783ac48e7so212798685a.0 for <63402@debbugs.gnu.org>; Thu, 18 May 2023 10:49:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684432134; x=1687024134; 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=mweOIvBXYiHwoWCpNrNTXmRvqevL4WPkYv5E8K/bjTo=; b=nmqyDtFDiF7BUkFBXe3h2XYONBsNAK7tZW94F+vU4UGhuwRR1bC0DcLjd+/GfFZD3f rrgDAPWgiD/yeKijugJ9fJQdem81N9kNeBSkVI2NTXDECOyX8Bqzel3mtXAO6TAmvaDk OSCz/UO+ppjPZrz0d5PggwLxiYskFOFCm7s5kI6G49nW1dEEax8ulMLfFHAlA7pzqdv5 WF3FKNVlWC8OqIULxMqJi27xLrzu9mG67nKvsW1YsTUGeNmQ+Jj7Z85DWvLbH8CA0N/Q ZKAZD+P6FW9CT/ibQAh4eUwBOFqvJ982wwtxGLpG0kJtZ1BO/mqqJ6zLt5N/7bikYkDs 40Ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684432134; x=1687024134; 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=mweOIvBXYiHwoWCpNrNTXmRvqevL4WPkYv5E8K/bjTo=; b=YjvFmmsTyawBj7yky7u7WIJUu5B7SFQK3V8o4GjiXxiXCBff41nX/hzQFgjJ03Zncx MxKLNnRv+R6YarKzMI9OzycsOTcpx9hFW6WCeDvxGye893z/45U4j124XvACrl4Rk1YV bdxw6wHGvEdZTcs6LxdJPumKhBVlCsvF/FH5aUktvaNaVdlMpvHVQ8AvgrIlRvAJTXr6 e1VDr2bdWmeQMnBRkHOVzxioKpdPVnC6rCdE4BY+CEw3Taw+voLgCa9jtp85cD3WSrSB VoQJPOESVRBag1Z9ZHhaCe3bxKpHV/rEiCiV41rWnOQnd49P6r4EOy0/jUHC9tjf42N6 eL5A== X-Gm-Message-State: AC+VfDx4q/J3X5FcS2D8mFLbJ3A0oW3QJKp3LK5t9Xq3FYP9EVAe30Q8 S8VtlxILJJdxzW+Jqex5ejPPOsmD0TLnpg== X-Google-Smtp-Source: ACHHUZ4sSHZLxA0GdN9ug6gGZoZcmZ3iihgBTby8hL80RIifc1X+Xk6sqAvVzQSfxqc2gxt7Jm8y3Q== X-Received: by 2002:a05:6214:411c:b0:5ef:67b9:8d37 with SMTP id kc28-20020a056214411c00b005ef67b98d37mr619645qvb.13.1684432134308; Thu, 18 May 2023 10:48:54 -0700 (PDT) Received: from localhost.localdomain (dsl-150-33.b2b2c.ca. [66.158.150.33]) by smtp.gmail.com with ESMTPSA id ml7-20020a056214584700b0061a0f7fb340sm689006qvb.6.2023.05.18.10.48.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 10:48:54 -0700 (PDT) From: Maxim Cournoyer Date: Thu, 18 May 2023 13:48:40 -0400 Message-Id: <235e307060c61947158361f4ef4eb40df72c79de.1684431342.git.maxim.cournoyer@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: 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-Flow: FLOW_IN X-Migadu-Country: US ARC-Seal: i=1; s=key1; d=yhetil.org; t=1684432227; a=rsa-sha256; cv=none; b=oO9RPFVH7hetDiz2bPfEKuYOFnkMV4ofn9cPBJVjbzs121AVbuj7bS3E1BuLjuIY8eGNk+ TCH32JaAnZ9M1biGrkJimp/MGrNEsRAjb8kRT4yVcorie9+X7NH0IlNTLLld7qAhX85vWA AGLo7lFklI28/FedDRq0SLnqZ3R9FYgkdJ6t/REvxzIumNsX2rypFyQowqc3Opqw8HilC6 kmRouNZDt0ofDoepE8z9U6St4bQGwEYzHI+6bDWffZ8+JXYzjYOlnbUvJp4nWd9iISbpwo pLEq9xzlc+sf9F+MT6cZ9WMWgoVE3LRdbN9Eu9m6fltvZk5Ru7SuIgANP3EdwA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20221208 header.b=nmqyDtFD; 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=1684432227; 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=mweOIvBXYiHwoWCpNrNTXmRvqevL4WPkYv5E8K/bjTo=; b=GPJqr5I500iq4f/3/2oAlUgIaiZmN0WdKOMB2DBtJWgbRTHLlSaiSgL/Do6O2DJOIrYzOI 8EH3OWT7gDLR5TWjsjF5t1ly03Tw3rU0BkmkC1l8cTXHUmBHqs9ynvgRZRuJ4O74zHX3nd oXT+1jfnI4IyPdMvnhRKoJUV50e25we58lOtmktI+QVi8jwonpZG9ryC4YNBxhC8TbCPGr xuQDe2qCyDsfDHSXhk86914n9XBzEIN5OknfAxPVz3/Mi5M1WglxoayUoZz64XxxOseHKw G4Z5VdT12xGrtUg2zt8TEo4gARmyAMTk9FjDsjJiC18b5k0dLzLyMBcIazQELg== X-Migadu-Scanner: scn1.migadu.com Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20221208 header.b=nmqyDtFD; 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-Migadu-Spam-Score: 6.31 X-Spam-Score: 6.31 X-Migadu-Queue-Id: 25C7B15967 X-TUID: 51AeVCmx6iws 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 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* (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