From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id OGlnKb3YZmQiWQEASxT56A (envelope-from ) for ; Fri, 19 May 2023 04:02:37 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id oEBqKL3YZmQ+pQAAG6o9tA (envelope-from ) for ; Fri, 19 May 2023 04:02:37 +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 4B62DE239 for ; Fri, 19 May 2023 04:02:37 +0200 (CEST) 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 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=1684461757; a=rsa-sha256; cv=none; b=gjh3ZvZL6fKHsxQtT8NXex4jgsthjNQ3oTwEER3pKWtgrie+XC0GrmW4sPRkregJSxaST0 CCH1gSCsiFiv0P9u/WWaCw3xrO8DUzKRL5TqpJOXdQF+PrdHH9rLY46G7OqYJzIZoFGQNa XmRvd9EBfJ3XGJwbx4L5hFjwG6P8vQEbJ/RRwyqLkCZPp4/Czkr05SIiQnBGotjgwr+aMN 1KhNikkCFsxrf9l4/euAyTgxk4gY5pZlVvreeEvULWfAHcKQBWW2c2/rBzFaGnuiv+awiM 9YfbOb8YqwbUahiaYQfEch53QC5ZyCWrngyBcD43Y3bV8Z2QduG1U+UiV0HSqA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20221208 header.b=PtOvVfcS; 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=1684461757; 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=xo5zpV0uTb1+0y0D4cUPi0Qz/eedNC0VVUztyJk81eg=; b=nHyECogcz93vV/Cbq06PW11dlgVT3w12+3HL/4uM4VzMb1LEj3zHy3cOSe9niPkrTD7N0r oJGPB0rbA8m6y4v8UIje44jp814TLJ5D0LMWRwhdVNbDfAr39H/9PtcCQ4g61LaqP7y/dN 7rj52dCpqLlWjuDSpI3tLbAl5ya/FAeydvG8EU17Wnp9psjof/sOHW1KP8skTqcjvEcqIH 4Mll5dbx1MrWzL/84DIM4IgZXe0NEY5FGK60YlpHu/Otu+AleaSLqV8ESjevAkJcs8C/4d EUhbvQQohI4BWxWB0aOfMCr7z3bVX/mV7XzGMqKVDCmyILGW2+b6U9efF/tZnw== X-Migadu-Spam-Score: -1.59 X-Spam-Score: -1.59 X-Migadu-Queue-Id: 4B62DE239 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=PtOvVfcS; 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: 54EKjBk7m/1K 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) -- 2.40.1