2016-04-05 03:30:21 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.caddy;
|
|
|
|
configFile = pkgs.writeText "Caddyfile" cfg.config;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.caddy = {
|
|
|
|
enable = mkEnableOption "Caddy web server";
|
|
|
|
|
|
|
|
config = mkOption {
|
|
|
|
description = "Verbatim Caddyfile to use";
|
|
|
|
};
|
|
|
|
|
2016-07-21 01:51:09 +02:00
|
|
|
ca = mkOption {
|
|
|
|
default = "https://acme-v01.api.letsencrypt.org/directory";
|
|
|
|
example = "https://acme-staging.api.letsencrypt.org/directory";
|
|
|
|
type = types.string;
|
|
|
|
description = "Certificate authority ACME server. The default (Let's Encrypt production server) should be fine for most people.";
|
|
|
|
};
|
|
|
|
|
2016-04-05 03:30:21 +02:00
|
|
|
email = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.string;
|
|
|
|
description = "Email address (for Let's Encrypt certificate)";
|
|
|
|
};
|
|
|
|
|
2016-07-21 01:51:09 +02:00
|
|
|
agree = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = "Agree to Let's Encrypt Subscriber Agreement";
|
|
|
|
};
|
|
|
|
|
2016-04-05 03:30:21 +02:00
|
|
|
dataDir = mkOption {
|
|
|
|
default = "/var/lib/caddy";
|
|
|
|
type = types.path;
|
2017-06-11 22:04:03 +02: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 03:30:21 +02:00
|
|
|
};
|
2017-01-14 04:29:26 +01:00
|
|
|
|
|
|
|
package = mkOption {
|
|
|
|
default = pkgs.caddy;
|
|
|
|
defaultText = "pkgs.caddy";
|
|
|
|
type = types.package;
|
|
|
|
description = "Caddy package to use.";
|
|
|
|
};
|
2016-04-05 03:30:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.caddy = {
|
|
|
|
description = "Caddy web server";
|
2017-06-11 22:02:06 +02:00
|
|
|
after = [ "network-online.target" ];
|
2016-04-05 03:30:21 +02:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2017-06-11 22:04:03 +02:00
|
|
|
environment = mkIf (versionAtLeast config.system.stateVersion "17.09")
|
|
|
|
{ CADDYPATH = cfg.dataDir; };
|
2016-04-05 03:30:21 +02:00
|
|
|
serviceConfig = {
|
2017-06-11 22:02:06 +02:00
|
|
|
ExecStart = ''
|
|
|
|
${cfg.package.bin}/bin/caddy -root=/var/tmp -conf=${configFile} \
|
|
|
|
-ca=${cfg.ca} -email=${cfg.email} ${optionalString cfg.agree "-agree"}
|
2016-07-21 01:51:09 +02:00
|
|
|
'';
|
2017-06-11 22:02:06 +02:00
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
2016-07-21 01:51:09 +02:00
|
|
|
Type = "simple";
|
|
|
|
User = "caddy";
|
|
|
|
Group = "caddy";
|
2017-06-11 22:02:06 +02:00
|
|
|
Restart = "on-failure";
|
|
|
|
StartLimitInterval = 86400;
|
|
|
|
StartLimitBurst = 5;
|
2016-07-21 01:51:09 +02:00
|
|
|
AmbientCapabilities = "cap_net_bind_service";
|
2017-06-11 22:02:06 +02:00
|
|
|
CapabilityBoundingSet = "cap_net_bind_service";
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
LimitNPROC = 64;
|
|
|
|
LimitNOFILE = 1048576;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectSystem = "full";
|
|
|
|
ReadWriteDirectories = cfg.dataDir;
|
2016-04-05 03:30:21 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
users.extraUsers.caddy = {
|
|
|
|
group = "caddy";
|
|
|
|
uid = config.ids.uids.caddy;
|
|
|
|
home = cfg.dataDir;
|
|
|
|
createHome = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
users.extraGroups.caddy.gid = config.ids.uids.caddy;
|
|
|
|
};
|
|
|
|
}
|