2024-02-05 11:46:52 +01:00
|
|
|
{
|
|
|
|
pkgs,
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}:
|
2023-08-10 16:43:46 +02:00
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.eboskma.caddy-proxy;
|
|
|
|
|
2024-04-10 22:19:33 +02:00
|
|
|
proxyHost = types.submodule {
|
|
|
|
options = {
|
|
|
|
externalHostname = mkOption {
|
|
|
|
description = "Hostname where this service should be reached";
|
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
proxyAddress = mkOption {
|
|
|
|
description = "Internal address where this service is reachable";
|
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
external = mkEnableOption "Make this host externally reachable.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-08-10 16:43:46 +02:00
|
|
|
mkProxyHost = target: {
|
|
|
|
extraConfig = ''
|
|
|
|
reverse_proxy ${target}
|
|
|
|
|
2024-04-10 22:19:33 +02:00
|
|
|
import cloudflare-tls
|
2023-08-10 16:43:46 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
mkLocalProxyHost = target: {
|
|
|
|
extraConfig = ''
|
|
|
|
@local_or_ts {
|
|
|
|
remote_ip 10.0.0.0/24 100.64.0.0/10
|
|
|
|
}
|
|
|
|
handle @local_or_ts {
|
|
|
|
reverse_proxy ${target}
|
|
|
|
}
|
|
|
|
handle {
|
2024-03-14 10:06:53 +01:00
|
|
|
error "Nope." 403
|
2023-08-10 16:43:46 +02:00
|
|
|
}
|
|
|
|
|
2024-04-10 22:19:33 +02:00
|
|
|
import cloudflare-tls
|
2023-08-10 16:43:46 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.eboskma.caddy-proxy = {
|
|
|
|
enable = mkEnableOption "Caddy proxy";
|
|
|
|
package = mkPackageOption pkgs "caddy" { };
|
2024-04-10 22:19:33 +02:00
|
|
|
proxyHosts = mkOption {
|
|
|
|
description = "Proxy hosts";
|
|
|
|
type = types.listOf proxyHost;
|
|
|
|
};
|
2023-08-10 16:43:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
services.caddy = {
|
|
|
|
enable = true;
|
|
|
|
package = cfg.package;
|
|
|
|
|
|
|
|
email = "erwin@datarift.nl";
|
|
|
|
|
2024-02-28 22:49:27 +01:00
|
|
|
acmeCA = "https://acme-v02.api.letsencrypt.org/directory";
|
2023-08-10 16:43:46 +02:00
|
|
|
|
2024-04-10 22:19:33 +02:00
|
|
|
extraConfig = ''
|
|
|
|
(cloudflare-tls) {
|
|
|
|
tls {
|
|
|
|
dns cloudflare {env.CF_API_TOKEN}
|
|
|
|
propagation_timeout -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
|
|
|
virtualHosts = builtins.listToAttrs (
|
|
|
|
map (
|
|
|
|
host:
|
|
|
|
let
|
|
|
|
mkProxy = if host ? external && host.external then mkProxyHost else mkLocalProxyHost;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
name = host.externalHostname;
|
|
|
|
value = mkProxy host.proxyAddress;
|
|
|
|
}
|
|
|
|
) cfg.proxyHosts
|
|
|
|
);
|
2023-08-10 16:43:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.caddy.serviceConfig.EnvironmentFile = [ config.sops.secrets.caddy-env.path ];
|
|
|
|
|
2024-02-05 11:46:52 +01:00
|
|
|
networking.firewall.allowedTCPPorts = [
|
|
|
|
80
|
|
|
|
443
|
|
|
|
];
|
2023-08-10 16:43:46 +02:00
|
|
|
};
|
|
|
|
}
|