2016-04-05 01:30:21 +00:00
|
|
|
|
{
|
|
|
|
|
config,
|
|
|
|
|
lib,
|
|
|
|
|
pkgs,
|
|
|
|
|
...
|
|
|
|
|
}:
|
|
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
cfg = config.services.caddy;
|
2020-09-07 09:42:00 +02:00
|
|
|
|
|
2024-08-21 17:46:39 -04:00
|
|
|
|
certs = config.security.acme.certs;
|
2021-11-29 22:39:27 -05:00
|
|
|
|
virtualHosts = attrValues cfg.virtualHosts;
|
2024-08-21 17:46:39 -04:00
|
|
|
|
acmeEnabledVhosts = filter (hostOpts: hostOpts.useACMEHost != null) virtualHosts;
|
|
|
|
|
vhostCertNames = unique (map (hostOpts: hostOpts.useACMEHost) acmeEnabledVhosts);
|
|
|
|
|
dependentCertNames = filter (cert: certs.${cert}.dnsProvider == null) vhostCertNames; # those that might depend on the HTTP server
|
|
|
|
|
independentCertNames = filter (cert: certs.${cert}.dnsProvider != null) vhostCertNames; # those that don't depend on the HTTP server
|
2021-11-29 22:39:27 -05:00
|
|
|
|
|
|
|
|
|
mkVHostConf =
|
|
|
|
|
hostOpts:
|
|
|
|
|
let
|
|
|
|
|
sslCertDir = config.security.acme.certs.${hostOpts.useACMEHost}.directory;
|
|
|
|
|
in
|
2021-08-12 22:02:12 +02:00
|
|
|
|
''
|
2021-11-29 22:39:27 -05:00
|
|
|
|
${hostOpts.hostName} ${concatStringsSep " " hostOpts.serverAliases} {
|
2023-06-05 17:59:10 +02:00
|
|
|
|
${optionalString (
|
|
|
|
|
hostOpts.listenAddresses != [ ]
|
|
|
|
|
) "bind ${concatStringsSep " " hostOpts.listenAddresses}"}
|
2021-11-29 22:39:27 -05:00
|
|
|
|
${optionalString (
|
|
|
|
|
hostOpts.useACMEHost != null
|
|
|
|
|
) "tls ${sslCertDir}/cert.pem ${sslCertDir}/key.pem"}
|
|
|
|
|
log {
|
|
|
|
|
${hostOpts.logFormat}
|
|
|
|
|
}
|
2024-12-10 20:26:33 +01:00
|
|
|
|
|
2021-11-29 22:39:27 -05:00
|
|
|
|
${hostOpts.extraConfig}
|
2024-12-10 20:26:33 +01:00
|
|
|
|
}
|
2021-11-29 22:39:27 -05:00
|
|
|
|
'';
|
|
|
|
|
|
2023-08-22 20:14:35 +02:00
|
|
|
|
settingsFormat = pkgs.formats.json { };
|
2021-11-29 22:39:27 -05:00
|
|
|
|
|
2023-08-22 20:14:35 +02:00
|
|
|
|
configFile =
|
|
|
|
|
if cfg.settings != { } then
|
|
|
|
|
settingsFormat.generate "caddy.json" cfg.settings
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
Caddyfile = pkgs.writeTextDir "Caddyfile" ''
|
|
|
|
|
{
|
|
|
|
|
${cfg.globalConfig}
|
|
|
|
|
}
|
|
|
|
|
${cfg.extraConfig}
|
2023-09-18 11:12:19 +02:00
|
|
|
|
${concatMapStringsSep "\n" mkVHostConf virtualHosts}
|
2023-08-22 20:14:35 +02:00
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
Caddyfile-formatted =
|
|
|
|
|
pkgs.runCommand "Caddyfile-formatted" { nativeBuildInputs = [ cfg.package ]; }
|
|
|
|
|
''
|
|
|
|
|
mkdir -p $out
|
|
|
|
|
cp --no-preserve=mode ${Caddyfile}/Caddyfile $out/Caddyfile
|
|
|
|
|
caddy fmt --overwrite $out/Caddyfile
|
|
|
|
|
'';
|
|
|
|
|
in
|
2022-09-26 02:23:45 +01:00
|
|
|
|
"${
|
|
|
|
|
if pkgs.stdenv.buildPlatform == pkgs.stdenv.hostPlatform then Caddyfile-formatted else Caddyfile
|
|
|
|
|
}/Caddyfile";
|
2022-01-08 15:05:34 -05:00
|
|
|
|
|
2023-02-21 17:21:38 +01:00
|
|
|
|
etcConfigFile = "caddy/caddy_config";
|
|
|
|
|
|
|
|
|
|
configPath = "/etc/${etcConfigFile}";
|
|
|
|
|
|
2024-08-24 19:18:07 -04:00
|
|
|
|
mkCertOwnershipAssertion = import ../../../security/acme/mk-cert-ownership-assertion.nix lib;
|
2021-08-12 22:02:12 +02:00
|
|
|
|
in
|
|
|
|
|
{
|
2020-10-19 14:25:36 +02:00
|
|
|
|
imports = [
|
|
|
|
|
(mkRemovedOptionModule [
|
|
|
|
|
"services"
|
|
|
|
|
"caddy"
|
|
|
|
|
"agree"
|
|
|
|
|
] "this option is no longer necessary for Caddy 2")
|
2021-11-29 22:39:27 -05:00
|
|
|
|
(mkRenamedOptionModule [ "services" "caddy" "ca" ] [ "services" "caddy" "acmeCA" ])
|
|
|
|
|
(mkRenamedOptionModule [ "services" "caddy" "config" ] [ "services" "caddy" "extraConfig" ])
|
2020-10-19 14:25:36 +02:00
|
|
|
|
];
|
|
|
|
|
|
2021-11-29 22:39:27 -05:00
|
|
|
|
# interface
|
2016-04-05 01:30:21 +00:00
|
|
|
|
options.services.caddy = {
|
|
|
|
|
enable = mkEnableOption "Caddy web server";
|
|
|
|
|
|
2021-11-29 22:39:27 -05:00
|
|
|
|
user = mkOption {
|
|
|
|
|
default = "caddy";
|
|
|
|
|
type = types.str;
|
|
|
|
|
description = ''
|
|
|
|
|
User account under which caddy runs.
|
|
|
|
|
|
|
|
|
|
::: {.note}
|
|
|
|
|
If left as the default value this user will automatically be created
|
|
|
|
|
on system activation, otherwise you are responsible for
|
|
|
|
|
ensuring the user exists before the Caddy service starts.
|
2022-08-30 02:30:04 +02:00
|
|
|
|
:::
|
2017-08-22 17:21:49 -04:00
|
|
|
|
'';
|
2021-11-29 22:39:27 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
|
default = "caddy";
|
|
|
|
|
type = types.str;
|
2020-05-08 09:35:55 +00:00
|
|
|
|
description = ''
|
2024-07-24 08:40:07 +00:00
|
|
|
|
Group under which caddy runs.
|
2021-11-29 22:39:27 -05:00
|
|
|
|
|
|
|
|
|
::: {.note}
|
2024-07-24 08:40:07 +00:00
|
|
|
|
If left as the default value this group will automatically be created
|
2021-11-29 22:39:27 -05:00
|
|
|
|
on system activation, otherwise you are responsible for
|
2024-07-24 08:40:07 +00:00
|
|
|
|
ensuring the group exists before the Caddy service starts.
|
2022-08-30 02:30:04 +02:00
|
|
|
|
:::
|
2020-05-08 09:35:55 +00:00
|
|
|
|
'';
|
2016-04-05 01:30:21 +00:00
|
|
|
|
};
|
|
|
|
|
|
2023-11-27 01:19:27 +01:00
|
|
|
|
package = mkPackageOption pkgs "caddy" { };
|
2021-08-19 23:50:55 +09:00
|
|
|
|
|
2021-11-29 22:39:27 -05:00
|
|
|
|
dataDir = mkOption {
|
|
|
|
|
type = types.path;
|
|
|
|
|
default = "/var/lib/caddy";
|
|
|
|
|
description = ''
|
|
|
|
|
The data directory for caddy.
|
2021-08-19 23:50:55 +09:00
|
|
|
|
|
2021-11-29 22:39:27 -05:00
|
|
|
|
::: {.note}
|
|
|
|
|
If left as the default value this directory will automatically be created
|
|
|
|
|
before the Caddy server starts, otherwise you are responsible for ensuring
|
|
|
|
|
the directory exists with appropriate ownership and permissions.
|
2022-08-30 02:30:04 +02:00
|
|
|
|
|
2021-11-29 22:39:27 -05:00
|
|
|
|
Caddy v2 replaced `CADDYPATH` with XDG directories.
|
|
|
|
|
See <https://caddyserver.com/docs/conventions#file-locations>.
|
2022-08-30 02:30:04 +02:00
|
|
|
|
:::
|
2021-11-29 22:39:27 -05:00
|
|
|
|
'';
|
2021-05-11 23:45:22 +02:00
|
|
|
|
};
|
|
|
|
|
|
2021-11-29 22:39:27 -05:00
|
|
|
|
logDir = mkOption {
|
|
|
|
|
type = types.path;
|
|
|
|
|
default = "/var/log/caddy";
|
|
|
|
|
description = ''
|
|
|
|
|
Directory for storing Caddy access logs.
|
|
|
|
|
|
|
|
|
|
::: {.note}
|
|
|
|
|
If left as the default value this directory will automatically be created
|
|
|
|
|
before the Caddy server starts, otherwise the sysadmin is responsible for
|
|
|
|
|
ensuring the directory exists with appropriate ownership and permissions.
|
2022-08-30 02:30:04 +02:00
|
|
|
|
:::
|
2021-11-29 22:39:27 -05:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
logFormat = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = ''
|
|
|
|
|
level ERROR
|
|
|
|
|
'';
|
|
|
|
|
example = literalExpression ''
|
|
|
|
|
mkForce "level INFO";
|
|
|
|
|
'';
|
|
|
|
|
description = ''
|
|
|
|
|
Configuration for the default logger. See
|
|
|
|
|
<https://caddyserver.com/docs/caddyfile/options#log>
|
|
|
|
|
for details.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
configFile = mkOption {
|
|
|
|
|
type = types.path;
|
|
|
|
|
default = configFile;
|
|
|
|
|
defaultText = "A Caddyfile automatically generated by values from services.caddy.*";
|
|
|
|
|
example = literalExpression ''
|
2023-12-06 21:34:38 +01:00
|
|
|
|
pkgs.writeText "Caddyfile" '''
|
2021-11-29 22:39:27 -05:00
|
|
|
|
example.com
|
|
|
|
|
|
|
|
|
|
root * /var/www/wordpress
|
|
|
|
|
php_fastcgi unix//run/php/php-version-fpm.sock
|
|
|
|
|
file_server
|
|
|
|
|
''';
|
|
|
|
|
'';
|
|
|
|
|
description = ''
|
|
|
|
|
Override the configuration file used by Caddy. By default,
|
|
|
|
|
NixOS generates one automatically.
|
2023-02-21 17:21:38 +01:00
|
|
|
|
|
|
|
|
|
The configuration file is exposed at {file}`${configPath}`.
|
2021-11-29 22:39:27 -05:00
|
|
|
|
'';
|
2021-05-11 23:45:22 +02:00
|
|
|
|
};
|
|
|
|
|
|
2020-05-04 02:10:26 +03:00
|
|
|
|
adapter = mkOption {
|
2023-12-06 21:34:38 +01:00
|
|
|
|
default =
|
|
|
|
|
if ((cfg.configFile != configFile) || (builtins.baseNameOf cfg.configFile) == "Caddyfile") then
|
|
|
|
|
"caddyfile"
|
|
|
|
|
else
|
|
|
|
|
null;
|
2023-02-21 17:21:38 +01:00
|
|
|
|
defaultText = literalExpression ''
|
2023-12-06 21:34:38 +01:00
|
|
|
|
if ((cfg.configFile != configFile) || (builtins.baseNameOf cfg.configFile) == "Caddyfile") then "caddyfile" else null
|
2023-02-21 17:21:38 +01:00
|
|
|
|
'';
|
2022-09-26 03:00:16 +01:00
|
|
|
|
example = literalExpression "nginx";
|
2022-09-26 02:23:45 +01:00
|
|
|
|
type = with types; nullOr str;
|
2022-09-26 03:00:16 +01:00
|
|
|
|
description = ''
|
2020-10-02 17:46:46 +10:00
|
|
|
|
Name of the config adapter to use.
|
2021-11-29 22:39:27 -05:00
|
|
|
|
See <https://caddyserver.com/docs/config-adapters>
|
|
|
|
|
for the full list.
|
|
|
|
|
|
2022-09-26 03:00:16 +01:00
|
|
|
|
If `null` is specified, the `--adapter` argument is omitted when
|
|
|
|
|
starting or restarting Caddy. Notably, this allows specification of a
|
|
|
|
|
configuration file in Caddy's native JSON format, as long as the
|
|
|
|
|
filename does not start with `Caddyfile` (in which case the `caddyfile`
|
|
|
|
|
adapter is implicitly enabled). See
|
|
|
|
|
<https://caddyserver.com/docs/command-line#caddy-run> for details.
|
2022-01-01 18:13:48 +00:00
|
|
|
|
|
2022-09-26 03:00:16 +01:00
|
|
|
|
::: {.note}
|
|
|
|
|
Any value other than `null` or `caddyfile` is only valid when providing
|
|
|
|
|
your own `configFile`.
|
|
|
|
|
:::
|
2020-05-04 02:10:26 +03:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-13 23:14:56 +01:00
|
|
|
|
resume = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
|
|
|
|
description = ''
|
2021-11-29 22:39:27 -05:00
|
|
|
|
Use saved config, if any (and prefer over any specified configuration passed with `--config`).
|
2020-12-13 23:14:56 +01:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-06 09:21:33 -05:00
|
|
|
|
globalConfig = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = "";
|
|
|
|
|
example = ''
|
|
|
|
|
debug
|
|
|
|
|
servers {
|
|
|
|
|
protocol {
|
|
|
|
|
experimental_http3
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
'';
|
|
|
|
|
description = ''
|
|
|
|
|
Additional lines of configuration appended to the global config section
|
|
|
|
|
of the `Caddyfile`.
|
|
|
|
|
|
|
|
|
|
Refer to <https://caddyserver.com/docs/caddyfile/options#global-options>
|
|
|
|
|
for details on supported values.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-29 22:39:27 -05:00
|
|
|
|
extraConfig = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = "";
|
|
|
|
|
example = ''
|
|
|
|
|
example.com {
|
|
|
|
|
encode gzip
|
|
|
|
|
log
|
|
|
|
|
root /srv/http
|
|
|
|
|
}
|
|
|
|
|
'';
|
2021-08-12 22:02:12 +02:00
|
|
|
|
description = ''
|
2021-11-29 22:39:27 -05:00
|
|
|
|
Additional lines of configuration appended to the automatically
|
|
|
|
|
generated `Caddyfile`.
|
2021-08-12 22:02:12 +02:00
|
|
|
|
'';
|
2016-07-21 01:51:09 +02:00
|
|
|
|
};
|
|
|
|
|
|
2021-11-29 22:39:27 -05:00
|
|
|
|
virtualHosts = mkOption {
|
|
|
|
|
type = with types; attrsOf (submodule (import ./vhost-options.nix { inherit cfg; }));
|
|
|
|
|
default = { };
|
|
|
|
|
example = literalExpression ''
|
|
|
|
|
{
|
|
|
|
|
"hydra.example.com" = {
|
|
|
|
|
serverAliases = [ "www.hydra.example.com" ];
|
|
|
|
|
extraConfig = '''
|
|
|
|
|
encode gzip
|
2024-07-24 10:38:48 +02:00
|
|
|
|
root * /srv/http
|
2021-11-29 22:39:27 -05:00
|
|
|
|
''';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
'';
|
|
|
|
|
description = ''
|
|
|
|
|
Declarative specification of virtual hosts served by Caddy.
|
|
|
|
|
'';
|
2016-04-05 01:30:21 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-11-29 22:39:27 -05:00
|
|
|
|
acmeCA = mkOption {
|
2023-06-05 17:59:08 +02:00
|
|
|
|
default = null;
|
|
|
|
|
example = "https://acme-v02.api.letsencrypt.org/directory";
|
2021-11-29 22:39:27 -05:00
|
|
|
|
type = with types; nullOr str;
|
2017-06-11 22:04:03 +02:00
|
|
|
|
description = ''
|
2023-06-05 17:59:08 +02:00
|
|
|
|
::: {.note}
|
|
|
|
|
Sets the [`acme_ca` option](https://caddyserver.com/docs/caddyfile/options#acme-ca)
|
|
|
|
|
in the global options block of the resulting Caddyfile.
|
|
|
|
|
:::
|
|
|
|
|
|
2021-11-29 22:39:27 -05:00
|
|
|
|
The URL to the ACME CA's directory. It is strongly recommended to set
|
2023-06-05 17:59:08 +02:00
|
|
|
|
this to `https://acme-staging-v02.api.letsencrypt.org/directory` for
|
|
|
|
|
Let's Encrypt's [staging endpoint](https://letsencrypt.org/docs/staging-environment/)
|
|
|
|
|
while testing or in development.
|
2020-05-08 09:35:55 +00:00
|
|
|
|
|
2023-06-05 17:59:08 +02:00
|
|
|
|
Value `null` should be prefered for production setups,
|
|
|
|
|
as it omits the `acme_ca` option to enable
|
|
|
|
|
[automatic issuer fallback](https://caddyserver.com/docs/automatic-https#issuer-fallback).
|
2017-06-11 22:04:03 +02:00
|
|
|
|
'';
|
2016-04-05 01:30:21 +00:00
|
|
|
|
};
|
2017-01-13 22:29:26 -05:00
|
|
|
|
|
2021-11-29 22:39:27 -05:00
|
|
|
|
email = mkOption {
|
|
|
|
|
default = null;
|
|
|
|
|
type = with types; nullOr str;
|
2020-05-04 02:10:26 +03:00
|
|
|
|
description = ''
|
2021-11-29 22:39:27 -05:00
|
|
|
|
Your email address. Mainly used when creating an ACME account with your
|
|
|
|
|
CA, and is highly recommended in case there are problems with your
|
|
|
|
|
certificates.
|
2020-05-04 02:10:26 +03:00
|
|
|
|
'';
|
2017-01-13 22:29:26 -05:00
|
|
|
|
};
|
2021-11-29 22:39:27 -05:00
|
|
|
|
|
2023-02-21 17:21:38 +01:00
|
|
|
|
enableReload = mkOption {
|
|
|
|
|
default = true;
|
|
|
|
|
type = types.bool;
|
|
|
|
|
description = ''
|
|
|
|
|
Reload Caddy instead of restarting it when configuration file changes.
|
|
|
|
|
|
|
|
|
|
Note that enabling this option requires the [admin API](https://caddyserver.com/docs/caddyfile/options#admin)
|
|
|
|
|
to not be turned off.
|
|
|
|
|
|
|
|
|
|
If you enable this option, consider setting [`grace_period`](https://caddyserver.com/docs/caddyfile/options#grace-period)
|
|
|
|
|
to a non-infinite value in {option}`services.caddy.globalConfig`
|
|
|
|
|
to prevent Caddy waiting for active connections to finish,
|
|
|
|
|
which could delay the reload essentially indefinitely.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2023-08-22 20:14:35 +02:00
|
|
|
|
|
|
|
|
|
settings = mkOption {
|
|
|
|
|
type = settingsFormat.type;
|
|
|
|
|
default = { };
|
|
|
|
|
description = ''
|
|
|
|
|
Structured configuration for Caddy to generate a Caddy JSON configuration file.
|
|
|
|
|
See <https://caddyserver.com/docs/json/> for available options.
|
|
|
|
|
|
|
|
|
|
::: {.warning}
|
|
|
|
|
Using a [Caddyfile](https://caddyserver.com/docs/caddyfile) instead of a JSON config is highly recommended by upstream.
|
|
|
|
|
There are only very few exception to this.
|
|
|
|
|
|
|
|
|
|
Please use a Caddyfile via {option}`services.caddy.configFile`, {option}`services.caddy.virtualHosts` or
|
|
|
|
|
{option}`services.caddy.extraConfig` with {option}`services.caddy.globalConfig` instead.
|
|
|
|
|
:::
|
|
|
|
|
|
|
|
|
|
::: {.note}
|
|
|
|
|
Takes presence over most `services.caddy.*` options, such as {option}`services.caddy.configFile` and {option}`services.caddy.virtualHosts`, if specified.
|
|
|
|
|
:::
|
|
|
|
|
'';
|
|
|
|
|
};
|
2024-12-09 18:50:58 -05:00
|
|
|
|
|
|
|
|
|
environmentFile = mkOption {
|
|
|
|
|
type = with types; nullOr path;
|
|
|
|
|
default = null;
|
|
|
|
|
example = "/run/secrets/caddy.env";
|
|
|
|
|
description = ''
|
|
|
|
|
Environment file as defined in {manpage}`systemd.exec(5)`.
|
|
|
|
|
|
|
|
|
|
You can use environment variables to pass secrets to the service without adding
|
|
|
|
|
them to the world-redable nix store.
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
# in configuration.nix
|
|
|
|
|
services.caddy.environmentFile = "/run/secrets/caddy.env";
|
|
|
|
|
services.caddy.globalConfig = '''
|
|
|
|
|
{
|
|
|
|
|
acme_ca https://acme.zerossl.com/v2/DV90
|
|
|
|
|
acme_eab {
|
|
|
|
|
key_id {$EAB_KEY_ID}
|
|
|
|
|
mac_key {$EAB_MAC_KEY}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
''';
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
# in /run/secrets/caddy.env
|
|
|
|
|
EAB_KEY_ID=secret
|
|
|
|
|
EAB_MAC_KEY=secret
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Find more examples
|
|
|
|
|
[here](https://caddyserver.com/docs/caddyfile/concepts#environment-variables)
|
|
|
|
|
'';
|
|
|
|
|
};
|
2016-04-05 01:30:21 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-11-29 22:39:27 -05:00
|
|
|
|
# implementation
|
2016-04-05 01:30:21 +00:00
|
|
|
|
config = mkIf cfg.enable {
|
2021-11-29 22:39:27 -05:00
|
|
|
|
|
|
|
|
|
assertions =
|
|
|
|
|
[
|
2022-09-26 02:23:45 +01:00
|
|
|
|
{
|
|
|
|
|
assertion = cfg.configFile == configFile -> cfg.adapter == "caddyfile" || cfg.adapter == null;
|
|
|
|
|
message = "To specify an adapter other than 'caddyfile' please provide your own configuration via `services.caddy.configFile`";
|
2021-11-29 22:39:27 -05:00
|
|
|
|
}
|
2022-01-08 15:05:34 -05:00
|
|
|
|
]
|
|
|
|
|
++ map (
|
|
|
|
|
name:
|
|
|
|
|
mkCertOwnershipAssertion {
|
|
|
|
|
cert = config.security.acme.certs.${name};
|
|
|
|
|
groups = config.users.groups;
|
2024-08-24 19:18:07 -04:00
|
|
|
|
services = [ config.systemd.services.caddy ];
|
2024-08-21 17:46:39 -04:00
|
|
|
|
}
|
|
|
|
|
) vhostCertNames;
|
2021-11-29 22:39:27 -05:00
|
|
|
|
|
2022-01-06 09:21:33 -05:00
|
|
|
|
services.caddy.globalConfig = ''
|
|
|
|
|
${optionalString (cfg.email != null) "email ${cfg.email}"}
|
|
|
|
|
${optionalString (cfg.acmeCA != null) "acme_ca ${cfg.acmeCA}"}
|
|
|
|
|
log {
|
|
|
|
|
${cfg.logFormat}
|
|
|
|
|
}
|
|
|
|
|
'';
|
2021-11-29 22:39:27 -05:00
|
|
|
|
|
2023-12-13 02:55:50 +01:00
|
|
|
|
# https://github.com/quic-go/quic-go/wiki/UDP-Buffer-Sizes
|
2022-10-16 14:54:57 -04:00
|
|
|
|
boot.kernel.sysctl."net.core.rmem_max" = mkDefault 2500000;
|
2023-12-13 02:55:50 +01:00
|
|
|
|
boot.kernel.sysctl."net.core.wmem_max" = mkDefault 2500000;
|
2022-10-16 14:54:57 -04:00
|
|
|
|
|
2021-11-29 09:16:25 -05:00
|
|
|
|
systemd.packages = [ cfg.package ];
|
2016-04-05 01:30:21 +00:00
|
|
|
|
systemd.services.caddy = {
|
2024-08-21 17:46:39 -04:00
|
|
|
|
wants = map (certName: "acme-finished-${certName}.target") vhostCertNames;
|
|
|
|
|
after =
|
|
|
|
|
map (certName: "acme-selfsigned-${certName}.service") vhostCertNames
|
|
|
|
|
++ map (certName: "acme-${certName}.service") independentCertNames; # avoid loading self-signed key w/ real cert, or vice-versa
|
|
|
|
|
before = map (certName: "acme-${certName}.service") dependentCertNames;
|
2021-11-29 22:39:27 -05:00
|
|
|
|
|
2016-04-05 01:30:21 +00:00
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2020-09-09 00:31:27 -07:00
|
|
|
|
startLimitIntervalSec = 14400;
|
|
|
|
|
startLimitBurst = 10;
|
2023-02-21 17:21:38 +01:00
|
|
|
|
reloadTriggers = optional cfg.enableReload cfg.configFile;
|
2024-08-19 21:03:32 -07:00
|
|
|
|
restartTriggers = optional (!cfg.enableReload) cfg.configFile;
|
2024-12-10 20:26:33 +01:00
|
|
|
|
|
2023-02-21 17:21:38 +01:00
|
|
|
|
serviceConfig =
|
|
|
|
|
let
|
|
|
|
|
runOptions = ''--config ${configPath} ${
|
|
|
|
|
optionalString (cfg.adapter != null) "--adapter ${cfg.adapter}"
|
|
|
|
|
}'';
|
|
|
|
|
in
|
|
|
|
|
{
|
2024-05-06 03:18:29 +02:00
|
|
|
|
# Override the `ExecStart` line from upstream's systemd unit file by our own:
|
2021-11-29 09:16:25 -05:00
|
|
|
|
# https://www.freedesktop.org/software/systemd/man/systemd.service.html#ExecStart=
|
|
|
|
|
# If the empty string is assigned to this option, the list of commands to start is reset, prior assignments of this option will have no effect.
|
2023-02-21 17:21:38 +01:00
|
|
|
|
ExecStart = [
|
|
|
|
|
""
|
|
|
|
|
''${cfg.package}/bin/caddy run ${runOptions} ${optionalString cfg.resume "--resume"}''
|
|
|
|
|
];
|
|
|
|
|
# Validating the configuration before applying it ensures we’ll get a proper error that will be reported when switching to the configuration
|
nixos/caddy: don't set ExecReload if enableReload is disabled
Otherwise, setting services.caddy.enableReload to false fails in a very bad fashion:
The reload command still gets executed, but fails:
```
Apr 26 21:23:01 n1-rk1 systemd[1]: Reloading Caddy...
Apr 26 21:23:01 n1-rk1 caddy[70793]: {"level":"info","ts":1714166581.733018,"msg":"using provided configuration","config_file":"/etc/caddy/caddy_config","config_adapter":"caddyfile"}
Apr 26 21:23:01 n1-rk1 caddy[70793]: {"level":"warn","ts":1714166581.7353032,"msg":"Caddyfile input is not formatted; run 'caddy fmt --overwrite' to fix inconsistencies","adapter":"caddyfile","file":"/etc/caddy/caddy_config","line":3}
Apr 26 21:23:01 n1-rk1 caddy[70793]: Error: sending configuration to instance: performing request: Post "http://localhost:2019/load": dial tcp [::1]:2019: connect: connection refused
Apr 26 21:23:01 n1-rk1 systemd[1]: caddy.service: Control process exited, code=exited, status=1/FAILURE
Apr 26 21:23:01 n1-rk1 systemd[1]: Reload failed for Caddy.
```
… and the server is not restarted either, as a ExecReload= command is
specified.
Fix this, by only setting ExecReload if the reload exists.
The first empty string is still necessary to reset the old option.
2024-04-27 00:46:11 +03:00
|
|
|
|
ExecReload = [
|
|
|
|
|
""
|
|
|
|
|
] ++ lib.optional cfg.enableReload "${lib.getExe cfg.package} reload ${runOptions} --force";
|
2021-05-11 23:45:22 +02:00
|
|
|
|
User = cfg.user;
|
|
|
|
|
Group = cfg.group;
|
2024-05-06 03:18:51 +02:00
|
|
|
|
ReadWritePaths = [ cfg.dataDir ];
|
2021-11-29 22:39:27 -05:00
|
|
|
|
StateDirectory = mkIf (cfg.dataDir == "/var/lib/caddy") [ "caddy" ];
|
|
|
|
|
LogsDirectory = mkIf (cfg.logDir == "/var/log/caddy") [ "caddy" ];
|
2023-11-04 14:19:14 +00:00
|
|
|
|
Restart = "on-failure";
|
|
|
|
|
RestartPreventExitStatus = 1;
|
2023-11-24 11:03:32 +00:00
|
|
|
|
RestartSec = "5s";
|
2024-12-09 18:50:58 -05:00
|
|
|
|
EnvironmentFile = optional (cfg.environmentFile != null) cfg.environmentFile;
|
2024-12-10 20:26:33 +01:00
|
|
|
|
|
2021-11-29 09:16:25 -05:00
|
|
|
|
# TODO: attempt to upstream these options
|
2017-06-11 22:02:06 +02:00
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
|
PrivateDevices = true;
|
|
|
|
|
ProtectHome = true;
|
2016-04-05 01:30:21 +00:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-11 23:45:22 +02:00
|
|
|
|
users.users = optionalAttrs (cfg.user == "caddy") {
|
|
|
|
|
caddy = {
|
|
|
|
|
group = cfg.group;
|
|
|
|
|
uid = config.ids.uids.caddy;
|
|
|
|
|
home = cfg.dataDir;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
users.groups = optionalAttrs (cfg.group == "caddy") {
|
|
|
|
|
caddy.gid = config.ids.gids.caddy;
|
2016-04-05 01:30:21 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-11-29 22:39:27 -05:00
|
|
|
|
security.acme.certs =
|
|
|
|
|
let
|
2024-08-21 17:46:39 -04:00
|
|
|
|
certCfg = map (
|
|
|
|
|
certName:
|
|
|
|
|
nameValuePair certName {
|
2022-06-26 13:19:10 +08:00
|
|
|
|
group = mkDefault cfg.group;
|
|
|
|
|
reloadServices = [ "caddy.service" ];
|
2024-08-21 17:46:39 -04:00
|
|
|
|
}
|
|
|
|
|
) vhostCertNames;
|
2021-11-29 22:39:27 -05:00
|
|
|
|
in
|
2022-06-26 13:19:10 +08:00
|
|
|
|
listToAttrs certCfg;
|
2021-11-29 22:39:27 -05:00
|
|
|
|
|
2023-02-21 17:21:38 +01:00
|
|
|
|
environment.etc.${etcConfigFile}.source = cfg.configFile;
|
2016-04-05 01:30:21 +00:00
|
|
|
|
};
|
|
|
|
|
}
|