2022-10-13 23:40:30 +00:00
|
|
|
{ config, pkgs, lib, ... }: {
|
2022-10-02 15:24:25 +00:00
|
|
|
|
|
|
|
options = {
|
2022-12-21 21:18:03 +00:00
|
|
|
caddy.enable = lib.mkEnableOption "Caddy reverse proxy.";
|
|
|
|
caddy.routes = lib.mkOption {
|
2022-10-02 17:40:10 +00:00
|
|
|
type = lib.types.listOf lib.types.attrs;
|
|
|
|
description = "Caddy JSON routes for http servers";
|
2022-12-21 21:18:03 +00:00
|
|
|
default = [ ];
|
2022-10-02 15:24:25 +00:00
|
|
|
};
|
2022-12-21 21:18:03 +00:00
|
|
|
caddy.blocks = lib.mkOption {
|
2022-10-15 19:00:37 +00:00
|
|
|
type = lib.types.listOf lib.types.attrs;
|
|
|
|
description = "Caddy JSON error blocks for http servers";
|
|
|
|
default = [ ];
|
|
|
|
};
|
2022-10-02 15:24:25 +00:00
|
|
|
};
|
|
|
|
|
2022-12-21 21:18:03 +00:00
|
|
|
config = lib.mkIf (config.caddy.enable && config.caddy.routes != [ ]) {
|
2022-10-02 15:24:25 +00:00
|
|
|
|
|
|
|
services.caddy = {
|
|
|
|
enable = true;
|
|
|
|
adapter = "''"; # Required to enable JSON
|
2022-10-02 17:40:10 +00:00
|
|
|
configFile = pkgs.writeText "Caddyfile" (builtins.toJSON {
|
|
|
|
apps.http.servers.main = {
|
|
|
|
listen = [ ":443" ];
|
2022-12-21 21:18:03 +00:00
|
|
|
routes = config.caddy.routes;
|
|
|
|
errors.routes = config.caddy.blocks;
|
2023-03-12 13:24:25 +00:00
|
|
|
# logs = { }; # Uncomment to collect access logs
|
|
|
|
};
|
|
|
|
logging.logs.main = {
|
|
|
|
encoder = { format = "console"; };
|
|
|
|
writer = {
|
|
|
|
output = "file";
|
|
|
|
filename = "${config.services.caddy.logDir}/caddy.log";
|
|
|
|
roll = true;
|
|
|
|
};
|
|
|
|
level = "INFO";
|
2022-10-02 17:40:10 +00:00
|
|
|
};
|
|
|
|
});
|
2022-10-02 15:24:25 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2022-10-03 12:19:29 +00:00
|
|
|
networking.firewall.allowedTCPPorts = [ 80 443 ];
|
|
|
|
networking.firewall.allowedUDPPorts = [ 443 ];
|
|
|
|
|
2022-10-02 15:24:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|