1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2015 David Thompson <davet@gnu.org>
3 ;;; Copyright © 2015 Ludovic Courtès <ludo@gnu.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (gnu services web)
21 #:use-module (gnu services)
22 #:use-module (gnu services shepherd)
23 #:use-module (gnu system shadow)
24 #:use-module (gnu packages admin)
25 #:use-module (gnu packages web)
26 #:use-module (guix records)
27 #:use-module (guix gexp)
28 #:use-module (ice-9 match)
29 #:export (nginx-service))
37 (define-record-type* <nginx-configuration>
38 nginx-configuration make-nginx-configuration
40 (nginx nginx-configuration-nginx) ;<package>
41 (log-directory nginx-configuration-log-directory) ;string
42 (run-directory nginx-configuration-run-directory) ;string
43 (file nginx-configuration-file)) ;string | file-like
45 (define (default-nginx-config log-directory run-directory)
46 (plain-file "nginx.conf"
49 "pid " run-directory "/pid;\n"
50 "error_log " log-directory "/error.log info;\n"
52 " access_log " log-directory "/access.log;\n"
58 (define %nginx-accounts
59 (list (user-group (name "nginx") (system? #t))
64 (comment "nginx server user")
65 (home-directory "/var/empty")
66 (shell #~(string-append #$shadow "/sbin/nologin")))))
68 (define nginx-activation
70 (($ <nginx-configuration> nginx log-directory run-directory config-file)
72 (use-modules (guix build utils))
74 (format #t "creating nginx log directory '~a'~%" #$log-directory)
75 (mkdir-p #$log-directory)
76 (format #t "creating nginx run directory '~a'~%" #$run-directory)
77 (mkdir-p #$run-directory)
78 ;; Check configuration file syntax.
79 (system* (string-append #$nginx "/bin/nginx")
80 "-c" #$config-file "-t")))))
82 (define nginx-shepherd-service
84 (($ <nginx-configuration> nginx log-directory run-directory config-file)
85 (let* ((nginx-binary #~(string-append #$nginx "/sbin/nginx"))
90 (system* #$nginx-binary "-c" #$config-file #$@args))))))
92 ;; TODO: Add 'reload' action.
93 (list (shepherd-service
95 (documentation "Run the nginx daemon.")
96 (requirement '(user-processes loopback))
97 (start (nginx-action "-p" run-directory))
98 (stop (nginx-action "-s" "stop"))))))))
100 (define nginx-service-type
101 (service-type (name 'nginx)
103 (list (service-extension shepherd-root-service-type
104 nginx-shepherd-service)
105 (service-extension activation-service-type
107 (service-extension account-service-type
108 (const %nginx-accounts))))))
110 (define* (nginx-service #:key (nginx nginx)
111 (log-directory "/var/log/nginx")
112 (run-directory "/var/run/nginx")
114 (default-nginx-config log-directory run-directory)))
115 "Return a service that runs NGINX, the nginx web server.
117 The nginx daemon loads its runtime configuration from CONFIG-FIGLE, stores log
118 files in LOG-DIRECTORY, and stores temporary runtime files in RUN-DIRECTORY."
119 (service nginx-service-type
122 (log-directory log-directory)
123 (run-directory run-directory)
124 (file config-file))))