dotfiles/modules/nixos/services/caddy.nix

48 lines
1.3 KiB
Nix
Raw Normal View History

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 {
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
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 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
};
}