forked from mirrors/nixpkgs
123 lines
3 KiB
Nix
123 lines
3 KiB
Nix
{ config, lib, pkgs, ... }@args:
|
|
|
|
with lib;
|
|
|
|
let topconfig = config;
|
|
in mkMultiInstance [ "services" "nginx" ] args ({ config, name, ... }:
|
|
|
|
let
|
|
cfg = config;
|
|
nginx = cfg.package;
|
|
configFile = pkgs.writeText "nginx.conf" ''
|
|
user ${cfg.user} ${cfg.group};
|
|
daemon off;
|
|
${cfg.config}
|
|
${optionalString (cfg.httpConfig != "") ''
|
|
http {
|
|
${cfg.httpConfig}
|
|
}
|
|
''}
|
|
${cfg.appendConfig}
|
|
'';
|
|
in
|
|
|
|
{
|
|
options = {
|
|
enable = mkOption {
|
|
default = false;
|
|
type = types.bool;
|
|
description = "
|
|
Enable the nginx Web Server.
|
|
";
|
|
};
|
|
|
|
package = mkOption {
|
|
default = pkgs.nginx;
|
|
type = types.package;
|
|
description = "
|
|
Nginx package to use.
|
|
";
|
|
};
|
|
|
|
config = mkOption {
|
|
default = "events {}";
|
|
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 appended inside of the http {} block.";
|
|
};
|
|
|
|
stateDir = mkOption {
|
|
default = "/var/spool/nginx";
|
|
description = "
|
|
Directory holding all state for nginx to run.
|
|
";
|
|
};
|
|
|
|
user = mkOption {
|
|
type = types.str;
|
|
default = "nginx";
|
|
description = "User account under which nginx runs.";
|
|
};
|
|
|
|
group = mkOption {
|
|
type = types.str;
|
|
default = "nginx";
|
|
description = "Group account under which nginx runs.";
|
|
};
|
|
|
|
};
|
|
|
|
config = mkIf cfg.enable {
|
|
# TODO: test user supplied config file pases syntax test
|
|
|
|
systemd.services."nginx-${name}" = {
|
|
description = "Nginx Web Server - ${name}";
|
|
after = [ "network.target" ];
|
|
wantedBy = [ "multi-user.target" ];
|
|
path = [ nginx ];
|
|
preStart =
|
|
''
|
|
mkdir -p ${cfg.stateDir}/logs
|
|
chmod 700 ${cfg.stateDir}
|
|
chown -R ${cfg.user}:${cfg.group} ${cfg.stateDir}
|
|
'';
|
|
serviceConfig = {
|
|
ExecStart = "${nginx}/bin/nginx -c ${configFile} -p ${cfg.stateDir}";
|
|
Restart = "on-failure";
|
|
RestartSec = "10s";
|
|
StartLimitInterval = "1min";
|
|
};
|
|
};
|
|
|
|
users.extraUsers = optionalAttrs (cfg.user == "nginx") (singleton
|
|
{ name = "nginx";
|
|
group = cfg.group;
|
|
uid = topconfig.ids.uids.nginx;
|
|
});
|
|
|
|
users.extraGroups = optionalAttrs (cfg.group == "nginx") (singleton
|
|
{ name = "nginx";
|
|
gid = topconfig.ids.gids.nginx;
|
|
});
|
|
};
|
|
})
|