2016-04-05 02:30:21 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.caddy;
|
|
|
|
configFile = pkgs.writeText "Caddyfile" cfg.config;
|
2017-08-22 22:21:49 +01:00
|
|
|
in {
|
2016-04-05 02:30:21 +01:00
|
|
|
options.services.caddy = {
|
|
|
|
enable = mkEnableOption "Caddy web server";
|
|
|
|
|
|
|
|
config = mkOption {
|
2017-08-22 22:21:49 +01:00
|
|
|
default = "";
|
|
|
|
example = ''
|
|
|
|
example.com {
|
|
|
|
gzip
|
|
|
|
minify
|
|
|
|
log syslog
|
|
|
|
|
|
|
|
root /srv/http
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
type = types.lines;
|
2016-04-05 02:30:21 +01:00
|
|
|
description = "Verbatim Caddyfile to use";
|
|
|
|
};
|
|
|
|
|
2016-07-21 00:51:09 +01:00
|
|
|
ca = mkOption {
|
2018-04-27 00:03:28 +01:00
|
|
|
default = "https://acme-v02.api.letsencrypt.org/directory";
|
|
|
|
example = "https://acme-staging-v02.api.letsencrypt.org/directory";
|
2019-08-08 21:48:27 +01:00
|
|
|
type = types.str;
|
2016-07-21 00:51:09 +01:00
|
|
|
description = "Certificate authority ACME server. The default (Let's Encrypt production server) should be fine for most people.";
|
|
|
|
};
|
|
|
|
|
2016-04-05 02:30:21 +01:00
|
|
|
email = mkOption {
|
|
|
|
default = "";
|
2019-08-08 21:48:27 +01:00
|
|
|
type = types.str;
|
2016-04-05 02:30:21 +01:00
|
|
|
description = "Email address (for Let's Encrypt certificate)";
|
|
|
|
};
|
|
|
|
|
2016-07-21 00:51:09 +01:00
|
|
|
agree = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = "Agree to Let's Encrypt Subscriber Agreement";
|
|
|
|
};
|
|
|
|
|
2016-04-05 02:30:21 +01:00
|
|
|
dataDir = mkOption {
|
|
|
|
default = "/var/lib/caddy";
|
|
|
|
type = types.path;
|
2017-06-11 21:04:03 +01: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.
|
|
|
|
'';
|
2016-04-05 02:30:21 +01:00
|
|
|
};
|
2017-01-14 03:29:26 +00:00
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
default = pkgs.caddy;
|
|
|
|
defaultText = "pkgs.caddy";
|
|
|
|
type = types.package;
|
|
|
|
description = "Caddy package to use.";
|
|
|
|
};
|
2016-04-05 02:30:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.caddy = {
|
|
|
|
description = "Caddy web server";
|
2020-01-23 13:07:05 +00:00
|
|
|
# upstream unit: https://github.com/caddyserver/caddy/blob/master/dist/init/linux-systemd/caddy.service
|
2017-06-11 21:02:06 +01: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 02:30:21 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2018-07-25 21:22:54 +01:00
|
|
|
environment = mkIf (versionAtLeast config.system.stateVersion "17.09")
|
2017-06-11 21:04:03 +01:00
|
|
|
{ CADDYPATH = cfg.dataDir; };
|
2016-04-05 02:30:21 +01:00
|
|
|
serviceConfig = {
|
2017-06-11 21:02:06 +01:00
|
|
|
ExecStart = ''
|
2020-01-23 13:07:05 +00:00
|
|
|
${cfg.package}/bin/caddy -log stdout -log-timestamps=false \
|
|
|
|
-root=/var/tmp -conf=${configFile} \
|
2017-06-11 21:02:06 +01:00
|
|
|
-ca=${cfg.ca} -email=${cfg.email} ${optionalString cfg.agree "-agree"}
|
2016-07-21 00:51:09 +01:00
|
|
|
'';
|
2020-01-23 13:07:05 +00:00
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -USR1 $MAINPID";
|
2016-07-21 00:51:09 +01:00
|
|
|
Type = "simple";
|
|
|
|
User = "caddy";
|
|
|
|
Group = "caddy";
|
2020-01-23 13:07:05 +00:00
|
|
|
Restart = "on-abnormal";
|
|
|
|
StartLimitIntervalSec = 14400;
|
|
|
|
StartLimitBurst = 10;
|
2016-07-21 00:51:09 +01:00
|
|
|
AmbientCapabilities = "cap_net_bind_service";
|
2017-06-11 21:02:06 +01:00
|
|
|
CapabilityBoundingSet = "cap_net_bind_service";
|
|
|
|
NoNewPrivileges = true;
|
2020-01-23 13:07:05 +00:00
|
|
|
LimitNPROC = 512;
|
2017-06-11 21:02:06 +01: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 02:30:21 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
users.users.caddy = {
|
2016-04-05 02:30:21 +01:00
|
|
|
group = "caddy";
|
|
|
|
uid = config.ids.uids.caddy;
|
|
|
|
home = cfg.dataDir;
|
|
|
|
createHome = true;
|
|
|
|
};
|
|
|
|
|
2018-06-30 00:58:35 +01:00
|
|
|
users.groups.caddy.gid = config.ids.uids.caddy;
|
2016-04-05 02:30:21 +01:00
|
|
|
};
|
|
|
|
}
|