1
0
Fork 1
mirror of https://github.com/NixOS/nixpkgs.git synced 2024-12-14 16:46:09 +00:00
nixpkgs/nixos/modules/services/web-servers/nginx/default.nix

151 lines
3.8 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
2013-04-14 10:14:27 +01:00
with lib;
2013-04-14 10:14:27 +01:00
2013-03-02 22:40:56 +00:00
let
cfg = config.services.nginx;
nginx = cfg.package;
2013-03-02 22:40:56 +00:00
configFile = pkgs.writeText "nginx.conf" ''
user ${cfg.user} ${cfg.group};
daemon off;
2013-03-02 22:40:56 +00:00
${cfg.config}
${optionalString (cfg.httpConfig != "") ''
http {
${cfg.httpConfig}
${cfg.httpServers}
${cfg.httpDefaultServer}
}
''}
${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 = {
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
package = mkOption {
default = pkgs.nginx;
type = types.package;
description = "
Nginx package to use.
";
};
2013-03-02 22:40:56 +00:00
config = mkOption {
default = "events {}";
2013-03-02 22:40:56 +00:00
description = "
Verbatim nginx.conf configuration.
";
};
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).
'';
};
httpConfig = mkOption {
type = types.lines;
default = "";
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.
'';
};
2013-03-02 22:40:56 +00:00
stateDir = mkOption {
default = "/var/spool/nginx";
description = "
Directory holding all state for nginx to run.
";
};
user = mkOption {
2014-12-10 01:40:00 +00:00
type = types.str;
default = "nginx";
description = "User account under which nginx runs.";
};
group = mkOption {
2014-12-10 01:40:00 +00:00
type = types.str;
default = "nginx";
description = "Group account under which nginx runs.";
};
};
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
systemd.services.nginx = {
description = "Nginx Web Server";
2013-03-02 22:40:56 +00:00
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
path = [ nginx ];
2013-03-02 22:40:56 +00:00
preStart =
''
mkdir -p ${cfg.stateDir}/logs
chmod 700 ${cfg.stateDir}
chown -R ${cfg.user}:${cfg.group} ${cfg.stateDir}
2013-03-02 22:40:56 +00:00
'';
serviceConfig = {
ExecStart = "${nginx}/bin/nginx -c ${configFile} -p ${cfg.stateDir}";
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
Restart = "on-failure";
RestartSec = "10s";
StartLimitInterval = "1min";
2013-03-02 22:40:56 +00:00
};
};
2013-04-14 10:14:27 +01:00
users.extraUsers = optionalAttrs (cfg.user == "nginx") (singleton
{ name = "nginx";
group = cfg.group;
uid = config.ids.uids.nginx;
});
2013-04-14 10:14:27 +01:00
users.extraGroups = optionalAttrs (cfg.group == "nginx") (singleton
{ name = "nginx";
gid = config.ids.gids.nginx;
});
};
}