Skyler Grey | 70c83de | 2023-05-01 21:36:12 +0000 | [diff] [blame] | 1 | # https://github.com/NixOS/nixpkgs/issues/14671#issuecomment-1016376290 |
| 2 | { lib, pkgs, fetchFromGitHub, buildGoModule, plugins ? [ ], vendorSha256 ? "" }: |
| 3 | |
| 4 | with lib; |
| 5 | with pkgs; |
| 6 | |
| 7 | let |
| 8 | pluginSrc = fetchFromGitHub { |
| 9 | owner = "mholt"; |
| 10 | repo = "caddy-l4"; |
| 11 | rev = "aa8cf68a3b5197c45a8b4ffd99b74465f0b5a6b1"; |
| 12 | hash = "sha256-3KcoOAB+YkOU8qKM75uQo58/dljRBmP25dionQ9K2dc="; |
| 13 | }; |
| 14 | caddySrc = srcOnly (fetchFromGitHub { |
| 15 | owner = "caddyserver"; |
| 16 | repo = "caddy"; |
| 17 | rev = "v2.6.4"; |
| 18 | hash = "sha256-3a3+nFHmGONvL/TyQRqgJtrSDIn0zdGy9YwhZP17mU0="; |
| 19 | }); |
| 20 | |
| 21 | combinedSrc = stdenv.mkDerivation { |
| 22 | name = "caddy-src"; |
| 23 | |
| 24 | nativeBuildInputs = [ go ]; |
| 25 | |
| 26 | buildCommand = '' |
| 27 | export GOCACHE="$TMPDIR/go-cache" |
| 28 | export GOPATH="$TMPDIR/go" |
| 29 | |
| 30 | mkdir -p "$out/ourcaddy" |
| 31 | |
| 32 | cp -r ${caddySrc} "$out/caddy" |
| 33 | cp -r ${pluginSrc} "$out/plugin" |
| 34 | |
| 35 | cd "$out/ourcaddy" |
| 36 | |
| 37 | go mod init caddy |
| 38 | echo "package main" >> main.go |
| 39 | echo 'import caddycmd "github.com/caddyserver/caddy/v2/cmd"' >> main.go |
| 40 | echo 'import _ "github.com/mholt/caddy-l4"' >> main.go |
| 41 | echo "func main(){ caddycmd.Main() }" >> main.go |
| 42 | go mod edit -require=github.com/caddyserver/caddy/v2@v2.6.4 |
| 43 | go mod edit -replace github.com/caddyserver/caddy/v2=../caddy |
| 44 | go mod edit -require=github.com/mholt/caddy-l4@v0.0.0 |
| 45 | go mod edit -replace github.com/mholt/caddy-l4=../plugin |
| 46 | ''; |
| 47 | }; |
| 48 | in |
| 49 | buildGoModule { |
| 50 | name = "meowdy"; |
| 51 | |
| 52 | src = combinedSrc; |
| 53 | |
| 54 | vendorHash = "sha256-GmgK2gPCkXXqVcxx+U0h7zJwRGBqFiBA7R0FwHY0SF0="; |
| 55 | |
| 56 | overrideModAttrs = _: { |
| 57 | postPatch = "cd ourcaddy"; |
| 58 | |
| 59 | postConfigure = '' |
| 60 | go mod tidy |
| 61 | ''; |
| 62 | |
| 63 | postInstall = '' |
| 64 | mkdir -p "$out/.magic" |
| 65 | cp go.mod go.sum "$out/.magic" |
| 66 | ''; |
| 67 | }; |
| 68 | |
| 69 | postPatch = "cd ourcaddy"; |
| 70 | |
| 71 | postConfigure = '' |
| 72 | cp vendor/.magic/go.* . |
| 73 | ''; |
| 74 | } |