2024-01-10 04:11:11 +00:00
|
|
|
# Caddy is a reverse proxy, like Nginx or Traefik. This creates an ingress
|
|
|
|
# point from my local network or the public (via Cloudflare). Instead of a
|
|
|
|
# Caddyfile, I'm using the more expressive JSON config file format. This means
|
|
|
|
# I can source routes from other areas in my config and build the JSON file
|
|
|
|
# using the result of the expression.
|
|
|
|
|
|
|
|
# Caddy helpfully provides automatic ACME cert generation and management, but
|
|
|
|
# it requires a form of validation. We are using a custom build of Caddy
|
|
|
|
# (compiled with an overlay) to insert a plugin for managing DNS validation
|
|
|
|
# with Cloudflare's DNS API.
|
|
|
|
|
2024-04-20 13:42:06 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
{
|
2022-10-02 15:24:25 +00:00
|
|
|
|
|
|
|
options = {
|
2023-07-18 03:52:37 +00:00
|
|
|
caddy = {
|
|
|
|
tlsPolicies = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.attrs;
|
|
|
|
description = "Caddy JSON TLS policies";
|
|
|
|
default = [ ];
|
|
|
|
};
|
|
|
|
routes = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.attrs;
|
|
|
|
description = "Caddy JSON routes for http servers";
|
|
|
|
default = [ ];
|
|
|
|
};
|
|
|
|
blocks = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.attrs;
|
|
|
|
description = "Caddy JSON error blocks for http servers";
|
|
|
|
default = [ ];
|
|
|
|
};
|
|
|
|
cidrAllowlist = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
|
|
|
description = "CIDR blocks to allow for requests";
|
2023-07-20 00:18:28 +00:00
|
|
|
default = [ ];
|
2023-07-18 03:52:37 +00:00
|
|
|
};
|
2022-10-15 19:00:37 +00:00
|
|
|
};
|
2022-10-02 15:24:25 +00:00
|
|
|
};
|
|
|
|
|
2023-07-18 03:52:37 +00:00
|
|
|
config = lib.mkIf config.services.caddy.enable {
|
|
|
|
|
|
|
|
# Force Caddy to 403 if not coming from allowlisted source
|
2023-07-20 00:18:28 +00:00
|
|
|
caddy.cidrAllowlist = [ "127.0.0.1/32" ];
|
2024-04-20 13:42:06 +00:00
|
|
|
caddy.routes = [
|
|
|
|
{
|
|
|
|
match = [ { not = [ { remote_ip.ranges = config.caddy.cidrAllowlist; } ]; } ];
|
|
|
|
handle = [
|
|
|
|
{
|
|
|
|
handler = "static_response";
|
|
|
|
status_code = "403";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
];
|
2023-07-18 03:52:37 +00:00
|
|
|
|
|
|
|
services.caddy = {
|
|
|
|
adapter = "''"; # Required to enable JSON
|
2024-04-20 13:42:06 +00:00
|
|
|
configFile = pkgs.writeText "Caddyfile" (
|
|
|
|
builtins.toJSON {
|
|
|
|
apps.http.servers.main = {
|
|
|
|
listen = [ ":443" ];
|
2024-01-10 04:11:11 +00:00
|
|
|
|
2024-04-20 13:42:06 +00:00
|
|
|
# These routes are pulled from the rest of this repo
|
|
|
|
routes = config.caddy.routes;
|
|
|
|
errors.routes = config.caddy.blocks;
|
2024-01-10 04:11:11 +00:00
|
|
|
|
2024-04-20 13:42:06 +00:00
|
|
|
logs = { }; # Uncommenting collects access logs
|
2023-07-04 22:20:43 +00:00
|
|
|
};
|
2024-04-20 13:42:06 +00:00
|
|
|
apps.http.servers.metrics = { }; # Enables Prometheus metrics
|
|
|
|
apps.tls.automation.policies = config.caddy.tlsPolicies;
|
|
|
|
|
|
|
|
# Setup logging to file
|
|
|
|
logging.logs.main = {
|
|
|
|
encoder = {
|
|
|
|
format = "console";
|
|
|
|
};
|
|
|
|
writer = {
|
|
|
|
output = "file";
|
|
|
|
filename = "${config.services.caddy.logDir}/caddy.log";
|
|
|
|
roll = true;
|
|
|
|
roll_size_mb = 1;
|
|
|
|
};
|
|
|
|
level = "INFO";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
2023-07-18 03:52:37 +00:00
|
|
|
};
|
2022-10-02 15:24:25 +00:00
|
|
|
|
2024-07-02 13:42:50 +00:00
|
|
|
systemd.services.caddy.serviceConfig = {
|
|
|
|
|
|
|
|
# Allows Caddy to serve lower ports (443, 80)
|
|
|
|
AmbientCapabilities = "CAP_NET_BIND_SERVICE";
|
|
|
|
|
|
|
|
# Prevent flooding of logs by rate-limiting
|
|
|
|
LogRateLimitIntervalSec = "5s"; # Limit period
|
|
|
|
LogRateLimitBurst = 100; # Limit threshold
|
|
|
|
|
|
|
|
};
|
2024-01-10 04:11:11 +00:00
|
|
|
|
|
|
|
# Required for web traffic to reach this machine
|
2024-04-20 13:42:06 +00:00
|
|
|
networking.firewall.allowedTCPPorts = [
|
|
|
|
80
|
|
|
|
443
|
|
|
|
];
|
2024-01-10 04:11:11 +00:00
|
|
|
|
|
|
|
# HTTP/3 QUIC uses UDP (not sure if being used)
|
2023-07-18 03:52:37 +00:00
|
|
|
networking.firewall.allowedUDPPorts = [ 443 ];
|
2022-10-03 12:19:29 +00:00
|
|
|
|
2024-01-10 04:11:11 +00:00
|
|
|
# Caddy exposes Prometheus metrics with the admin API
|
|
|
|
# https://caddyserver.com/docs/api
|
2023-07-18 03:52:37 +00:00
|
|
|
prometheus.scrapeTargets = [ "127.0.0.1:2019" ];
|
|
|
|
};
|
2022-10-02 15:24:25 +00:00
|
|
|
}
|