Skip to content

Commit c0c6de8

Browse files
committed
chore(fmt): Run alejandra fmt
1 parent 4c54d13 commit c0c6de8

File tree

4 files changed

+139
-141
lines changed

4 files changed

+139
-141
lines changed

modules/nixos/caddy/default.nix

Lines changed: 105 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
pkgs,
55
...
66
}:
7-
8-
with lib;
9-
10-
let
7+
with lib; let
118
cfg = config.services.caddy;
129

1310
certs = config.security.acme.certs;
@@ -17,67 +14,63 @@ let
1714
dependentCertNames = filter (cert: certs.${cert}.dnsProvider == null) vhostCertNames; # those that might depend on the HTTP server
1815
independentCertNames = filter (cert: certs.${cert}.dnsProvider != null) vhostCertNames; # those that don't depend on the HTTP server
1916

20-
mkVHostConf =
21-
hostOpts:
22-
let
23-
sslCertDir = config.security.acme.certs.${hostOpts.useACMEHost}.directory;
24-
in
25-
''
26-
${hostOpts.hostName} ${concatStringsSep " " hostOpts.serverAliases} {
27-
${optionalString (
28-
hostOpts.listenAddresses != [ ]
29-
) "bind ${concatStringsSep " " hostOpts.listenAddresses}"}
30-
${optionalString (
31-
hostOpts.useACMEHost != null
32-
) "tls ${sslCertDir}/cert.pem ${sslCertDir}/key.pem"}
33-
log {
34-
${hostOpts.logFormat}
35-
}
36-
37-
${hostOpts.extraConfig}
17+
mkVHostConf = hostOpts: let
18+
sslCertDir = config.security.acme.certs.${hostOpts.useACMEHost}.directory;
19+
in ''
20+
${hostOpts.hostName} ${concatStringsSep " " hostOpts.serverAliases} {
21+
${optionalString (
22+
hostOpts.listenAddresses != []
23+
) "bind ${concatStringsSep " " hostOpts.listenAddresses}"}
24+
${optionalString (
25+
hostOpts.useACMEHost != null
26+
) "tls ${sslCertDir}/cert.pem ${sslCertDir}/key.pem"}
27+
log {
28+
${hostOpts.logFormat}
3829
}
39-
'';
4030
41-
settingsFormat = pkgs.formats.json { };
31+
${hostOpts.extraConfig}
32+
}
33+
'';
34+
35+
settingsFormat = pkgs.formats.json {};
4236

4337
configFile =
44-
if cfg.settings != { } then
45-
settingsFormat.generate "caddy.json" cfg.settings
46-
else
47-
let
48-
Caddyfile = pkgs.writeTextDir "Caddyfile" ''
49-
{
50-
${cfg.globalConfig}
51-
}
52-
${cfg.extraConfig}
53-
${concatMapStringsSep "\n" mkVHostConf virtualHosts}
54-
'';
55-
56-
Caddyfile-formatted = pkgs.runCommand "Caddyfile-formatted" { } ''
57-
mkdir -p $out
58-
cp --no-preserve=mode ${Caddyfile}/Caddyfile $out/Caddyfile
59-
${lib.getExe cfg.package} fmt --overwrite $out/Caddyfile
60-
'';
61-
in
62-
"${
63-
if pkgs.stdenv.buildPlatform == pkgs.stdenv.hostPlatform then Caddyfile-formatted else Caddyfile
64-
}/Caddyfile";
38+
if cfg.settings != {}
39+
then settingsFormat.generate "caddy.json" cfg.settings
40+
else let
41+
Caddyfile = pkgs.writeTextDir "Caddyfile" ''
42+
{
43+
${cfg.globalConfig}
44+
}
45+
${cfg.extraConfig}
46+
${concatMapStringsSep "\n" mkVHostConf virtualHosts}
47+
'';
48+
49+
Caddyfile-formatted = pkgs.runCommand "Caddyfile-formatted" {} ''
50+
mkdir -p $out
51+
cp --no-preserve=mode ${Caddyfile}/Caddyfile $out/Caddyfile
52+
${lib.getExe cfg.package} fmt --overwrite $out/Caddyfile
53+
'';
54+
in "${
55+
if pkgs.stdenv.buildPlatform == pkgs.stdenv.hostPlatform
56+
then Caddyfile-formatted
57+
else Caddyfile
58+
}/Caddyfile";
6559

6660
etcConfigFile = "caddy/caddy_config";
6761

6862
configPath = "/etc/${etcConfigFile}";
6963

7064
mkCertOwnershipAssertion = import ../../../security/acme/mk-cert-ownership-assertion.nix lib;
71-
in
72-
{
65+
in {
7366
imports = [
7467
(mkRemovedOptionModule [
7568
"services"
7669
"caddy"
7770
"agree"
7871
] "this option is no longer necessary for Caddy 2")
79-
(mkRenamedOptionModule [ "services" "caddy" "ca" ] [ "services" "caddy" "acmeCA" ])
80-
(mkRenamedOptionModule [ "services" "caddy" "config" ] [ "services" "caddy" "extraConfig" ])
72+
(mkRenamedOptionModule ["services" "caddy" "ca"] ["services" "caddy" "acmeCA"])
73+
(mkRenamedOptionModule ["services" "caddy" "config"] ["services" "caddy" "extraConfig"])
8174
];
8275

8376
# interface
@@ -112,7 +105,7 @@ in
112105
'';
113106
};
114107

115-
package = mkPackageOption pkgs "caddy" { };
108+
package = mkPackageOption pkgs "caddy" {};
116109

117110
dataDir = mkOption {
118111
type = types.path;
@@ -183,10 +176,9 @@ in
183176

184177
adapter = mkOption {
185178
default =
186-
if ((cfg.configFile != configFile) || (builtins.baseNameOf cfg.configFile) == "Caddyfile") then
187-
"caddyfile"
188-
else
189-
null;
179+
if ((cfg.configFile != configFile) || (builtins.baseNameOf cfg.configFile) == "Caddyfile")
180+
then "caddyfile"
181+
else null;
190182
defaultText = literalExpression ''
191183
if ((cfg.configFile != configFile) || (builtins.baseNameOf cfg.configFile) == "Caddyfile") then "caddyfile" else null
192184
'';
@@ -256,8 +248,8 @@ in
256248
};
257249

258250
virtualHosts = mkOption {
259-
type = with types; attrsOf (submodule (import ./vhost-options.nix { inherit cfg; }));
260-
default = { };
251+
type = with types; attrsOf (submodule (import ./vhost-options.nix {inherit cfg;}));
252+
default = {};
261253
example = literalExpression ''
262254
{
263255
"hydra.example.com" = {
@@ -323,7 +315,7 @@ in
323315

324316
settings = mkOption {
325317
type = settingsFormat.type;
326-
default = { };
318+
default = {};
327319
description = ''
328320
Structured configuration for Caddy to generate a Caddy JSON configuration file.
329321
See <https://caddyserver.com/docs/json/> for available options.
@@ -380,21 +372,22 @@ in
380372

381373
# implementation
382374
config = mkIf cfg.enable {
383-
384-
assertions = [
385-
{
386-
assertion = cfg.configFile == configFile -> cfg.adapter == "caddyfile" || cfg.adapter == null;
387-
message = "To specify an adapter other than 'caddyfile' please provide your own configuration via `services.caddy.configFile`";
388-
}
389-
]
390-
++ map (
391-
name:
392-
mkCertOwnershipAssertion {
393-
cert = config.security.acme.certs.${name};
394-
groups = config.users.groups;
395-
services = [ config.systemd.services.caddy ];
396-
}
397-
) vhostCertNames;
375+
assertions =
376+
[
377+
{
378+
assertion = cfg.configFile == configFile -> cfg.adapter == "caddyfile" || cfg.adapter == null;
379+
message = "To specify an adapter other than 'caddyfile' please provide your own configuration via `services.caddy.configFile`";
380+
}
381+
]
382+
++ map (
383+
name:
384+
mkCertOwnershipAssertion {
385+
cert = config.security.acme.certs.${name};
386+
groups = config.users.groups;
387+
services = [config.systemd.services.caddy];
388+
}
389+
)
390+
vhostCertNames;
398391

399392
services.caddy.globalConfig = ''
400393
${optionalString (cfg.email != null) "email ${cfg.email}"}
@@ -408,54 +401,53 @@ in
408401
boot.kernel.sysctl."net.core.rmem_max" = mkDefault 2500000;
409402
boot.kernel.sysctl."net.core.wmem_max" = mkDefault 2500000;
410403

411-
systemd.packages = [ cfg.package ];
404+
systemd.packages = [cfg.package];
412405
systemd.services.caddy = {
413406
wants = map (certName: "acme-finished-${certName}.target") vhostCertNames;
414407
after =
415408
map (certName: "acme-selfsigned-${certName}.service") vhostCertNames
416409
++ map (certName: "acme-${certName}.service") independentCertNames; # avoid loading self-signed key w/ real cert, or vice-versa
417410
before = map (certName: "acme-${certName}.service") dependentCertNames;
418411

419-
wantedBy = [ "multi-user.target" ];
412+
wantedBy = ["multi-user.target"];
420413
startLimitIntervalSec = 14400;
421414
startLimitBurst = 10;
422415
reloadTriggers = optional cfg.enableReload cfg.configFile;
423416
restartTriggers = optional (!cfg.enableReload) cfg.configFile;
424417

425-
serviceConfig =
426-
let
427-
runOptions = ''--config ${configPath} ${
418+
serviceConfig = let
419+
runOptions = ''--config ${configPath} ${
428420
optionalString (cfg.adapter != null) "--adapter ${cfg.adapter}"
429421
}'';
430-
in
431-
{
432-
# Override the `ExecStart` line from upstream's systemd unit file by our own:
433-
# https://www.freedesktop.org/software/systemd/man/systemd.service.html#ExecStart=
434-
# If the empty string is assigned to this option, the list of commands to start is reset, prior assignments of this option will have no effect.
435-
ExecStart = [
436-
""
437-
''${lib.getExe cfg.package} run ${runOptions} ${optionalString cfg.resume "--resume"}''
438-
];
439-
# Validating the configuration before applying it ensures we’ll get a proper error that will be reported when switching to the configuration
440-
ExecReload = [
422+
in {
423+
# Override the `ExecStart` line from upstream's systemd unit file by our own:
424+
# https://www.freedesktop.org/software/systemd/man/systemd.service.html#ExecStart=
425+
# If the empty string is assigned to this option, the list of commands to start is reset, prior assignments of this option will have no effect.
426+
ExecStart = [
427+
""
428+
''${lib.getExe cfg.package} run ${runOptions} ${optionalString cfg.resume "--resume"}''
429+
];
430+
# Validating the configuration before applying it ensures we’ll get a proper error that will be reported when switching to the configuration
431+
ExecReload =
432+
[
441433
""
442434
]
443435
++ lib.optional cfg.enableReload "${lib.getExe cfg.package} reload ${runOptions} --force";
444-
User = cfg.user;
445-
Group = cfg.group;
446-
ReadWritePaths = [ cfg.dataDir ];
447-
StateDirectory = mkIf (cfg.dataDir == "/var/lib/caddy") [ "caddy" ];
448-
LogsDirectory = mkIf (cfg.logDir == "/var/log/caddy") [ "caddy" ];
449-
Restart = "on-failure";
450-
RestartPreventExitStatus = 1;
451-
RestartSec = "5s";
452-
EnvironmentFile = optional (cfg.environmentFile != null) cfg.environmentFile;
453-
454-
# TODO: attempt to upstream these options
455-
NoNewPrivileges = true;
456-
PrivateDevices = true;
457-
ProtectHome = true;
458-
};
436+
User = cfg.user;
437+
Group = cfg.group;
438+
ReadWritePaths = [cfg.dataDir];
439+
StateDirectory = mkIf (cfg.dataDir == "/var/lib/caddy") ["caddy"];
440+
LogsDirectory = mkIf (cfg.logDir == "/var/log/caddy") ["caddy"];
441+
Restart = "on-failure";
442+
RestartPreventExitStatus = 1;
443+
RestartSec = "5s";
444+
EnvironmentFile = optional (cfg.environmentFile != null) cfg.environmentFile;
445+
446+
# TODO: attempt to upstream these options
447+
NoNewPrivileges = true;
448+
PrivateDevices = true;
449+
ProtectHome = true;
450+
};
459451
};
460452

461453
users.users = optionalAttrs (cfg.user == "caddy") {
@@ -470,16 +462,17 @@ in
470462
caddy.gid = config.ids.gids.caddy;
471463
};
472464

473-
security.acme.certs =
474-
let
475-
certCfg = map (
465+
security.acme.certs = let
466+
certCfg =
467+
map (
476468
certName:
477-
nameValuePair certName {
478-
group = mkDefault cfg.group;
479-
reloadServices = [ "caddy.service" ];
480-
}
481-
) vhostCertNames;
482-
in
469+
nameValuePair certName {
470+
group = mkDefault cfg.group;
471+
reloadServices = ["caddy.service"];
472+
}
473+
)
474+
vhostCertNames;
475+
in
483476
listToAttrs certCfg;
484477

485478
environment.etc.${etcConfigFile}.source = cfg.configFile;

modules/nixos/caddy/vhost-options.nix

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
{ cfg }:
2-
{
1+
{cfg}: {
32
config,
43
lib,
54
name,
65
...
7-
}:
8-
let
6+
}: let
97
inherit (lib) literalExpression mkOption types;
10-
in
11-
{
8+
in {
129
options = {
13-
1410
hostName = mkOption {
1511
type = types.str;
1612
default = name;
@@ -19,7 +15,7 @@ in
1915

2016
serverAliases = mkOption {
2117
type = with types; listOf str;
22-
default = [ ];
18+
default = [];
2319
example = [
2420
"www.example.org"
2521
"example.org"
@@ -34,7 +30,7 @@ in
3430
description = ''
3531
A list of host interfaces to bind to for this virtual host.
3632
'';
37-
default = [ ];
33+
default = [];
3834
example = [
3935
"127.0.0.1"
4036
"::1"
@@ -58,7 +54,7 @@ in
5854
logFormat = mkOption {
5955
type = types.lines;
6056
default = ''
61-
output file ${cfg.logDir}/access-${lib.replaceStrings [ "/" " " ] [ "_" "_" ] config.hostName}.log
57+
output file ${cfg.logDir}/access-${lib.replaceStrings ["/" " "] ["_" "_"] config.hostName}.log
6258
'';
6359
defaultText = ''
6460
output file ''${config.services.caddy.logDir}/access-''${hostName}.log
@@ -83,6 +79,5 @@ in
8379
automatically generated `Caddyfile`.
8480
'';
8581
};
86-
8782
};
8883
}

0 commit comments

Comments
 (0)