Patchwork make nginx service extensible

login
register
mail settings
Submitter Julien Lepiller
Date Dec. 13, 2016, 8:56 p.m.
Message ID <20161213215615.5518d4c0@lepiller.eu>
Download mbox | patch
Permalink /patch/18432/
State New
Headers show

Comments

Julien Lepiller - Dec. 13, 2016, 8:56 p.m.
Hi,

here are three patches for the nginx service. I finally worked on
them.

1. s/vhost/server/ in the web.scm and guix.texi
2. fix a mistake with the list of names and index.
3. make the service extensible, change the default options and document
   it.

I hope that's correct now :)

Patch

From a0e24caf1f8a7d89c4ecafc57f9cfe2c098dc89c Mon Sep 17 00:00:00 2001
From: Julien Lepiller <julien@lepiller.eu>
Date: Tue, 13 Dec 2016 21:00:53 +0100
Subject: [PATCH 3/3] services: Make nginx service extensible

* gnu/services/web.scm (nginx-service-type): Make extensible.
  (nginx-service): Change default options.
---
 doc/guix.texi        | 17 +++++++++++++++--
 gnu/services/web.scm | 34 ++++++++++++++++++++++++++--------
 2 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index d1836ee04..7a7058b1b 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -11713,8 +11713,8 @@  The @code{(gnu services web)} module provides the following service:
 @deffn {Scheme Procedure} nginx-service [#:nginx nginx] @
        [#:log-directory ``/var/log/nginx''] @
        [#:run-directory ``/var/run/nginx''] @
-       [#:server-list (list (nginx-server-configuration))] @
-       [#:config-file]
+       [#:server-list '()] @
+       [#:config-file @code{#f}]
 
 Return a service that runs @var{nginx}, the nginx web server.
 
@@ -11730,6 +11730,19 @@  this to work, use the default value for @var{config-file}.
 
 @end deffn
 
+@deffn {Scheme Variable} nginx-service-type
+This is type for the nginx web server.
+
+This service can be extended to add more vhosts than the default one.
+
+@example
+(simple-service 'my-extra-vhost nginx-service-type
+                (list (nginx-vhost-configuration (https-port #f)
+                                                 (root "/srv/http/extra-website"))))
+@end example
+
+@end deffn
+
 @deftp {Data Type} nginx-server-configuration
 Data type representing the configuration of an nginx server block.
 This type has the following parameters:
diff --git a/gnu/services/web.scm b/gnu/services/web.scm
index a36352225..db895405a 100644
--- a/gnu/services/web.scm
+++ b/gnu/services/web.scm
@@ -27,6 +27,7 @@ 
   #:use-module (gnu packages web)
   #:use-module (guix records)
   #:use-module (guix gexp)
+  #:use-module (srfi srfi-1)
   #:use-module (ice-9 match)
   #:export (nginx-configuration
             nginx-configuration?
@@ -67,6 +68,7 @@ 
   (nginx         nginx-configuration-nginx)         ;<package>
   (log-directory nginx-configuration-log-directory) ;string
   (run-directory nginx-configuration-run-directory) ;string
+  (server-blocks nginx-configuration-server-blocks) ;list
   (file          nginx-configuration-file))         ;string | file-like
 
 (define (config-domain-strings names)
@@ -148,7 +150,8 @@  of index files."
 
 (define nginx-activation
   (match-lambda
-    (($ <nginx-configuration> nginx log-directory run-directory config-file)
+    (($ <nginx-configuration> nginx log-directory run-directory server-blocks
+                              config-file)
      #~(begin
          (use-modules (guix build utils))
 
@@ -164,17 +167,25 @@  of index files."
          (mkdir-p (string-append #$run-directory "/scgi_temp"))
          ;; Check configuration file syntax.
          (system* (string-append #$nginx "/sbin/nginx")
-                  "-c" #$config-file "-t")))))
+                  "-c" #$(or config-file
+                             (default-nginx-config log-directory
+                               run-directory server-blocks))
+                  "-t")))))
 
 (define nginx-shepherd-service
   (match-lambda
-    (($ <nginx-configuration> nginx log-directory run-directory config-file)
+    (($ <nginx-configuration> nginx log-directory run-directory server-blocks
+                              config-file)
      (let* ((nginx-binary (file-append nginx "/sbin/nginx"))
             (nginx-action
              (lambda args
                #~(lambda _
                    (zero?
-                    (system* #$nginx-binary "-c" #$config-file #$@args))))))
+                    (system* #$nginx-binary "-c"
+                             #$(or config-file
+                                   (default-nginx-config log-directory
+                                     run-directory server-blocks))
+                             #$@args))))))
 
        ;; TODO: Add 'reload' action.
        (list (shepherd-service
@@ -192,14 +203,20 @@  of index files."
                        (service-extension activation-service-type
                                           nginx-activation)
                        (service-extension account-service-type
-                                          (const %nginx-accounts))))))
+                                          (const %nginx-accounts))))
+                (compose concatenate)
+                (extend (lambda (config servers)
+                          (nginx-configuration
+                            (inherit config)
+                            (server-blocks
+                              (append (nginx-configuration-server-blocks config)
+                              servers)))))))
 
 (define* (nginx-service #:key (nginx nginx)
                         (log-directory "/var/log/nginx")
                         (run-directory "/var/run/nginx")
-                        (server-list (list (nginx-server-configuration)))
-                        (config-file
-                         (default-nginx-config log-directory run-directory server-list)))
+                        (server-list '())
+                        (config-file #f))
   "Return a service that runs NGINX, the nginx web server.
 
 The nginx daemon loads its runtime configuration from CONFIG-FILE, stores log
@@ -209,4 +226,5 @@  files in LOG-DIRECTORY, and stores temporary runtime files in RUN-DIRECTORY."
             (nginx nginx)
             (log-directory log-directory)
             (run-directory run-directory)
+            (server-blocks server-list)
             (file config-file))))
-- 
2.11.0