@@ -0,0 +1,303 @@
1+{ stdenv, lib, fetchFromGitHub, pkgconfig, bazel, c-ares, backward-cpp
2+, libevent, gtest, gperftools, http-parser, lightstep-tracer-cpp
3+, nghttp2, protobuf3_2, tclap, rapidjson, spdlog, boringssl, buildEnv
4+}:
5+
6+let
7+ protobuf_bzl =
8+ fetchFromGitHub {
9+ owner = "htuch";
10+ repo = "protobuf";
11+ rev = "d490587268931da78c942a6372ef57bb53db80da";
12+ sha256 = "100494s66xclw88bdnpb6d386vgw0gwz90sni37q7fqmi9w99z6v";
13+ };
14+
15+ # Based on ci/prebuilt/BUILD
16+ #
17+ # The paths have been fixed up, and the static archives have been replaced
18+ # with dynamic libraries where presently possible.
19+ #
20+ # At the moment, this doesn't _need_ to be a map that we dynamically create a
21+ # BUILD file from (we could instead just include the contents directly);
22+ # however, this sets us up to be ready if we (or upstream) decide to split
23+ # things into multiple bazel repos, instead of one.
24+ ccTargets = {
25+ ares = {
26+ pkg = c-ares;
27+ srcs = ''["lib/libcares.so"]'';
28+ hdrs = ''glob(["include/ares*.h"])'';
29+ includes = ''["include"]'';
30+ };
31+
32+ backward = {
33+ pkg = backward-cpp;
34+ hdrs = ''["include/backward.hpp"]'';
35+ includes = ''["include"]'';
36+ };
37+
38+ crypto = {
39+ pkg = boringssl;
40+ srcs = ''["lib/libcrypto.a"]'';
41+ hdrs = ''glob(["include/openssl/**/*.h"])'';
42+ includes = ''["include"]'';
43+ };
44+
45+ event = {
46+ pkg = libevent;
47+ srcs = ''["lib/libevent.so"]'';
48+ hdrs = ''glob(["include/event2/**/*.h"])'';
49+ includes = ''["include"]'';
50+ };
51+
52+ event_pthreads = {
53+ pkg = libevent;
54+ srcs = ''["lib/libevent_pthreads.so"]'';
55+ deps = ''[":event"]'';
56+ };
57+
58+ googletest = {
59+ pkg = gtest;
60+ srcs = ''[ "lib/libgmock.so", "lib/libgtest.so" ]'';
61+ hdrs = ''glob(["include/gmock/**/*.h", "include/gtest/**/*.h"])'';
62+ includes = ''["include"]'';
63+ };
64+
65+ http_parser = {
66+ pkg = http-parser;
67+ srcs = ''["lib/libhttp_parser.so"]'';
68+ hdrs = ''glob(["include/http_parser.h"])'';
69+ includes = ''["include"]'';
70+ };
71+
72+ lightstep = {
73+ pkg = lightstep-tracer-cpp;
74+ srcs = ''["lib/liblightstep_core_cxx11.a"]'';
75+ hdrs = ''glob([ "include/lightstep/**/*.h", "include/mapbox_variant/**/*.hpp" ]) + [ "include/collector.pb.h", "include/lightstep_carrier.pb.h" ]'';
76+ includes = ''["include"]'';
77+ deps = ''[":protobuf"]'';
78+ };
79+
80+ nghttp2 = {
81+ pkg = nghttp2;
82+ srcs = ''["lib/libnghttp2.so"]'';
83+ hdrs = ''glob(["include/nghttp2/**/*.h"])'';
84+ includes = ''["include"]'';
85+ };
86+
87+ protobuf = {
88+ pkg = protobuf3_2;
89+ srcs = ''glob(["lib/libproto*.so"])'';
90+ hdrs = ''glob(["include/google/protobuf/**/*.h"])'';
91+ includes = ''["include"]'';
92+ };
93+
94+ rapidjson = {
95+ pkg = rapidjson;
96+ hdrs = ''glob(["include/rapidjson/**/*.h"])'';
97+ includes = ''["include"]'';
98+ };
99+
100+ spdlog = {
101+ pkg = spdlog;
102+ name = "spdlog";
103+ hdrs = ''glob([ "include/spdlog/**/*.cc", "include/spdlog/**/*.h" ])'';
104+ includes = ''["include"]'';
105+ };
106+
107+ ssl = {
108+ pkg = boringssl;
109+ srcs = ''["lib/libssl.a"]'';
110+ deps = ''[":crypto"]'';
111+ };
112+
113+ tclap = {
114+ pkg = tclap;
115+ hdrs = ''glob(["include/tclap/**/*.h"])'';
116+ includes = ''["include"]'';
117+ };
118+
119+ tcmalloc_and_profiler = {
120+ pkg = gperftools;
121+ srcs = ''["lib/libtcmalloc_and_profiler.so"]'';
122+ hdrs = ''glob(["include/gperftools/**/*.h"])'';
123+ strip_include_prefix = ''"include"'';
124+ };
125+ };
126+
127+ # Generate the BUILD file.
128+ buildFile =
129+ let field = name: attrs:
130+ if attrs ? "${name}" then " ${name} = ${attrs.${name}},\n" else "";
131+ in
132+ ''
133+ licenses(["notice"]) # Apache 2
134+
135+ package(default_visibility = ["//visibility:public"])
136+
137+ '' +
138+ lib.concatStringsSep "\n\n" (
139+ lib.mapAttrsToList (name: value:
140+ "cc_library(\n"
141+ + " name = \"${name}\",\n"
142+ + field "srcs" value
143+ + field "hdrs" value
144+ + field "deps" value
145+ + field "includes" value
146+ + field "strip_include_prefix" value
147+ + ")"
148+ ) ccTargets
149+ ) + ''
150+
151+ filegroup(
152+ name = "protoc",
153+ srcs = ["bin/protoc"],
154+ )
155+ '';
156+
157+ workspaceFile =
158+ ''
159+ workspace(name = "nix")
160+
161+ load("//bazel:repositories.bzl", "envoy_dependencies")
162+ load("//bazel:cc_configure.bzl", "cc_configure")
163+
164+ new_local_repository(
165+ name = "nix_envoy_deps",
166+ path = "${repoEnv}",
167+ build_file = "nix_envoy_deps.BUILD"
168+ )
169+
170+ envoy_dependencies(
171+ path = "@nix_envoy_deps//",
172+ skip_protobuf_bzl = True,
173+ )
174+
175+ new_local_repository(
176+ name = "protobuf_bzl",
177+ path = "${protobuf_bzl}",
178+ # We only want protobuf.bzl, so don't support building out of this repo.
179+ build_file_content = "",
180+ )
181+
182+ cc_configure()
183+ '';
184+
185+ # The tree we'll use for our new_local_repository in our generated WORKSPACE.
186+ repoEnv = buildEnv {
187+ name = "repo-env";
188+ paths = lib.concatMap (p:
189+ lib.unique [(lib.getBin p) (lib.getLib p) (lib.getDev p)]
190+ ) allDeps;
191+ };
192+
193+ rpath = stdenv.lib.makeLibraryPath (allDeps ++ [ stdenv.cc.cc ]);
194+
195+ allDeps = [
196+ c-ares
197+ backward-cpp
198+ libevent
199+ gtest
200+ gperftools
201+ http-parser
202+ lightstep-tracer-cpp
203+ nghttp2
204+ protobuf3_2
205+ tclap
206+ rapidjson
207+ spdlog
208+ boringssl
209+ ];
210+
211+ # Envoy checks at runtime that the git sha is valid,
212+ # so we really can't avoid putting some sort of sha here.
213+ rev = "3afc7712a04907ffd25ed497626639febfe65735";
214+
215+in
216+
217+stdenv.mkDerivation rec {
218+ name = "envoy-${version}";
219+ version = "1.3.0";
220+
221+ src = fetchFromGitHub {
222+ owner = "lyft";
223+ repo = "envoy";
224+ rev = "v${version}";
225+ sha256 = "0j1c9lyvncyhiq3kyhx91ckcjd2h68x56js7xb6ni6bzxinv6zb6";
226+ };
227+
228+ nativeBuildInputs = [
229+ pkgconfig bazel
230+ ];
231+
232+ buildInputs = allDeps;
233+
234+ patches = [ ./nixos.patch ];
235+
236+ hardeningDisable = "all";
237+ dontPatchELF = true;
238+ dontStrip = true;
239+
240+ # set up our workspace,
241+ # and prevent an error where bazel/get_workspace_status tries to determine the
242+ # version by invoking git.
243+ postUnpack = ''
244+ cat <<'EOF' > $sourceRoot/WORKSPACE
245+ ${workspaceFile}
246+ EOF
247+
248+ cat <<'EOF' > $sourceRoot/nix_envoy_deps.BUILD
249+ ${buildFile}
250+ EOF
251+
252+ cat <<'EOF' > $sourceRoot/bazel/get_workspace_status
253+ #!${stdenv.shell}
254+ echo "BUILD_SCM_REVISION ${rev}"
255+ echo "BUILD_SCM_STATUS Modified"
256+ EOF
257+ '';
258+
259+ buildPhase = ''
260+ runHook preBuild
261+
262+ mkdir .home
263+ export HOME=$PWD/.home
264+
265+ BAZEL_OPTIONS="--package_path %workspace%:$PWD"
266+ BAZEL_BUILD_OPTIONS="\
267+ --strategy=Genrule=standalone \
268+ --spawn_strategy=standalone \
269+ --verbose_failures \
270+ $BAZEL_OPTIONS \
271+ --action_env=HOME \
272+ --action_env=PYTHONUSERBASE \
273+ --show_task_finish"
274+
275+ bazel \
276+ --batch \
277+ build \
278+ -s --verbose_failures \
279+ --experimental_ui \
280+ $BAZEL_BUILD_OPTIONS \
281+ -c opt \
282+ //source/exe:envoy-static
283+
284+ exe=bazel-bin/source/exe/envoy-static
285+ chmod +w $exe
286+ patchelf --set-rpath ${rpath} $exe
287+
288+ runHook postBuild
289+ '';
290+
291+ installPhase = ''
292+ mkdir -p $out/bin
293+ mv $exe $out/bin/envoy
294+ '';
295+
296+ meta = with lib; {
297+ description = "L7 proxy and communication bus designed for large modern service oriented architectures";
298+ homepage = "https://lyft.github.io/envoy/";
299+ license = licenses.asl20;
300+ platforms = platforms.linux;
301+ maintainers = with maintainers; [ cstrahan ];
302+ };
303+}