From e1c0a976b17e3659bad713a06f1b863307ef3a7c Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sun, 17 Nov 2024 11:01:22 +0100 Subject: [PATCH] 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/powerdns@v1.0.1" ]; hash = "sha256-F/jqR4iEsklJFycTjSaW8B/V3iTGqqGOzwYBUXxRKrc="; }; }; ``` --- pkgs/by-name/ca/caddy/package.nix | 86 ++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/pkgs/by-name/ca/caddy/package.nix b/pkgs/by-name/ca/caddy/package.nix index d6832d396c773..c9a4fe61b3cea 100644 --- a/pkgs/by-name/ca/caddy/package.nix +++ b/pkgs/by-name/ca/caddy/package.nix @@ -6,6 +6,9 @@ , testers , installShellFiles , stdenv +, go +, xcaddy +, cacert }: let version = "2.8.4"; @@ -32,7 +35,8 @@ buildGoModule { subPackages = [ "cmd/caddy" ]; ldflags = [ - "-s" "-w" + "-s" + "-w" "-X github.com/caddyserver/caddy/v2.CustomVersion=${version}" ]; @@ -61,12 +65,82 @@ buildGoModule { --zsh <($out/bin/caddy completion zsh) ''; - passthru.tests = { - inherit (nixosTests) caddy; - version = testers.testVersion { - command = "${caddy}/bin/caddy version"; - package = caddy; + passthru = { + tests = { + inherit (nixosTests) caddy; + version = testers.testVersion { + command = "${caddy}/bin/caddy version"; + package = caddy; + }; }; + withPlugins = + { plugins + , hash ? lib.fakeHash + }: caddy.overrideAttrs (finalAttrs: prevAttrs: + let + pluginsSorted = builtins.sort builtins.lessThan plugins; + pluginsList = lib.concatMapStrings (plugin: "${plugin}-") pluginsSorted; + pluginsHash = builtins.hashString "md5" pluginsList; + pluginsWithoutVersion = builtins.filter (p: !lib.hasInfix "@" p) pluginsSorted; + in + assert lib.assertMsg (builtins.length pluginsWithoutVersion == 0) + "All plugins should have a version (eg ${builtins.elemAt pluginsWithoutVersion 0}@x.y.z)!"; + { + vendorHash = null; + subPackages = [ "." ]; + + src = stdenv.mkDerivation { + pname = "caddy-src-with-plugins-${pluginsHash}"; + version = finalAttrs.version; + + nativeBuildInputs = [ + go + xcaddy + cacert + ]; + dontUnpack = true; + buildPhase = + let + withArgs = lib.concatMapStrings (plugin: "--with ${plugin} ") pluginsSorted; + in + '' + export GOCACHE=$TMPDIR/go-cache + export GOPATH="$TMPDIR/go" + XCADDY_SKIP_BUILD=1 TMPDIR="$PWD" xcaddy build v${finalAttrs.version} ${withArgs} + (cd buildenv* && go mod vendor) + ''; + installPhase = '' + mv buildenv* $out + ''; + + outputHashMode = "recursive"; + outputHash = hash; + outputHashAlgo = "sha256"; + }; + + + doInstallCheck = true; + installCheckPhase = '' + runHook preInstallCheck + + ${lib.toShellVar "notfound" pluginsSorted} + while read kind module version; do + [[ "$kind" = "dep" ]] || continue + module="''${module}@''${version}" + for i in "''${!notfound[@]}"; do + if [[ ''${notfound[i]} = ''${module} ]]; then + unset 'notfound[i]' + fi + done + done < <($out/bin/caddy build-info) + if (( ''${#notfound[@]} )); then + >&2 echo "Plugins not found: ''${notfound[@]}" + exit 1 + fi + + runHook postInstallCheck + ''; + }); }; meta = with lib; {