2020-04-24 23:36:52 +00:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
cfg = config.services.caddy;
|
2020-09-25 04:45:31 +00:00
|
|
|
|
|
2021-12-26 17:43:05 +00:00
|
|
|
|
virtualHosts = attrValues cfg.virtualHosts;
|
|
|
|
|
acmeVHosts = filter (hostOpts: hostOpts.useACMEHost != null) virtualHosts;
|
|
|
|
|
|
|
|
|
|
mkVHostConf = hostOpts:
|
|
|
|
|
let
|
|
|
|
|
sslCertDir = config.security.acme.certs.${hostOpts.useACMEHost}.directory;
|
|
|
|
|
in
|
2021-08-18 13:19:15 +00:00
|
|
|
|
''
|
2021-12-26 17:43:05 +00:00
|
|
|
|
${hostOpts.hostName} ${concatStringsSep " " hostOpts.serverAliases} {
|
2023-07-15 17:15:38 +00:00
|
|
|
|
${optionalString (hostOpts.listenAddresses != [ ]) "bind ${concatStringsSep " " hostOpts.listenAddresses}"}
|
2021-12-26 17:43:05 +00:00
|
|
|
|
${optionalString (hostOpts.useACMEHost != null) "tls ${sslCertDir}/cert.pem ${sslCertDir}/key.pem"}
|
|
|
|
|
log {
|
|
|
|
|
${hostOpts.logFormat}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
${hostOpts.extraConfig}
|
|
|
|
|
}
|
|
|
|
|
'';
|
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
|
settingsFormat = pkgs.formats.json { };
|
2021-12-26 17:43:05 +00:00
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
|
configFile =
|
|
|
|
|
if cfg.settings != { } then
|
|
|
|
|
settingsFormat.generate "caddy.json" cfg.settings
|
|
|
|
|
else
|
|
|
|
|
let
|
|
|
|
|
Caddyfile = pkgs.writeTextDir "Caddyfile" ''
|
|
|
|
|
{
|
|
|
|
|
${cfg.globalConfig}
|
|
|
|
|
}
|
|
|
|
|
${cfg.extraConfig}
|
|
|
|
|
${concatMapStringsSep "\n" mkVHostConf virtualHosts}
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
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-30 11:47:45 +00:00
|
|
|
|
"${if pkgs.stdenv.buildPlatform == pkgs.stdenv.hostPlatform then Caddyfile-formatted else Caddyfile}/Caddyfile";
|
2022-01-13 20:06:32 +00:00
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
|
etcConfigFile = "caddy/caddy_config";
|
|
|
|
|
|
|
|
|
|
configPath = "/etc/${etcConfigFile}";
|
|
|
|
|
|
2022-01-13 20:06:32 +00:00
|
|
|
|
acmeHosts = unique (catAttrs "useACMEHost" acmeVHosts);
|
|
|
|
|
|
|
|
|
|
mkCertOwnershipAssertion = import ../../../security/acme/mk-cert-ownership-assertion.nix;
|
2021-08-18 13:19:15 +00:00
|
|
|
|
in
|
|
|
|
|
{
|
2020-11-03 02:18:15 +00:00
|
|
|
|
imports = [
|
|
|
|
|
(mkRemovedOptionModule [ "services" "caddy" "agree" ] "this option is no longer necessary for Caddy 2")
|
2021-12-26 17:43:05 +00:00
|
|
|
|
(mkRenamedOptionModule [ "services" "caddy" "ca" ] [ "services" "caddy" "acmeCA" ])
|
|
|
|
|
(mkRenamedOptionModule [ "services" "caddy" "config" ] [ "services" "caddy" "extraConfig" ])
|
2020-11-03 02:18:15 +00:00
|
|
|
|
];
|
|
|
|
|
|
2021-12-26 17:43:05 +00:00
|
|
|
|
# interface
|
2020-04-24 23:36:52 +00:00
|
|
|
|
options.services.caddy = {
|
2024-04-21 15:54:59 +00:00
|
|
|
|
enable = mkEnableOption "Caddy web server";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2021-12-26 17:43:05 +00:00
|
|
|
|
user = mkOption {
|
|
|
|
|
default = "caddy";
|
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2021-12-26 17:43:05 +00:00
|
|
|
|
User account under which caddy runs.
|
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
|
::: {.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.
|
|
|
|
|
:::
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
2021-12-26 17:43:05 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
|
default = "caddy";
|
|
|
|
|
type = types.str;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2021-12-26 17:43:05 +00:00
|
|
|
|
Group account under which caddy runs.
|
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
|
::: {.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.
|
|
|
|
|
:::
|
2020-09-25 04:45:31 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
|
package = mkPackageOption pkgs "caddy" { };
|
2021-08-22 07:53:02 +00:00
|
|
|
|
|
2021-12-26 17:43:05 +00:00
|
|
|
|
dataDir = mkOption {
|
|
|
|
|
type = types.path;
|
|
|
|
|
default = "/var/lib/caddy";
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2021-12-26 17:43:05 +00:00
|
|
|
|
The data directory for caddy.
|
2021-08-22 07:53:02 +00:00
|
|
|
|
|
2022-09-09 14:08:57 +00: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.
|
|
|
|
|
|
|
|
|
|
Caddy v2 replaced `CADDYPATH` with XDG directories.
|
|
|
|
|
See <https://caddyserver.com/docs/conventions#file-locations>.
|
|
|
|
|
:::
|
2021-12-26 17:43:05 +00:00
|
|
|
|
'';
|
2021-05-20 23:08:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-12-26 17:43:05 +00:00
|
|
|
|
logDir = mkOption {
|
|
|
|
|
type = types.path;
|
|
|
|
|
default = "/var/log/caddy";
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2021-12-26 17:43:05 +00:00
|
|
|
|
Directory for storing Caddy access logs.
|
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
|
::: {.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.
|
|
|
|
|
:::
|
2021-12-26 17:43:05 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
logFormat = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = ''
|
|
|
|
|
level ERROR
|
|
|
|
|
'';
|
|
|
|
|
example = literalExpression ''
|
|
|
|
|
mkForce "level INFO";
|
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2021-12-26 17:43:05 +00:00
|
|
|
|
Configuration for the default logger. See
|
2022-08-21 13:32:41 +00:00
|
|
|
|
<https://caddyserver.com/docs/caddyfile/options#log>
|
2021-12-26 17:43:05 +00:00
|
|
|
|
for details.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
configFile = mkOption {
|
|
|
|
|
type = types.path;
|
|
|
|
|
default = configFile;
|
|
|
|
|
defaultText = "A Caddyfile automatically generated by values from services.caddy.*";
|
|
|
|
|
example = literalExpression ''
|
2024-01-02 11:29:13 +00:00
|
|
|
|
pkgs.writeText "Caddyfile" '''
|
2021-12-26 17:43:05 +00:00
|
|
|
|
example.com
|
|
|
|
|
|
|
|
|
|
root * /var/www/wordpress
|
|
|
|
|
php_fastcgi unix//run/php/php-version-fpm.sock
|
|
|
|
|
file_server
|
|
|
|
|
''';
|
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2021-12-26 17:43:05 +00:00
|
|
|
|
Override the configuration file used by Caddy. By default,
|
|
|
|
|
NixOS generates one automatically.
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
|
|
The configuration file is exposed at {file}`${configPath}`.
|
2021-12-26 17:43:05 +00:00
|
|
|
|
'';
|
2021-05-20 23:08:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
|
adapter = mkOption {
|
2024-01-02 11:29:13 +00:00
|
|
|
|
default = if ((cfg.configFile != configFile) || (builtins.baseNameOf cfg.configFile) == "Caddyfile") then "caddyfile" else null;
|
2023-07-15 17:15:38 +00:00
|
|
|
|
defaultText = literalExpression ''
|
2024-01-02 11:29:13 +00:00
|
|
|
|
if ((cfg.configFile != configFile) || (builtins.baseNameOf cfg.configFile) == "Caddyfile") then "caddyfile" else null
|
2023-07-15 17:15:38 +00:00
|
|
|
|
'';
|
2022-09-30 11:47:45 +00:00
|
|
|
|
example = literalExpression "nginx";
|
|
|
|
|
type = with types; nullOr str;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2020-10-07 09:15:18 +00:00
|
|
|
|
Name of the config adapter to use.
|
2022-09-09 14:08:57 +00:00
|
|
|
|
See <https://caddyserver.com/docs/config-adapters>
|
2021-12-26 17:43:05 +00:00
|
|
|
|
for the full list.
|
|
|
|
|
|
2022-09-30 11:47:45 +00: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-09-09 14:08:57 +00:00
|
|
|
|
::: {.note}
|
2022-09-30 11:47:45 +00:00
|
|
|
|
Any value other than `null` or `caddyfile` is only valid when providing
|
|
|
|
|
your own `configFile`.
|
2022-09-09 14:08:57 +00:00
|
|
|
|
:::
|
2020-09-25 04:45:31 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-08-18 13:19:15 +00:00
|
|
|
|
resume = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2022-08-21 13:32:41 +00:00
|
|
|
|
Use saved config, if any (and prefer over any specified configuration passed with `--config`).
|
2021-08-18 13:19:15 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-13 20:06:32 +00:00
|
|
|
|
globalConfig = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = "";
|
|
|
|
|
example = ''
|
|
|
|
|
debug
|
|
|
|
|
servers {
|
|
|
|
|
protocol {
|
|
|
|
|
experimental_http3
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2022-01-13 20:06:32 +00:00
|
|
|
|
Additional lines of configuration appended to the global config section
|
2022-08-21 13:32:41 +00:00
|
|
|
|
of the `Caddyfile`.
|
2022-01-13 20:06:32 +00:00
|
|
|
|
|
2022-08-21 13:32:41 +00:00
|
|
|
|
Refer to <https://caddyserver.com/docs/caddyfile/options#global-options>
|
2022-01-13 20:06:32 +00:00
|
|
|
|
for details on supported values.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-26 17:43:05 +00:00
|
|
|
|
extraConfig = mkOption {
|
|
|
|
|
type = types.lines;
|
|
|
|
|
default = "";
|
|
|
|
|
example = ''
|
|
|
|
|
example.com {
|
|
|
|
|
encode gzip
|
|
|
|
|
log
|
|
|
|
|
root /srv/http
|
|
|
|
|
}
|
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2021-12-26 17:43:05 +00:00
|
|
|
|
Additional lines of configuration appended to the automatically
|
2022-08-21 13:32:41 +00:00
|
|
|
|
generated `Caddyfile`.
|
2021-08-18 13:19:15 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-12-26 17:43:05 +00: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
|
|
|
|
|
root /srv/http
|
|
|
|
|
''';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
'';
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2021-12-26 17:43:05 +00:00
|
|
|
|
Declarative specification of virtual hosts served by Caddy.
|
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-12-26 17:43:05 +00:00
|
|
|
|
acmeCA = mkOption {
|
2023-07-15 17:15:38 +00:00
|
|
|
|
default = null;
|
|
|
|
|
example = "https://acme-v02.api.letsencrypt.org/directory";
|
2021-12-26 17:43:05 +00:00
|
|
|
|
type = with types; nullOr str;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2023-07-15 17:15:38 +00: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-12-26 17:43:05 +00:00
|
|
|
|
The URL to the ACME CA's directory. It is strongly recommended to set
|
2023-07-15 17:15:38 +00: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-09-25 04:45:31 +00:00
|
|
|
|
|
2023-07-15 17:15:38 +00: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).
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
2021-12-26 17:43:05 +00:00
|
|
|
|
email = mkOption {
|
|
|
|
|
default = null;
|
|
|
|
|
type = with types; nullOr str;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2021-12-26 17:43:05 +00: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-09-25 04:45:31 +00:00
|
|
|
|
'';
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
2021-12-26 17:43:05 +00:00
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
|
enableReload = mkOption {
|
|
|
|
|
default = true;
|
|
|
|
|
type = types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2023-07-15 17:15:38 +00:00
|
|
|
|
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-10-09 19:29:22 +00:00
|
|
|
|
|
|
|
|
|
settings = mkOption {
|
|
|
|
|
type = settingsFormat.type;
|
|
|
|
|
default = {};
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2023-10-09 19:29:22 +00:00
|
|
|
|
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.
|
|
|
|
|
:::
|
|
|
|
|
'';
|
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-12-26 17:43:05 +00:00
|
|
|
|
# implementation
|
2020-04-24 23:36:52 +00:00
|
|
|
|
config = mkIf cfg.enable {
|
2021-12-26 17:43:05 +00:00
|
|
|
|
|
|
|
|
|
assertions = [
|
2022-09-30 11:47:45 +00: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-12-26 17:43:05 +00:00
|
|
|
|
}
|
2022-01-13 20:06:32 +00:00
|
|
|
|
] ++ map (name: mkCertOwnershipAssertion {
|
|
|
|
|
inherit (cfg) group user;
|
|
|
|
|
cert = config.security.acme.certs.${name};
|
|
|
|
|
groups = config.users.groups;
|
|
|
|
|
}) acmeHosts;
|
2021-12-26 17:43:05 +00:00
|
|
|
|
|
2022-01-13 20:06:32 +00:00
|
|
|
|
services.caddy.globalConfig = ''
|
|
|
|
|
${optionalString (cfg.email != null) "email ${cfg.email}"}
|
|
|
|
|
${optionalString (cfg.acmeCA != null) "acme_ca ${cfg.acmeCA}"}
|
|
|
|
|
log {
|
|
|
|
|
${cfg.logFormat}
|
|
|
|
|
}
|
|
|
|
|
'';
|
2021-12-26 17:43:05 +00:00
|
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
|
# https://github.com/quic-go/quic-go/wiki/UDP-Buffer-Sizes
|
2022-10-30 15:09:59 +00:00
|
|
|
|
boot.kernel.sysctl."net.core.rmem_max" = mkDefault 2500000;
|
2024-01-02 11:29:13 +00:00
|
|
|
|
boot.kernel.sysctl."net.core.wmem_max" = mkDefault 2500000;
|
2022-10-30 15:09:59 +00:00
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
|
systemd.packages = [ cfg.package ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
systemd.services.caddy = {
|
2021-12-26 17:43:05 +00:00
|
|
|
|
wants = map (hostOpts: "acme-finished-${hostOpts.useACMEHost}.target") acmeVHosts;
|
|
|
|
|
after = map (hostOpts: "acme-selfsigned-${hostOpts.useACMEHost}.service") acmeVHosts;
|
|
|
|
|
before = map (hostOpts: "acme-${hostOpts.useACMEHost}.service") acmeVHosts;
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2020-11-03 02:18:15 +00:00
|
|
|
|
startLimitIntervalSec = 14400;
|
|
|
|
|
startLimitBurst = 10;
|
2023-07-15 17:15:38 +00:00
|
|
|
|
reloadTriggers = optional cfg.enableReload cfg.configFile;
|
2021-12-06 16:07:01 +00:00
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
|
serviceConfig = let
|
|
|
|
|
runOptions = ''--config ${configPath} ${optionalString (cfg.adapter != null) "--adapter ${cfg.adapter}"}'';
|
|
|
|
|
in {
|
2024-05-15 15:35:15 +00:00
|
|
|
|
# Override the `ExecStart` line from upstream's systemd unit file by our own:
|
2021-12-06 16:07:01 +00: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-07-15 17:15:38 +00: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
|
2024-05-15 15:35:15 +00:00
|
|
|
|
ExecReload = [ "" ] ++ lib.optional cfg.enableReload "${lib.getExe cfg.package} reload ${runOptions} --force";
|
2021-05-20 23:08:51 +00:00
|
|
|
|
User = cfg.user;
|
|
|
|
|
Group = cfg.group;
|
2024-05-15 15:35:15 +00:00
|
|
|
|
ReadWritePaths = [ cfg.dataDir ];
|
2021-12-26 17:43:05 +00:00
|
|
|
|
StateDirectory = mkIf (cfg.dataDir == "/var/lib/caddy") [ "caddy" ];
|
|
|
|
|
LogsDirectory = mkIf (cfg.logDir == "/var/log/caddy") [ "caddy" ];
|
2024-01-02 11:29:13 +00:00
|
|
|
|
Restart = "on-failure";
|
|
|
|
|
RestartPreventExitStatus = 1;
|
|
|
|
|
RestartSec = "5s";
|
2021-12-06 16:07:01 +00:00
|
|
|
|
|
|
|
|
|
# TODO: attempt to upstream these options
|
2020-04-24 23:36:52 +00:00
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
|
PrivateDevices = true;
|
|
|
|
|
ProtectHome = true;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-20 23:08:51 +00: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;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
2021-12-26 17:43:05 +00:00
|
|
|
|
security.acme.certs =
|
|
|
|
|
let
|
2022-09-09 14:08:57 +00:00
|
|
|
|
certCfg = map (useACMEHost: nameValuePair useACMEHost {
|
|
|
|
|
group = mkDefault cfg.group;
|
|
|
|
|
reloadServices = [ "caddy.service" ];
|
|
|
|
|
}) acmeHosts;
|
2021-12-26 17:43:05 +00:00
|
|
|
|
in
|
2022-09-09 14:08:57 +00:00
|
|
|
|
listToAttrs certCfg;
|
2021-12-26 17:43:05 +00:00
|
|
|
|
|
2023-07-15 17:15:38 +00:00
|
|
|
|
environment.etc.${etcConfigFile}.source = cfg.configFile;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
}
|