1
Fork 0
satellite/modules/nixos/nginx.nix

114 lines
3.1 KiB
Nix
Raw Normal View History

2024-06-13 15:47:36 +02:00
{ config, lib, ... }:
2024-10-11 14:11:52 +02:00
let
cfg = config.satellite.nginx;
2024-06-13 15:47:36 +02:00
in
{
options.satellite.nginx = {
domain = lib.mkOption {
description = "Root domain to use as a default for configurations.";
type = lib.types.str;
2024-07-08 03:06:27 +02:00
default = config.satellite.dns.domain;
2024-06-13 15:47:36 +02:00
};
2024-01-31 21:59:11 +01:00
2024-06-13 15:47:36 +02:00
at = lib.mkOption {
description = "Per-subdomain nginx configuration";
2024-10-11 14:11:52 +02:00
type = lib.types.attrsOf (
lib.types.submodule (
{ name, config, ... }:
{
options.subdomain = lib.mkOption {
description = ''
Subdomain to use for host generation.
Only required if `host` is not set manually.
'';
type = lib.types.str;
default = name;
};
2024-06-13 15:47:36 +02:00
2024-10-11 14:11:52 +02:00
options.host = lib.mkOption {
description = "Host to route requests from";
type = lib.types.str;
};
2024-10-11 14:11:52 +02:00
config.host = "${config.subdomain}.${cfg.domain}";
2024-07-08 03:06:27 +02:00
2024-10-11 14:11:52 +02:00
options.url = lib.mkOption {
description = "External https url used to access this host";
type = lib.types.str;
};
2024-06-13 15:47:36 +02:00
2024-10-11 14:11:52 +02:00
config.url = "https://${config.host}";
2024-06-13 15:47:36 +02:00
2024-10-11 14:11:52 +02:00
options.port = lib.mkOption {
description = "Port to proxy requests to";
type = lib.types.nullOr lib.types.port;
default = null;
};
2024-06-13 15:47:36 +02:00
2024-10-11 14:11:52 +02:00
options.files = lib.mkOption {
description = "Path to serve files from";
type = lib.types.nullOr lib.types.path;
default = null;
};
}
)
);
2024-06-13 15:47:36 +02:00
default = { };
};
2024-01-31 21:59:11 +01:00
};
2024-06-13 15:47:36 +02:00
config = {
assertions =
2024-10-11 14:11:52 +02:00
let
assertSingleTarget = config: {
2024-06-13 15:47:36 +02:00
assertion = (config.port == null) == (config.files != null);
message = ''
2024-07-08 03:06:27 +02:00
Precisely one of the options 'satellite.nginx.at.${config.subdomain}.port'
and 'satellite.nginx.at.${config.subdomain}.files' must be specified.
2024-06-13 15:47:36 +02:00
'';
};
2024-10-11 14:11:52 +02:00
in
lib.mapAttrsToList (_: assertSingleTarget) cfg.at;
2024-06-13 15:47:36 +02:00
services.nginx.virtualHosts =
2024-10-11 14:11:52 +02:00
let
mkNginxConfig = args: {
name = args.host;
value =
let
extra =
if args.port != null then
{
locations."/" = {
proxyPass = "http://localhost:${toString args.port}";
proxyWebsockets = true;
};
}
else
{ root = args.files; };
in
{
enableACME = true;
acmeRoot = null;
forceSSL = true;
2024-06-13 15:47:36 +02:00
}
2024-10-11 14:11:52 +02:00
// extra;
};
in
lib.attrsets.mapAttrs' (_: mkNginxConfig) cfg.at;
2024-07-08 03:06:27 +02:00
satellite.dns.records =
2024-10-11 14:11:52 +02:00
let
mkDnsRecord =
{ subdomain, ... }:
{
type = "CNAME";
zone = cfg.domain;
at = subdomain;
to = config.networking.hostName;
};
in
lib.attrsets.mapAttrsToList (_: mkDnsRecord) cfg.at;
};
2024-01-31 21:59:11 +01:00
}