Subject: [PATCH 2/2] services: nftables: Make it extendable.

From: Brice Waegeneire <[email-masked]>
Date: Mon, 12 Jul 2021 23:08:23 +0200

* 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))))
 
 
-- 
2.31.1