@@ -0,0 +1,101 @@
1+{ stdenv
2+, buildBazelPackage
3+, fetchFromGitHub
4+, cacert
5+, git
6+, glibcLocales
7+, go
8+, iproute
9+, iptables
10+, makeWrapper
11+, procps
12+, python3
13+}:
14+
15+let
16+ preBuild = ''
17+ patchShebangs .
18+
19+ # Tell rules_go to use the Go binary found in the PATH
20+ sed -E -i \
21+ -e 's|go_version\s*=\s*"[^"]+",|go_version = "host",|g' \
22+ WORKSPACE
23+
24+ # The gazelle Go tooling needs CA certs
25+ export SSL_CERT_FILE="${cacert}/etc/ssl/certs/ca-bundle.crt"
26+
27+ # If we don't reset our GOPATH, the rules_go stdlib builder tries to
28+ # install something into it. Ideally that wouldn't happen, but for now we
29+ # can also get around it by unsetting GOPATH entirely, since rules_go
30+ # doesn't need it.
31+ export GOPATH=
32+ '';
33+
34+in buildBazelPackage rec {
35+ name = "gvisor-${version}";
36+ version = "2019-11-14";
37+
38+ src = fetchFromGitHub {
39+ owner = "google";
40+ repo = "gvisor";
41+ rev = "release-20191114.0";
42+ sha256 = "0kyixjjlws9iz2r2srgpdd4rrq94vpxkmh2rmmzxd9mcqy2i9bg1";
43+ };
44+
45+ nativeBuildInputs = [ git glibcLocales go makeWrapper python3 ];
46+
47+ bazelTarget = "//runsc:runsc";
48+
49+ # gvisor uses the Starlark implementation of rules_cc, not the built-in one,
50+ # so we shouldn't delete it from our dependencies.
51+ removeRulesCC = false;
52+
53+ fetchAttrs = {
54+ inherit preBuild;
55+
56+ preInstall = ''
57+ # Remove the go_sdk (it's just a copy of the go derivation) and all
58+ # references to it from the marker files. Bazel does not need to download
59+ # this sdk because we have patched the WORKSPACE file to point to the one
60+ # currently present in PATH. Without removing the go_sdk from the marker
61+ # file, the hash of it will change anytime the Go derivation changes and
62+ # that would lead to impurities in the marker files which would result in
63+ # a different sha256 for the fetch phase.
64+ rm -rf $bazelOut/external/{go_sdk,\@go_sdk.marker}
65+
66+ # Remove the gazelle tools, they contain go binaries that are built
67+ # non-deterministically. As long as the gazelle version matches the tools
68+ # should be equivalent.
69+ rm -rf $bazelOut/external/{bazel_gazelle_go_repository_tools,\@bazel_gazelle_go_repository_tools.marker}
70+
71+ # Remove the gazelle repository cache
72+ chmod -R +w $bazelOut/external/bazel_gazelle_go_repository_cache
73+ rm -rf $bazelOut/external/{bazel_gazelle_go_repository_cache,\@bazel_gazelle_go_repository_cache.marker}
74+
75+ # Remove log file(s)
76+ rm -f "$bazelOut"/java.log "$bazelOut"/java.log.*
77+ '';
78+
79+ sha256 = "122qk6iv8hd7g2a84y9aqqhij4r0m47vpxzbqhhh6k5livc73qd6";
80+ };
81+
82+ buildAttrs = {
83+ inherit preBuild;
84+
85+ installPhase = ''
86+ install -Dm755 bazel-bin/runsc/*_pure_stripped/runsc $out/bin/runsc
87+
88+ # Needed for the 'runsc do' subcomand
89+ wrapProgram $out/bin/runsc \
90+ --prefix PATH : ${stdenv.lib.makeBinPath [ iproute iptables procps ]}
91+ '';
92+ };
93+
94+ meta = with stdenv.lib; {
95+ description = "Container Runtime Sandbox";
96+ homepage = https://github.com/google/gvisor;
97+ license = licenses.asl20;
98+ maintainers = with maintainers; [ andrew-d ];
99+ platforms = [ "x86_64-linux" ];
100+ };
101+}