summary refs log tree commit diff
path: root/gnu/services
diff options
context:
space:
mode:
authorChristopher Baines <mail@cbaines.net>2018-03-04 15:05:02 +0000
committerChristopher Baines <mail@cbaines.net>2018-03-10 16:20:52 +0000
commit936e7a5245d5b50be8b968b39760220066ae24f5 (patch)
tree34e18deaae5e7c595092f656810485b332eabd14 /gnu/services
parent0fa4702c041a5746ec3a58b39e4276c39d18e7da (diff)
downloadguix-936e7a5245d5b50be8b968b39760220066ae24f5.tar.gz
services: Rework the PostgreSQL config file to use a record type.
For the default config file representation. This makes it possible to more
easily change the configuration file, and have dynamic content.

* gnu/services/databases.scm (<postgresql-config-file>): New record type.
(%default-postgres-config): Remove this, it's been replaced by the
configuration file.
(<postgresql-configuration>): Alter the default for the config file field.
(postgresql-service): Alter the default value for the config-file parameter.
Diffstat (limited to 'gnu/services')
-rw-r--r--gnu/services/databases.scm85
1 files changed, 65 insertions, 20 deletions
diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
index 3ca8f471fc..7799093264 100644
--- a/gnu/services/databases.scm
+++ b/gnu/services/databases.scm
@@ -29,8 +29,16 @@
   #:use-module (guix modules)
   #:use-module (guix records)
   #:use-module (guix gexp)
+  #:use-module (srfi srfi-1)
   #:use-module (ice-9 match)
-  #:export (postgresql-configuration
+  #:export (<postgresql-config-file>
+            postgresql-config-file
+            postgresql-config-file?
+            postgresql-config-file-log-destination
+            postgresql-config-file-hba-file
+            postgresql-config-file-ident-file
+            postgresql-config-file-extra-config
+
             postgresql-configuration?
             postgresql-service
             postgresql-service-type
@@ -68,6 +76,60 @@
 ;;;
 ;;; Code:
 
+(define %default-postgres-hba
+  (plain-file "pg_hba.conf"
+              "
+local	all	all			trust
+host	all	all	127.0.0.1/32 	trust
+host	all	all	::1/128 	trust"))
+
+(define %default-postgres-ident
+  (plain-file "pg_ident.conf"
+              "# MAPNAME       SYSTEM-USERNAME         PG-USERNAME"))
+
+(define-record-type* <postgresql-config-file>
+  postgresql-config-file make-postgresql-config-file
+  postgresql-config-file?
+  (log-destination postgresql-config-file-log-destination
+                   (default "syslog"))
+  (hba-file        postgresql-config-file-hba-file
+                   (default %default-postgres-hba))
+  (ident-file      postgresql-config-file-ident-file
+                   (default %default-postgres-ident))
+  (extra-config    postgresql-config-file-extra-config
+                   (default '())))
+
+(define-gexp-compiler (postgresql-config-file-compiler
+                       (file <postgresql-config-file>) system target)
+  (match file
+    (($ <postgresql-config-file> log-destination hba-file
+                                 ident-file extra-config)
+     (define (quote' string)
+       (if string
+           (list "'" string "'")
+           '()))
+
+     (define contents
+       (append-map
+        (match-lambda
+          ((key) '())
+          ((key . #f) '())
+          ((key values ...) `(,key " = " ,@values "\n")))
+
+        `(("log_destination" ,@(quote' log-destination))
+          ("hba_file" ,@(quote' hba-file))
+          ("ident_file" ,@(quote' ident-file))
+          ,@extra-config)))
+
+     (gexp->derivation
+      "postgresql.conf"
+      #~(call-with-output-file (ungexp output "out")
+          (lambda (port)
+            (display
+             (string-append #$@contents)
+             port)))
+      #:local-build? #t))))
+
 (define-record-type* <postgresql-configuration>
   postgresql-configuration make-postgresql-configuration
   postgresql-configuration?
@@ -78,27 +140,10 @@
   (locale         postgresql-configuration-locale
                   (default "en_US.utf8"))
   (config-file    postgresql-configuration-file
-                  (default %default-postgres-config))
+                  (default (postgresql-config-file)))
   (data-directory postgresql-configuration-data-directory
                   (default "/var/lib/postgresql/data")))
 
-(define %default-postgres-hba
-  (plain-file "pg_hba.conf"
-              "
-local	all	all			trust
-host	all	all	127.0.0.1/32 	trust
-host	all	all	::1/128 	trust"))
-
-(define %default-postgres-ident
-  (plain-file "pg_ident.conf"
-             "# MAPNAME       SYSTEM-USERNAME         PG-USERNAME"))
-
-(define %default-postgres-config
-  (mixed-text-file "postgresql.conf"
-                   "log_destination = 'syslog'\n"
-                   "hba_file = '" %default-postgres-hba "'\n"
-                   "ident_file = '" %default-postgres-ident "'\n"))
-
 (define %postgresql-accounts
   (list (user-group (name "postgres") (system? #t))
         (user-account
@@ -192,7 +237,7 @@ host	all	all	::1/128 	trust"))
 (define* (postgresql-service #:key (postgresql postgresql)
                              (port 5432)
                              (locale "en_US.utf8")
-                             (config-file %default-postgres-config)
+                             (config-file (postgresql-config-file))
                              (data-directory "/var/lib/postgresql/data"))
   "Return a service that runs @var{postgresql}, the PostgreSQL database server.