infrastructure/nixos/common/modules/nginx.nix

31 lines
813 B
Nix
Raw Normal View History

2023-05-05 10:04:38 +02:00
{ lib, hosts, config, ... }:
with lib;
let cfg = config.services.v.nginx;
in {
2023-05-11 13:27:59 +02:00
options.services.v.nginx.autoExpose =
2023-05-05 10:04:38 +02:00
mkEnableOption "generate vhosts";
config = let
proxy = url: {
enableACME = true;
forceSSL = true;
locations."/" = {
proxyPass = url;
proxyWebsockets = true;
};
};
hosts' =
filter (hasAttr "exposes") (attrValues hosts.${config.networking.domain});
exposes = { ip, exposes, ... }:
map ({ domain, port ? 80}: { inherit ip domain port; }) (attrValues exposes);
mkVhost = { ip, domain, port}: {
"${domain}" = proxy "http://${ip}:${toString port}";
};
vhosts = foldr (el: acc: acc // mkVhost el) { } (concatMap exposes hosts');
2023-05-11 13:27:59 +02:00
in mkIf cfg.autoExpose {
2023-05-05 10:04:38 +02:00
services.nginx.virtualHosts = vhosts;
};
}