2021-08-24 22:30:44 +02:00
|
|
|
# A simple abstraction layer for almost all of my services' needs
|
2021-11-05 14:14:10 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
2021-08-24 22:30:44 +02:00
|
|
|
let
|
|
|
|
cfg = config.my.services.nginx;
|
2021-01-31 16:37:58 +01:00
|
|
|
|
2022-11-29 17:19:24 +01:00
|
|
|
domain = config.networking.domain;
|
|
|
|
|
2023-12-25 19:28:53 +01:00
|
|
|
virtualHostOption = with lib; types.submodule ({ name, ... }: {
|
2021-08-24 22:30:44 +02:00
|
|
|
options = {
|
|
|
|
subdomain = mkOption {
|
|
|
|
type = types.str;
|
2023-12-25 19:28:53 +01:00
|
|
|
default = name;
|
2021-08-24 22:30:44 +02:00
|
|
|
example = "dev";
|
|
|
|
description = ''
|
|
|
|
Which subdomain, under config.networking.domain, to use
|
|
|
|
for this virtual host.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = with types; nullOr port;
|
|
|
|
default = null;
|
|
|
|
example = 8080;
|
|
|
|
description = ''
|
|
|
|
Which port to proxy to, through 127.0.0.1, for this virtual host.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-03-16 21:30:47 +01:00
|
|
|
redirect = mkOption {
|
|
|
|
type = with types; nullOr str;
|
|
|
|
default = null;
|
|
|
|
example = "https://example.com";
|
|
|
|
description = ''
|
|
|
|
Which domain to redirect to (301 response), for this virtual host.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-08-24 22:30:44 +02:00
|
|
|
root = mkOption {
|
|
|
|
type = with types; nullOr path;
|
|
|
|
default = null;
|
|
|
|
example = "/var/www/blog";
|
|
|
|
description = ''
|
2023-10-28 13:14:42 +02:00
|
|
|
The root folder for this virtual host.
|
2021-08-24 22:30:44 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2023-05-13 20:19:26 +02:00
|
|
|
socket = mkOption {
|
|
|
|
type = with types; nullOr path;
|
|
|
|
default = null;
|
|
|
|
example = "FIXME";
|
|
|
|
description = ''
|
2023-10-28 13:14:42 +02:00
|
|
|
The UNIX socket for this virtual host.
|
2023-05-13 20:19:26 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-08-30 14:53:13 +02:00
|
|
|
sso = {
|
|
|
|
enable = mkEnableOption "SSO authentication";
|
|
|
|
};
|
|
|
|
|
2021-08-24 22:30:44 +02:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.attrs; # FIXME: forward type of virtualHosts
|
2024-09-27 15:44:23 +02:00
|
|
|
example = {
|
|
|
|
locations."/socket" = {
|
|
|
|
proxyPass = "http://127.0.0.1:8096/";
|
|
|
|
proxyWebsockets = true;
|
|
|
|
};
|
|
|
|
};
|
2021-08-24 22:30:44 +02:00
|
|
|
default = { };
|
|
|
|
description = ''
|
|
|
|
Any extra configuration that should be applied to this virtual host.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2023-12-25 19:28:53 +01:00
|
|
|
});
|
2021-08-24 22:30:44 +02:00
|
|
|
in
|
2021-01-31 16:37:58 +01:00
|
|
|
{
|
2021-09-25 16:01:41 +02:00
|
|
|
imports = [
|
|
|
|
./sso
|
|
|
|
];
|
|
|
|
|
2021-08-24 22:30:44 +02:00
|
|
|
options.my.services.nginx = with lib; {
|
2021-08-30 15:37:31 +02:00
|
|
|
enable = mkEnableOption "Nginx";
|
2021-08-24 22:30:44 +02:00
|
|
|
|
2021-09-25 13:24:23 +02:00
|
|
|
acme = {
|
|
|
|
credentialsFile = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "/var/lib/acme/creds.env";
|
|
|
|
description = ''
|
|
|
|
Gandi API key file as an 'EnvironmentFile' (see `systemd.exec(5)`)
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-08-24 22:30:44 +02:00
|
|
|
monitoring = {
|
|
|
|
enable = my.mkDisableOption "monitoring through grafana and prometheus";
|
|
|
|
};
|
|
|
|
|
|
|
|
virtualHosts = mkOption {
|
2023-12-25 19:25:08 +01:00
|
|
|
type = types.attrsOf virtualHostOption;
|
|
|
|
default = { };
|
2024-09-27 15:44:23 +02:00
|
|
|
example = {
|
|
|
|
gitea = {
|
|
|
|
subdomain = "git";
|
|
|
|
port = 8080;
|
|
|
|
};
|
|
|
|
dev = {
|
|
|
|
root = "/var/www/dev";
|
|
|
|
};
|
|
|
|
jellyfin = {
|
|
|
|
port = 8096;
|
|
|
|
extraConfig = {
|
|
|
|
locations."/socket" = {
|
|
|
|
proxyPass = "http://127.0.0.1:8096/";
|
|
|
|
proxyWebsockets = true;
|
2021-08-24 22:30:44 +02:00
|
|
|
};
|
2023-12-25 19:25:08 +01:00
|
|
|
};
|
2024-09-27 15:44:23 +02:00
|
|
|
};
|
|
|
|
};
|
2021-08-24 22:30:44 +02:00
|
|
|
description = ''
|
|
|
|
List of virtual hosts to set-up using default settings.
|
|
|
|
'';
|
|
|
|
};
|
2021-08-30 14:53:13 +02:00
|
|
|
|
|
|
|
sso = {
|
2021-09-25 13:41:43 +02:00
|
|
|
authKeyFile = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "/var/lib/nginx-sso/auth-key.txt";
|
|
|
|
description = ''
|
|
|
|
Path to the auth key.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-08-30 14:53:13 +02:00
|
|
|
subdomain = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "login";
|
|
|
|
example = "auth";
|
|
|
|
description = "Which subdomain, to use for SSO.";
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 8082;
|
|
|
|
example = 8080;
|
|
|
|
description = "Port to use for internal webui.";
|
|
|
|
};
|
2021-09-25 13:41:43 +02:00
|
|
|
|
|
|
|
users = mkOption {
|
|
|
|
type = types.attrsOf (types.submodule {
|
|
|
|
options = {
|
|
|
|
passwordHashFile = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "/var/lib/nginx-sso/alice/password-hash.txt";
|
|
|
|
description = "Path to file containing the user's password hash.";
|
|
|
|
};
|
|
|
|
totpSecretFile = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "/var/lib/nginx-sso/alice/totp-secret.txt";
|
|
|
|
description = "Path to file containing the user's TOTP secret.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2024-09-27 15:44:23 +02:00
|
|
|
example = {
|
|
|
|
alice = {
|
|
|
|
passwordHashFile = "/var/lib/nginx-sso/alice/password-hash.txt";
|
|
|
|
totpSecretFile = "/var/lib/nginx-sso/alice/totp-secret.txt";
|
|
|
|
};
|
|
|
|
};
|
2021-09-25 13:41:43 +02:00
|
|
|
description = "Definition of users";
|
|
|
|
};
|
|
|
|
|
|
|
|
groups = mkOption {
|
|
|
|
type = with types; attrsOf (listOf str);
|
2024-09-27 15:44:23 +02:00
|
|
|
example = {
|
|
|
|
root = [ "alice" ];
|
|
|
|
users = [ "alice" "bob" ];
|
|
|
|
};
|
2021-09-25 13:41:43 +02:00
|
|
|
description = "Groups of users";
|
|
|
|
};
|
2021-08-30 14:53:13 +02:00
|
|
|
};
|
2021-08-24 22:30:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
assertions = [ ]
|
2023-12-25 19:25:08 +01:00
|
|
|
++ (lib.flip lib.mapAttrsToList cfg.virtualHosts (_: { subdomain, ... } @ args:
|
2021-08-24 22:30:44 +02:00
|
|
|
let
|
2023-05-13 20:19:26 +02:00
|
|
|
conflicts = [ "port" "root" "socket" "redirect" ];
|
2021-08-24 22:30:44 +02:00
|
|
|
optionsNotNull = builtins.map (v: args.${v} != null) conflicts;
|
|
|
|
optionsSet = lib.filter lib.id optionsNotNull;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
assertion = builtins.length optionsSet == 1;
|
|
|
|
message = ''
|
|
|
|
Subdomain '${subdomain}' must have exactly one of ${
|
|
|
|
lib.concatStringsSep ", " (builtins.map (v: "'${v}'") conflicts)
|
|
|
|
} configured.
|
|
|
|
'';
|
|
|
|
}))
|
|
|
|
++ (
|
|
|
|
let
|
|
|
|
ports = lib.my.mapFilter
|
|
|
|
(v: v != null)
|
|
|
|
({ port, ... }: port)
|
2023-12-25 19:25:08 +01:00
|
|
|
(lib.attrValues cfg.virtualHosts);
|
2021-08-24 22:30:44 +02:00
|
|
|
portCounts = lib.my.countValues ports;
|
|
|
|
nonUniquesCounts = lib.filterAttrs (_: v: v != 1) portCounts;
|
|
|
|
nonUniques = builtins.attrNames nonUniquesCounts;
|
|
|
|
mkAssertion = port: {
|
|
|
|
assertion = false;
|
|
|
|
message = "Port ${port} cannot appear in multiple virtual hosts.";
|
|
|
|
};
|
|
|
|
in
|
|
|
|
map mkAssertion nonUniques
|
|
|
|
) ++ (
|
|
|
|
let
|
2023-12-25 19:25:08 +01:00
|
|
|
subs = lib.mapAttrsToList (_: { subdomain, ... }: subdomain) cfg.virtualHosts;
|
2021-08-24 22:30:44 +02:00
|
|
|
subsCounts = lib.my.countValues subs;
|
|
|
|
nonUniquesCounts = lib.filterAttrs (_: v: v != 1) subsCounts;
|
|
|
|
nonUniques = builtins.attrNames nonUniquesCounts;
|
|
|
|
mkAssertion = v: {
|
|
|
|
assertion = false;
|
|
|
|
message = ''
|
|
|
|
Subdomain '${v}' cannot appear in multiple virtual hosts.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
in
|
|
|
|
map mkAssertion nonUniques
|
|
|
|
)
|
|
|
|
;
|
|
|
|
|
2021-01-31 16:37:58 +01:00
|
|
|
services.nginx = {
|
|
|
|
enable = true;
|
|
|
|
statusPage = true; # For monitoring scraping.
|
|
|
|
|
2023-03-28 16:48:41 +02:00
|
|
|
recommendedBrotliSettings = true;
|
2021-01-31 16:37:58 +01:00
|
|
|
recommendedGzipSettings = true;
|
|
|
|
recommendedOptimisation = true;
|
|
|
|
recommendedProxySettings = true;
|
2023-03-28 16:50:05 +02:00
|
|
|
recommendedTlsSettings = true;
|
2023-03-28 16:48:41 +02:00
|
|
|
recommendedZstdSettings = true;
|
2021-08-24 22:30:44 +02:00
|
|
|
|
|
|
|
virtualHosts =
|
|
|
|
let
|
|
|
|
domain = config.networking.domain;
|
|
|
|
mkVHost = ({ subdomain, ... } @ args: lib.nameValuePair
|
|
|
|
"${subdomain}.${domain}"
|
2021-09-23 22:11:14 +02:00
|
|
|
(lib.my.recursiveMerge [
|
2021-08-24 22:30:44 +02:00
|
|
|
# Base configuration
|
|
|
|
{
|
|
|
|
forceSSL = true;
|
|
|
|
useACMEHost = domain;
|
|
|
|
}
|
|
|
|
# Proxy to port
|
|
|
|
(lib.optionalAttrs (args.port != null) {
|
|
|
|
locations."/".proxyPass =
|
|
|
|
"http://127.0.0.1:${toString args.port}";
|
|
|
|
})
|
|
|
|
# Serve filesystem content
|
|
|
|
(lib.optionalAttrs (args.root != null) {
|
|
|
|
inherit (args) root;
|
|
|
|
})
|
2023-05-13 20:19:26 +02:00
|
|
|
# Serve to UNIX socket
|
|
|
|
(lib.optionalAttrs (args.socket != null) {
|
|
|
|
locations."/".proxyPass =
|
|
|
|
"http://unix:${args.socket}";
|
|
|
|
})
|
2023-03-16 21:30:47 +01:00
|
|
|
# Redirect to a different domain
|
|
|
|
(lib.optionalAttrs (args.redirect != null) {
|
|
|
|
locations."/".return = "301 ${args.redirect}$request_uri";
|
|
|
|
})
|
2021-08-24 22:30:44 +02:00
|
|
|
# VHost specific configuration
|
|
|
|
args.extraConfig
|
2021-08-30 14:53:13 +02:00
|
|
|
# SSO configuration
|
|
|
|
(lib.optionalAttrs args.sso.enable {
|
|
|
|
extraConfig = (args.extraConfig.extraConfig or "") + ''
|
|
|
|
error_page 401 = @error401;
|
|
|
|
'';
|
|
|
|
|
|
|
|
locations."@error401".return = ''
|
|
|
|
302 https://${cfg.sso.subdomain}.${config.networking.domain}/login?go=$scheme://$http_host$request_uri
|
|
|
|
'';
|
|
|
|
|
|
|
|
locations."/" = {
|
|
|
|
extraConfig =
|
|
|
|
(args.extraConfig.locations."/".extraConfig or "") + ''
|
|
|
|
# Use SSO
|
|
|
|
auth_request /sso-auth;
|
|
|
|
|
|
|
|
# Set username through header
|
|
|
|
auth_request_set $username $upstream_http_x_username;
|
|
|
|
proxy_set_header X-User $username;
|
|
|
|
|
|
|
|
# Renew SSO cookie on request
|
|
|
|
auth_request_set $cookie $upstream_http_set_cookie;
|
|
|
|
add_header Set-Cookie $cookie;
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
locations."/sso-auth" = {
|
|
|
|
proxyPass = "http://localhost:${toString cfg.sso.port}/auth";
|
|
|
|
extraConfig = ''
|
|
|
|
# Do not allow requests from outside
|
|
|
|
internal;
|
|
|
|
|
|
|
|
# Do not forward the request body
|
|
|
|
proxy_pass_request_body off;
|
|
|
|
proxy_set_header Content-Length "";
|
|
|
|
|
|
|
|
# Set X-Application according to subdomain for matching
|
|
|
|
proxy_set_header X-Application "${subdomain}";
|
|
|
|
|
|
|
|
# Set origin URI for matching
|
|
|
|
proxy_set_header X-Origin-URI $request_uri;
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
})
|
2021-08-24 22:30:44 +02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
in
|
2023-12-25 19:25:08 +01:00
|
|
|
lib.my.genAttrs' (lib.attrValues cfg.virtualHosts) mkVHost;
|
2021-08-30 14:53:13 +02:00
|
|
|
|
|
|
|
sso = {
|
|
|
|
enable = true;
|
|
|
|
|
|
|
|
configuration = {
|
|
|
|
listen = {
|
|
|
|
addr = "127.0.0.1";
|
|
|
|
inherit (cfg.sso) port;
|
|
|
|
};
|
|
|
|
|
2021-08-30 17:34:26 +02:00
|
|
|
audit_log = {
|
|
|
|
target = [
|
|
|
|
"fd://stdout"
|
|
|
|
];
|
|
|
|
events = [
|
|
|
|
"access_denied"
|
|
|
|
"login_success"
|
|
|
|
"login_failure"
|
|
|
|
"logout"
|
|
|
|
"validate"
|
|
|
|
];
|
|
|
|
headers = [
|
|
|
|
"x-origin-uri"
|
|
|
|
"x-application"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2021-08-30 14:53:13 +02:00
|
|
|
cookie = {
|
|
|
|
domain = ".${config.networking.domain}";
|
|
|
|
secure = true;
|
2021-09-25 13:41:43 +02:00
|
|
|
authentication_key = {
|
|
|
|
_secret = cfg.sso.authKeyFile;
|
|
|
|
};
|
2021-08-30 14:53:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
login = {
|
|
|
|
title = "Ambroisie's SSO";
|
|
|
|
default_method = "simple";
|
|
|
|
hide_mfa_field = false;
|
|
|
|
names = {
|
|
|
|
simple = "Username / Password";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
providers = {
|
|
|
|
simple =
|
|
|
|
let
|
2021-09-25 13:41:43 +02:00
|
|
|
applyUsers = lib.flip lib.mapAttrs cfg.sso.users;
|
2021-08-30 14:53:13 +02:00
|
|
|
in
|
|
|
|
{
|
2021-09-25 13:41:43 +02:00
|
|
|
users = applyUsers (_: v: { _secret = v.passwordHashFile; });
|
2021-08-30 14:53:13 +02:00
|
|
|
|
|
|
|
mfa = applyUsers (_: v: [{
|
|
|
|
provider = "totp";
|
|
|
|
attributes = {
|
2021-09-25 13:41:43 +02:00
|
|
|
secret = {
|
|
|
|
_secret = v.totpSecretFile;
|
|
|
|
};
|
2021-08-30 14:53:13 +02:00
|
|
|
};
|
|
|
|
}]);
|
|
|
|
|
2021-09-25 13:41:43 +02:00
|
|
|
inherit (cfg.sso) groups;
|
2021-08-30 14:53:13 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
acl = {
|
|
|
|
rule_sets = [
|
|
|
|
{
|
|
|
|
rules = [{ field = "x-application"; present = true; }];
|
|
|
|
allow = [ "@root" ];
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2021-01-31 16:37:58 +01:00
|
|
|
};
|
|
|
|
|
2023-12-25 19:25:08 +01:00
|
|
|
my.services.nginx.virtualHosts = {
|
2023-12-25 19:43:44 +01:00
|
|
|
${cfg.sso.subdomain} = {
|
2021-08-30 14:53:13 +02:00
|
|
|
inherit (cfg.sso) port;
|
2023-12-25 19:25:08 +01:00
|
|
|
};
|
|
|
|
};
|
2021-08-30 14:53:13 +02:00
|
|
|
|
2021-01-31 16:37:58 +01:00
|
|
|
networking.firewall.allowedTCPPorts = [ 80 443 ];
|
|
|
|
|
|
|
|
# Nginx needs to be able to read the certificates
|
|
|
|
users.users.nginx.extraGroups = [ "acme" ];
|
|
|
|
|
|
|
|
security.acme = {
|
2022-01-14 13:30:20 +01:00
|
|
|
defaults.email = lib.my.mkMailAddress "bruno.acme" "belanyi.fr";
|
|
|
|
|
2021-01-31 16:37:58 +01:00
|
|
|
acceptTerms = true;
|
2021-08-24 22:30:44 +02:00
|
|
|
# Use DNS wildcard certificate
|
2021-01-31 16:37:58 +01:00
|
|
|
certs =
|
|
|
|
{
|
|
|
|
"${domain}" = {
|
|
|
|
extraDomainNames = [ "*.${domain}" ];
|
|
|
|
dnsProvider = "gandiv5";
|
2021-09-25 13:24:23 +02:00
|
|
|
inherit (cfg.acme) credentialsFile;
|
2021-01-31 16:37:58 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2021-08-24 22:30:44 +02:00
|
|
|
|
2022-11-29 17:19:24 +01:00
|
|
|
systemd.services."acme-${domain}" = {
|
|
|
|
serviceConfig = {
|
|
|
|
Environment = [
|
|
|
|
# Since I do a "weird" setup with a wildcard CNAME
|
|
|
|
"LEGO_DISABLE_CNAME_SUPPORT=true"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-10-26 14:15:34 +02:00
|
|
|
services.grafana.provision.dashboards.settings.providers = lib.mkIf cfg.monitoring.enable [
|
2021-07-13 19:10:17 +02:00
|
|
|
{
|
|
|
|
name = "NGINX";
|
2021-07-29 13:17:42 +02:00
|
|
|
options.path = pkgs.nur.repos.alarsyo.grafanaDashboards.nginx;
|
2021-07-13 19:10:17 +02:00
|
|
|
disableDeletion = true;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-08-24 22:30:44 +02:00
|
|
|
services.prometheus = lib.mkIf cfg.monitoring.enable {
|
2021-07-13 19:10:17 +02:00
|
|
|
exporters.nginx = {
|
|
|
|
enable = true;
|
|
|
|
listenAddress = "127.0.0.1";
|
|
|
|
};
|
|
|
|
|
|
|
|
scrapeConfigs = [
|
|
|
|
{
|
|
|
|
job_name = "nginx";
|
|
|
|
static_configs = [
|
|
|
|
{
|
|
|
|
targets = [ "127.0.0.1:${toString config.services.prometheus.exporters.nginx.port}" ];
|
|
|
|
labels = {
|
|
|
|
instance = config.networking.hostName;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
2021-01-31 16:37:58 +01:00
|
|
|
};
|
|
|
|
}
|