btrbk: add mainProgram (#356350)
[NixPkgs.git] / pkgs / by-name / en / envoy / package.nix
blobc2e5025e298bee9996f1a71d009d10a8aef006d9
2   lib,
3   bazel_6,
4   bazel-gazelle,
5   buildBazelPackage,
6   fetchFromGitHub,
7   stdenv,
8   cacert,
9   cargo,
10   rustc,
11   rustPlatform,
12   cmake,
13   gn,
14   go,
15   jdk,
16   ninja,
17   patchelf,
18   python3,
19   linuxHeaders,
20   nixosTests,
22   # v8 (upstream default), wavm, wamr, wasmtime, disabled
23   wasmRuntime ? "wamr",
26 let
27   srcVer = {
28     # We need the commit hash, since Bazel stamps the build with it.
29     # However, the version string is more useful for end-users.
30     # These are contained in a attrset of their own to make it obvious that
31     # people should update both.
32     version = "1.32.0";
33     rev = "86dc7ef91ca15fb4957a74bd599397413fc26a24";
34     hash = "sha256-Wcbt62RfaNcTntmPjaAM0cP3LJangm4ht7Q0bzEpu5A=";
35   };
37   # these need to be updated for any changes to fetchAttrs
38   depsHash =
39     {
40       x86_64-linux = "sha256-LkDNPFT7UUCsGPG1dMnwzdIw0lzc5+3JYDoblF5oZVk=";
41       aarch64-linux = "sha256-DkibjmY1YND9Q2aQ41bhNdch0SKM5ghY2mjYSQfV30M=";
42     }
43     .${stdenv.system} or (throw "unsupported system ${stdenv.system}");
45 buildBazelPackage rec {
46   pname = "envoy";
47   inherit (srcVer) version;
48   bazel = bazel_6;
49   src = fetchFromGitHub {
50     owner = "envoyproxy";
51     repo = "envoy";
52     inherit (srcVer) hash rev;
54     postFetch = ''
55       chmod -R +w $out
56       rm $out/.bazelversion
57       echo ${srcVer.rev} > $out/SOURCE_VERSION
58     '';
59   };
61   postPatch = ''
62     sed -i 's,#!/usr/bin/env python3,#!${python3}/bin/python,' bazel/foreign_cc/luajit.patch
63     sed -i '/javabase=/d' .bazelrc
64     sed -i '/"-Werror"/d' bazel/envoy_internal.bzl
66     mkdir -p bazel/nix/
67     substitute ${./bazel_nix.BUILD.bazel} bazel/nix/BUILD.bazel \
68       --subst-var-by bash "$(type -p bash)"
69     ln -sf "${cargo}/bin/cargo" bazel/nix/cargo
70     ln -sf "${rustc}/bin/rustc" bazel/nix/rustc
71     ln -sf "${rustc}/bin/rustdoc" bazel/nix/rustdoc
72     ln -sf "${rustPlatform.rustLibSrc}" bazel/nix/ruststd
73     substituteInPlace bazel/dependency_imports.bzl \
74       --replace-fail 'crate_universe_dependencies()' 'crate_universe_dependencies(rust_toolchain_cargo_template="@@//bazel/nix:cargo", rust_toolchain_rustc_template="@@//bazel/nix:rustc")' \
75       --replace-fail 'crates_repository(' 'crates_repository(rust_toolchain_cargo_template="@@//bazel/nix:cargo", rust_toolchain_rustc_template="@@//bazel/nix:rustc",'
77     substitute ${./rules_rust_extra.patch} bazel/nix/rules_rust_extra.patch \
78       --subst-var-by bash "$(type -p bash)"
79     cat bazel/nix/rules_rust_extra.patch bazel/rules_rust.patch > bazel/nix/rules_rust.patch
80     mv bazel/nix/rules_rust.patch bazel/rules_rust.patch
81   '';
83   patches = [
84     # use system Python, not bazel-fetched binary Python
85     ./0001-nixpkgs-use-system-Python.patch
87     # use system Go, not bazel-fetched binary Go
88     ./0002-nixpkgs-use-system-Go.patch
90     # use system C/C++ tools
91     ./0003-nixpkgs-use-system-C-C-toolchains.patch
92   ];
94   nativeBuildInputs = [
95     cmake
96     python3
97     gn
98     go
99     jdk
100     ninja
101     patchelf
102     cacert
103   ];
105   buildInputs = [ linuxHeaders ];
107   fetchAttrs = {
108     sha256 = depsHash;
109     env.CARGO_BAZEL_REPIN = true;
110     dontUseCmakeConfigure = true;
111     dontUseGnConfigure = true;
112     postPatch = ''
113       ${postPatch}
115       substituteInPlace bazel/dependency_imports.bzl \
116         --replace-fail 'crate_universe_dependencies(' 'crate_universe_dependencies(bootstrap=True, ' \
117         --replace-fail 'crates_repository(' 'crates_repository(generator="@@cargo_bazel_bootstrap//:cargo-bazel", '
118     '';
119     preInstall = ''
120       # Strip out the path to the build location (by deleting the comment line).
121       find $bazelOut/external -name requirements.bzl | while read requirements; do
122         sed -i '/# Generated from /d' "$requirements"
123       done
125       # Remove references to paths in the Nix store.
126       sed -i \
127         -e 's,${python3},__NIXPYTHON__,' \
128         -e 's,${stdenv.shellPackage},__NIXSHELL__,' \
129         $bazelOut/external/com_github_luajit_luajit/build.py \
130         $bazelOut/external/local_config_sh/BUILD \
131         $bazelOut/external/*_pip3/BUILD.bazel
133       rm -r $bazelOut/external/go_sdk
134       rm -r $bazelOut/external/local_jdk
135       rm -r $bazelOut/external/bazel_gazelle_go_repository_tools/bin
137       # Remove compiled python
138       find $bazelOut -name '*.pyc' -delete
140       # Remove Unix timestamps from go cache.
141       rm -rf $bazelOut/external/bazel_gazelle_go_repository_cache/{gocache,pkg/mod/cache,pkg/sumdb}
143       # fix tcmalloc failure https://github.com/envoyproxy/envoy/issues/30838
144       sed -i '/TCMALLOC_GCC_FLAGS = \[/a"-Wno-changes-meaning",' $bazelOut/external/com_github_google_tcmalloc/tcmalloc/copts.bzl
146       # Install repinned rules_rust lockfile
147       cp source/extensions/dynamic_modules/sdk/rust/Cargo.Bazel.lock $bazelOut/external/Cargo.Bazel.lock
149       # Don't save cargo_bazel_bootstrap or the crate index cache
150       rm -rf $bazelOut/external/cargo_bazel_bootstrap $bazelOut/external/dynamic_modules_rust_sdk_crate_index/.cargo_home $bazelOut/external/dynamic_modules_rust_sdk_crate_index/splicing-output
151     '';
152   };
153   buildAttrs = {
154     dontUseCmakeConfigure = true;
155     dontUseGnConfigure = true;
156     dontUseNinjaInstall = true;
157     preConfigure = ''
158       # Make executables work, for the most part.
159       find $bazelOut/external -type f -executable | while read execbin; do
160         file "$execbin" | grep -q ': ELF .*, dynamically linked,' || continue
161         patchelf \
162           --set-interpreter $(cat ${stdenv.cc}/nix-support/dynamic-linker) \
163           "$execbin" || echo "$execbin"
164       done
166       ln -s ${bazel-gazelle}/bin $bazelOut/external/bazel_gazelle_go_repository_tools/bin
168       sed -i 's,#!/usr/bin/env bash,#!${stdenv.shell},' $bazelOut/external/rules_foreign_cc/foreign_cc/private/framework/toolchains/linux_commands.bzl
170       # Add paths to Nix store back.
171       sed -i \
172         -e 's,__NIXPYTHON__,${python3},' \
173         -e 's,__NIXSHELL__,${stdenv.shellPackage},' \
174         $bazelOut/external/com_github_luajit_luajit/build.py \
175         $bazelOut/external/local_config_sh/BUILD \
176         $bazelOut/external/*_pip3/BUILD.bazel
178       # Install repinned rules_rust lockfile
179       cp $bazelOut/external/Cargo.Bazel.lock source/extensions/dynamic_modules/sdk/rust/Cargo.Bazel.lock
180     '';
181     installPhase = ''
182       install -Dm0755 bazel-bin/source/exe/envoy-static $out/bin/envoy
183     '';
184   };
186   removeRulesCC = false;
187   removeLocalConfigCc = true;
188   removeLocal = false;
189   bazelTargets = [ "//source/exe:envoy-static" ];
190   bazelBuildFlags =
191     [
192       "-c opt"
193       "--spawn_strategy=standalone"
194       "--noexperimental_strict_action_env"
195       "--cxxopt=-Wno-error"
196       "--linkopt=-Wl,-z,noexecstack"
198       # Force use of system Java.
199       "--extra_toolchains=@local_jdk//:all"
200       "--java_runtime_version=local_jdk"
201       "--tool_java_runtime_version=local_jdk"
203       # Force use of system Rust.
204       "--extra_toolchains=//bazel/nix:rust_nix_aarch64,//bazel/nix:rust_nix_x86_64"
206       # undefined reference to 'grpc_core::*Metadata*::*Memento*
207       #
208       # During linking of the final binary, we see undefined references to grpc_core related symbols.
209       # The missing symbols would be instantiations of a template class from https://github.com/grpc/grpc/blob/v1.59.4/src/core/lib/transport/metadata_batch.h
210       # "ParseMemento" and "MementoToValue" are only implemented for some types
211       # and appear unused and unimplemented for the undefined cases reported by the linker.
212       "--linkopt=-Wl,--unresolved-symbols=ignore-in-object-files"
214       "--define=wasm=${wasmRuntime}"
215     ]
216     ++ (lib.optionals stdenv.hostPlatform.isAarch64 [
217       # external/com_github_google_tcmalloc/tcmalloc/internal/percpu_tcmalloc.h:611:9: error: expected ':' or '::' before '[' token
218       #   611 |       : [end_ptr] "=&r"(end_ptr), [cpu_id] "=&r"(cpu_id),
219       #       |         ^
220       "--define=tcmalloc=disabled"
221     ]);
223   bazelFetchFlags = [
224     "--define=wasm=${wasmRuntime}"
226     # Force use of system Rust.
227     "--extra_toolchains=//bazel/nix:rust_nix_aarch64,//bazel/nix:rust_nix_x86_64"
229     # https://github.com/bazelbuild/rules_go/issues/3844
230     "--repo_env=GOPROXY=https://proxy.golang.org,direct"
231     "--repo_env=GOSUMDB=sum.golang.org"
232   ];
234   requiredSystemFeatures = [ "big-parallel" ];
236   passthru.tests = {
237     envoy = nixosTests.envoy;
238     # tested as a core component of Pomerium
239     pomerium = nixosTests.pomerium;
240   };
242   meta = with lib; {
243     homepage = "https://envoyproxy.io";
244     changelog = "https://github.com/envoyproxy/envoy/releases/tag/v${version}";
245     description = "Cloud-native edge and service proxy";
246     mainProgram = "envoy";
247     license = licenses.asl20;
248     maintainers = with maintainers; [ lukegb ];
249     platforms = [
250       "x86_64-linux"
251       "aarch64-linux"
252     ];
253   };