nixos/caddy: add support for v2
This commit is contained in:
parent
03a6e9457b
commit
8cc592abfa
|
@ -5,12 +5,30 @@ with lib;
|
||||||
let
|
let
|
||||||
cfg = config.services.caddy;
|
cfg = config.services.caddy;
|
||||||
configFile = pkgs.writeText "Caddyfile" cfg.config;
|
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 {
|
in {
|
||||||
options.services.caddy = {
|
options.services.caddy = {
|
||||||
enable = mkEnableOption "Caddy web server";
|
enable = mkEnableOption "Caddy web server";
|
||||||
|
|
||||||
config = mkOption {
|
config = mkOption {
|
||||||
default = "";
|
default = "";
|
||||||
|
# TODO: update example text on v2.0 release
|
||||||
example = ''
|
example = ''
|
||||||
example.com {
|
example.com {
|
||||||
gzip
|
gzip
|
||||||
|
@ -24,6 +42,17 @@ in {
|
||||||
description = "Verbatim Caddyfile to use";
|
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 {
|
ca = mkOption {
|
||||||
default = "https://acme-v02.api.letsencrypt.org/directory";
|
default = "https://acme-v02.api.letsencrypt.org/directory";
|
||||||
example = "https://acme-staging-v02.api.letsencrypt.org/directory";
|
example = "https://acme-staging-v02.api.letsencrypt.org/directory";
|
||||||
|
@ -56,8 +85,14 @@ in {
|
||||||
package = mkOption {
|
package = mkOption {
|
||||||
default = pkgs.caddy;
|
default = pkgs.caddy;
|
||||||
defaultText = "pkgs.caddy";
|
defaultText = "pkgs.caddy";
|
||||||
|
example = "pkgs.caddy2";
|
||||||
type = types.package;
|
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" ];
|
after = [ "network-online.target" ];
|
||||||
wants = [ "network-online.target" ]; # systemd-networkd-wait-online.service
|
wants = [ "network-online.target" ]; # systemd-networkd-wait-online.service
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
environment = mkIf (versionAtLeast config.system.stateVersion "17.09")
|
environment = mkIf (versionAtLeast config.system.stateVersion "17.09" && !isCaddy2)
|
||||||
{ CADDYPATH = cfg.dataDir; };
|
{ CADDYPATH = cfg.dataDir; };
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
ExecStart = ''
|
ExecStart = if isCaddy2 then ''
|
||||||
|
${cfg.package}/bin/caddy run --config ${configJSON}
|
||||||
|
'' else ''
|
||||||
${cfg.package}/bin/caddy -log stdout -log-timestamps=false \
|
${cfg.package}/bin/caddy -log stdout -log-timestamps=false \
|
||||||
-root=/var/tmp -conf=${configFile} \
|
-root=/var/tmp -conf=${configFile} \
|
||||||
-ca=${cfg.ca} -email=${cfg.email} ${optionalString cfg.agree "-agree"}
|
-ca=${cfg.ca} -email=${cfg.email} ${optionalString cfg.agree "-agree"}
|
||||||
|
|
Loading…
Reference in New Issue