@@ -0,0 +1,244 @@
1+{ stdenv, fetchurl, tzdata, iana-etc, runCommand
2+, perl, which, pkgconfig, patch, procps, pcre, cacert, Security, Foundation
3+, mailcap, runtimeShell
4+, buildPackages, pkgsTargetTarget
5+}:
6+
7+let
8+
9+ inherit (stdenv.lib) optionals optionalString;
10+
11+ goBootstrap = runCommand "go-bootstrap" {} ''
12+ mkdir $out
13+ cp -rf ${buildPackages.go_bootstrap}/* $out/
14+ chmod -R u+w $out
15+ find $out -name "*.c" -delete
16+ cp -rf $out/bin/* $out/share/go/bin/
17+ '';
18+
19+ goarch = platform: {
20+ "i686" = "386";
21+ "x86_64" = "amd64";
22+ "aarch64" = "arm64";
23+ "arm" = "arm";
24+ "armv5tel" = "arm";
25+ "armv6l" = "arm";
26+ "armv7l" = "arm";
27+ }.${platform.parsed.cpu.name} or (throw "Unsupported system");
28+
29+in
30+
31+stdenv.mkDerivation rec {
32+ pname = "go";
33+ version = "1.13.8";
34+
35+ src = fetchurl {
36+ url = "https://dl.google.com/go/go${version}.src.tar.gz";
37+ sha256 = "0d7cxffk72568h46srzswrxd0bsdip7amgkf499wzn6l6d3g0fxi";
38+ };
39+
40+ # perl is used for testing go vet
41+ nativeBuildInputs = [ perl which pkgconfig patch procps ];
42+ buildInputs = [ cacert pcre ]
43+ ++ optionals stdenv.isLinux [ stdenv.cc.libc.out ]
44+ ++ optionals (stdenv.hostPlatform.libc == "glibc") [ stdenv.cc.libc.static ];
45+
46+ depsTargetTargetPropagated = optionals stdenv.isDarwin [ Security Foundation ];
47+
48+ hardeningDisable = [ "all" ];
49+
50+ prePatch = ''
51+ patchShebangs ./ # replace /bin/bash
52+
53+ # This source produces shell script at run time,
54+ # and thus it is not corrected by patchShebangs.
55+ substituteInPlace misc/cgo/testcarchive/carchive_test.go \
56+ --replace '#!/usr/bin/env bash' '#!${runtimeShell}'
57+
58+ # Patch the mimetype database location which is missing on NixOS.
59+ # but also allow static binaries built with NixOS to run outside nix
60+ sed -i 's,\"/etc/mime.types,"${mailcap}/etc/mime.types\"\,\n\t&,' src/mime/type_unix.go
61+
62+ # Disabling the 'os/http/net' tests (they want files not available in
63+ # chroot builds)
64+ rm src/net/{listen,parse}_test.go
65+ rm src/syscall/exec_linux_test.go
66+
67+ # !!! substituteInPlace does not seems to be effective.
68+ # The os test wants to read files in an existing path. Just don't let it be /usr/bin.
69+ sed -i 's,/usr/bin,'"`pwd`", src/os/os_test.go
70+ sed -i 's,/bin/pwd,'"`type -P pwd`", src/os/os_test.go
71+ # Disable the unix socket test
72+ sed -i '/TestShutdownUnix/aif true \{ return\; \}' src/net/net_test.go
73+ # Disable the hostname test
74+ sed -i '/TestHostname/aif true \{ return\; \}' src/os/os_test.go
75+ # ParseInLocation fails the test
76+ sed -i '/TestParseInSydney/aif true \{ return\; \}' src/time/format_test.go
77+ # Remove the api check as it never worked
78+ sed -i '/src\/cmd\/api\/run.go/ireturn nil' src/cmd/dist/test.go
79+ # Remove the coverage test as we have removed this utility
80+ sed -i '/TestCoverageWithCgo/aif true \{ return\; \}' src/cmd/go/go_test.go
81+ # Remove the timezone naming test
82+ sed -i '/TestLoadFixed/aif true \{ return\; \}' src/time/time_test.go
83+ # Remove disable setgid test
84+ sed -i '/TestRespectSetgidDir/aif true \{ return\; \}' src/cmd/go/internal/work/build_test.go
85+ # Remove cert tests that conflict with NixOS's cert resolution
86+ sed -i '/TestEnvVars/aif true \{ return\; \}' src/crypto/x509/root_unix_test.go
87+ # TestWritevError hangs sometimes
88+ sed -i '/TestWritevError/aif true \{ return\; \}' src/net/writev_test.go
89+ # TestVariousDeadlines fails sometimes
90+ sed -i '/TestVariousDeadlines/aif true \{ return\; \}' src/net/timeout_test.go
91+
92+ sed -i 's,/etc/protocols,${iana-etc}/etc/protocols,' src/net/lookup_unix.go
93+ sed -i 's,/etc/services,${iana-etc}/etc/services,' src/net/port_unix.go
94+
95+ # Disable cgo lookup tests not works, they depend on resolver
96+ rm src/net/cgo_unix_test.go
97+
98+ '' + optionalString stdenv.isLinux ''
99+ # prepend the nix path to the zoneinfo files but also leave the original value for static binaries
100+ # that run outside a nix server
101+ sed -i 's,\"/usr/share/zoneinfo/,"${tzdata}/share/zoneinfo/\"\,\n\t&,' src/time/zoneinfo_unix.go
102+
103+ '' + optionalString stdenv.isAarch32 ''
104+ echo '#!${runtimeShell}' > misc/cgo/testplugin/test.bash
105+ '' + optionalString stdenv.isDarwin ''
106+ substituteInPlace src/race.bash --replace \
107+ "sysctl machdep.cpu.extfeatures | grep -qv EM64T" true
108+ sed -i 's,strings.Contains(.*sysctl.*,true {,' src/cmd/dist/util.go
109+ sed -i 's,"/etc","'"$TMPDIR"'",' src/os/os_test.go
110+ sed -i 's,/_go_os_test,'"$TMPDIR"'/_go_os_test,' src/os/path_test.go
111+
112+ sed -i '/TestChdirAndGetwd/aif true \{ return\; \}' src/os/os_test.go
113+ sed -i '/TestCredentialNoSetGroups/aif true \{ return\; \}' src/os/exec/exec_posix_test.go
114+ sed -i '/TestRead0/aif true \{ return\; \}' src/os/os_test.go
115+ sed -i '/TestSystemRoots/aif true \{ return\; \}' src/crypto/x509/root_darwin_test.go
116+
117+ sed -i '/TestGoInstallRebuildsStalePackagesInOtherGOPATH/aif true \{ return\; \}' src/cmd/go/go_test.go
118+ sed -i '/TestBuildDashIInstallsDependencies/aif true \{ return\; \}' src/cmd/go/go_test.go
119+
120+ sed -i '/TestDisasmExtld/aif true \{ return\; \}' src/cmd/objdump/objdump_test.go
121+
122+ sed -i 's/unrecognized/unknown/' src/cmd/link/internal/ld/lib.go
123+
124+ # TestCurrent fails because Current is not implemented on Darwin
125+ sed -i 's/TestCurrent/testCurrent/g' src/os/user/user_test.go
126+ sed -i 's/TestLookup/testLookup/g' src/os/user/user_test.go
127+
128+ touch $TMPDIR/group $TMPDIR/hosts $TMPDIR/passwd
129+ '';
130+
131+ patches = [
132+ ./remove-tools-1.11.patch
133+ ./ssl-cert-file-1.13.patch
134+ ./remove-test-pie-1.13.patch
135+ ./creds-test.patch
136+ ./go-1.9-skip-flaky-19608.patch
137+ ./go-1.9-skip-flaky-20072.patch
138+ ./skip-external-network-tests.patch
139+ ./skip-nohup-tests.patch
140+ ] ++ [
141+ # breaks under load: https://github.com/golang/go/issues/25628
142+ (if stdenv.isAarch32
143+ then ./skip-test-extra-files-on-aarch32.patch
144+ else ./skip-test-extra-files-on-386.patch)
145+ ];
146+
147+ postPatch = ''
148+ find . -name '*.orig' -exec rm {} ';'
149+ '';
150+
151+ GOOS = stdenv.targetPlatform.parsed.kernel.name;
152+ GOARCH = goarch stdenv.targetPlatform;
153+ # GOHOSTOS/GOHOSTARCH must match the building system, not the host system.
154+ # Go will nevertheless build a for host system that we will copy over in
155+ # the install phase.
156+ GOHOSTOS = stdenv.buildPlatform.parsed.kernel.name;
157+ GOHOSTARCH = goarch stdenv.buildPlatform;
158+
159+ # {CC,CXX}_FOR_TARGET must be only set for cross compilation case as go expect those
160+ # to be different from CC/CXX
161+ CC_FOR_TARGET = if (stdenv.buildPlatform != stdenv.targetPlatform) then
162+ "${pkgsTargetTarget.stdenv.cc}/bin/${pkgsTargetTarget.stdenv.cc.targetPrefix}cc"
163+ else
164+ null;
165+ CXX_FOR_TARGET = if (stdenv.buildPlatform != stdenv.targetPlatform) then
166+ "${pkgsTargetTarget.stdenv.cc}/bin/${pkgsTargetTarget.stdenv.cc.targetPrefix}c++"
167+ else
168+ null;
169+
170+ GOARM = toString (stdenv.lib.intersectLists [(stdenv.hostPlatform.parsed.cpu.version or "")] ["5" "6" "7"]);
171+ GO386 = 387; # from Arch: don't assume sse2 on i686
172+ CGO_ENABLED = 1;
173+ # Hopefully avoids test timeouts on Hydra
174+ GO_TEST_TIMEOUT_SCALE = 3;
175+
176+ # Indicate that we are running on build infrastructure
177+ # Some tests assume things like home directories and users exists
178+ GO_BUILDER_NAME = "nix";
179+
180+ GOROOT_BOOTSTRAP="${goBootstrap}/share/go";
181+
182+ postConfigure = ''
183+ export GOCACHE=$TMPDIR/go-cache
184+ # this is compiled into the binary
185+ export GOROOT_FINAL=$out/share/go
186+
187+ export PATH=$(pwd)/bin:$PATH
188+
189+ # Independent from host/target, CC should produce code for the building system.
190+ export CC=${buildPackages.stdenv.cc}/bin/cc
191+ ulimit -a
192+ '';
193+
194+ postBuild = ''
195+ (cd src && ./make.bash)
196+ '';
197+
198+ doCheck = stdenv.hostPlatform == stdenv.targetPlatform && !stdenv.isDarwin;
199+
200+ checkPhase = ''
201+ runHook preCheck
202+ (cd src && HOME=$TMPDIR GOCACHE=$TMPDIR/go-cache ./run.bash --no-rebuild)
203+ runHook postCheck
204+ '';
205+
206+ preInstall = ''
207+ rm -r pkg/obj
208+ # Contains the wrong perl shebang when cross compiling,
209+ # since it is not used for anything we can deleted as well.
210+ rm src/regexp/syntax/make_perl_groups.pl
211+ '' + (if (stdenv.buildPlatform != stdenv.hostPlatform) then ''
212+ mv bin/*_*/* bin
213+ rmdir bin/*_*
214+ ${optionalString (!(GOHOSTARCH == GOARCH && GOOS == GOHOSTOS)) ''
215+ rm -rf pkg/${GOHOSTOS}_${GOHOSTARCH} pkg/tool/${GOHOSTOS}_${GOHOSTARCH}
216+ ''}
217+ '' else if (stdenv.hostPlatform != stdenv.targetPlatform) then ''
218+ rm -rf bin/*_*
219+ ${optionalString (!(GOHOSTARCH == GOARCH && GOOS == GOHOSTOS)) ''
220+ rm -rf pkg/${GOOS}_${GOARCH} pkg/tool/${GOOS}_${GOARCH}
221+ ''}
222+ '' else "");
223+
224+ installPhase = ''
225+ runHook preInstall
226+ mkdir -p $GOROOT_FINAL
227+ cp -a bin pkg src lib misc api doc $GOROOT_FINAL
228+ ln -s $GOROOT_FINAL/bin $out/bin
229+ runHook postInstall
230+ '';
231+
232+ setupHook = ./setup-hook.sh;
233+
234+ disallowedReferences = [ goBootstrap ];
235+
236+ meta = with stdenv.lib; {
237+ branch = "1.13";
238+ homepage = "http://golang.org/";
239+ description = "The Go Programming language";
240+ license = licenses.bsd3;
241+ maintainers = with maintainers; [ cstrahan orivej mic92 rvolosatovs kalbasit Frostman ];
242+ platforms = platforms.linux ++ platforms.darwin;
243+ };
244+}