blob: 0709f7159bb03f206f26c94a7d70efca4f5717ed (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
{ config, pkgs, lib, ... }:
let
inherit (builtins)
elem filter isBool ;
inherit (lib)
concatMapStrings concatStringsSep filterAttrs mapAttrsToList mkDefault
mkEnableOption mkIf mkOption ;
inherit (lib.types)
attrsOf bool either enum int lines nullOr path str submodule ;
cfg = config.nixsap.apps.nginx;
explicit = filterAttrs (n: v: n != "_module" && v != null);
attrs = opts: submodule { options = opts; };
default = d: t: mkOption { type = t; default = d; };
optional = t: mkOption { type = nullOr t; default = null; };
show = v: if isBool v then (if v then "on" else "off") else toString v;
format = indent: set:
let mkEntry = k: v: "${indent}${k} ${show v};";
in concatStringsSep "\n" (mapAttrsToList mkEntry (explicit set));
mkServer = name: text: pkgs.writeText "nginx-${name}.conf" ''
# ${name}:
server {
${text}
}
'';
# Hardcode defaults that could be overriden in server context.
# Add options for http-only directives.
nginx-conf = pkgs.writeText "nginx.conf" ''
daemon off;
user ${cfg.user} ${cfg.user};
pid ${cfg.runDir}/nginx.pid;
${format "" (filterAttrs (n: _: ! elem n ["events" "http"]) cfg.conf)}
events {
${format " " cfg.conf.events}
}
http {
${cfg.conf.http.context}
${concatMapStrings (s: "include ${s};\n") (mapAttrsToList mkServer cfg.conf.http.servers)}
}
'';
exec = "${pkgs.nginx}/bin/nginx -c ${nginx-conf} -p ${cfg.stateDir}";
enabled = {} != explicit cfg.conf.http.servers;
in {
options.nixsap.apps.nginx = {
user = mkOption {
description = "User to run as";
type = str;
default = "nginx";
};
stateDir = mkOption {
description = "Directory holding all state for nginx to run";
type = path;
default = "/nginx";
};
logDir = mkOption {
description = ''
Nginx directory for logs. This is read-only. Use it in configuration
files of nginx itself or logrotate.
'';
type = path;
readOnly = true;
default = "${cfg.stateDir}/logs";
};
runDir = mkOption {
description = ''
Directory for sockets and PID-file.
UNIX-sockets created by nginx are world-writable.
So if you want some privacy, put sockets in this directory.
It is owned by nginx user and group, and has mode 0640.
'';
type = path;
readOnly = true;
default = "/run/nginx";
};
conf = default {} (attrs {
pcre_jit = optional bool;
timer_resolution = optional int;
worker_cpu_affinity = optional str;
worker_priority = optional int;
worker_processes = default "auto" (either int (enum ["auto"]));
worker_rlimit_core = optional int;
worker_rlimit_nofile = optional int;
events = default {} (attrs {
accept_mutex = optional bool;
accept_mutex_delay = optional int;
multi_accept = optional bool;
worker_aio_requests = optional int;
worker_connections = optional int;
});
http = default {} (attrs {
servers = default {} (attrsOf lines);
context = mkOption {
description = ''
Default directives in the http context. You normally don't
need to change it, because most of directives can be overriden
in server or location contexts. This parameter has a reasonale
default value which you should append in nixos modules, i. e. by
adding geoip directives or maps. Use `lib.mkForce` to completely
omit default directives.
'';
type = lines;
};
});
});
};
config = {
nixsap.apps.nginx.conf.http.context = ''
include ${pkgs.nginx}/conf/mime.types;
default_type application/octet-stream;
# This is `combined` format with $remote_user replaced by $http_from.
# $http_from is provided by Sproxy: https://hackage.haskell.org/package/sproxy2
log_format sproxy '$remote_addr - $http_from [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
access_log off;
error_log stderr info;
gzip on;
keepalive_timeout 65;
sendfile on;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
tcp_nodelay on;
tcp_nopush on;
types_hash_max_size 2048;
# https://www.nginx.com/blog/mitigating-the-httpoxy-vulnerability-with-nginx/
fastcgi_param HTTP_PROXY "";
proxy_set_header Proxy "";
'';
nixsap.system.users.daemons = mkIf enabled [ cfg.user ];
nixsap.apps.logrotate.conf.nginx = mkIf enabled {
files = [ "${cfg.logDir}/*.log" ];
directives = {
delaycompress = mkDefault true;
missingok = mkDefault true;
notifempty = mkDefault true;
rotate = mkDefault 14;
sharedscripts = true;
daily = mkDefault true;
create = mkDefault "0640 ${cfg.user} ${cfg.user}";
postrotate = pkgs.writeBashScript "logrotate-nginx-postrotate"
"systemctl kill -s SIGUSR1 --kill-who=main nginx.service";
};
};
systemd.services.nginx = mkIf enabled {
description = "web/proxy server";
wants = [ "keys.target" ];
after = [ "keys.target" "local-fs.target" "network.target" ];
wantedBy = [ "multi-user.target" ];
preStart = ''
rm -rf '${cfg.runDir}'
mkdir -p '${cfg.stateDir}/logs' '${cfg.runDir}'
chown -Rc '${cfg.user}:${cfg.user}' '${cfg.stateDir}' '${cfg.runDir}'
chmod -Rc u=rwX,g=rX,o= '${cfg.stateDir}' '${cfg.runDir}'
'';
serviceConfig = {
ExecStart = exec;
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
RestartSec = "10s";
StartLimitInterval = "1min";
Restart = "always";
};
};
};
}
|