Subject: [bug#73876] [PATCH v2 3/3] gnu: databases: Rewrite final-postgresql for postgresql-service.

From: Nicolas Graves <[email-masked]>
Date: Sun, 20 Oct 2024 16:21:35 +0200

* gnu/services/databases.scm (final-postgresql): Rewrite using a
directory-union and rename to extend-postgresql.
---
 gnu/services/databases.scm | 31 +++++++++----------------------
 1 file changed, 9 insertions(+), 22 deletions(-)

diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
index fa332d7978..aed0f414ab 100644
--- a/gnu/services/databases.scm
+++ b/gnu/services/databases.scm
@@ -205,26 +205,13 @@ (define (create-postgresql-account config)
                (home-directory "/var/empty")
                (shell (file-append shadow "/sbin/nologin")))))))
 
-(define (final-postgresql postgresql extension-packages)
-  (if (null? extension-packages)
-    postgresql
-    (package
-      (inherit postgresql)
-      (source #f)
-      (build-system trivial-build-system)
-      (arguments
-       `(#:modules ((guix build utils) (guix build union))
-         #:builder
-         (begin
-           (use-modules (guix build utils) (guix build union) (srfi srfi-26))
-           (union-build (assoc-ref %outputs "out")
-                        (map (lambda (input) (cdr input))
-                             %build-inputs))
-           #t)))
-      (inputs
-       `(("postgresql" ,postgresql)
-         ,@(map (lambda (extension) (list "extension" extension))
-                extension-packages))))))
+(define-public (extend-postgresql postgresql extensions)
+  (if (null? extensions)
+      postgresql
+      (let ((packages (cons* postgresql extensions)))
+        (directory-union
+         (string-join (map package-name packages) "+")
+         packages))))
 
 (define postgresql-activation
   (match-lambda
@@ -250,7 +237,7 @@ (define postgresql-activation
 
            (unless (file-exists? #$data-directory)
              (let ((initdb (string-append
-                            #$(final-postgresql postgresql
+                            #$(extend-postgresql postgresql
                                                 extension-packages)
                             "/bin/initdb"))
                    (initdb-args
@@ -297,7 +284,7 @@ (define postgresql-shepherd-service
                     ((_ mode)
                      (let ((user (getpwnam "postgres"))
                            (pg_ctl #$(file-append
-                                      (final-postgresql postgresql
+                                      (extend-postgresql postgresql
                                                         extension-packages)
                                                   "/bin/pg_ctl"))
                            (options (format #f "--config-file=~a -p ~d"
-- 
2.46.0