>From 25a296057969a35b86ea7371577504c43bf96334 Mon Sep 17 00:00:00 2001 From: Julien Lepiller Date: Thu, 27 Oct 2016 19:47:27 +0200 Subject: [PATCH] service: Make nginx-service extensible gnu/services/web.scm (nginx-service-type): Make extensible --- doc/guix.texi | 15 ++++++++++++++- gnu/services/web.scm | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 1293b8b..b6e62b3 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -10399,7 +10399,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. @@ -10415,6 +10415,19 @@ this to work, use the default value for @var{config-file}. @end deffn address@hidden [Scheme Variable] nginx-service-type +This is the type for the @code{nginx} web server. + +This service can be extended to add more vhosts than the default one. + address@hidden +(simple-service 'my-extra-vhost nginx-service-type + (list (nginx-vhost-configuration (https-port #f) + (root "/var/www/extra-website")))) address@hidden example + address@hidden 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..0fb1a0b 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,7 @@ (nginx nginx-configuration-nginx) ; (log-directory nginx-configuration-log-directory) ;string (run-directory nginx-configuration-run-directory) ;string + (vhosts nginx-configuration-vhosts) ;list of (file nginx-configuration-file)) ;string | file-like (define (config-domain-strings names) @@ -102,11 +104,13 @@ of index files." " server_name " (config-domain-strings (nginx-vhost-configuration-server-name vhost)) ";\n" - (if (nginx-vhost-configuration-ssl-certificate vhost) + (if (and (nginx-vhost-configuration-https-port vhost) + (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) + (if (and (nginx-vhost-configuration-https-port vhost) + (nginx-vhost-configuration-ssl-certificate-key vhost)) (string-append " ssl_certificate_key " (nginx-vhost-configuration-ssl-certificate-key vhost) ";\n") "") @@ -148,7 +152,7 @@ of index files." (define nginx-activation (match-lambda - (($ nginx log-directory run-directory config-file) + (($ nginx log-directory run-directory vhosts config-file) #~(begin (use-modules (guix build utils)) @@ -164,17 +168,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 log-directory run-directory config-file) + (($ 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 address@hidden)))))) + (system* #$nginx-binary "-c" #$(or config-file + (default-nginx-config + log-directory + run-directory + vhosts)) + address@hidden)))))) ;; TODO: Add 'reload' action. (list (shepherd-service @@ -192,14 +204,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 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 (list (nginx-vhost-configuration + (https-port #f)))) + (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 +227,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.1