nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 stdenv,
4 buildGoModule,
5 fetchFromGitHub,
6 removeReferencesTo,
7 tzdata,
8 wire,
9 yarn-berry_4,
10 yarn-berry_4-fetcher,
11 buildPackages,
12 python3,
13 jq,
14 moreutils,
15 nix-update-script,
16 nixosTests,
17 xcbuild,
18 faketty,
19 nodejs,
20}:
21
22buildGoModule (finalAttrs: {
23 pname = "grafana";
24 version = "12.3.2";
25
26 subPackages = [
27 "pkg/cmd/grafana"
28 "pkg/cmd/grafana-server"
29 "pkg/cmd/grafana-cli"
30 ];
31
32 src = fetchFromGitHub {
33 owner = "grafana";
34 repo = "grafana";
35 rev = "v${finalAttrs.version}";
36 hash = "sha256-yyToc7jVLqCwZINhya35KGuCRP24TzWouHUm8Yd8e1o=";
37 };
38
39 # borrowed from: https://github.com/NixOS/nixpkgs/blob/d70d9425f49f9aba3c49e2c389fe6d42bac8c5b0/pkgs/development/tools/analysis/snyk/default.nix#L20-L22
40 env = {
41 CYPRESS_INSTALL_BINARY = 0;
42 PUPPETEER_SKIP_DOWNLOAD = 1;
43
44 # The build OOMs on memory constrained aarch64 without this
45 NODE_OPTIONS = "--max_old_space_size=4096";
46 };
47
48 missingHashes = ./missing-hashes.json;
49 # Since this is not a dependency attribute the buildPackages has to be specified.
50 offlineCache = buildPackages.yarn-berry_4-fetcher.fetchYarnBerryDeps {
51 inherit (finalAttrs) src missingHashes;
52 hash = "sha256-RnYxki15s2crHHBDjpw7vLIMt5fIzM9rWTNjwQlLJ/o=";
53 };
54
55 disallowedRequisites = [ finalAttrs.offlineCache ];
56
57 vendorHash = "sha256-PN5YM0qstHm68ZvBsHBcRVD9NfrJ48EvBJlbwfsyBVY=";
58
59 # Grafana seems to just set it to the latest version available
60 # nowadays.
61 # However, while `substituteInPlace --replace-fail` is desirable to keep
62 # the section up-to-date, this gets increasingly annoying since there's
63 # an inconsistent 1% with a different version. So we now blindly set all
64 # `go` directives to whatever nixpkgs provides and make it the maintainer's
65 # duty to ensure that the mandated
66 # Go version is compatible with what we provide.
67 # This is still better than maintaining some list of go.mod files (or exclusions of that)
68 # where to patch the go version (and where to not do that).
69 postPatch = ''
70 find . \( -name go.mod -or -name "go.work" \) -type f -exec sed -i -e 's/^go .*/go ${finalAttrs.passthru.go.version}/g' {} \;
71 '';
72
73 proxyVendor = true;
74
75 depsBuildBuild = [ buildPackages.stdenv.cc ];
76
77 nativeBuildInputs = [
78 wire
79 jq
80 moreutils
81 removeReferencesTo
82 # required to run old node-gyp
83 (python3.pythonOnBuildForHost.withPackages (ps: [ ps.distutils ]))
84 faketty
85 nodejs
86 yarn-berry_4
87 yarn-berry_4-fetcher.yarnBerryConfigHook
88 ]
89 ++ lib.optionals stdenv.hostPlatform.isDarwin [ xcbuild.xcbuild ];
90
91 # We have to remove this setupHook, otherwise it also runs in the `goModules`
92 # derivation and fails because `offlineCache` is missing there.
93 overrideModAttrs = (
94 old: {
95 nativeBuildInputs = lib.filter (
96 x: lib.getName x != (lib.getName buildPackages.yarn-berry_4-fetcher.yarnBerryConfigHook)
97 ) old.nativeBuildInputs;
98 }
99 );
100
101 postConfigure = ''
102 # Generate DI code that's required to compile the package.
103 # From https://github.com/grafana/grafana/blob/v8.2.3/Makefile#L33-L35
104 wire gen -tags oss ./pkg/server
105 wire gen -tags oss ./pkg/cmd/grafana-cli/runner
106
107 # ```
108 # go-1.25.4/share/go/pkg/tool/linux_amd64/link: running aarch64-unknown-linux-gnu-gcc failed: exit status 1
109 # aarch64-unknown-linux-gnu-gcc -m64 -s -o $WORK/b001/exe/gen -rdynamic /build/go-link-507658645/go.o
110 # aarch64-unknown-linux-gnu-gcc: error: unrecognized command-line option '-m64'
111 # ```
112 # Above log is due to https://github.com/golang/go/blob/b194f5d24a71e34f147c90e4351d80ac75be55de/src/cmd/cgo/gcc.go#L1763
113 CC="$CC_FOR_BUILD" LD="$CC_FOR_BUILD" GOOS= GOARCH= CGO_ENABLED=0 go generate ./kinds/gen.go
114 CC="$CC_FOR_BUILD" LD="$LD_FOR_BUILD" GOOS= GOARCH= CGO_ENABLED=0 go generate ./public/app/plugins/gen.go
115 '';
116
117 postBuild = ''
118 # After having built all the Go code, run the JS builders now.
119
120 # Workaround for https://github.com/nrwl/nx/issues/22445
121 faketty yarn run build
122 yarn run plugins:build-bundled
123 '';
124
125 ldflags = [
126 "-s"
127 "-w"
128 "-X main.version=${finalAttrs.version}"
129 ];
130
131 # Tests start http servers which need to bind to local addresses:
132 # panic: httptest: failed to listen on a port: listen tcp6 [::1]:0: bind: operation not permitted
133 __darwinAllowLocalNetworking = true;
134
135 # On Darwin, files under /usr/share/zoneinfo exist, but fail to open in sandbox:
136 # TestValueAsTimezone: date_formats_test.go:33: Invalid has err for input "Europe/Amsterdam": operation not permitted
137 preCheck = ''
138 export ZONEINFO=${tzdata}/share/zoneinfo
139 '';
140
141 postInstall = ''
142 mkdir -p $out/share/grafana
143 cp -r public conf $out/share/grafana/
144 '';
145
146 postFixup = ''
147 while read line; do
148 remove-references-to -t $offlineCache "$line"
149 done < <(find $out -type f -name '*.js.map' -or -name '*.js')
150 '';
151
152 passthru = {
153 tests = { inherit (nixosTests) grafana; };
154 updateScript = nix-update-script { };
155 };
156
157 meta = {
158 description = "Gorgeous metric viz, dashboards & editors for Graphite, InfluxDB & OpenTSDB";
159 license = lib.licenses.agpl3Only;
160 homepage = "https://grafana.com";
161 maintainers = with lib.maintainers; [
162 offline
163 fpletz
164 globin
165 ma27
166 Frostman
167 ryan4yin
168 ];
169 platforms = [
170 "x86_64-linux"
171 "x86_64-darwin"
172 "aarch64-linux"
173 "aarch64-darwin"
174 ];
175 mainProgram = "grafana-server";
176 };
177})