Skip to content

Commit 930ee4a

Browse files
committed
caddy: add support for compiling Caddy with plugins
This adds a `withPlugins` function to Caddy package. ```nix services.caddy = { enable = true; package = pkgs.caddy.withPlugins { plugins = [ "github.com/caddy-dns/[email protected]" ]; hash = "sha256-F/jqR4iEsklJFycTjSaW8B/V3iTGqqGOzwYBUXxRKrc="; }; }; ```
1 parent cfa7244 commit 930ee4a

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

nixos/doc/manual/release-notes/rl-2505.section.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@
160160

161161
- `bind.cacheNetworks` now only controls access for recursive queries, where it previously controlled access for all queries.
162162

163+
- Caddy can now be built with plugins by using `caddy.withPlugins`, a `passthru` function that accepts an attribute set as a parameter. The `plugins` argument represents a list of Caddy plugins, with each Caddy plugin being a versioned module. The `hash` argument represents the `vendorHash` of the resulting Caddy source code with the plugins added.
164+
165+
Example:
166+
```nix
167+
services.caddy = {
168+
enable = true;
169+
package = pkgs.caddy.withPlugins {
170+
plugins = [ "github.com/caddy-dns/[email protected]" ];
171+
hash = "sha256-F/jqR4iEsklJFycTjSaW8B/V3iTGqqGOzwYBUXxRKrc=";
172+
};
173+
};
174+
```
175+
176+
To get the necessary hash of the vendored dependencies, omit `hash`. The build will fail and tell you the correct value.
177+
163178
- `programs.fzf.keybindings` now supports the fish shell.
164179

165180
<!-- To avoid merge conflicts, consider adding your item at an arbitrary place in the list instead. -->

pkgs/by-name/ca/caddy/package.nix

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
, testers
77
, installShellFiles
88
, stdenv
9+
, go
10+
, xcaddy
11+
, cacert
12+
, git
913
}:
1014
let
1115
version = "2.8.4";
@@ -32,7 +36,8 @@ buildGoModule {
3236
subPackages = [ "cmd/caddy" ];
3337

3438
ldflags = [
35-
"-s" "-w"
39+
"-s"
40+
"-w"
3641
"-X github.com/caddyserver/caddy/v2.CustomVersion=${version}"
3742
];
3843

@@ -61,12 +66,83 @@ buildGoModule {
6166
--zsh <($out/bin/caddy completion zsh)
6267
'';
6368

64-
passthru.tests = {
65-
inherit (nixosTests) caddy;
66-
version = testers.testVersion {
67-
command = "${caddy}/bin/caddy version";
68-
package = caddy;
69+
passthru = {
70+
tests = {
71+
inherit (nixosTests) caddy;
72+
version = testers.testVersion {
73+
command = "${caddy}/bin/caddy version";
74+
package = caddy;
75+
};
6976
};
77+
withPlugins =
78+
{ plugins
79+
, hash ? lib.fakeHash
80+
}: caddy.overrideAttrs (finalAttrs: prevAttrs:
81+
let
82+
pluginsSorted = builtins.sort builtins.lessThan plugins;
83+
pluginsList = lib.concatMapStrings (plugin: "${plugin}-") pluginsSorted;
84+
pluginsHash = builtins.hashString "md5" pluginsList;
85+
pluginsWithoutVersion = builtins.filter (p: !lib.hasInfix "@" p) pluginsSorted;
86+
in
87+
assert lib.assertMsg (builtins.length pluginsWithoutVersion == 0)
88+
"All plugins should have a version (eg ${builtins.elemAt pluginsWithoutVersion 0}@x.y.z)!";
89+
{
90+
vendorHash = null;
91+
subPackages = [ "." ];
92+
93+
src = stdenv.mkDerivation {
94+
pname = "caddy-src-with-plugins-${pluginsHash}";
95+
version = finalAttrs.version;
96+
97+
nativeBuildInputs = [
98+
go
99+
xcaddy
100+
cacert
101+
git
102+
];
103+
dontUnpack = true;
104+
buildPhase =
105+
let
106+
withArgs = lib.concatMapStrings (plugin: "--with ${plugin} ") pluginsSorted;
107+
in
108+
''
109+
export GOCACHE=$TMPDIR/go-cache
110+
export GOPATH="$TMPDIR/go"
111+
XCADDY_SKIP_BUILD=1 TMPDIR="$PWD" xcaddy build v${finalAttrs.version} ${withArgs}
112+
(cd buildenv* && go mod vendor)
113+
'';
114+
installPhase = ''
115+
mv buildenv* $out
116+
'';
117+
118+
outputHashMode = "recursive";
119+
outputHash = hash;
120+
outputHashAlgo = "sha256";
121+
};
122+
123+
124+
doInstallCheck = true;
125+
installCheckPhase = ''
126+
runHook preInstallCheck
127+
128+
${lib.toShellVar "notfound" pluginsSorted}
129+
while read kind module version; do
130+
[[ "$kind" = "dep" ]] || continue
131+
module="''${module}@''${version}"
132+
for i in "''${!notfound[@]}"; do
133+
if [[ ''${notfound[i]} = ''${module} ]]; then
134+
unset 'notfound[i]'
135+
fi
136+
done
137+
done < <($out/bin/caddy build-info)
138+
if (( ''${#notfound[@]} )); then
139+
>&2 echo "Plugins not found: ''${notfound[@]}"
140+
exit 1
141+
fi
142+
143+
runHook postInstallCheck
144+
'';
145+
});
70146
};
71147

72148
meta = with lib; {

0 commit comments

Comments
 (0)