unofficial mirror of guix-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] make nginx service extensible
@ 2016-12-13 20:56 Julien Lepiller
  2017-01-03 13:39 ` Ludovic Courtès
  2017-01-06 15:03 ` Thompson, David
  0 siblings, 2 replies; 3+ messages in thread
From: Julien Lepiller @ 2016-12-13 20:56 UTC (permalink / raw)
  To: guix-devel

[-- Attachment #1: Type: text/plain, Size: 285 bytes --]

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 :)

[-- Attachment #2: 0001-gnu-services-Rename-nginx-vhosts-to-server.patch --]
[-- Type: text/x-patch, Size: 9179 bytes --]

From 52f220a2bb5a7186c9dde79b6d9a419bc58feec4 Mon Sep 17 00:00:00 2001
From: Julien Lepiller <julien@lepiller.eu>
Date: Tue, 13 Dec 2016 20:44:31 +0100
Subject: [PATCH 1/3] gnu: services: Rename nginx vhosts to server.

* gnu/services/web.scm (nginx-vhost-configuration): Rename to...
  (nginx-server-configuration): ...this.
---
 doc/guix.texi        | 20 +++++++++---------
 gnu/services/web.scm | 60 ++++++++++++++++++++++++++--------------------------
 2 files changed, 40 insertions(+), 40 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 0cb1bc766..d1836ee04 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -11713,7 +11713,7 @@ 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''] @
-       [#:vhost-list (list (nginx-vhost-configuration))] @
+       [#:server-list (list (nginx-server-configuration))] @
        [#:config-file]
 
 Return a service that runs @var{nginx}, the nginx web server.
@@ -11724,32 +11724,32 @@ files are written to @var{run-directory}.  For proper operation, these
 arguments should match what is in @var{config-file} to ensure that the
 directories are created when the service is activated.
 
-As an alternative to using a @var{config-file}, @var{vhost-list} can be
-used to specify the list of @dfn{virtual hosts} required on the host.  For
+As an alternative to using a @var{config-file}, @var{server-list} can be
+used to specify the list of @dfn{server blocks} required on the host.  For
 this to work, use the default value for @var{config-file}.
 
 @end deffn
 
-@deftp {Data Type} nginx-vhost-configuration
-Data type representing the configuration of an nginx virtual host.
+@deftp {Data Type} nginx-server-configuration
+Data type representing the configuration of an nginx server block.
 This type has the following parameters:
 
 @table @asis
 @item @code{http-port} (default: @code{80})
 Nginx will listen for HTTP connection on this port.  Set it at @code{#f} if
 nginx should not listen for HTTP (non secure) connection for this
-@dfn{virtual host}.
+@dfn{server block}.
 
 @item @code{https-port} (default: @code{443})
 Nginx will listen for HTTPS connection on this port.  Set it at @code{#f} if
-nginx should not listen for HTTPS (secure) connection for this @dfn{virtual host}.
+nginx should not listen for HTTPS (secure) connection for this @dfn{server block}.
 
 Note that nginx can listen for HTTP and HTTPS connections in the same
-@dfn{virtual host}.
+@dfn{server block}.
 
 @item @code{server-name} (default: @code{(list 'default)})
-A list of server names this vhost represents. @code{'default} represents the
-default vhost for connections matching no other vhost.
+A list of server names this server represents. @code{'default} represents the
+default server for connections matching no other server.
 
 @item @code{root} (default: @code{"/srv/http"})
 Root of the website nginx will serve.
diff --git a/gnu/services/web.scm b/gnu/services/web.scm
index 8f6e5bf6b..12a146d8b 100644
--- a/gnu/services/web.scm
+++ b/gnu/services/web.scm
@@ -30,8 +30,8 @@
   #:use-module (ice-9 match)
   #:export (nginx-configuration
             nginx-configuration?
-            nginx-vhost-configuration
-            nginx-vhost-configuration?
+            nginx-server-configuration
+            nginx-server-configuration?
             nginx-service
             nginx-service-type))
 
@@ -41,24 +41,24 @@
 ;;;
 ;;; Code:
 
-(define-record-type* <nginx-vhost-configuration>
-  nginx-vhost-configuration make-nginx-vhost-configuration
-  nginx-vhost-configuration?
-  (http-port           nginx-vhost-configuration-http-port
+(define-record-type* <nginx-server-configuration>
+  nginx-server-configuration make-nginx-server-configuration
+  nginx-server-configuration?
+  (http-port           nginx-server-configuration-http-port
                        (default 80))
-  (https-port          nginx-vhost-configuration-https-port
+  (https-port          nginx-server-configuration-https-port
                        (default 443))
-  (server-name         nginx-vhost-configuration-server-name
+  (server-name         nginx-server-configuration-server-name
                        (default (list 'default)))
-  (root                nginx-vhost-configuration-root
+  (root                nginx-server-configuration-root
                        (default "/srv/http"))
-  (index               nginx-vhost-configuration-index
+  (index               nginx-server-configuration-index
                        (default (list "index.html")))
-  (ssl-certificate     nginx-vhost-configuration-ssl-certificate
+  (ssl-certificate     nginx-server-configuration-ssl-certificate
                        (default "/etc/nginx/cert.pem"))
-  (ssl-certificate-key nginx-vhost-configuration-ssl-certificate-key
+  (ssl-certificate-key nginx-server-configuration-ssl-certificate-key
                        (default "/etc/nginx/key.pem"))
-  (server-tokens?      nginx-vhost-configuration-server-tokens?
+  (server-tokens?      nginx-server-configuration-server-tokens?
                        (default #f)))
 
 (define-record-type* <nginx-configuration>
@@ -86,37 +86,37 @@ of index files."
         ((? string? str) str))
        names)))
 
-(define (default-nginx-vhost-config vhost)
+(define (default-nginx-server-config server)
   (string-append
    "    server {\n"
-   (if (nginx-vhost-configuration-http-port vhost)
+   (if (nginx-server-configuration-http-port server)
        (string-append "      listen "
-                      (number->string (nginx-vhost-configuration-http-port vhost))
+                      (number->string (nginx-server-configuration-http-port server))
                       ";\n")
        "")
-   (if (nginx-vhost-configuration-https-port vhost)
+   (if (nginx-server-configuration-https-port server)
        (string-append "      listen "
-                      (number->string (nginx-vhost-configuration-https-port vhost))
+                      (number->string (nginx-server-configuration-https-port server))
                       " ssl;\n")
        "")
    "      server_name " (config-domain-strings
-                         (nginx-vhost-configuration-server-name vhost))
+                         (nginx-server-configuration-server-name server))
                         ";\n"
-   (if (nginx-vhost-configuration-ssl-certificate vhost)
+   (if (nginx-server-configuration-ssl-certificate server)
        (string-append "      ssl_certificate "
-                      (nginx-vhost-configuration-ssl-certificate vhost) ";\n")
+                      (nginx-server-configuration-ssl-certificate server) ";\n")
        "")
-   (if (nginx-vhost-configuration-ssl-certificate-key vhost)
+   (if (nginx-server-configuration-ssl-certificate-key server)
        (string-append "      ssl_certificate_key "
-                      (nginx-vhost-configuration-ssl-certificate-key vhost) ";\n")
+                      (nginx-server-configuration-ssl-certificate-key server) ";\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)
+   "      root " (nginx-server-configuration-root server) ";\n"
+   "      index " (config-index-strings (nginx-server-configuration-index server)) ";\n"
+   "      server_tokens " (if (nginx-server-configuration-server-tokens? server)
                               "on" "off") ";\n"
    "    }\n"))
 
-(define (default-nginx-config log-directory run-directory vhost-list)
+(define (default-nginx-config log-directory run-directory server-list)
   (plain-file "nginx.conf"
               (string-append
                "user nginx nginx;\n"
@@ -129,7 +129,7 @@ of index files."
                "    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)))
+               (let ((http (map default-nginx-server-config server-list)))
                  (do ((http http (cdr http))
                       (block "" (string-append (car http) "\n" block )))
                      ((null? http) block)))
@@ -197,9 +197,9 @@ of index files."
 (define* (nginx-service #:key (nginx nginx)
                         (log-directory "/var/log/nginx")
                         (run-directory "/var/run/nginx")
-                        (vhost-list (list (nginx-vhost-configuration)))
+                        (server-list (list (nginx-server-configuration)))
                         (config-file
-                         (default-nginx-config log-directory run-directory vhost-list)))
+                         (default-nginx-config log-directory run-directory server-list)))
   "Return a service that runs NGINX, the nginx web server.
 
 The nginx daemon loads its runtime configuration from CONFIG-FILE, stores log
-- 
2.11.0


[-- Attachment #3: 0002-services-Fix-multiple-index-and-server-name.patch --]
[-- Type: text/x-patch, Size: 1067 bytes --]

From 237557b0e0cff28dc53f384f2297f658bff06459 Mon Sep 17 00:00:00 2001
From: Julien Lepiller <julien@lepiller.eu>
Date: Tue, 13 Dec 2016 20:48:16 +0100
Subject: [PATCH 2/3] services: Fix multiple index and server name

* gnu/services/web.scm (config-domain-strings, config-index-string): separate
  names with a space.
---
 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 12a146d8b..a36352225 100644
--- a/gnu/services/web.scm
+++ b/gnu/services/web.scm
@@ -74,8 +74,8 @@
 of domain names."
  (string-join
   (map (match-lambda
-        ('default "_")
-        ((? string? str) str))
+        ('default "_ ")
+        ((? string? str) (string-append str " ")))
        names)))
 
 (define (config-index-strings names)
@@ -83,7 +83,7 @@ of domain names."
 of index files."
  (string-join
   (map (match-lambda
-        ((? string? str) str))
+        ((? string? str) (string-append str " ")))
        names)))
 
 (define (default-nginx-server-config server)
-- 
2.11.0


[-- Attachment #4: 0003-services-Make-nginx-service-extensible.patch --]
[-- Type: text/x-patch, Size: 5757 bytes --]

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] make nginx service extensible
  2016-12-13 20:56 [PATCH] make nginx service extensible Julien Lepiller
@ 2017-01-03 13:39 ` Ludovic Courtès
  2017-01-06 15:03 ` Thompson, David
  1 sibling, 0 replies; 3+ messages in thread
From: Ludovic Courtès @ 2017-01-03 13:39 UTC (permalink / raw)
  To: Julien Lepiller; +Cc: guix-devel

Hi Julien,

Sorry for the looong delay!  At last I got around to reviewing these
patches from last year.  :-)

Julien Lepiller <julien@lepiller.eu> skribis:

> From 52f220a2bb5a7186c9dde79b6d9a419bc58feec4 Mon Sep 17 00:00:00 2001
> From: Julien Lepiller <julien@lepiller.eu>
> Date: Tue, 13 Dec 2016 20:44:31 +0100
> Subject: [PATCH 1/3] gnu: services: Rename nginx vhosts to server.
>
> * gnu/services/web.scm (nginx-vhost-configuration): Rename to...
>   (nginx-server-configuration): ...this.
> ---
>  doc/guix.texi        | 20 +++++++++---------
>  gnu/services/web.scm | 60 ++++++++++++++++++++++++++--------------------------
>  2 files changed, 40 insertions(+), 40 deletions(-)

Pushed with an adjusted commit log to mention changes in guix.texi as
well.

  http://git.savannah.gnu.org/cgit/guix.git/commit/?id=3b9b12ef49d0b6d7a8887513acb9e9a1a8325148

> From 237557b0e0cff28dc53f384f2297f658bff06459 Mon Sep 17 00:00:00 2001
> From: Julien Lepiller <julien@lepiller.eu>
> Date: Tue, 13 Dec 2016 20:48:16 +0100
> Subject: [PATCH 2/3] services: Fix multiple index and server name
>
> * gnu/services/web.scm (config-domain-strings, config-index-string): separate
>   names with a space.

Pushed.

  http://git.savannah.gnu.org/cgit/guix.git/commit/?id=4e9ae301ce759f9cf9a09f47dc521d0bc8409f6c

> 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(-)

Pushed with an adjusted commit log to mention all the changes.

  http://git.savannah.gnu.org/cgit/guix.git/commit/?id=d338237d8c2408e0cd13ecfeb303e327ff7e3d9b

> +@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

I also changed “vhost” to “server” here.

Thank you!

On a related note: you might have seen the discussion about the ‘root’
parameter of server blocks:

  https://lists.gnu.org/archive/html/help-guix/2017-01/msg00005.html

To fix it, we need to have ‘default-nginx-config’ use ‘computed-file’
instead of ‘plain-file’, and to move have ‘default-nginx-server-config’
return a gexp instead of a string.

So something like:

  (define (default-nginx-server-config server)
    #~(string-append
        "server {"
        ;; …
        "  root " #$(nginx-server-configuration-root server) ";\n"
        "};\n"))

  (define (default-nginx-config …)
    (computed-file "nginx.conf"
                   #~(call-with-output-file #$output
                       (lambda (port)
                         (display … port)))))

That way, people could use (file-append …) or similar for ‘root’ and
other parameters that denote a file name.

Could you look into it?  :-)

Ludo’.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] make nginx service extensible
  2016-12-13 20:56 [PATCH] make nginx service extensible Julien Lepiller
  2017-01-03 13:39 ` Ludovic Courtès
@ 2017-01-06 15:03 ` Thompson, David
  1 sibling, 0 replies; 3+ messages in thread
From: Thompson, David @ 2017-01-06 15:03 UTC (permalink / raw)
  To: Julien Lepiller; +Cc: guix-devel

Hi Julien,

On Tue, Dec 13, 2016 at 3:56 PM, Julien Lepiller <julien@lepiller.eu> wrote:
> 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 :)

Thank you for making these improvements.  I wrote the original
barebones nginx-service and just never got around to making it
extensible.  This should make a bunch of people very happy. :)

- Dave

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-01-06 15:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-13 20:56 [PATCH] make nginx service extensible Julien Lepiller
2017-01-03 13:39 ` Ludovic Courtès
2017-01-06 15:03 ` Thompson, David

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).