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