1 { lib, stdenv, fetchurl, tzdata, iana-etc, runCommand
2 , perl, which, pkg-config, patch, procps, pcre, cacert, Security, Foundation
3 , mailcap, runtimeShell
12 inherit (lib) optionals optionalString;
16 go_bootstrap = buildPackages.callPackage ./bootstrap.nix { };
18 goBootstrap = runCommand "go-bootstrap" {} ''
20 cp -rf ${go_bootstrap}/* $out/
22 find $out -name "*.c" -delete
23 cp -rf $out/bin/* $out/share/go/bin/
34 "powerpc64le" = "ppc64le";
35 }.${platform.parsed.cpu.name} or (throw "Unsupported system");
37 # We need a target compiler which is still runnable at build time,
38 # to handle the cross-building case where build != host == target
39 targetCC = pkgsBuildTarget.targetPackages.stdenv.cc;
42 stdenv.mkDerivation rec {
47 url = "https://dl.google.com/go/go${version}.src.tar.gz";
48 sha256 = "0jci03f5z09xibbdqg4lnv2k3crhal1phzwr6lc4ajp514i3plby";
51 # perl is used for testing go vet
52 nativeBuildInputs = [ perl which pkg-config patch procps ];
53 buildInputs = [ cacert pcre ]
54 ++ optionals stdenv.isLinux [ stdenv.cc.libc.out ]
55 ++ optionals (stdenv.hostPlatform.libc == "glibc") [ stdenv.cc.libc.static ];
57 depsTargetTargetPropagated = optionals stdenv.isDarwin [ Security Foundation ];
59 hardeningDisable = [ "all" ];
62 patchShebangs ./ # replace /bin/bash
64 # This source produces shell script at run time,
65 # and thus it is not corrected by patchShebangs.
66 substituteInPlace misc/cgo/testcarchive/carchive_test.go \
67 --replace '#!/usr/bin/env bash' '#!${runtimeShell}'
69 # Patch the mimetype database location which is missing on NixOS.
70 # but also allow static binaries built with NixOS to run outside nix
71 sed -i 's,\"/etc/mime.types,"${mailcap}/etc/mime.types\"\,\n\t&,' src/mime/type_unix.go
73 # Disabling the 'os/http/net' tests (they want files not available in
75 rm src/net/{listen,parse}_test.go
76 rm src/syscall/exec_linux_test.go
78 # !!! substituteInPlace does not seems to be effective.
79 # The os test wants to read files in an existing path. Just don't let it be /usr/bin.
80 sed -i 's,/usr/bin,'"`pwd`", src/os/os_test.go
81 sed -i 's,/bin/pwd,'"`type -P pwd`", src/os/os_test.go
82 # Disable the unix socket test
83 sed -i '/TestShutdownUnix/aif true \{ return\; \}' src/net/net_test.go
84 # Disable the hostname test
85 sed -i '/TestHostname/aif true \{ return\; \}' src/os/os_test.go
86 # ParseInLocation fails the test
87 sed -i '/TestParseInSydney/aif true \{ return\; \}' src/time/format_test.go
88 # Remove the api check as it never worked
89 sed -i '/src\/cmd\/api\/run.go/ireturn nil' src/cmd/dist/test.go
90 # Remove the coverage test as we have removed this utility
91 sed -i '/TestCoverageWithCgo/aif true \{ return\; \}' src/cmd/go/go_test.go
92 # Remove the timezone naming test
93 sed -i '/TestLoadFixed/aif true \{ return\; \}' src/time/time_test.go
94 # Remove disable setgid test
95 sed -i '/TestRespectSetgidDir/aif true \{ return\; \}' src/cmd/go/internal/work/build_test.go
96 # Remove cert tests that conflict with NixOS's cert resolution
97 sed -i '/TestEnvVars/aif true \{ return\; \}' src/crypto/x509/root_unix_test.go
98 # TestWritevError hangs sometimes
99 sed -i '/TestWritevError/aif true \{ return\; \}' src/net/writev_test.go
100 # TestVariousDeadlines fails sometimes
101 sed -i '/TestVariousDeadlines/aif true \{ return\; \}' src/net/timeout_test.go
103 sed -i 's,/etc/protocols,${iana-etc}/etc/protocols,' src/net/lookup_unix.go
104 sed -i 's,/etc/services,${iana-etc}/etc/services,' src/net/port_unix.go
106 # Disable cgo lookup tests not works, they depend on resolver
107 rm src/net/cgo_unix_test.go
109 '' + optionalString stdenv.isLinux ''
110 # prepend the nix path to the zoneinfo files but also leave the original value for static binaries
111 # that run outside a nix server
112 sed -i 's,\"/usr/share/zoneinfo/,"${tzdata}/share/zoneinfo/\"\,\n\t&,' src/time/zoneinfo_unix.go
114 '' + optionalString stdenv.isAarch32 ''
115 echo '#!${runtimeShell}' > misc/cgo/testplugin/test.bash
116 '' + optionalString stdenv.isDarwin ''
117 substituteInPlace src/race.bash --replace \
118 "sysctl machdep.cpu.extfeatures | grep -qv EM64T" true
119 sed -i 's,strings.Contains(.*sysctl.*,true {,' src/cmd/dist/util.go
120 sed -i 's,"/etc","'"$TMPDIR"'",' src/os/os_test.go
121 sed -i 's,/_go_os_test,'"$TMPDIR"'/_go_os_test,' src/os/path_test.go
123 sed -i '/TestChdirAndGetwd/aif true \{ return\; \}' src/os/os_test.go
124 sed -i '/TestCredentialNoSetGroups/aif true \{ return\; \}' src/os/exec/exec_posix_test.go
125 sed -i '/TestRead0/aif true \{ return\; \}' src/os/os_test.go
126 sed -i '/TestSystemRoots/aif true \{ return\; \}' src/crypto/x509/root_darwin_test.go
128 sed -i '/TestGoInstallRebuildsStalePackagesInOtherGOPATH/aif true \{ return\; \}' src/cmd/go/go_test.go
129 sed -i '/TestBuildDashIInstallsDependencies/aif true \{ return\; \}' src/cmd/go/go_test.go
131 sed -i '/TestDisasmExtld/aif true \{ return\; \}' src/cmd/objdump/objdump_test.go
133 sed -i 's/unrecognized/unknown/' src/cmd/link/internal/ld/lib.go
135 # TestCurrent fails because Current is not implemented on Darwin
136 sed -i 's/TestCurrent/testCurrent/g' src/os/user/user_test.go
137 sed -i 's/TestLookup/testLookup/g' src/os/user/user_test.go
139 touch $TMPDIR/group $TMPDIR/hosts $TMPDIR/passwd
143 ./remove-tools-1.11.patch
144 ./ssl-cert-file-1.13.patch
145 ./remove-test-pie-1.14.patch
147 ./go-1.9-skip-flaky-19608.patch
148 ./go-1.9-skip-flaky-20072.patch
149 ./skip-external-network-tests.patch
150 ./skip-nohup-tests.patch
151 ./go_no_vendor_checks-1_14.patch
153 # support TZ environment variable starting with colon
155 name = "tz-support-colon.patch";
156 url = "https://github.com/golang/go/commit/58fe2cd4022c77946ce4b598cf3e30ccc8367143.patch";
157 sha256 = "0vphwiqrm0qykfj3rfayr65qzk22fksg7qkamvaz0lmf6fqvbd2f";
160 # fix rare TestDontCacheBrokenHTTP2Conn failure
162 url = "https://github.com/golang/go/commit/ea1437a8cdf6bb3c2d2447833a5d06dbd75f7ae4.patch";
163 sha256 = "1lyzy4nf8c34a966vw45j3j7hzpvncq2gqspfxffzkyh17xd8sgy";
166 # breaks under load: https://github.com/golang/go/issues/25628
168 then ./skip-test-extra-files-on-aarch32-1.14.patch
169 else ./skip-test-extra-files-on-386-1.14.patch)
173 find . -name '*.orig' -exec rm {} ';'
176 GOOS = stdenv.targetPlatform.parsed.kernel.name;
177 GOARCH = goarch stdenv.targetPlatform;
178 # GOHOSTOS/GOHOSTARCH must match the building system, not the host system.
179 # Go will nevertheless build a for host system that we will copy over in
181 GOHOSTOS = stdenv.buildPlatform.parsed.kernel.name;
182 GOHOSTARCH = goarch stdenv.buildPlatform;
184 # {CC,CXX}_FOR_TARGET must be only set for cross compilation case as go expect those
185 # to be different from CC/CXX
186 CC_FOR_TARGET = if (stdenv.buildPlatform != stdenv.targetPlatform) then
187 "${targetCC}/bin/${targetCC.targetPrefix}cc"
190 CXX_FOR_TARGET = if (stdenv.buildPlatform != stdenv.targetPlatform) then
191 "${targetCC}/bin/${targetCC.targetPrefix}c++"
195 GOARM = toString (lib.intersectLists [(stdenv.hostPlatform.parsed.cpu.version or "")] ["5" "6" "7"]);
196 GO386 = 387; # from Arch: don't assume sse2 on i686
198 # Hopefully avoids test timeouts on Hydra
199 GO_TEST_TIMEOUT_SCALE = 3;
201 # Indicate that we are running on build infrastructure
202 # Some tests assume things like home directories and users exists
203 GO_BUILDER_NAME = "nix";
205 GOROOT_BOOTSTRAP="${goBootstrap}/share/go";
208 export GOCACHE=$TMPDIR/go-cache
209 # this is compiled into the binary
210 export GOROOT_FINAL=$out/share/go
212 export PATH=$(pwd)/bin:$PATH
214 ${optionalString (stdenv.buildPlatform != stdenv.targetPlatform) ''
215 # Independent from host/target, CC should produce code for the building system.
216 # We only set it when cross-compiling.
217 export CC=${buildPackages.stdenv.cc}/bin/cc
223 (cd src && ./make.bash)
226 doCheck = stdenv.hostPlatform == stdenv.targetPlatform && !stdenv.isDarwin;
230 (cd src && HOME=$TMPDIR GOCACHE=$TMPDIR/go-cache ./run.bash --no-rebuild)
236 # Contains the wrong perl shebang when cross compiling,
237 # since it is not used for anything we can deleted as well.
238 rm src/regexp/syntax/make_perl_groups.pl
239 '' + (if (stdenv.buildPlatform != stdenv.hostPlatform) then ''
242 ${optionalString (!(GOHOSTARCH == GOARCH && GOOS == GOHOSTOS)) ''
243 rm -rf pkg/${GOHOSTOS}_${GOHOSTARCH} pkg/tool/${GOHOSTOS}_${GOHOSTARCH}
245 '' else if (stdenv.hostPlatform != stdenv.targetPlatform) then ''
247 ${optionalString (!(GOHOSTARCH == GOARCH && GOOS == GOHOSTOS)) ''
248 rm -rf pkg/${GOOS}_${GOARCH} pkg/tool/${GOOS}_${GOARCH}
254 mkdir -p $GOROOT_FINAL
255 cp -a bin pkg src lib misc api doc $GOROOT_FINAL
256 ln -s $GOROOT_FINAL/bin $out/bin
260 disallowedReferences = [ goBootstrap ];
263 homepage = "http://golang.org/";
264 description = "The Go Programming language";
265 license = licenses.bsd3;
266 maintainers = teams.golang.members;
267 platforms = platforms.linux ++ platforms.darwin;
268 knownVulnerabilities = [
269 "Support for Go 1.14 ended with the release of Go 1.16: https://golang.org/doc/devel/release.html#policy"