services: Rename 'dmd' services to 'shepherd'.
[jackhill/guix/guix.git] / gnu / services / web.scm
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>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
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.
11 ;;;
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.
16 ;;;
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/>.
19
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))
30
31 ;;; Commentary:
32 ;;;
33 ;;; Web services.
34 ;;;
35 ;;; Code:
36
37 (define-record-type* <nginx-configuration>
38 nginx-configuration make-nginx-configuration
39 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
44
45 (define (default-nginx-config log-directory run-directory)
46 (plain-file "nginx.conf"
47 (string-append
48 "user nginx nginx;\n"
49 "pid " run-directory "/pid;\n"
50 "error_log " log-directory "/error.log info;\n"
51 "http {\n"
52 " access_log " log-directory "/access.log;\n"
53 " root /var/www;\n"
54 " server {}\n"
55 "}\n"
56 "events {}\n")))
57
58 (define %nginx-accounts
59 (list (user-group (name "nginx") (system? #t))
60 (user-account
61 (name "nginx")
62 (group "nginx")
63 (system? #t)
64 (comment "nginx server user")
65 (home-directory "/var/empty")
66 (shell #~(string-append #$shadow "/sbin/nologin")))))
67
68 (define nginx-activation
69 (match-lambda
70 (($ <nginx-configuration> nginx log-directory run-directory config-file)
71 #~(begin
72 (use-modules (guix build utils))
73
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")))))
81
82 (define nginx-shepherd-service
83 (match-lambda
84 (($ <nginx-configuration> nginx log-directory run-directory config-file)
85 (let* ((nginx-binary #~(string-append #$nginx "/sbin/nginx"))
86 (nginx-action
87 (lambda args
88 #~(lambda _
89 (zero?
90 (system* #$nginx-binary "-c" #$config-file #$@args))))))
91
92 ;; TODO: Add 'reload' action.
93 (list (shepherd-service
94 (provision '(nginx))
95 (documentation "Run the nginx daemon.")
96 (requirement '(user-processes loopback))
97 (start (nginx-action "-p" run-directory))
98 (stop (nginx-action "-s" "stop"))))))))
99
100 (define nginx-service-type
101 (service-type (name 'nginx)
102 (extensions
103 (list (service-extension shepherd-root-service-type
104 nginx-shepherd-service)
105 (service-extension activation-service-type
106 nginx-activation)
107 (service-extension account-service-type
108 (const %nginx-accounts))))))
109
110 (define* (nginx-service #:key (nginx nginx)
111 (log-directory "/var/log/nginx")
112 (run-directory "/var/run/nginx")
113 (config-file
114 (default-nginx-config log-directory run-directory)))
115 "Return a service that runs NGINX, the nginx web server.
116
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
120 (nginx-configuration
121 (nginx nginx)
122 (log-directory log-directory)
123 (run-directory run-directory)
124 (file config-file))))