blob: 3d42fd87b7eb8b637593fd97423e204ead335a4e [file] [log] [blame]
Skyler Grey70c83de2023-05-01 21:36:12 +00001# https://github.com/NixOS/nixpkgs/issues/14671#issuecomment-1016376290
Skyler Grey0e71dcd2023-05-21 00:05:17 +02002{ lib, pkgs, fetchFromGitHub, buildGoModule, plugins ? [ ] }:
Skyler Grey70c83de2023-05-01 21:36:12 +00003
4with lib;
5with pkgs;
6
7let
Skyler Grey19f9fa22023-05-24 17:51:24 +02008 caddySrc = fetchFromGitHub {
9 # github.com/caddyserver/caddy/v2
10 owner = "caddyserver";
11 repo = "caddy";
12 rev = "v2.6.4";
13 hash = "sha256-3a3+nFHmGONvL/TyQRqgJtrSDIn0zdGy9YwhZP17mU0=";
14 };
15 l4Src = fetchFromGitHub {
16 # github.com/mholt/caddy-l4
Skyler Grey70c83de2023-05-01 21:36:12 +000017 owner = "mholt";
18 repo = "caddy-l4";
19 rev = "aa8cf68a3b5197c45a8b4ffd99b74465f0b5a6b1";
20 hash = "sha256-3KcoOAB+YkOU8qKM75uQo58/dljRBmP25dionQ9K2dc=";
21 };
Skyler Grey19f9fa22023-05-24 17:51:24 +020022 cloudflareSrc = fetchFromGitHub {
23 # github.com/caddy-dns/cloudflare
24 owner = "caddy-dns";
25 repo = "cloudflare";
26 rev = "a9d3ae2690a1d232bc9f8fc8b15bd4e0a6960eec";
27 hash = "sha256-bqnk4XkhUI7YhCv24ha8mds5EaYphnYj8wy/mFOieqI=";
Skyler Grey0e71dcd2023-05-21 00:05:17 +020028 };
Skyler Grey70c83de2023-05-01 21:36:12 +000029
30 combinedSrc = stdenv.mkDerivation {
31 name = "caddy-src";
32
33 nativeBuildInputs = [ go ];
34
35 buildCommand = ''
36 export GOCACHE="$TMPDIR/go-cache"
37 export GOPATH="$TMPDIR/go"
38
39 mkdir -p "$out/ourcaddy"
40
41 cp -r ${caddySrc} "$out/caddy"
Skyler Grey19f9fa22023-05-24 17:51:24 +020042 cp -r ${l4Src} "$out/l4"
43 cp -r ${cloudflareSrc} "$out/cloudflare"
Skyler Grey70c83de2023-05-01 21:36:12 +000044
45 cd "$out/ourcaddy"
46
47 go mod init caddy
48 echo "package main" >> main.go
Skyler Grey19f9fa22023-05-24 17:51:24 +020049
Skyler Grey70c83de2023-05-01 21:36:12 +000050 echo 'import caddycmd "github.com/caddyserver/caddy/v2/cmd"' >> main.go
Skyler Grey19f9fa22023-05-24 17:51:24 +020051
Skyler Grey0e71dcd2023-05-21 00:05:17 +020052 echo 'import _ "github.com/caddyserver/caddy/v2/modules/standard"' >> main.go
Skyler Grey70c83de2023-05-01 21:36:12 +000053 echo 'import _ "github.com/mholt/caddy-l4"' >> main.go
Skyler Grey19f9fa22023-05-24 17:51:24 +020054 echo 'import _ "github.com/caddy-dns/cloudflare"' >> main.go
55
Skyler Grey70c83de2023-05-01 21:36:12 +000056 echo "func main(){ caddycmd.Main() }" >> main.go
Skyler Grey19f9fa22023-05-24 17:51:24 +020057
Skyler Grey70c83de2023-05-01 21:36:12 +000058 go mod edit -require=github.com/caddyserver/caddy/v2@v2.6.4
59 go mod edit -replace github.com/caddyserver/caddy/v2=../caddy
60 go mod edit -require=github.com/mholt/caddy-l4@v0.0.0
Skyler Grey19f9fa22023-05-24 17:51:24 +020061 go mod edit -replace github.com/mholt/caddy-l4=../l4
62 go mod edit -require=github.com/caddy-dns/cloudflare@v0.0.0
63 go mod edit -replace github.com/caddy-dns/cloudflare=../cloudflare
Skyler Grey70c83de2023-05-01 21:36:12 +000064 '';
65 };
Skyler Greyfe1740c2023-10-21 01:24:18 +000066in buildGoModule {
Skyler Grey19f9fa22023-05-24 17:51:24 +020067 name = "caddy-with-plugins";
Skyler Grey70c83de2023-05-01 21:36:12 +000068
69 src = combinedSrc;
70
Skyler Grey19f9fa22023-05-24 17:51:24 +020071 vendorHash = "sha256-34o91x7Y7DjIHom2Tk2ARBcJ3PzBVm+ALWK9ucj1g5A=";
Skyler Grey70c83de2023-05-01 21:36:12 +000072
73 overrideModAttrs = _: {
74 postPatch = "cd ourcaddy";
75
76 postConfigure = ''
77 go mod tidy
78 '';
79
80 postInstall = ''
81 mkdir -p "$out/.magic"
82 cp go.mod go.sum "$out/.magic"
83 '';
84 };
85
86 postPatch = "cd ourcaddy";
87
88 postConfigure = ''
89 cp vendor/.magic/go.* .
90 '';
91}