2015-03-18 14:09:24 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2013-04-14 10:14:27 +01:00
|
|
|
|
2014-04-14 15:26:48 +01:00
|
|
|
with lib;
|
2013-04-14 10:14:27 +01:00
|
|
|
|
2013-03-02 22:40:56 +00:00
|
|
|
let
|
2015-03-18 14:09:24 +00:00
|
|
|
cfg = config.services.nginx;
|
2013-10-20 21:52:02 +01:00
|
|
|
nginx = cfg.package;
|
2013-03-02 22:40:56 +00:00
|
|
|
configFile = pkgs.writeText "nginx.conf" ''
|
2013-05-05 20:44:06 +01:00
|
|
|
user ${cfg.user} ${cfg.group};
|
2013-04-14 10:17:16 +01:00
|
|
|
daemon off;
|
2013-03-02 22:40:56 +00:00
|
|
|
${cfg.config}
|
2014-03-18 03:31:56 +00:00
|
|
|
${optionalString (cfg.httpConfig != "") ''
|
|
|
|
http {
|
2015-08-03 18:16:38 +01:00
|
|
|
${cfg.httpConfig}
|
|
|
|
${cfg.httpServers}
|
|
|
|
${cfg.httpDefaultServer}
|
2014-03-18 03:31:56 +00:00
|
|
|
}
|
|
|
|
''}
|
2014-02-12 17:35:39 +00:00
|
|
|
${cfg.appendConfig}
|
2013-03-02 22:40:56 +00:00
|
|
|
'';
|
|
|
|
in
|
2013-04-14 10:14:27 +01:00
|
|
|
|
2013-03-02 22:40:56 +00:00
|
|
|
{
|
|
|
|
options = {
|
2015-03-18 14:09:24 +00:00
|
|
|
services.nginx = {
|
2013-03-02 22:40:56 +00:00
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
2014-12-10 01:40:00 +00:00
|
|
|
type = types.bool;
|
2013-03-02 22:40:56 +00:00
|
|
|
description = "
|
|
|
|
Enable the nginx Web Server.
|
|
|
|
";
|
|
|
|
};
|
2013-04-14 10:14:27 +01:00
|
|
|
|
2013-10-20 21:52:02 +01:00
|
|
|
package = mkOption {
|
|
|
|
default = pkgs.nginx;
|
2014-02-27 12:22:04 +00:00
|
|
|
type = types.package;
|
2013-10-20 21:52:02 +01:00
|
|
|
description = "
|
|
|
|
Nginx package to use.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2013-03-02 22:40:56 +00:00
|
|
|
config = mkOption {
|
2013-04-14 10:17:16 +01:00
|
|
|
default = "events {}";
|
2013-03-02 22:40:56 +00:00
|
|
|
description = "
|
|
|
|
Verbatim nginx.conf configuration.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
2014-02-12 17:35:39 +00:00
|
|
|
appendConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Configuration lines appended to the generated Nginx
|
|
|
|
configuration file. Commonly used by different modules
|
|
|
|
providing http snippets. <option>appendConfig</option>
|
|
|
|
can be specified more than once and it's value will be
|
|
|
|
concatenated (contrary to <option>config</option> which
|
|
|
|
can be set only once).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-03-18 03:31:56 +00:00
|
|
|
httpConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
2015-08-03 18:16:38 +01:00
|
|
|
description = ''
|
|
|
|
Configuration lines to be placed at the top inside of
|
|
|
|
the http {} block. The option is intended to be used for
|
|
|
|
the default configuration of the servers.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
httpServers = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Configuration lines to be placed inside of the http {}
|
|
|
|
block. The option is intended to be used for defining
|
|
|
|
individual servers.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
httpDefaultServer = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Configuration lines to be placed at the bottom inside of
|
|
|
|
the http {} block. The option is intended to be used for
|
|
|
|
setting up the default servers. The default server is used
|
|
|
|
if no previously specified server matches a request.
|
|
|
|
'';
|
2014-03-18 03:31:56 +00:00
|
|
|
};
|
|
|
|
|
2013-03-02 22:40:56 +00:00
|
|
|
stateDir = mkOption {
|
|
|
|
default = "/var/spool/nginx";
|
|
|
|
description = "
|
|
|
|
Directory holding all state for nginx to run.
|
|
|
|
";
|
|
|
|
};
|
2013-05-05 20:44:06 +01:00
|
|
|
|
|
|
|
user = mkOption {
|
2014-12-10 01:40:00 +00:00
|
|
|
type = types.str;
|
2013-05-05 20:44:06 +01:00
|
|
|
default = "nginx";
|
|
|
|
description = "User account under which nginx runs.";
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
2014-12-10 01:40:00 +00:00
|
|
|
type = types.str;
|
2013-05-05 20:44:06 +01:00
|
|
|
default = "nginx";
|
|
|
|
description = "Group account under which nginx runs.";
|
|
|
|
};
|
|
|
|
|
2015-03-18 14:09:24 +00:00
|
|
|
};
|
|
|
|
|
2013-03-02 22:40:56 +00:00
|
|
|
};
|
2013-04-14 10:14:27 +01:00
|
|
|
|
2013-03-02 22:40:56 +00:00
|
|
|
config = mkIf cfg.enable {
|
|
|
|
# TODO: test user supplied config file pases syntax test
|
2013-04-14 10:14:27 +01:00
|
|
|
|
2015-03-18 14:09:24 +00:00
|
|
|
systemd.services.nginx = {
|
|
|
|
description = "Nginx Web Server";
|
2013-03-02 22:40:56 +00:00
|
|
|
after = [ "network.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2013-05-05 20:44:06 +01:00
|
|
|
path = [ nginx ];
|
2013-03-02 22:40:56 +00:00
|
|
|
preStart =
|
|
|
|
''
|
|
|
|
mkdir -p ${cfg.stateDir}/logs
|
2014-06-03 15:00:27 +01:00
|
|
|
chmod 700 ${cfg.stateDir}
|
2013-05-05 20:44:06 +01:00
|
|
|
chown -R ${cfg.user}:${cfg.group} ${cfg.stateDir}
|
2013-03-02 22:40:56 +00:00
|
|
|
'';
|
|
|
|
serviceConfig = {
|
2013-05-05 20:44:06 +01:00
|
|
|
ExecStart = "${nginx}/bin/nginx -c ${configFile} -p ${cfg.stateDir}";
|
2015-04-25 14:17:06 +01:00
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
2015-01-19 09:46:45 +00:00
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "10s";
|
|
|
|
StartLimitInterval = "1min";
|
2013-03-02 22:40:56 +00:00
|
|
|
};
|
|
|
|
};
|
2013-04-14 10:14:27 +01:00
|
|
|
|
2013-05-06 16:11:04 +01:00
|
|
|
users.extraUsers = optionalAttrs (cfg.user == "nginx") (singleton
|
|
|
|
{ name = "nginx";
|
2014-06-03 15:00:27 +01:00
|
|
|
group = cfg.group;
|
2015-03-18 14:09:24 +00:00
|
|
|
uid = config.ids.uids.nginx;
|
2013-05-06 16:11:04 +01:00
|
|
|
});
|
2013-04-14 10:14:27 +01:00
|
|
|
|
2013-05-06 16:11:04 +01:00
|
|
|
users.extraGroups = optionalAttrs (cfg.group == "nginx") (singleton
|
|
|
|
{ name = "nginx";
|
2015-03-18 14:09:24 +00:00
|
|
|
gid = config.ids.gids.nginx;
|
2013-05-06 16:11:04 +01:00
|
|
|
});
|
|
|
|
};
|
2015-03-18 14:09:24 +00:00
|
|
|
}
|