3
0
Fork 0
forked from mirrors/nixpkgs

nixos/caddy: add support for v2

This commit is contained in:
Oleksii Filonenko 2020-05-04 02:10:26 +03:00 committed by Sylvain Fankhauser
parent 03a6e9457b
commit 8cc592abfa
No known key found for this signature in database
GPG key ID: 4228AB9EC0612ADA

View file

@ -5,12 +5,30 @@ with lib;
let
cfg = config.services.caddy;
configFile = pkgs.writeText "Caddyfile" cfg.config;
# v2-specific options
isCaddy2 = versionAtLeast cfg.package.version "2.0";
tlsConfig = {
apps.tls.automation.policies = [{
issuer = {
inherit (cfg) ca email;
module = "acme";
};
}];
};
adaptedConfig = importJSON (pkgs.runCommand "caddy-config-adapted.json" { } ''
${cfg.package}/bin/caddy adapt \
--config ${configFile} --adapter ${cfg.adapter} > $out
'');
configJSON = pkgs.writeText "caddy-config.json" (builtins.toJSON
(recursiveUpdate adaptedConfig tlsConfig));
in {
options.services.caddy = {
enable = mkEnableOption "Caddy web server";
config = mkOption {
default = "";
# TODO: update example text on v2.0 release
example = ''
example.com {
gzip
@ -24,6 +42,17 @@ in {
description = "Verbatim Caddyfile to use";
};
adapter = mkOption {
default = "caddyfile";
example = "nginx";
type = types.str;
description = ''
Name of the config adapter to use.
See https://caddyserver.com/docs/config-adapters for the full list.
'';
};
ca = mkOption {
default = "https://acme-v02.api.letsencrypt.org/directory";
example = "https://acme-staging-v02.api.letsencrypt.org/directory";
@ -56,8 +85,14 @@ in {
package = mkOption {
default = pkgs.caddy;
defaultText = "pkgs.caddy";
example = "pkgs.caddy2";
type = types.package;
description = "Caddy package to use.";
description = ''
Caddy package to use.
Note: to use Caddy v2, set this to <option>pkgs.caddy2</option>.
v2 will become the default after it is released.
'';
};
};
@ -68,10 +103,12 @@ in {
after = [ "network-online.target" ];
wants = [ "network-online.target" ]; # systemd-networkd-wait-online.service
wantedBy = [ "multi-user.target" ];
environment = mkIf (versionAtLeast config.system.stateVersion "17.09")
environment = mkIf (versionAtLeast config.system.stateVersion "17.09" && !isCaddy2)
{ CADDYPATH = cfg.dataDir; };
serviceConfig = {
ExecStart = ''
ExecStart = if isCaddy2 then ''
${cfg.package}/bin/caddy run --config ${configJSON}
'' else ''
${cfg.package}/bin/caddy -log stdout -log-timestamps=false \
-root=/var/tmp -conf=${configFile} \
-ca=${cfg.ca} -email=${cfg.email} ${optionalString cfg.agree "-agree"}