Commit | Line | Data |
---|---|---|
58724c48 DT |
1 | ;;; GNU Guix --- Functional package management for GNU |
2 | ;;; Copyright © 2015 David Thompson <davet@gnu.org> | |
24e96431 | 3 | ;;; Copyright © 2015, 2016 Ludovic Courtès <ludo@gnu.org> |
6c12abbd | 4 | ;;; Copyright © 2016 ng0 <ng0@we.make.ritual.n0.is> |
58724c48 DT |
5 | ;;; |
6 | ;;; This file is part of GNU Guix. | |
7 | ;;; | |
8 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
9 | ;;; under the terms of the GNU General Public License as published by | |
10 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
11 | ;;; your option) any later version. | |
12 | ;;; | |
13 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
14 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | ;;; GNU General Public License for more details. | |
17 | ;;; | |
18 | ;;; You should have received a copy of the GNU General Public License | |
19 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
20 | ||
21 | (define-module (gnu services web) | |
22 | #:use-module (gnu services) | |
0190c1c0 | 23 | #:use-module (gnu services shepherd) |
58724c48 DT |
24 | #:use-module (gnu system shadow) |
25 | #:use-module (gnu packages admin) | |
26 | #:use-module (gnu packages web) | |
27 | #:use-module (guix records) | |
58724c48 | 28 | #:use-module (guix gexp) |
0adfe95a | 29 | #:use-module (ice-9 match) |
24e96431 TČ |
30 | #:export (nginx-configuration |
31 | nginx-configuration? | |
32 | nginx-service | |
33 | nginx-service-type)) | |
58724c48 DT |
34 | |
35 | ;;; Commentary: | |
36 | ;;; | |
37 | ;;; Web services. | |
38 | ;;; | |
39 | ;;; Code: | |
40 | ||
0adfe95a LC |
41 | (define-record-type* <nginx-configuration> |
42 | nginx-configuration make-nginx-configuration | |
43 | nginx-configuration? | |
44 | (nginx nginx-configuration-nginx) ;<package> | |
45 | (log-directory nginx-configuration-log-directory) ;string | |
46 | (run-directory nginx-configuration-run-directory) ;string | |
47 | (file nginx-configuration-file)) ;string | file-like | |
48 | ||
58724c48 DT |
49 | (define (default-nginx-config log-directory run-directory) |
50 | (plain-file "nginx.conf" | |
51 | (string-append | |
52 | "user nginx nginx;\n" | |
53 | "pid " run-directory "/pid;\n" | |
54 | "error_log " log-directory "/error.log info;\n" | |
55 | "http {\n" | |
56 | " access_log " log-directory "/access.log;\n" | |
57 | " root /var/www;\n" | |
58 | " server {}\n" | |
59 | "}\n" | |
60 | "events {}\n"))) | |
61 | ||
0adfe95a LC |
62 | (define %nginx-accounts |
63 | (list (user-group (name "nginx") (system? #t)) | |
64 | (user-account | |
65 | (name "nginx") | |
66 | (group "nginx") | |
67 | (system? #t) | |
68 | (comment "nginx server user") | |
69 | (home-directory "/var/empty") | |
9e41130b | 70 | (shell (file-append shadow "/sbin/nologin"))))) |
0adfe95a LC |
71 | |
72 | (define nginx-activation | |
73 | (match-lambda | |
74 | (($ <nginx-configuration> nginx log-directory run-directory config-file) | |
75 | #~(begin | |
76 | (use-modules (guix build utils)) | |
77 | ||
78 | (format #t "creating nginx log directory '~a'~%" #$log-directory) | |
79 | (mkdir-p #$log-directory) | |
80 | (format #t "creating nginx run directory '~a'~%" #$run-directory) | |
81 | (mkdir-p #$run-directory) | |
82 | ;; Check configuration file syntax. | |
d21047d5 | 83 | (system* (string-append #$nginx "/sbin/nginx") |
0adfe95a LC |
84 | "-c" #$config-file "-t"))))) |
85 | ||
d4053c71 | 86 | (define nginx-shepherd-service |
0adfe95a LC |
87 | (match-lambda |
88 | (($ <nginx-configuration> nginx log-directory run-directory config-file) | |
9e41130b | 89 | (let* ((nginx-binary (file-append nginx "/sbin/nginx")) |
0adfe95a LC |
90 | (nginx-action |
91 | (lambda args | |
92 | #~(lambda _ | |
93 | (zero? | |
94 | (system* #$nginx-binary "-c" #$config-file #$@args)))))) | |
95 | ||
96 | ;; TODO: Add 'reload' action. | |
d4053c71 | 97 | (list (shepherd-service |
0adfe95a LC |
98 | (provision '(nginx)) |
99 | (documentation "Run the nginx daemon.") | |
100 | (requirement '(user-processes loopback)) | |
101 | (start (nginx-action "-p" run-directory)) | |
102 | (stop (nginx-action "-s" "stop")))))))) | |
103 | ||
104 | (define nginx-service-type | |
105 | (service-type (name 'nginx) | |
106 | (extensions | |
d4053c71 AK |
107 | (list (service-extension shepherd-root-service-type |
108 | nginx-shepherd-service) | |
0adfe95a LC |
109 | (service-extension activation-service-type |
110 | nginx-activation) | |
111 | (service-extension account-service-type | |
112 | (const %nginx-accounts)))))) | |
113 | ||
58724c48 DT |
114 | (define* (nginx-service #:key (nginx nginx) |
115 | (log-directory "/var/log/nginx") | |
116 | (run-directory "/var/run/nginx") | |
117 | (config-file | |
118 | (default-nginx-config log-directory run-directory))) | |
119 | "Return a service that runs NGINX, the nginx web server. | |
120 | ||
6c12abbd | 121 | The nginx daemon loads its runtime configuration from CONFIG-FILE, stores log |
58724c48 | 122 | files in LOG-DIRECTORY, and stores temporary runtime files in RUN-DIRECTORY." |
0adfe95a LC |
123 | (service nginx-service-type |
124 | (nginx-configuration | |
125 | (nginx nginx) | |
126 | (log-directory log-directory) | |
127 | (run-directory run-directory) | |
128 | (file config-file)))) |