On Fri, 04 Nov 2016 14:21:43 +0100
l...@gnu.org (Ludovic Courtès) wrote:

> Hartmut Goebel <h.goe...@crazy-compilers.com> skribis:
> 
> > Am 03.11.2016 um 15:54 schrieb Ludovic Courtès:  
> >> Assuming ‘demo-website’ is bound to a package object, you should
> >> be able to do:
> >>
> >>   (root (file-append demo-website "/"))
> >>
> >> or simply:
> >>
> >>   (root demo-website)  
> >
> > Hmm, this does not work for me:
> >
> >
> > ice-9/boot-9.scm:702:27: In procedure map:
> > ice-9/boot-9.scm:702:27: In procedure string-append: Wrong type
> > (expecting string): #<package
> > taler-demo-landing-page@0.0.0-1.105bea68 ./taler/packages/demo.scm:100
> > 3e2a600>
> >
> >
> > I tried with current master branch.  
> 
> Doh!  Indeed, ‘default-nginx-vhost-config’ must be rewritten to do
> something like:
> 
>   #~(string-append #$foo #$bar …)
> 
> instead of:
> 
>   (string-append foo bar …)
> 
> More precisely, it could ‘string-append’ all the string literals yet
> use a #~(string-append …) gexp for ‘root’.
> 
> Julien?  :-)

I think it's good now. There are three patches:
1. making the service extensible (mostly what you wrote last time)
   I used an empty list as the default list of vhosts.
2. a patch to fix an issue when multiple index files and server name
   were specified (there were no space in between)
3. a patch to allow any configuration option to come from the store (I
   couldn't write something specific to the root option, but I think
   it's fine this way). I tested with the example I added in the
   documentation, and it works fine.

I hope I did it right :)

> 
> Ludo’.

>From 55ccf3041bff562345816b500ee5a2aeda9e3226 Mon Sep 17 00:00:00 2001
From: Julien Lepiller <jul...@lepiller.eu>
Date: Thu, 3 Nov 2016 18:11:01 +0100
Subject: [PATCH 1/3] Make nginx-service extensible

* gnu/services/web.scm (nginx-service-type): Make extensible.
---
 doc/guix.texi        | 15 ++++++++++++++-
 gnu/services/web.scm | 33 +++++++++++++++++++++++++--------
 2 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 1075a7e..5c42140 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -10401,7 +10401,7 @@ The @code{(gnu services web)} module provides the following service:
        [#:log-directory ``/var/log/nginx''] @
        [#:run-directory ``/var/run/nginx''] @
        [#:vhost-list (list (nginx-vhost-configuration))] @
-       [#:config-file]
+       [#:config-file @code{#f}]
 
 Return a service that runs @var{nginx}, the nginx web server.
 
@@ -10417,6 +10417,19 @@ this to work, use the default value for @var{config-file}.
 
 @end deffn
 
+@deffn {Scheme Variable} nginx-service-type
+This is the 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 "/var/www/extra-website"))))
+@end example
+
+@end deffn
+
 @deftp {Data Type} nginx-vhost-configuration
 Data type representing the configuration of an nginx virtual host.
 This type has the following parameters:
diff --git a/gnu/services/web.scm b/gnu/services/web.scm
index 59e1e54..50f83f3 100644
--- a/gnu/services/web.scm
+++ b/gnu/services/web.scm
@@ -28,6 +28,7 @@
   #:use-module (guix records)
   #:use-module (guix gexp)
   #:use-module (ice-9 match)
+  #:use-module (srfi srfi-1)
   #:export (nginx-configuration
             nginx-configuration?
             nginx-vhost-configuration
@@ -67,6 +68,8 @@
   (nginx         nginx-configuration-nginx)         ;<package>
   (log-directory nginx-configuration-log-directory) ;string
   (run-directory nginx-configuration-run-directory) ;string
+  (vhosts        nginx-configuration-vhosts
+                 (default '()))   ;list of <nginx-vhost-configuration>
   (file          nginx-configuration-file))         ;string | file-like
 
 (define (config-domain-strings names)
@@ -148,7 +151,7 @@ of index files."
 
 (define nginx-activation
   (match-lambda
-    (($ <nginx-configuration> nginx log-directory run-directory config-file)
+    (($ <nginx-configuration> nginx log-directory run-directory vhosts 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 vhosts))
+                   "-t")))))
 
 (define nginx-shepherd-service
   (match-lambda
-    (($ <nginx-configuration> nginx log-directory run-directory config-file)
+    (($ <nginx-configuration> nginx log-directory run-directory vhosts 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
+                                                         vhosts))
+                             #$@args))))))
 
        ;; TODO: Add 'reload' action.
        (list (shepherd-service
@@ -192,14 +203,19 @@ 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 vhosts)
+                          (nginx-configuration
+                            (inherit config)
+                            (vhosts (append (nginx-configuration-vhosts config)
+                                            vhosts)))))))
 
 (define* (nginx-service #:key (nginx nginx)
                         (log-directory "/var/log/nginx")
                         (run-directory "/var/run/nginx")
-                        (vhost-list (list (nginx-vhost-configuration)))
-                        (config-file
-                         (default-nginx-config log-directory run-directory vhost-list)))
+                        (vhost-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 +225,5 @@ files in LOG-DIRECTORY, and stores temporary runtime files in RUN-DIRECTORY."
             (nginx nginx)
             (log-directory log-directory)
             (run-directory run-directory)
+            (vhosts vhost-list)
             (file config-file))))
-- 
2.10.2

>From e5e123a2ec512a7aed2f1f18ed0f7d2b86ba3a9b Mon Sep 17 00:00:00 2001
From: Julien Lepiller <jul...@lepiller.eu>
Date: Thu, 3 Nov 2016 18:11:43 +0100
Subject: [PATCH 2/3] service: Fix multiple index and server name

* gnu/services/web.scm (config-index-strings and config-vhost-strings):
   Add a space between entries.
---
 gnu/services/web.scm | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/gnu/services/web.scm b/gnu/services/web.scm
index 50f83f3..e36d284 100644
--- a/gnu/services/web.scm
+++ b/gnu/services/web.scm
@@ -77,8 +77,8 @@
 of domain names."
  (string-concatenate
   (map (match-lambda
-        ('default "_")
-        ((? string? str) str))
+        ('default "_ ")
+        ((? string? str) (string-append str " ")))
        names)))
 
 (define (config-index-strings names)
@@ -86,7 +86,7 @@ of domain names."
 of index files."
  (string-concatenate
   (map (match-lambda
-        ((? string? str) str))
+        ((? string? str) (string-append str " ")))
        names)))
 
 (define (default-nginx-vhost-config vhost)
-- 
2.10.2

>From a1305d1280642bcff46fc736717f119bd4999e74 Mon Sep 17 00:00:00 2001
From: Julien Lepiller <jul...@lepiller.eu>
Date: Fri, 4 Nov 2016 17:36:21 +0100
Subject: [PATCH 3/3] services: Accept gexps as nginx configuration value.

* gnu/services/web.scm (default-nginx-config): Use computed-file instead of plain-file.
---
 doc/guix.texi        |  8 +++++
 gnu/services/web.scm | 96 +++++++++++++++++++++++++++-------------------------
 2 files changed, 58 insertions(+), 46 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 5c42140..2b07bc4 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -10428,6 +10428,14 @@ This service can be extended to add more vhosts than the default one.
                                                  (root "/var/www/extra-website"))))
 @end example
 
+or if you want to make a package available, you can write something like:
+
+@example
+(simple-service 'gtk-doc-vhost nginx-service-type
+                (list (nginx-vhost-configuration 
+                       (root (file-append gtk+2 "/share/gtk-doc/html/gdk2/")))))
+@end example
+
 @end deffn
 
 @deftp {Data Type} nginx-vhost-configuration
diff --git a/gnu/services/web.scm b/gnu/services/web.scm
index e36d284..df6e680 100644
--- a/gnu/services/web.scm
+++ b/gnu/services/web.scm
@@ -90,54 +90,58 @@ of index files."
        names)))
 
 (define (default-nginx-vhost-config vhost)
-  (string-append
-   "    server {\n"
-   (if (nginx-vhost-configuration-http-port vhost)
-       (string-append "      listen "
-                      (number->string (nginx-vhost-configuration-http-port vhost))
-                      ";\n")
-       "")
-   (if (nginx-vhost-configuration-https-port vhost)
-       (string-append "      listen "
-                      (number->string (nginx-vhost-configuration-https-port vhost))
-                      " ssl;\n")
-       "")
-   "      server_name " (config-domain-strings
-                         (nginx-vhost-configuration-server-name vhost))
-                        ";\n"
-   (if (nginx-vhost-configuration-ssl-certificate vhost)
-       (string-append "      ssl_certificate "
-                      (nginx-vhost-configuration-ssl-certificate vhost) ";\n")
-       "")
-   (if (nginx-vhost-configuration-ssl-certificate-key vhost)
-       (string-append "      ssl_certificate_key "
-                      (nginx-vhost-configuration-ssl-certificate-key vhost) ";\n")
-       "")
-   "      root " (nginx-vhost-configuration-root vhost) ";\n"
-   "      index " (config-index-strings (nginx-vhost-configuration-index vhost)) ";\n"
-   "      server_tokens " (if (nginx-vhost-configuration-server-tokens? vhost)
-                              "on" "off") ";\n"
-   "    }\n"))
+  #~(string-append
+     "    server {\n"
+     #$(if (nginx-vhost-configuration-http-port vhost)
+           (string-append "      listen "
+                          (number->string (nginx-vhost-configuration-http-port vhost))
+                          ";\n")
+           "")
+     #$(if (nginx-vhost-configuration-https-port vhost)
+          (string-append "      listen "
+                          (number->string (nginx-vhost-configuration-https-port vhost))
+                          " ssl;\n")
+           "")
+     "      server_name " #$(config-domain-strings
+                             (nginx-vhost-configuration-server-name vhost))
+                          ";\n"
+     #$(if (nginx-vhost-configuration-ssl-certificate vhost)
+           (string-append "      ssl_certificate "
+                          (nginx-vhost-configuration-ssl-certificate vhost) ";\n")
+           "")
+     #$(if (nginx-vhost-configuration-ssl-certificate-key vhost)
+           (string-append "      ssl_certificate_key "
+                          (nginx-vhost-configuration-ssl-certificate-key vhost) ";\n")
+           "")
+     "      root " #$(nginx-vhost-configuration-root vhost) ";\n"
+     "      index " #$(config-index-strings (nginx-vhost-configuration-index vhost)) ";\n"
+     "      server_tokens " #$(if (nginx-vhost-configuration-server-tokens? vhost)
+                                  "on" "off") ";\n"
+     "    }\n"))
 
 (define (default-nginx-config log-directory run-directory vhost-list)
-  (plain-file "nginx.conf"
-              (string-append
-               "user nginx nginx;\n"
-               "pid " run-directory "/pid;\n"
-               "error_log " log-directory "/error.log info;\n"
-               "http {\n"
-               "    client_body_temp_path " run-directory "/client_body_temp;\n"
-               "    proxy_temp_path " run-directory "/proxy_temp;\n"
-               "    fastcgi_temp_path " run-directory "/fastcgi_temp;\n"
-               "    uwsgi_temp_path " run-directory "/uwsgi_temp;\n"
-               "    scgi_temp_path " run-directory "/scgi_temp;\n"
-               "    access_log " log-directory "/access.log;\n"
-               (let ((http (map default-nginx-vhost-config vhost-list)))
-                 (do ((http http (cdr http))
-                      (block "" (string-append (car http) "\n" block )))
-                     ((null? http) block)))
-               "}\n"
-               "events {}\n")))
+  (computed-file
+    "nginx.conf"
+    #~(call-with-output-file #$output
+        (lambda (port)
+          (format port
+                  (string-append
+                     "user nginx nginx;\n"
+                     "pid " #$run-directory "/pid;\n"
+                     "error_log " #$log-directory "/error.log info;\n"
+                     "http {\n"
+                     "    client_body_temp_path " #$run-directory "/client_body_temp;\n"
+                     "    proxy_temp_path " #$run-directory "/proxy_temp;\n"
+                     "    fastcgi_temp_path " #$run-directory "/fastcgi_temp;\n"
+                     "    uwsgi_temp_path " #$run-directory "/uwsgi_temp;\n"
+                     "    scgi_temp_path " #$run-directory "/scgi_temp;\n"
+                     "    access_log " #$log-directory "/access.log;\n"
+                     #$(let ((http (map default-nginx-vhost-config vhost-list)))
+                         (do ((http http (cdr http))
+                              (block "" #~(string-append #$(car http) "\n" #$block )))
+                             ((null? http) block)))
+                     "}\n"
+                     "events {}\n"))))))
 
 (define %nginx-accounts
   (list (user-group (name "nginx") (system? #t))
-- 
2.10.2

Reply via email to