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