···
21
+
, threadsCross ? null # for MinGW
24
+
# threadsCross is just for MinGW
25
+
assert threadsCross != null -> stdenv.targetPlatform.isWindows;
28
+
go_bootstrap = buildPackages.callPackage ./bootstrap.nix { };
30
+
goBootstrap = runCommand "go-bootstrap" { } ''
32
+
cp -rf ${go_bootstrap}/* $out/
34
+
find $out -name "*.c" -delete
35
+
cp -rf $out/bin/* $out/share/go/bin/
38
+
goarch = platform: {
41
+
"aarch64" = "arm64";
47
+
"mipsel" = "mipsle";
48
+
"riscv64" = "riscv64";
50
+
"powerpc64le" = "ppc64le";
51
+
"mips64el" = "mips64le";
52
+
}.${platform.parsed.cpu.name} or (throw "Unsupported system: ${platform.parsed.cpu.name}");
54
+
# We need a target compiler which is still runnable at build time,
55
+
# to handle the cross-building case where build != host == target
56
+
targetCC = pkgsBuildTarget.targetPackages.stdenv.cc;
58
+
isCross = stdenv.buildPlatform != stdenv.targetPlatform;
61
+
stdenv.mkDerivation rec {
66
+
url = "https://go.dev/dl/go${version}.src.tar.gz";
67
+
sha256 = "sha256-lBnMcNxaJSPymncFPK//ZY7SHvNWHZtrAgKA686rKLk=";
71
+
# perl is used for testing go vet
72
+
nativeBuildInputs = [ perl which pkg-config procps ];
73
+
buildInputs = [ cacert pcre ]
74
+
++ lib.optionals stdenv.isLinux [ stdenv.cc.libc.out ]
75
+
++ lib.optionals (stdenv.hostPlatform.libc == "glibc") [ stdenv.cc.libc.static ];
77
+
propagatedBuildInputs = lib.optionals stdenv.isDarwin [ xcbuild ];
79
+
depsTargetTargetPropagated = lib.optionals stdenv.isDarwin [ Security Foundation ];
81
+
depsBuildTarget = lib.optional isCross targetCC;
83
+
depsTargetTarget = lib.optional (threadsCross != null) threadsCross;
85
+
hardeningDisable = [ "all" ];
88
+
patchShebangs ./ # replace /bin/bash
90
+
# This source produces shell script at run time,
91
+
# and thus it is not corrected by patchShebangs.
92
+
substituteInPlace misc/cgo/testcarchive/carchive_test.go \
93
+
--replace '#!/usr/bin/env bash' '#!${runtimeShell}'
95
+
# Patch the mimetype database location which is missing on NixOS.
96
+
# but also allow static binaries built with NixOS to run outside nix
97
+
sed -i 's,\"/etc/mime.types,"${mailcap}/etc/mime.types\"\,\n\t&,' src/mime/type_unix.go
99
+
# Disabling the 'os/http/net' tests (they want files not available in
101
+
rm src/net/{listen,parse}_test.go
102
+
rm src/syscall/exec_linux_test.go
104
+
# !!! substituteInPlace does not seems to be effective.
105
+
# The os test wants to read files in an existing path. Just don't let it be /usr/bin.
106
+
sed -i 's,/usr/bin,'"`pwd`", src/os/os_test.go
107
+
sed -i 's,/bin/pwd,'"`type -P pwd`", src/os/os_test.go
109
+
sed -i '/TestFallocate/aif true \{ return\; \}' src/cmd/link/internal/ld/fallocate_test.go
110
+
# Skip this test since ssl patches mess it up.
111
+
sed -i '/TestLoadSystemCertsLoadColonSeparatedDirs/aif true \{ return\; \}' src/crypto/x509/root_unix_test.go
112
+
# Disable another PIE test which breaks.
113
+
sed -i '/TestTrivialPIE/aif true \{ return\; \}' misc/cgo/testshared/shared_test.go
114
+
# Disable the BuildModePie test
115
+
sed -i '/TestBuildmodePIE/aif true \{ return\; \}' src/cmd/go/go_test.go
116
+
# Disable the unix socket test
117
+
sed -i '/TestShutdownUnix/aif true \{ return\; \}' src/net/net_test.go
118
+
# Disable the hostname test
119
+
sed -i '/TestHostname/aif true \{ return\; \}' src/os/os_test.go
120
+
# ParseInLocation fails the test
121
+
sed -i '/TestParseInSydney/aif true \{ return\; \}' src/time/format_test.go
122
+
# Remove the api check as it never worked
123
+
sed -i '/src\/cmd\/api\/run.go/ireturn nil' src/cmd/dist/test.go
124
+
# Remove the coverage test as we have removed this utility
125
+
sed -i '/TestCoverageWithCgo/aif true \{ return\; \}' src/cmd/go/go_test.go
126
+
# Remove the timezone naming test
127
+
sed -i '/TestLoadFixed/aif true \{ return\; \}' src/time/time_test.go
128
+
# Remove disable setgid test
129
+
sed -i '/TestRespectSetgidDir/aif true \{ return\; \}' src/cmd/go/internal/work/build_test.go
130
+
# Remove cert tests that conflict with NixOS's cert resolution
131
+
sed -i '/TestEnvVars/aif true \{ return\; \}' src/crypto/x509/root_unix_test.go
132
+
# TestWritevError hangs sometimes
133
+
sed -i '/TestWritevError/aif true \{ return\; \}' src/net/writev_test.go
134
+
# TestVariousDeadlines fails sometimes
135
+
sed -i '/TestVariousDeadlines/aif true \{ return\; \}' src/net/timeout_test.go
137
+
sed -i 's,/etc/protocols,${iana-etc}/etc/protocols,' src/net/lookup_unix.go
138
+
sed -i 's,/etc/services,${iana-etc}/etc/services,' src/net/port_unix.go
140
+
# Disable cgo lookup tests not works, they depend on resolver
141
+
rm src/net/cgo_unix_test.go
143
+
# prepend the nix path to the zoneinfo files but also leave the original value for static binaries
144
+
# that run outside a nix server
145
+
sed -i 's,\"/usr/share/zoneinfo/,"${tzdata}/share/zoneinfo/\"\,\n\t&,' src/time/zoneinfo_unix.go
147
+
'' + lib.optionalString stdenv.isAarch32 ''
148
+
echo '#!${runtimeShell}' > misc/cgo/testplugin/test.bash
149
+
'' + lib.optionalString stdenv.isDarwin ''
150
+
substituteInPlace src/race.bash --replace \
151
+
"sysctl machdep.cpu.extfeatures | grep -qv EM64T" true
152
+
sed -i 's,strings.Contains(.*sysctl.*,true {,' src/cmd/dist/util.go
153
+
sed -i 's,"/etc","'"$TMPDIR"'",' src/os/os_test.go
154
+
sed -i 's,/_go_os_test,'"$TMPDIR"'/_go_os_test,' src/os/path_test.go
156
+
sed -i '/TestChdirAndGetwd/aif true \{ return\; \}' src/os/os_test.go
157
+
sed -i '/TestCredentialNoSetGroups/aif true \{ return\; \}' src/os/exec/exec_posix_test.go
158
+
sed -i '/TestRead0/aif true \{ return\; \}' src/os/os_test.go
159
+
sed -i '/TestSystemRoots/aif true \{ return\; \}' src/crypto/x509/root_darwin_test.go
161
+
sed -i '/TestGoInstallRebuildsStalePackagesInOtherGOPATH/aif true \{ return\; \}' src/cmd/go/go_test.go
162
+
sed -i '/TestBuildDashIInstallsDependencies/aif true \{ return\; \}' src/cmd/go/go_test.go
164
+
sed -i '/TestDisasmExtld/aif true \{ return\; \}' src/cmd/objdump/objdump_test.go
166
+
sed -i 's/unrecognized/unknown/' src/cmd/link/internal/ld/lib.go
168
+
# TestCurrent fails because Current is not implemented on Darwin
169
+
sed -i 's/TestCurrent/testCurrent/g' src/os/user/user_test.go
170
+
sed -i 's/TestLookup/testLookup/g' src/os/user/user_test.go
172
+
touch $TMPDIR/group $TMPDIR/hosts $TMPDIR/passwd
176
+
./remove-tools-1.11.patch
177
+
./ssl-cert-file-1.16.patch
178
+
./remove-test-pie-1.15.patch
179
+
./skip-chown-tests-1.16.patch
180
+
./skip-external-network-tests-1.16.patch
181
+
./skip-nohup-tests.patch
182
+
./skip-cgo-tests-1.19.patch
183
+
./go_no_vendor_checks-1.16.patch
187
+
find . -name '*.orig' -exec rm {} ';'
190
+
GOOS = stdenv.targetPlatform.parsed.kernel.name;
191
+
GOARCH = goarch stdenv.targetPlatform;
192
+
# GOHOSTOS/GOHOSTARCH must match the building system, not the host system.
193
+
# Go will nevertheless build a for host system that we will copy over in
194
+
# the install phase.
195
+
GOHOSTOS = stdenv.buildPlatform.parsed.kernel.name;
196
+
GOHOSTARCH = goarch stdenv.buildPlatform;
198
+
# {CC,CXX}_FOR_TARGET must be only set for cross compilation case as go expect those
199
+
# to be different from CC/CXX
202
+
"${targetCC}/bin/${targetCC.targetPrefix}cc"
207
+
"${targetCC}/bin/${targetCC.targetPrefix}c++"
211
+
GOARM = toString (lib.intersectLists [ (stdenv.hostPlatform.parsed.cpu.version or "") ] [ "5" "6" "7" ]);
212
+
GO386 = "softfloat"; # from Arch: don't assume sse2 on i686
214
+
# Hopefully avoids test timeouts on Hydra
215
+
GO_TEST_TIMEOUT_SCALE = 3;
217
+
# Indicate that we are running on build infrastructure
218
+
# Some tests assume things like home directories and users exists
219
+
GO_BUILDER_NAME = "nix";
221
+
GOROOT_BOOTSTRAP = "${goBootstrap}/share/go";
224
+
export GOCACHE=$TMPDIR/go-cache
225
+
# this is compiled into the binary
226
+
export GOROOT_FINAL=$out/share/go
228
+
export PATH=$(pwd)/bin:$PATH
230
+
${lib.optionalString isCross ''
231
+
# Independent from host/target, CC should produce code for the building system.
232
+
# We only set it when cross-compiling.
233
+
export CC=${buildPackages.stdenv.cc}/bin/cc
239
+
(cd src && ./make.bash)
242
+
doCheck = stdenv.hostPlatform == stdenv.targetPlatform && !stdenv.isDarwin;
246
+
(cd src && HOME=$TMPDIR GOCACHE=$TMPDIR/go-cache ./run.bash --no-rebuild)
252
+
# Contains the wrong perl shebang when cross compiling,
253
+
# since it is not used for anything we can deleted as well.
254
+
rm src/regexp/syntax/make_perl_groups.pl
255
+
'' + (if (stdenv.buildPlatform != stdenv.hostPlatform) then ''
258
+
${lib.optionalString (!(GOHOSTARCH == GOARCH && GOOS == GOHOSTOS)) ''
259
+
rm -rf pkg/${GOHOSTOS}_${GOHOSTARCH} pkg/tool/${GOHOSTOS}_${GOHOSTARCH}
261
+
'' else if (stdenv.hostPlatform != stdenv.targetPlatform) then ''
263
+
${lib.optionalString (!(GOHOSTARCH == GOARCH && GOOS == GOHOSTOS)) ''
264
+
rm -rf pkg/${GOOS}_${GOARCH} pkg/tool/${GOOS}_${GOARCH}
270
+
mkdir -p $GOROOT_FINAL
271
+
cp -a bin pkg src lib misc api doc $GOROOT_FINAL
272
+
ln -s $GOROOT_FINAL/bin $out/bin
273
+
runHook postInstall
276
+
disallowedReferences = [ goBootstrap ];
279
+
homepage = "https://go.dev/";
280
+
description = "The Go Programming language";
281
+
license = licenses.bsd3;
282
+
maintainers = teams.golang.members;
283
+
platforms = platforms.linux ++ platforms.darwin;