diff mbox

improve nginx-service

Message ID 20161106121142.19cdb128@lepiller.eu
State New
Headers show

Commit Message

Julien Lepiller Nov. 6, 2016, 11:11 a.m. UTC
On Fri, 4 Nov 2016 23:34:31 +0100
Hartmut Goebel <h.goebel@crazy-compilers.com> wrote:

> Am 04.11.2016 um 23:12 schrieb Julien Lepiller:
> >> >     gnu/services/web.scm:118:34: In procedure
> >> > default-nginx-vhost-config: gnu/services/web.scm:118:34: In
> >> > procedure struct_vtable: Wrong type argument in position 1
> >> > (expecting struct): (nginx-vhost-configuration (root
> >> > taler-landing-page))  
> > What is your configuration exactly, and what is taler-landing-page?
> >  
> 
> The configuration is like in your gtk-doc-vhost, except that I'm not
> using gtk+2 and omitting the "string-append" for the path.
> 

I attached new patches that add more documentation (I hope it helps).

Using this configuration works for me:

(nginx-service)
(service (service-type
           (name 'foo)
           (extensions
             (list (service-extension
                     nginx-service-type
                     (const (list
                             ;; a vhost that actually serves html files
                              (nginx-vhost-configuration
                              (https-port #f)
                              (ssl-certificate #f)
                              (ssl-certificate-key #f)
                              (root #~(string-append #$cups
                                         "/share/doc/cups/ja")))
                             ;; just to show how to use a package
                             ;; directly
                             (nginx-vhost-configuration
                              (server-name (list "help"))
                              (https-port #f)
                              (ssl-certificate #f)
                              (ssl-certificate-key #f)
                              (root cups)))))))) #t)

It would work with simple-service too.

It generates this configuration file for nginx:

user nginx nginx;
pid /var/run/nginx/pid;
error_log /var/log/nginx/error.log info;
http {
    client_body_temp_path /var/run/nginx/client_body_temp;
    proxy_temp_path /var/run/nginx/proxy_temp;
    fastcgi_temp_path /var/run/nginx/fastcgi_temp;
    uwsgi_temp_path /var/run/nginx/uwsgi_temp;
    scgi_temp_path /var/run/nginx/scgi_temp;
    access_log /var/log/nginx/access.log;
    server {
      listen 80;
      server_name help ;
      root /gnu/store/l6n860s5fmaxhwcx17mjrfw4wcqx8xy8-cups-2.1.0;
      index index.html ;
      server_tokens off;
    }

    server {
      listen 80;
      server_name _ ;
      root /gnu/store/l6n860s5fmaxhwcx17mjrfw4wcqx8xy8-cups-2.1.0/share/doc/cups/ja;
      index index.html ;
      server_tokens off;
    }

}
events {}
diff mbox

Patch

From c7f6944a7483edf2719ecf47a2adf2d4b346700a Mon Sep 17 00:00:00 2001
From: Julien Lepiller <julien@lepiller.eu>
Date: Sun, 6 Nov 2016 11:28:01 +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        |  9 +++++
 gnu/services/web.scm | 96 +++++++++++++++++++++++++++-------------------------
 2 files changed, 59 insertions(+), 46 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 4b60a4a..d125546 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -10433,6 +10433,15 @@  of them.  For example:
                 (list vh)) 
 @end example
 
+or if you want to make a package available, you can write something like:
+
+@example
+(define vh (nginx-vhost-configuration 
+                       (root my-package)))
+(define vh2 (nginx-vhost-configuration 
+                        (root #~(string-append #$cups "/share/doc/cups/ja"))))
+@end example
+
 Is a service that adds a new @dfn{virtual host} to the list of existing ones.
 You can add as many such services as you want.
 
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