all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Brice Waegeneire <brice@waegenei.re>
To: 49540@debbugs.gnu.org
Cc: iyzsong@member.fsf.org, solene@perso.pw
Subject: [bug#49540] [PATCH 2/2] services: nftables: Make it extendable.
Date: Mon, 12 Jul 2021 23:08:23 +0200	[thread overview]
Message-ID: <20210712210823.16987-2-brice@waegenei.re> (raw)
In-Reply-To: <20210712210543.16598-1-brice@waegenei.re>

* gnu/services/networking.scm (%default-nftables-rules): New variable…
(define-record-type): …replace %default-nftables-ruleset with it.
(%default-nftables-ruleset): Deprecate it.
(nftables-ruleset): New procedure…
(nftables-shepherd-service): …use it.
(nftables-service-type): Make it extendable.
---
 gnu/services/networking.scm | 40 ++++++++++++++++++++++++++++++-------
 1 file changed, 33 insertions(+), 7 deletions(-)

diff --git a/gnu/services/networking.scm b/gnu/services/networking.scm
index 3058c14caf..53c06dcfed 100644
--- a/gnu/services/networking.scm
+++ b/gnu/services/networking.scm
@@ -1714,9 +1714,8 @@ COMMIT
 ;;; nftables
 ;;;
 
-(define %default-nftables-ruleset
-  (plain-file "nftables.conf"
-              "# A simple and safe firewall
+(define %default-nftables-rules
+  "# A simple and safe firewall
 
 # Start with our table clean of previous state
 add table inet guix
@@ -1752,7 +1751,11 @@ table inet guix {
     type filter hook output priority 0; policy accept;
   }
 }
-"))
+")
+
+(define-deprecated %default-nftables-ruleset
+  %default-nftables-rules
+  (plain-file "nftables.conf" %default-nftables-rules))
 
 (define-record-type* <nftables-configuration>
   nftables-configuration
@@ -1760,13 +1763,28 @@ table inet guix {
   nftables-configuration?
   (package nftables-configuration-package
            (default nftables))
-  (ruleset nftables-configuration-ruleset ; file-like object
-           (default %default-nftables-ruleset)))
+  ; file-like object | list of strings and file-like objects
+  (ruleset nftables-configuration-ruleset
+           (default (list %default-nftables-rules))))
+
+(define (nftables-ruleset ruleset)
+  (if (file-like? ruleset)
+      ruleset
+      (apply mixed-text-file
+             `("nftables.conf"
+               ,@(fold-right
+                 (lambda (rule result)
+                   (if (file-like? rule)
+                       (append (list "include \"" rule "\"\n") result)
+                       (append (list rule "\n") result)))
+                 '()
+                 ruleset)))))
 
 (define nftables-shepherd-service
   (match-lambda
     (($ <nftables-configuration> package ruleset)
-     (let ((nft (file-append package "/sbin/nft")))
+     (let ((nft (file-append package "/sbin/nft"))
+           (ruleset (nftables-ruleset ruleset)))
        (shepherd-service
         (documentation "Packet filtering and classification")
         (provision '(nftables))
@@ -1785,6 +1803,14 @@ table inet guix {
                              (compose list nftables-shepherd-service))
           (service-extension profile-service-type
                              (compose list nftables-configuration-package))))
+   (compose concatenate)
+   (extend (lambda (config additional-rules)
+             (let ((ruleset (nftables-configuration-ruleset config)))
+               (nftables-configuration
+                (inherit config)
+                (ruleset (if (list? ruleset)
+                             (append ruleset additional-rules)
+                             (cons* ruleset additional-rules)))))))
    (default-value (nftables-configuration))))
 
 \f
-- 
2.31.1





      parent reply	other threads:[~2021-07-12 21:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-12 21:05 [bug#49540] [PATCH 0/2] services: nftables: Make it extandable Brice Waegeneire
2021-07-12 21:08 ` [bug#49540] [PATCH 1/2] services: nftables: Only manage delete our own table Brice Waegeneire
2021-07-12 21:08 ` Brice Waegeneire [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210712210823.16987-2-brice@waegenei.re \
    --to=brice@waegenei.re \
    --cc=49540@debbugs.gnu.org \
    --cc=iyzsong@member.fsf.org \
    --cc=solene@perso.pw \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.