2016-04-05 01:30:21 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.caddy;
|
|
|
|
configFile = pkgs.writeText "Caddyfile" cfg.config;
|
2020-05-03 23:10:26 +00:00
|
|
|
|
|
|
|
tlsConfig = {
|
|
|
|
apps.tls.automation.policies = [{
|
|
|
|
issuer = {
|
|
|
|
inherit (cfg) ca email;
|
|
|
|
module = "acme";
|
|
|
|
};
|
|
|
|
}];
|
|
|
|
};
|
2020-09-07 07:42:00 +00:00
|
|
|
|
|
|
|
adaptedConfig = pkgs.runCommand "caddy-config-adapted.json" { } ''
|
2020-05-03 23:10:26 +00:00
|
|
|
${cfg.package}/bin/caddy adapt \
|
|
|
|
--config ${configFile} --adapter ${cfg.adapter} > $out
|
2020-09-07 07:42:00 +00:00
|
|
|
'';
|
|
|
|
tlsJSON = pkgs.writeText "tls.json" (builtins.toJSON tlsConfig);
|
|
|
|
configJSON = pkgs.runCommand "caddy-config.json" { } ''
|
|
|
|
${pkgs.jq}/bin/jq -s '.[0] * .[1]' ${adaptedConfig} ${tlsJSON} > $out
|
|
|
|
'';
|
2017-08-22 21:21:49 +00:00
|
|
|
in {
|
2020-10-19 12:25:36 +00:00
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "services" "caddy" "agree" ] "this option is no longer necessary for Caddy 2")
|
|
|
|
];
|
|
|
|
|
2016-04-05 01:30:21 +00:00
|
|
|
options.services.caddy = {
|
|
|
|
enable = mkEnableOption "Caddy web server";
|
|
|
|
|
|
|
|
config = mkOption {
|
2017-08-22 21:21:49 +00:00
|
|
|
default = "";
|
|
|
|
example = ''
|
|
|
|
example.com {
|
2020-05-08 09:35:55 +00:00
|
|
|
encode gzip
|
|
|
|
log
|
|
|
|
root /srv/http
|
2017-08-22 21:21:49 +00:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
type = types.lines;
|
2020-05-08 09:35:55 +00:00
|
|
|
description = ''
|
|
|
|
Verbatim Caddyfile to use.
|
|
|
|
Caddy v2 supports multiple config formats via adapters (see <option>services.caddy.adapter</option>).
|
|
|
|
'';
|
2016-04-05 01:30:21 +00:00
|
|
|
};
|
|
|
|
|
2020-05-03 23:10:26 +00:00
|
|
|
adapter = mkOption {
|
|
|
|
default = "caddyfile";
|
|
|
|
example = "nginx";
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
2020-10-02 07:46:46 +00:00
|
|
|
Name of the config adapter to use.
|
2020-05-03 23:10:26 +00:00
|
|
|
See https://caddyserver.com/docs/config-adapters for the full list.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-07-20 23:51:09 +00:00
|
|
|
ca = mkOption {
|
2018-04-26 23:03:28 +00:00
|
|
|
default = "https://acme-v02.api.letsencrypt.org/directory";
|
|
|
|
example = "https://acme-staging-v02.api.letsencrypt.org/directory";
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.str;
|
2016-07-20 23:51:09 +00:00
|
|
|
description = "Certificate authority ACME server. The default (Let's Encrypt production server) should be fine for most people.";
|
|
|
|
};
|
|
|
|
|
2016-04-05 01:30:21 +00:00
|
|
|
email = mkOption {
|
|
|
|
default = "";
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.str;
|
2016-04-05 01:30:21 +00:00
|
|
|
description = "Email address (for Let's Encrypt certificate)";
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
|
|
|
default = "/var/lib/caddy";
|
|
|
|
type = types.path;
|
2017-06-11 20:04:03 +00:00
|
|
|
description = ''
|
|
|
|
The data directory, for storing certificates. Before 17.09, this
|
|
|
|
would create a .caddy directory. With 17.09 the contents of the
|
|
|
|
.caddy directory are in the specified data directory instead.
|
2020-05-08 09:35:55 +00:00
|
|
|
|
|
|
|
Caddy v2 replaced CADDYPATH with XDG directories.
|
|
|
|
See https://caddyserver.com/docs/conventions#file-locations.
|
2017-06-11 20:04:03 +00:00
|
|
|
'';
|
2016-04-05 01:30:21 +00:00
|
|
|
};
|
2017-01-14 03:29:26 +00:00
|
|
|
|
|
|
|
package = mkOption {
|
2020-05-08 19:23:33 +00:00
|
|
|
default = pkgs.caddy;
|
|
|
|
defaultText = "pkgs.caddy";
|
2020-10-02 07:46:46 +00:00
|
|
|
example = "pkgs.caddy";
|
2017-01-14 03:29:26 +00:00
|
|
|
type = types.package;
|
2020-05-03 23:10:26 +00:00
|
|
|
description = ''
|
|
|
|
Caddy package to use.
|
|
|
|
'';
|
2017-01-14 03:29:26 +00:00
|
|
|
};
|
2016-04-05 01:30:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.caddy = {
|
|
|
|
description = "Caddy web server";
|
2020-05-08 09:35:55 +00:00
|
|
|
# upstream unit: https://github.com/caddyserver/dist/blob/master/init/caddy.service
|
2017-06-11 20:02:06 +00:00
|
|
|
after = [ "network-online.target" ];
|
2020-01-23 13:07:05 +00:00
|
|
|
wants = [ "network-online.target" ]; # systemd-networkd-wait-online.service
|
2016-04-05 01:30:21 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
2020-10-02 07:46:46 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/caddy run --config ${configJSON}";
|
|
|
|
ExecReload = "${cfg.package}/bin/caddy reload --config ${configJSON}";
|
2016-07-20 23:51:09 +00:00
|
|
|
Type = "simple";
|
|
|
|
User = "caddy";
|
|
|
|
Group = "caddy";
|
2020-01-23 13:07:05 +00:00
|
|
|
Restart = "on-abnormal";
|
|
|
|
StartLimitIntervalSec = 14400;
|
|
|
|
StartLimitBurst = 10;
|
2016-07-20 23:51:09 +00:00
|
|
|
AmbientCapabilities = "cap_net_bind_service";
|
2017-06-11 20:02:06 +00:00
|
|
|
CapabilityBoundingSet = "cap_net_bind_service";
|
|
|
|
NoNewPrivileges = true;
|
2020-01-23 13:07:05 +00:00
|
|
|
LimitNPROC = 512;
|
2017-06-11 20:02:06 +00:00
|
|
|
LimitNOFILE = 1048576;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectSystem = "full";
|
|
|
|
ReadWriteDirectories = cfg.dataDir;
|
2020-01-23 13:07:05 +00:00
|
|
|
KillMode = "mixed";
|
|
|
|
KillSignal = "SIGQUIT";
|
|
|
|
TimeoutStopSec = "5s";
|
2016-04-05 01:30:21 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-06-29 23:58:35 +00:00
|
|
|
users.users.caddy = {
|
2016-04-05 01:30:21 +00:00
|
|
|
group = "caddy";
|
|
|
|
uid = config.ids.uids.caddy;
|
|
|
|
home = cfg.dataDir;
|
|
|
|
createHome = true;
|
|
|
|
};
|
|
|
|
|
2018-06-29 23:58:35 +00:00
|
|
|
users.groups.caddy.gid = config.ids.uids.caddy;
|
2016-04-05 01:30:21 +00:00
|
|
|
};
|
|
|
|
}
|