Files
cnix/modules/server/caddy/default.nix
2025-08-29 15:25:40 +02:00

61 lines
1.3 KiB
Nix

{
config,
lib,
...
}:
let
inherit (lib) mkIf mkEnableOption;
cfg = config.server.caddy;
in
{
options = {
server.caddy.enable = mkEnableOption "Enables caddy";
};
config = mkIf cfg.enable {
networking.firewall =
let
ports = [
80
443
];
in
{
allowedTCPPorts = ports;
};
security.acme = {
acceptTerms = true;
defaults.email = config.server.email;
certs.${config.server.domain} = {
reloadServices = [ "caddy.service" ];
domain = "${config.server.domain}";
extraDomainNames = [ "*.${config.server.domain}" ];
dnsProvider = "cloudflare";
dnsResolver = "1.1.1.1:53";
dnsPropagationCheck = true;
group = config.services.caddy.group;
environmentFile = config.age.secrets.cloudflareDnsCredentials.path;
};
};
services.caddy = {
enable = true;
globalConfig = ''
auto_https off
'';
virtualHosts = {
"http://${config.server.domain}" = {
extraConfig = ''
redir https://{host}{uri}
'';
};
"http://*.${config.server.domain}" = {
extraConfig = ''
redir https://{host}{uri}
'';
};
};
};
};
}