Subject: Re: [PATCH] make postgresql find its plugins

From: Julien Lepiller <[email-masked]>
Date: Fri, 14 Sep 2018 22:03:53 +0200

Le Fri, 14 Sep 2018 17:29:19 +0200,
Gábor Boskovits <[email-masked]> a écrit :

> This looks good to me.
> 
> The extensions field is a good idea.
> 
> I also believe that after adding an extensions field it would be
> easier to document this.

Here is a new series of 2 patches: the first one is unchanged (only
rebased to master) and the second one introduces the extensions field
and documents it. I added an example in the documentation.

> 
> Do you think that it might be possible to list these extension
> packages somehow, or even stop them being directly installable,
> noting to use the extensions field in your service definition? Would
> that make sense?

Extension packages still have to be visible for users to list them in
the new field, so it seems hard to hide them at the same time :) I
agree it would make sense to be able to list all such extensions, and
so is listing extensions to other packages. Maybe we can think of a
generic mechanism for that?
From db5d8bd717f84956a083c6dd7c772433c089da81 Mon Sep 17 00:00:00 2001
From: Julien Lepiller <[email-masked]>
Date: Sat, 28 Jul 2018 17:38:38 +0200
Subject: [PATCH 1/2] gnu: postgresql: Fix finding extensions.

* gnu/packages/patches/postgresql-disable-resolve_symlinks.patch: New
file.
* gnu/local.mk (dist_patch_DATA): Add it.
* gnu/packages/databases.scm (postgresql)[source]: Use it.
---
 gnu/local.mk                                  |  1 +
 gnu/packages/databases.scm                    |  5 ++--
 .../postgresql-disable-resolve_symlinks.patch | 25 +++++++++++++++++++
 3 files changed, 29 insertions(+), 2 deletions(-)
 create mode 100644 gnu/packages/patches/postgresql-disable-resolve_symlinks.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 7b230cb6f..010aa76e5 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1053,6 +1053,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/plotutils-libpng-jmpbuf.patch		\
   %D%/packages/patches/portaudio-audacity-compat.patch		\
   %D%/packages/patches/portmidi-modular-build.patch		\
+  %D%/packages/patches/postgresql-disable-resolve_symlinks.patch	\
   %D%/packages/patches/potrace-tests.patch			\
   %D%/packages/patches/procmail-ambiguous-getline-debian.patch  \
   %D%/packages/patches/procmail-CVE-2014-3618.patch		\
diff --git a/gnu/packages/databases.scm b/gnu/packages/databases.scm
index 9db892a0b..722099732 100644
--- a/gnu/packages/databases.scm
+++ b/gnu/packages/databases.scm
@@ -18,7 +18,7 @@
 ;;; Copyright © 2016 Andy Patterson <[email-masked]>
 ;;; Copyright © 2016 Danny Milosavljevic <[email-masked]>
 ;;; Copyright © 2016, 2017, 2018 Marius Bakke <[email-masked]>
-;;; Copyright © 2017 Julien Lepiller <[email-masked]>
+;;; Copyright © 2017, 2018 Julien Lepiller <[email-masked]>
 ;;; Copyright © 2017 Thomas Danckaert <[email-masked]>
 ;;; Copyright © 2017 Jelle Licht <[email-masked]>
 ;;; Copyright © 2017 Adriano Peluso <[email-masked]>
@@ -812,7 +812,8 @@ as a drop-in replacement of MySQL.")
                                   version "/postgresql-" version ".tar.bz2"))
               (sha256
                (base32
-                "04a07jkvc5s6zgh6jr78149kcjmsxclizsqabjw44ld4j5n633kc"))))
+                "04a07jkvc5s6zgh6jr78149kcjmsxclizsqabjw44ld4j5n633kc"))
+              (patches (search-patches "postgresql-disable-resolve_symlinks.patch"))))
     (build-system gnu-build-system)
     (arguments
      `(#:configure-flags '("--with-uuid=e2fs")
diff --git a/gnu/packages/patches/postgresql-disable-resolve_symlinks.patch b/gnu/packages/patches/postgresql-disable-resolve_symlinks.patch
new file mode 100644
index 000000000..97ef6928f
--- /dev/null
+++ b/gnu/packages/patches/postgresql-disable-resolve_symlinks.patch
@@ -0,0 +1,25 @@
+From 223c82d1d6ed1f29f26307249827ff679e09c780 Mon Sep 17 00:00:00 2001
+From: Julien Lepiller <[email-masked]>
+Date: Sat, 28 Jul 2018 12:22:12 +0200
+Subject: [PATCH] disable resolve_symlink
+
+---
+ src/common/exec.c | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/src/common/exec.c b/src/common/exec.c
+index 878fc29..6b3e283 100644
+--- a/src/common/exec.c
++++ b/src/common/exec.c
+@@ -218,6 +218,8 @@ find_my_exec(const char *argv0, char *retpath)
+ static int
+ resolve_symlinks(char *path)
+ {
++	// On GuixSD we *want* stuff relative to symlinks.
++	return 0;
+ #ifdef HAVE_READLINK
+ 	struct stat buf;
+ 	char		orig_wd[MAXPGPATH],
+--
+2.18.0
+
-- 
2.18.0

From 7a4684462142031c1590c6a0ef12a54aff04e396 Mon Sep 17 00:00:00 2001
From: Julien Lepiller <[email-masked]>
Date: Fri, 14 Sep 2018 21:44:08 +0200
Subject: [PATCH 2/2] gnu: postgresql: Add extensions.

* gnu/services/databases.scm (postgresql-configuration): Add extensions.
(postgresql-shepherd-service): New key #:extensions.
* doc/guix.texi (Database Services): Document it.
---
 doc/guix.texi              | 34 +++++++++++++++++++++++++++++++++-
 gnu/services/databases.scm | 37 ++++++++++++++++++++++++++++++-------
 2 files changed, 63 insertions(+), 8 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index cccf166d0..ecd486112 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -13718,13 +13718,45 @@ The @code{(gnu services databases)} module provides the following services.
 
 @deffn {Scheme Procedure} postgresql-service [#:postgresql postgresql] @
        [#:config-file] [#:data-directory ``/var/lib/postgresql/data''] @
-       [#:port 5432] [#:locale ``en_US.utf8'']
+       [#:port 5432] [#:locale ``en_US.utf8''] [#:extensions '()]
 Return a service that runs @var{postgresql}, the PostgreSQL database
 server.
 
 The PostgreSQL daemon loads its runtime configuration from @var{config-file},
 creates a database cluster with @var{locale} as the default
 locale, stored in @var{data-directory}.  It then listens on @var{port}.
+
+@cindex postgresql extensions
+Additional extensions are loaded from packages listed in @var{extensions}.
+Extensions are available at runtime.  For instance, to create a geographic
+database using the @code{postgis} extension, a user can configure the
+postgresql-service as in this example:
+
+@cindex postgis
+@example
+(use-package-modules databases geo)
+
+(operating-system
+  ...
+  ;; postgresql is required to run `psql' but postgis is not required for
+  ;; proper operation.
+  (packages (cons* postgresql %base-packages))
+  (services
+    (cons*
+      (postgresql-service #:extensions (list postgis))
+      %base-services)))
+@end example
+
+Then the extension becomes visible and you can initialise an empty geographic
+database in this way:
+
+@example
+psql -U postgres
+> create database postgistest;
+> \connect postgistest;
+> create extension postgis;
+> create extension postgis_topology;
+@end example
 @end deffn
 
 @deffn {Scheme Procedure} mysql-service [#:config (mysql-configuration)]
diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
index aff78a056..37bef2095 100644
--- a/gnu/services/databases.scm
+++ b/gnu/services/databases.scm
@@ -4,6 +4,7 @@
 ;;; Copyright © 2016 Leo Famulari <[email-masked]>
 ;;; Copyright © 2017 Christopher Baines <[email-masked]>
 ;;; Copyright © 2018 Clément Lassieur <[email-masked]>
+;;; Copyright © 2018 Julien Lepiller <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -150,7 +151,9 @@ host	all	all	::1/128 	trust"))
   (config-file    postgresql-configuration-file
                   (default (postgresql-config-file)))
   (data-directory postgresql-configuration-data-directory
-                  (default "/var/lib/postgresql/data")))
+                  (default "/var/lib/postgresql/data"))
+  (extensions     postgresql-configuration-extensions
+                  (default '())))
 
 (define %postgresql-accounts
   (list (user-group (name "postgres") (system? #t))
@@ -162,15 +165,33 @@ host	all	all	::1/128 	trust"))
          (home-directory "/var/empty")
          (shell (file-append shadow "/sbin/nologin")))))
 
+(define (final-postgresql postgresql extensions)
+  (if (null? extensions)
+    postgresql
+    (package
+      (inherit postgresql)
+      (source #f)
+      (build-system trivial-build-system)
+      (arguments
+       `(#:modules ((guix build utils))
+         #:builder
+         (begin
+           (use-modules (guix build utils) (srfi srfi-26))
+           (union-build (assoc-ref %outputs "out") %build-inputs)
+           #f)))
+      (inputs
+       `(("postgresql" ,postgresql)
+         ,@(map (lambda (extension) (cons "extension" extension)) extensions))))))
+
 (define postgresql-activation
   (match-lambda
-    (($ <postgresql-configuration> postgresql port locale config-file data-directory)
+    (($ <postgresql-configuration> postgresql port locale config-file data-directory extensions)
      #~(begin
          (use-modules (guix build utils)
                       (ice-9 match))
 
          (let ((user (getpwnam "postgres"))
-               (initdb (string-append #$postgresql "/bin/initdb"))
+               (initdb (string-append #$(final-postgresql postgresql extensions) "/bin/initdb"))
                (initdb-args
                 (append
                  (if #$locale
@@ -202,7 +223,7 @@ host	all	all	::1/128 	trust"))
 
 (define postgresql-shepherd-service
   (match-lambda
-    (($ <postgresql-configuration> postgresql port locale config-file data-directory)
+    (($ <postgresql-configuration> postgresql port locale config-file data-directory extensions)
      (let* ((pg_ctl-wrapper
              ;; Wrapper script that switches to the 'postgres' user before
              ;; launching daemon.
@@ -214,7 +235,7 @@ host	all	all	::1/128 	trust"))
                   (match (command-line)
                     ((_ mode)
                      (let ((user (getpwnam "postgres"))
-                           (pg_ctl #$(file-append postgresql "/bin/pg_ctl"))
+                           (pg_ctl #$(file-append (final-postgresql postgresql extensions) "/bin/pg_ctl"))
                            (options (format #f "--config-file=~a -p ~d"
                                             #$config-file #$port)))
                        (setgid (passwd:gid user))
@@ -253,7 +274,8 @@ host	all	all	::1/128 	trust"))
                              (port 5432)
                              (locale "en_US.utf8")
                              (config-file (postgresql-config-file))
-                             (data-directory "/var/lib/postgresql/data"))
+                             (data-directory "/var/lib/postgresql/data")
+                             (extensions '()))
   "Return a service that runs @var{postgresql}, the PostgreSQL database server.
 
 The PostgreSQL daemon loads its runtime configuration from @var{config-file}
@@ -264,7 +286,8 @@ and stores the database cluster in @var{data-directory}."
             (port port)
             (locale locale)
             (config-file config-file)
-            (data-directory data-directory))))
+            (data-directory data-directory)
+            (extensions extensions))))
 
 
 ;;;
-- 
2.18.0