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;
|
2016-01-25 18:36:21 +00:00
|
|
|
virtualHosts = mapAttrs (vhostName: vhostConfig:
|
|
|
|
vhostConfig // (optionalAttrs vhostConfig.enableACME {
|
|
|
|
sslCertificate = "/var/lib/acme/${vhostName}/fullchain.pem";
|
|
|
|
sslCertificateKey = "/var/lib/acme/${vhostName}/key.pem";
|
|
|
|
})
|
|
|
|
) cfg.virtualHosts;
|
|
|
|
|
2016-07-28 12:55:06 +01:00
|
|
|
configFile = pkgs.writeText "nginx.conf" ''
|
2013-05-05 20:44:06 +01:00
|
|
|
user ${cfg.user} ${cfg.group};
|
2016-01-24 15:50:54 +00:00
|
|
|
error_log stderr;
|
2013-04-14 10:17:16 +01:00
|
|
|
daemon off;
|
2015-09-07 13:17:33 +01:00
|
|
|
|
2014-03-18 03:31:56 +00:00
|
|
|
http {
|
2015-09-07 13:17:33 +01:00
|
|
|
include ${cfg.package}/conf/mime.types;
|
2016-01-24 15:50:54 +00:00
|
|
|
include ${cfg.package}/conf/fastcgi.conf;
|
|
|
|
|
2016-07-25 17:07:53 +01:00
|
|
|
${optionalString (cfg.recommendedOptimisation) ''
|
|
|
|
# optimisation
|
|
|
|
sendfile on;
|
|
|
|
tcp_nopush on;
|
|
|
|
tcp_nodelay on;
|
|
|
|
keepalive_timeout 65;
|
|
|
|
types_hash_max_size 2048;
|
|
|
|
''}
|
2016-01-24 15:50:54 +00:00
|
|
|
|
2016-02-01 13:08:45 +00:00
|
|
|
ssl_protocols ${cfg.sslProtocols};
|
2016-02-01 13:09:13 +00:00
|
|
|
ssl_ciphers ${cfg.sslCiphers};
|
2016-02-01 16:30:43 +00:00
|
|
|
${optionalString (cfg.sslDhparam != null) "ssl_dhparam ${cfg.sslDhparam};"}
|
2016-01-24 15:50:54 +00:00
|
|
|
|
2016-07-25 17:07:53 +01:00
|
|
|
${optionalString (cfg.recommendedTlsSettings) ''
|
|
|
|
ssl_session_cache shared:SSL:42m;
|
|
|
|
ssl_session_timeout 23m;
|
|
|
|
ssl_ecdh_curve secp384r1;
|
|
|
|
ssl_prefer_server_ciphers on;
|
|
|
|
ssl_stapling on;
|
|
|
|
ssl_stapling_verify on;
|
|
|
|
''}
|
|
|
|
|
|
|
|
${optionalString (cfg.recommendedGzipSettings) ''
|
|
|
|
gzip on;
|
|
|
|
gzip_disable "msie6";
|
|
|
|
gzip_proxied any;
|
|
|
|
gzip_comp_level 9;
|
|
|
|
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
|
|
|
|
''}
|
|
|
|
|
|
|
|
${optionalString (cfg.recommendedProxySettings) ''
|
|
|
|
proxy_set_header Host $host;
|
|
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
|
|
proxy_set_header X-Forwarded-Host $host;
|
|
|
|
proxy_set_header X-Forwarded-Server $host;
|
|
|
|
proxy_set_header Accept-Encoding "";
|
|
|
|
|
|
|
|
proxy_redirect off;
|
|
|
|
proxy_connect_timeout 90;
|
|
|
|
proxy_send_timeout 90;
|
|
|
|
proxy_read_timeout 90;
|
|
|
|
proxy_http_version 1.0;
|
|
|
|
''}
|
|
|
|
|
2016-01-24 15:50:54 +00:00
|
|
|
client_max_body_size 10m;
|
|
|
|
|
|
|
|
server_tokens ${if cfg.serverTokens then "on" else "off"};
|
|
|
|
${vhosts}
|
2016-07-25 16:10:36 +01:00
|
|
|
}
|
2016-07-25 17:07:53 +01:00
|
|
|
|
|
|
|
${cfg.config}
|
|
|
|
|
|
|
|
# Keep this seperate to allow overriding previous settings
|
2016-07-25 16:10:36 +01:00
|
|
|
${optionalString (cfg.httpConfig != "") ''
|
|
|
|
http {
|
|
|
|
include ${cfg.package}/conf/mime.types;
|
|
|
|
include ${cfg.package}/conf/fastcgi.conf;
|
2015-09-07 13:17:33 +01:00
|
|
|
${cfg.httpConfig}
|
2014-03-18 03:31:56 +00:00
|
|
|
}
|
2016-07-25 16:10:36 +01:00
|
|
|
''}
|
2014-02-12 17:35:39 +00:00
|
|
|
${cfg.appendConfig}
|
2013-03-02 22:40:56 +00:00
|
|
|
'';
|
2016-01-24 15:50:54 +00:00
|
|
|
|
|
|
|
vhosts = concatStringsSep "\n" (mapAttrsToList (serverName: vhost:
|
|
|
|
let
|
|
|
|
ssl = vhost.enableSSL || vhost.forceSSL;
|
|
|
|
port = if vhost.port != null then vhost.port else (if ssl then 443 else 80);
|
2016-04-29 08:17:30 +01:00
|
|
|
listenString = toString port + optionalString ssl " ssl http2"
|
2016-04-03 11:58:34 +01:00
|
|
|
+ optionalString vhost.default " default";
|
2016-02-17 03:01:50 +00:00
|
|
|
acmeLocation = optionalString vhost.enableACME ''
|
|
|
|
location /.well-known/acme-challenge {
|
|
|
|
try_files $uri @acme-fallback;
|
|
|
|
root ${vhost.acmeRoot};
|
2016-03-25 00:58:45 +00:00
|
|
|
auth_basic off;
|
2016-02-17 03:01:50 +00:00
|
|
|
}
|
|
|
|
location @acme-fallback {
|
2016-03-25 00:58:45 +00:00
|
|
|
auth_basic off;
|
2016-02-17 03:01:50 +00:00
|
|
|
proxy_pass http://${vhost.acmeFallbackHost};
|
|
|
|
}
|
|
|
|
'';
|
2016-01-24 15:50:54 +00:00
|
|
|
in ''
|
2016-01-25 18:36:21 +00:00
|
|
|
${optionalString vhost.forceSSL ''
|
2016-01-24 15:50:54 +00:00
|
|
|
server {
|
2016-04-03 11:58:34 +01:00
|
|
|
listen 80 ${optionalString vhost.default "default"};
|
|
|
|
listen [::]:80 ${optionalString vhost.default "default"};
|
2016-01-24 15:50:54 +00:00
|
|
|
|
|
|
|
server_name ${serverName} ${concatStringsSep " " vhost.serverAliases};
|
2016-02-17 03:01:50 +00:00
|
|
|
${acmeLocation}
|
2016-01-26 13:29:30 +00:00
|
|
|
location / {
|
|
|
|
return 301 https://$host${optionalString (port != 443) ":${port}"}$request_uri;
|
|
|
|
}
|
2016-01-24 15:50:54 +00:00
|
|
|
}
|
2016-01-25 18:36:21 +00:00
|
|
|
''}
|
2016-01-24 15:50:54 +00:00
|
|
|
|
|
|
|
server {
|
|
|
|
listen ${listenString};
|
|
|
|
listen [::]:${listenString};
|
|
|
|
|
|
|
|
server_name ${serverName} ${concatStringsSep " " vhost.serverAliases};
|
2016-02-17 03:01:50 +00:00
|
|
|
${acmeLocation}
|
2016-01-24 15:50:54 +00:00
|
|
|
${optionalString (vhost.root != null) "root ${vhost.root};"}
|
|
|
|
${optionalString (vhost.globalRedirect != null) ''
|
2016-03-25 00:59:34 +00:00
|
|
|
return 301 http${optionalString ssl "s"}://${vhost.globalRedirect}$request_uri;
|
2016-01-24 15:50:54 +00:00
|
|
|
''}
|
|
|
|
${optionalString ssl ''
|
|
|
|
ssl_certificate ${vhost.sslCertificate};
|
|
|
|
ssl_certificate_key ${vhost.sslCertificateKey};
|
|
|
|
''}
|
|
|
|
|
2016-03-25 01:00:09 +00:00
|
|
|
${optionalString (vhost.basicAuth != {}) (mkBasicAuth serverName vhost.basicAuth)}
|
|
|
|
|
|
|
|
${mkLocations vhost.locations}
|
2016-01-24 15:50:54 +00:00
|
|
|
|
|
|
|
${vhost.extraConfig}
|
|
|
|
}
|
|
|
|
''
|
2016-01-25 18:36:21 +00:00
|
|
|
) virtualHosts);
|
2016-03-25 01:00:09 +00:00
|
|
|
mkLocations = locations: concatStringsSep "\n" (mapAttrsToList (location: config: ''
|
2016-01-24 15:50:54 +00:00
|
|
|
location ${location} {
|
|
|
|
${optionalString (config.proxyPass != null) "proxy_pass ${config.proxyPass};"}
|
|
|
|
${optionalString (config.root != null) "root ${config.root};"}
|
2016-02-10 15:02:38 +00:00
|
|
|
${config.extraConfig}
|
2016-01-24 15:50:54 +00:00
|
|
|
}
|
|
|
|
'') locations);
|
2016-03-25 01:00:09 +00:00
|
|
|
mkBasicAuth = serverName: authDef: let
|
|
|
|
htpasswdFile = pkgs.writeText "${serverName}.htpasswd" (
|
|
|
|
concatStringsSep "\n" (mapAttrsToList (user: password: ''
|
|
|
|
${user}:{PLAIN}${password}
|
|
|
|
'') authDef)
|
|
|
|
);
|
|
|
|
in ''
|
|
|
|
auth_basic secured;
|
|
|
|
auth_basic_user_file ${htpasswdFile};
|
|
|
|
'';
|
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 = {
|
2016-07-25 17:07:53 +01:00
|
|
|
enable = mkEnableOption "Nginx Web Server";
|
|
|
|
|
|
|
|
recommendedTlsSettings = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = "
|
|
|
|
Enable recommended TLS settings.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
recommendedOptimisation = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = "
|
|
|
|
Enable recommended optimisation settings.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
recommendedGzipSettings = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = "
|
|
|
|
Enable recommended gzip settings.
|
|
|
|
";
|
|
|
|
};
|
|
|
|
|
|
|
|
recommendedProxySettings = mkOption {
|
2013-03-02 22:40:56 +00:00
|
|
|
default = false;
|
2014-12-10 01:40:00 +00:00
|
|
|
type = types.bool;
|
2013-03-02 22:40:56 +00:00
|
|
|
description = "
|
2016-07-25 17:07:53 +01:00
|
|
|
Enable recommended proxy settings.
|
2013-03-02 22:40:56 +00:00
|
|
|
";
|
|
|
|
};
|
2013-04-14 10:14:27 +01:00
|
|
|
|
2013-10-20 21:52:02 +01:00
|
|
|
package = mkOption {
|
|
|
|
default = pkgs.nginx;
|
2016-01-17 18:34:55 +00:00
|
|
|
defaultText = "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-09-07 13:17:33 +01:00
|
|
|
description = "Configuration lines to be appended inside of the http {} block.";
|
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.";
|
|
|
|
};
|
|
|
|
|
2016-01-24 15:50:54 +00:00
|
|
|
serverTokens = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Show nginx version in headers and error pages";
|
|
|
|
};
|
2015-03-18 14:09:24 +00:00
|
|
|
|
2016-02-01 13:09:13 +00:00
|
|
|
sslCiphers = mkOption {
|
|
|
|
type = types.str;
|
2016-04-29 08:17:30 +01:00
|
|
|
default = "EECDH+aRSA+AESGCM:EDH+aRSA:EECDH+aRSA:+AES256:+AES128:+SHA1:!CAMELLIA:!SEED:!3DES:!DES:!RC4:!eNULL";
|
2016-02-01 13:09:13 +00:00
|
|
|
description = "Ciphers to choose from when negotiating tls handshakes.";
|
|
|
|
};
|
|
|
|
|
2016-02-01 13:08:45 +00:00
|
|
|
sslProtocols = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "TLSv1.2";
|
|
|
|
example = "TLSv1 TLSv1.1 TLSv1.2";
|
|
|
|
description = "Allowed TLS protocol versions.";
|
|
|
|
};
|
|
|
|
|
2016-02-01 16:30:43 +00:00
|
|
|
sslDhparam = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
2016-05-09 15:46:44 +01:00
|
|
|
example = "/path/to/dhparams.pem";
|
2016-02-01 16:30:43 +00:00
|
|
|
description = "Path to DH parameters file.";
|
|
|
|
};
|
|
|
|
|
2016-01-24 15:50:54 +00:00
|
|
|
virtualHosts = mkOption {
|
|
|
|
type = types.attrsOf (types.submodule (import ./vhost-options.nix {
|
|
|
|
inherit lib;
|
|
|
|
}));
|
|
|
|
default = {
|
|
|
|
localhost = {};
|
|
|
|
};
|
2016-05-09 15:46:44 +01:00
|
|
|
example = literalExample ''
|
|
|
|
{
|
|
|
|
"hydra.example.com" = {
|
|
|
|
forceSSL = true;
|
|
|
|
enableACME = true;
|
|
|
|
locations."/" = {
|
|
|
|
proxyPass = "http://localhost:3000";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2016-01-24 15:50:54 +00:00
|
|
|
'';
|
2016-05-09 15:46:44 +01:00
|
|
|
description = "Declarative vhost config";
|
2016-01-24 15:50:54 +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 {
|
2016-07-28 12:55:06 +01:00
|
|
|
# TODO: test user supplied config file pases syntax test
|
|
|
|
|
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" ];
|
|
|
|
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 = {
|
2016-01-25 18:36:21 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/nginx -c ${configFile} -p ${cfg.stateDir}";
|
2015-04-25 14:17:06 +01:00
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
2016-01-24 15:50:54 +00:00
|
|
|
Restart = "always";
|
2015-01-19 09:46:45 +00:00
|
|
|
RestartSec = "10s";
|
|
|
|
StartLimitInterval = "1min";
|
2013-03-02 22:40:56 +00:00
|
|
|
};
|
|
|
|
};
|
2013-04-14 10:14:27 +01:00
|
|
|
|
2016-02-08 05:12:49 +00:00
|
|
|
security.acme.certs = filterAttrs (n: v: v != {}) (
|
|
|
|
mapAttrs (vhostName: vhostConfig:
|
|
|
|
optionalAttrs vhostConfig.enableACME {
|
|
|
|
webroot = vhostConfig.acmeRoot;
|
|
|
|
extraDomains = genAttrs vhostConfig.serverAliases (alias: null);
|
|
|
|
}
|
|
|
|
) virtualHosts
|
|
|
|
);
|
2016-01-25 18:36:21 +00: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
|
|
|
}
|