nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib
2, stdenv
3, fetchpatch
4, fetchurl
5, tzdata
6, substituteAll
7, iana-etc
8, Security
9, Foundation
10, xcbuild
11, mailcap
12, buildPackages
13, pkgsBuildTarget
14, threadsCross
15, testers
16, skopeo
17, buildGo119Module
18}:
19
20let
21 useGccGoBootstrap = stdenv.buildPlatform.isMusl || stdenv.buildPlatform.isRiscV;
22 goBootstrap = if useGccGoBootstrap then buildPackages.gccgo12 else buildPackages.callPackage ./bootstrap116.nix { };
23
24 skopeoTest = skopeo.override { buildGoModule = buildGo119Module; };
25
26 goarch = platform: {
27 "aarch64" = "arm64";
28 "arm" = "arm";
29 "armv5tel" = "arm";
30 "armv6l" = "arm";
31 "armv7l" = "arm";
32 "i686" = "386";
33 "mips" = "mips";
34 "mips64el" = "mips64le";
35 "mipsel" = "mipsle";
36 "powerpc64le" = "ppc64le";
37 "riscv64" = "riscv64";
38 "s390x" = "s390x";
39 "x86_64" = "amd64";
40 }.${platform.parsed.cpu.name} or (throw "Unsupported system: ${platform.parsed.cpu.name}");
41
42 # We need a target compiler which is still runnable at build time,
43 # to handle the cross-building case where build != host == target
44 targetCC = pkgsBuildTarget.targetPackages.stdenv.cc;
45
46 isCross = stdenv.buildPlatform != stdenv.targetPlatform;
47in
48stdenv.mkDerivation rec {
49 pname = "go";
50 version = "1.19.9";
51
52 src = fetchurl {
53 url = "https://go.dev/dl/go${version}.src.tar.gz";
54 hash = "sha256-ExGQpGl6cMWx0jLfXT9Vo/nsDnjkBRYZb/s/Ca5qV0Q=";
55 };
56
57 strictDeps = true;
58 buildInputs = [ ]
59 ++ lib.optionals stdenv.isLinux [ stdenv.cc.libc.out ]
60 ++ lib.optionals (stdenv.hostPlatform.libc == "glibc") [ stdenv.cc.libc.static ];
61
62 depsTargetTargetPropagated = lib.optionals stdenv.targetPlatform.isDarwin [ Foundation Security xcbuild ];
63
64 depsBuildTarget = lib.optional isCross targetCC;
65
66 depsTargetTarget = lib.optional stdenv.targetPlatform.isWindows threadsCross.package;
67
68 postPatch = ''
69 patchShebangs .
70 '';
71
72 patches = [
73 (substituteAll {
74 src = ./iana-etc-1.17.patch;
75 iana = iana-etc;
76 })
77 # Patch the mimetype database location which is missing on NixOS.
78 # but also allow static binaries built with NixOS to run outside nix
79 (substituteAll {
80 src = ./mailcap-1.17.patch;
81 inherit mailcap;
82 })
83 # prepend the nix path to the zoneinfo files but also leave the original value for static binaries
84 # that run outside a nix server
85 (substituteAll {
86 src = ./tzdata-1.19.patch;
87 inherit tzdata;
88 })
89 ./remove-tools-1.11.patch
90 ./go_no_vendor_checks-1.16.patch
91
92 # runtime: support riscv64 SV57 mode
93 (fetchpatch {
94 url = "https://github.com/golang/go/commit/1e3c19f3fee12e5e2b7802a54908a4d4d03960da.patch";
95 sha256 = "sha256-mk/9gXwQEcAkiRemF6GiNU0c0fhDR29/YcKgQR7ONTA=";
96 })
97 ];
98
99 GOOS = stdenv.targetPlatform.parsed.kernel.name;
100 GOARCH = goarch stdenv.targetPlatform;
101 # GOHOSTOS/GOHOSTARCH must match the building system, not the host system.
102 # Go will nevertheless build a for host system that we will copy over in
103 # the install phase.
104 GOHOSTOS = stdenv.buildPlatform.parsed.kernel.name;
105 GOHOSTARCH = goarch stdenv.buildPlatform;
106
107 # {CC,CXX}_FOR_TARGET must be only set for cross compilation case as go expect those
108 # to be different from CC/CXX
109 CC_FOR_TARGET =
110 if isCross then
111 "${targetCC}/bin/${targetCC.targetPrefix}cc"
112 else
113 null;
114 CXX_FOR_TARGET =
115 if isCross then
116 "${targetCC}/bin/${targetCC.targetPrefix}c++"
117 else
118 null;
119
120 GOARM = toString (lib.intersectLists [ (stdenv.hostPlatform.parsed.cpu.version or "") ] [ "5" "6" "7" ]);
121 GO386 = "softfloat"; # from Arch: don't assume sse2 on i686
122 CGO_ENABLED = 1;
123
124 GOROOT_BOOTSTRAP = if useGccGoBootstrap then goBootstrap else "${goBootstrap}/share/go";
125
126 buildPhase = ''
127 runHook preBuild
128 export GOCACHE=$TMPDIR/go-cache
129 # this is compiled into the binary
130 export GOROOT_FINAL=$out/share/go
131
132 export PATH=$(pwd)/bin:$PATH
133
134 ${lib.optionalString isCross ''
135 # Independent from host/target, CC should produce code for the building system.
136 # We only set it when cross-compiling.
137 export CC=${buildPackages.stdenv.cc}/bin/cc
138 ''}
139 ulimit -a
140
141 pushd src
142 ./make.bash
143 popd
144 runHook postBuild
145 '';
146
147 preInstall = ''
148 rm -r pkg/obj
149 # Contains the wrong perl shebang when cross compiling,
150 # since it is not used for anything we can deleted as well.
151 rm src/regexp/syntax/make_perl_groups.pl
152 '' + (if (stdenv.buildPlatform.system != stdenv.hostPlatform.system) then ''
153 mv bin/*_*/* bin
154 rmdir bin/*_*
155 ${lib.optionalString (!(GOHOSTARCH == GOARCH && GOOS == GOHOSTOS)) ''
156 rm -rf pkg/${GOHOSTOS}_${GOHOSTARCH} pkg/tool/${GOHOSTOS}_${GOHOSTARCH}
157 ''}
158 '' else lib.optionalString (stdenv.hostPlatform.system != stdenv.targetPlatform.system) ''
159 rm -rf bin/*_*
160 ${lib.optionalString (!(GOHOSTARCH == GOARCH && GOOS == GOHOSTOS)) ''
161 rm -rf pkg/${GOOS}_${GOARCH} pkg/tool/${GOOS}_${GOARCH}
162 ''}
163 '');
164
165 installPhase = ''
166 runHook preInstall
167 mkdir -p $GOROOT_FINAL
168 cp -a bin pkg src lib misc api doc $GOROOT_FINAL
169 ln -s $GOROOT_FINAL/bin $out/bin
170 runHook postInstall
171 '';
172
173 disallowedReferences = [ goBootstrap ];
174
175 passthru = {
176 inherit goBootstrap skopeoTest;
177 tests = {
178 skopeo = testers.testVersion { package = skopeoTest; };
179 };
180 };
181
182 meta = with lib; {
183 changelog = "https://go.dev/doc/devel/release#go${lib.versions.majorMinor version}";
184 description = "The Go Programming language";
185 homepage = "https://go.dev/";
186 license = licenses.bsd3;
187 maintainers = teams.golang.members;
188 platforms = platforms.darwin ++ platforms.linux;
189 };
190}