From patchwork Thu May 18 17:48:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Cournoyer X-Patchwork-Id: 50131 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 4B9FD27BBE9; Thu, 18 May 2023 18:50:29 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 2A32F27BBE2 for ; Thu, 18 May 2023 18:50:27 +0100 (BST) 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 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+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches 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)