From patchwork Fri May 19 01:59:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Cournoyer X-Patchwork-Id: 50144 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 04F7D27BBE9; Fri, 19 May 2023 03:02:39 +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=ham 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 57B0C27BBE2 for ; Fri, 19 May 2023 03:02:37 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pzpRU-0000b9-Iu; Thu, 18 May 2023 22:02:04 -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 1pzpRT-0000aZ-BL for guix-patches@gnu.org; Thu, 18 May 2023 22:02: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 1pzpRT-0006JV-2e for guix-patches@gnu.org; Thu, 18 May 2023 22:02:03 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pzpRS-00074c-Sm for guix-patches@gnu.org; Thu, 18 May 2023 22:02:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#63402] [PATCH v5 3/5] services: wireguard: Clean-up configuration file serializer. Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 19 May 2023 02:02: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.168446168227110 (code B ref 63402); Fri, 19 May 2023 02:02:02 +0000 Received: (at 63402) by debbugs.gnu.org; 19 May 2023 02:01:22 +0000 Received: from localhost ([127.0.0.1]:54952 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pzpQo-00073B-5K for submit@debbugs.gnu.org; Thu, 18 May 2023 22:01:22 -0400 Received: from mail-qk1-f171.google.com ([209.85.222.171]:48345) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pzpQl-00072u-Ke for 63402@debbugs.gnu.org; Thu, 18 May 2023 22:01:20 -0400 Received: by mail-qk1-f171.google.com with SMTP id af79cd13be357-757731a32ecso144411085a.0 for <63402@debbugs.gnu.org>; Thu, 18 May 2023 19:01:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684461674; x=1687053674; 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=xo5zpV0uTb1+0y0D4cUPi0Qz/eedNC0VVUztyJk81eg=; b=PtOvVfcSi5hnaemvJTqFcoIIvIxkl7vehLcC7aYgG26SAAcPhZ9NgZPXOoM7vA1HFj 6C7PEPEPIXZ7XL5YCafpZPmuiANAdAGRTKSLB/z4uNTLYwYlKnF1Wn9Thj0fKUNWKGxM UrWzJyX7DHwEdKeN7etK48s3OTf6tWGz1hfXg35RlNnZyKdYAqpy9D9Mfbcepe8e4MEB 2gme/MWwNhehYY5WooEJtBm9mra9ltOor4WB//mUFRkvcbzIaqq6NhTQ5FUamBzSwG4t Lo/wFR5AHgHGt3wMS8oHm0cgGP43si4c78TTynJtETqtFuDmLr16IgQOa8JDYPHKOWYZ A5hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684461674; x=1687053674; 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=xo5zpV0uTb1+0y0D4cUPi0Qz/eedNC0VVUztyJk81eg=; b=KTRmTTJAw6ReTb4UkwHF363IYEZUSbw/ajWsDpkIkvKX9yqJ+AiwUUipWU5fUGrhle NWokwwV0xwgLhCCEQyEc6fWd89dEGOur+9LA6Ab2lU90v6MWtuCN9lFT36nUMkcE19lZ RsXQ49rVaT6oEjkRslIE0kbry6ZP4tBm6K4sP5mcFPchohaNNuOPiEqeUTS+251LZXz6 VO7jaJwtdnlGKJQ+MRFhEXF5CmbtZ2cT3E3XFWGM+bOTUIsvuroer3PdLsY2W2LLbRAd V/kY9ysTxgW53d8rFnC0+mYEjtwxDhApXca7sN0ApmS7bjIaoJQlXaX8XFkhkC462YiN R1xA== X-Gm-Message-State: AC+VfDy45NCOVWVJXLUp/SywGdTWWwcjFTNgxhTdeoP0h8whBaUL0fdN tvfULSj1hXw2fUZ5BMazQ/45qBbPLXWkPQ== X-Google-Smtp-Source: ACHHUZ5lcbUoUjZUw08K0ZB0DfgQk/boXtAOQ7wAK5Fem/d5sTdgTBspyQXe6Sh4N6iWIwWus11R8w== X-Received: by 2002:a05:6214:509b:b0:61a:943c:11fd with SMTP id kk27-20020a056214509b00b0061a943c11fdmr1856105qvb.4.1684461673926; Thu, 18 May 2023 19:01:13 -0700 (PDT) Received: from localhost.localdomain (dsl-150-33.b2b2c.ca. [66.158.150.33]) by smtp.gmail.com with ESMTPSA id mg14-20020a056214560e00b0062389d885f5sm964348qvb.47.2023.05.18.19.01.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 May 2023 19:01:13 -0700 (PDT) From: Maxim Cournoyer Date: Thu, 18 May 2023 21:59:15 -0400 Message-Id: <21fe58a5dac60b6d9640c19a57ac1187180d3df3.1684461197.git.maxim.cournoyer@gmail.com> X-Mailer: git-send-email 2.40.1 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 b19ba887a1..e2f46852e2 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -32639,7 +32639,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 9cf08c194a..8740722b6f 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)