Skip to content

Commit 9ceb117

Browse files
authored
caddy: add suport for compiling Caddy with plugins (#358586)
2 parents 6ff8d99 + e57d662 commit 9ceb117

File tree

3 files changed

+110
-6
lines changed

3 files changed

+110
-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
@@ -267,6 +267,21 @@
267267

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

270+
- 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.
271+
272+
Example:
273+
```nix
274+
services.caddy = {
275+
enable = true;
276+
package = pkgs.caddy.withPlugins {
277+
plugins = [ "github.com/caddy-dns/[email protected]" ];
278+
hash = "sha256-F/jqR4iEsklJFycTjSaW8B/V3iTGqqGOzwYBUXxRKrc=";
279+
};
280+
};
281+
```
282+
283+
To get the necessary hash of the vendored dependencies, omit `hash`. The build will fail and tell you the correct value.
284+
270285
- `programs.fzf.keybindings` now supports the fish shell.
271286

272287
<!-- 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: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
{ lib
22
, buildGoModule
3+
, callPackage
34
, fetchFromGitHub
45
, nixosTests
56
, caddy
67
, testers
78
, installShellFiles
89
, stdenv
10+
, go
11+
, xcaddy
12+
, cacert
13+
, git
914
}:
1015
let
1116
version = "2.8.4";
@@ -32,7 +37,8 @@ buildGoModule {
3237
subPackages = [ "cmd/caddy" ];
3338

3439
ldflags = [
35-
"-s" "-w"
40+
"-s"
41+
"-w"
3642
"-X github.com/caddyserver/caddy/v2.CustomVersion=${version}"
3743
];
3844

@@ -61,12 +67,15 @@ buildGoModule {
6167
--zsh <($out/bin/caddy completion zsh)
6268
'';
6369

64-
passthru.tests = {
65-
inherit (nixosTests) caddy;
66-
version = testers.testVersion {
67-
command = "${caddy}/bin/caddy version";
68-
package = caddy;
70+
passthru = {
71+
tests = {
72+
inherit (nixosTests) caddy;
73+
version = testers.testVersion {
74+
command = "${caddy}/bin/caddy version";
75+
package = caddy;
76+
};
6977
};
78+
withPlugins = callPackage ./plugins.nix { inherit caddy; };
7079
};
7180

7281
meta = with lib; {

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
lib,
3+
stdenv,
4+
go,
5+
xcaddy,
6+
cacert,
7+
git,
8+
caddy,
9+
}:
10+
{
11+
plugins,
12+
hash ? lib.fakeHash,
13+
}:
14+
let
15+
pluginsSorted = lib.sort lib.lessThan plugins;
16+
pluginsList = lib.concatMapStrings (plugin: "${plugin}-") pluginsSorted;
17+
pluginsHash = builtins.hashString "md5" pluginsList;
18+
pluginsWithoutVersion = lib.filter (p: !lib.hasInfix "@" p) pluginsSorted;
19+
in
20+
assert lib.assertMsg (
21+
lib.length pluginsWithoutVersion == 0
22+
) "All plugins should have a version (eg ${lib.elemAt pluginsWithoutVersion 0}@x.y.z)!";
23+
caddy.overrideAttrs (
24+
finalAttrs: prevAttrs: {
25+
vendorHash = null;
26+
subPackages = [ "." ];
27+
28+
src = stdenv.mkDerivation {
29+
pname = "caddy-src-with-plugins-${pluginsHash}";
30+
version = finalAttrs.version;
31+
32+
nativeBuildInputs = [
33+
go
34+
xcaddy
35+
cacert
36+
git
37+
];
38+
dontUnpack = true;
39+
buildPhase =
40+
let
41+
withArgs = lib.concatMapStrings (plugin: "--with ${plugin} ") pluginsSorted;
42+
in
43+
''
44+
export GOCACHE=$TMPDIR/go-cache
45+
export GOPATH="$TMPDIR/go"
46+
XCADDY_SKIP_BUILD=1 TMPDIR="$PWD" xcaddy build v${finalAttrs.version} ${withArgs}
47+
(cd buildenv* && go mod vendor)
48+
'';
49+
installPhase = ''
50+
mv buildenv* $out
51+
'';
52+
53+
outputHashMode = "recursive";
54+
outputHash = hash;
55+
outputHashAlgo = "sha256";
56+
};
57+
58+
doInstallCheck = true;
59+
installCheckPhase = ''
60+
runHook preInstallCheck
61+
62+
${lib.toShellVar "notfound" pluginsSorted}
63+
while read kind module version; do
64+
[[ "$kind" = "dep" ]] || continue
65+
module="''${module}@''${version}"
66+
for i in "''${!notfound[@]}"; do
67+
if [[ ''${notfound[i]} = ''${module} ]]; then
68+
unset 'notfound[i]'
69+
fi
70+
done
71+
done < <($out/bin/caddy build-info)
72+
if (( ''${#notfound[@]} )); then
73+
>&2 echo "Plugins not found: ''${notfound[@]}"
74+
exit 1
75+
fi
76+
77+
runHook postInstallCheck
78+
'';
79+
}
80+
)

0 commit comments

Comments
 (0)