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