nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 fetchpatch,
6 fetchurl,
7 buildPackages,
8 cmake,
9 zlib,
10 c-ares,
11 pkg-config,
12 re2,
13 openssl,
14 protobuf,
15 grpc,
16 abseil-cpp,
17 libnsl,
18
19 # tests
20 python3,
21 arrow-cpp,
22}:
23
24# This package should be updated together with all related python grpc packages
25# to ensure compatibility.
26# nixpkgs-update: no auto update
27stdenv.mkDerivation rec {
28 pname = "grpc";
29 version = "1.73.1"; # N.B: if you change this, please update:
30 # pythonPackages.grpcio
31 # pythonPackages.grpcio-channelz
32 # pythonPackages.grpcio-health-checking
33 # pythonPackages.grpcio-reflection
34 # pythonPackages.grpcio-status
35 # pythonPackages.grpcio-testing
36 # pythonPackages.grpcio-tools
37
38 src = fetchFromGitHub {
39 owner = "grpc";
40 repo = "grpc";
41 rev = "v${version}";
42 hash = "sha256-VAr+f+xqZfrP4XfCnZ9KxVTO6pHQe9gB2DgaQuen840=";
43 fetchSubmodules = true;
44 };
45
46 patches = [
47 (fetchpatch {
48 # armv6l support, https://github.com/grpc/grpc/pull/21341
49 name = "grpc-link-libatomic.patch";
50 url = "https://github.com/lopsided98/grpc/commit/a9b917666234f5665c347123d699055d8c2537b2.patch";
51 hash = "sha256-Lm0GQsz/UjBbXXEE14lT0dcRzVmCKycrlrdBJj+KLu8=";
52 })
53 ]
54 ++ lib.optionals stdenv.hostPlatform.isDarwin [
55 # fix build of 1.63.0 and newer on darwin: https://github.com/grpc/grpc/issues/36654
56 ./dynamic-lookup-darwin.patch
57 # https://github.com/grpc/grpc/issues/39170
58 (fetchurl {
59 url = "https://raw.githubusercontent.com/rdhafidh/vcpkg/0ae97b7b81562bd66ab99d022551db1449c079f9/ports/grpc/00017-add-src-upb.patch";
60 hash = "sha256-0zaJqeCM90DTtUR6xCUorahUpiJF3D/KODYkUXQh2ok=";
61 })
62 ];
63
64 nativeBuildInputs = [
65 cmake
66 pkg-config
67 ]
68 ++ lib.optional (stdenv.hostPlatform != stdenv.buildPlatform) grpc;
69 propagatedBuildInputs = [
70 c-ares
71 re2
72 zlib
73 abseil-cpp
74 ];
75 buildInputs = [
76 openssl
77 protobuf
78 ]
79 ++ lib.optionals stdenv.hostPlatform.isLinux [ libnsl ];
80
81 cmakeFlags = [
82 "-DgRPC_ZLIB_PROVIDER=package"
83 "-DgRPC_CARES_PROVIDER=package"
84 "-DgRPC_RE2_PROVIDER=package"
85 "-DgRPC_SSL_PROVIDER=package"
86 "-DgRPC_PROTOBUF_PROVIDER=package"
87 "-DgRPC_ABSL_PROVIDER=package"
88 "-DBUILD_SHARED_LIBS=ON"
89 ]
90 ++ lib.optionals (stdenv.hostPlatform != stdenv.buildPlatform) [
91 "-D_gRPC_PROTOBUF_PROTOC_EXECUTABLE=${buildPackages.protobuf}/bin/protoc"
92 "-D_gRPC_CPP_PLUGIN=${buildPackages.grpc}/bin/grpc_cpp_plugin"
93 ]
94 # The build scaffold defaults to c++14 on darwin, even when the compiler uses
95 # a more recent c++ version by default [1]. However, downgrades are
96 # problematic, because the compatibility types in abseil will have different
97 # interface definitions than the ones used for building abseil itself.
98 # [1] https://github.com/grpc/grpc/blob/v1.57.0/CMakeLists.txt#L239-L243
99 ++ (
100 let
101 defaultCxxIsOlderThan17 =
102 (stdenv.cc.isClang && lib.versionAtLeast stdenv.cc.cc.version "16.0")
103 || (stdenv.cc.isGNU && lib.versionAtLeast stdenv.cc.cc.version "11.0");
104 in
105 lib.optionals (stdenv.hostPlatform.isDarwin && defaultCxxIsOlderThan17) [
106 "-DCMAKE_CXX_STANDARD=17"
107 ]
108 );
109
110 # CMake creates a build directory by default, this conflicts with the
111 # basel BUILD file on case-insensitive filesystems.
112 preConfigure = ''
113 rm -vf BUILD
114 '';
115
116 # When natively compiling, grpc_cpp_plugin is executed from the build directory,
117 # needing to load dynamic libraries from the build directory, so we set
118 # LD_LIBRARY_PATH to enable this. When cross compiling we need to avoid this,
119 # since it can cause the grpc_cpp_plugin executable from buildPackages to
120 # crash if build and host architecture are compatible (e. g. pkgsLLVM).
121 preBuild = lib.optionalString (stdenv.hostPlatform == stdenv.buildPlatform) ''
122 export LD_LIBRARY_PATH=$(pwd)''${LD_LIBRARY_PATH:+:}$LD_LIBRARY_PATH
123 '';
124
125 env.NIX_CFLAGS_COMPILE = toString (
126 [
127 "-Wno-error"
128 ]
129 ++ lib.optionals stdenv.hostPlatform.isDarwin [
130 # Workaround for https://github.com/llvm/llvm-project/issues/48757
131 "-Wno-elaborated-enum-base"
132 ]
133 );
134
135 enableParallelBuilding = true;
136
137 passthru.tests = {
138 inherit (python3.pkgs) grpcio-status grpcio-tools jaxlib;
139 inherit arrow-cpp;
140 };
141
142 meta = {
143 description = "C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)";
144 license = lib.licenses.asl20;
145 maintainers = with lib.maintainers; [ lnl7 ];
146 homepage = "https://grpc.io/";
147 platforms = lib.platforms.all;
148 changelog = "https://github.com/grpc/grpc/releases/tag/v${version}";
149 };
150}