lol
1{ lib
2, stdenv
3, fetchurl
4, tzdata
5, substituteAll
6, iana-etc
7, Security
8, Foundation
9, xcbuild
10, mailcap
11, buildPackages
12, pkgsBuildTarget
13, threadsCross
14, testers
15, skopeo
16, buildGo122Module
17}:
18
19let
20 useGccGoBootstrap = stdenv.buildPlatform.isMusl;
21 goBootstrap = if useGccGoBootstrap then buildPackages.gccgo12 else buildPackages.callPackage ./bootstrap121.nix { };
22
23 skopeoTest = skopeo.override { buildGoModule = buildGo122Module; };
24
25 goarch = platform: {
26 "aarch64" = "arm64";
27 "arm" = "arm";
28 "armv5tel" = "arm";
29 "armv6l" = "arm";
30 "armv7l" = "arm";
31 "i686" = "386";
32 "mips" = "mips";
33 "mips64el" = "mips64le";
34 "mipsel" = "mipsle";
35 "powerpc64" = "ppc64";
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 (finalAttrs: {
49 pname = "go";
50 version = "1.22.3";
51
52 src = fetchurl {
53 url = "https://go.dev/dl/go${finalAttrs.version}.src.tar.gz";
54 hash = "sha256-gGSO80+QMZPXKlnA3/AZ9fmK4MmqE63gsOy/+ZGnb2g=";
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.22.patch
91 ];
92
93 GOOS = stdenv.targetPlatform.parsed.kernel.name;
94 GOARCH = goarch stdenv.targetPlatform;
95 # GOHOSTOS/GOHOSTARCH must match the building system, not the host system.
96 # Go will nevertheless build a for host system that we will copy over in
97 # the install phase.
98 GOHOSTOS = stdenv.buildPlatform.parsed.kernel.name;
99 GOHOSTARCH = goarch stdenv.buildPlatform;
100
101 # {CC,CXX}_FOR_TARGET must be only set for cross compilation case as go expect those
102 # to be different from CC/CXX
103 CC_FOR_TARGET =
104 if isCross then
105 "${targetCC}/bin/${targetCC.targetPrefix}cc"
106 else
107 null;
108 CXX_FOR_TARGET =
109 if isCross then
110 "${targetCC}/bin/${targetCC.targetPrefix}c++"
111 else
112 null;
113
114 GOARM = toString (lib.intersectLists [ (stdenv.hostPlatform.parsed.cpu.version or "") ] [ "5" "6" "7" ]);
115 GO386 = "softfloat"; # from Arch: don't assume sse2 on i686
116 CGO_ENABLED = 1;
117
118 GOROOT_BOOTSTRAP = if useGccGoBootstrap then goBootstrap else "${goBootstrap}/share/go";
119
120 buildPhase = ''
121 runHook preBuild
122 export GOCACHE=$TMPDIR/go-cache
123 # this is compiled into the binary
124 export GOROOT_FINAL=$out/share/go
125
126 export PATH=$(pwd)/bin:$PATH
127
128 ${lib.optionalString isCross ''
129 # Independent from host/target, CC should produce code for the building system.
130 # We only set it when cross-compiling.
131 export CC=${buildPackages.stdenv.cc}/bin/cc
132 ''}
133 ulimit -a
134
135 pushd src
136 ./make.bash
137 popd
138 runHook postBuild
139 '';
140
141 preInstall = ''
142 # Contains the wrong perl shebang when cross compiling,
143 # since it is not used for anything we can deleted as well.
144 rm src/regexp/syntax/make_perl_groups.pl
145 '' + (if (stdenv.buildPlatform.system != stdenv.hostPlatform.system) then ''
146 mv bin/*_*/* bin
147 rmdir bin/*_*
148 ${lib.optionalString (!(finalAttrs.GOHOSTARCH == finalAttrs.GOARCH && finalAttrs.GOOS == finalAttrs.GOHOSTOS)) ''
149 rm -rf pkg/${finalAttrs.GOHOSTOS}_${finalAttrs.GOHOSTARCH} pkg/tool/${finalAttrs.GOHOSTOS}_${finalAttrs.GOHOSTARCH}
150 ''}
151 '' else lib.optionalString (stdenv.hostPlatform.system != stdenv.targetPlatform.system) ''
152 rm -rf bin/*_*
153 ${lib.optionalString (!(finalAttrs.GOHOSTARCH == finalAttrs.GOARCH && finalAttrs.GOOS == finalAttrs.GOHOSTOS)) ''
154 rm -rf pkg/${finalAttrs.GOOS}_${finalAttrs.GOARCH} pkg/tool/${finalAttrs.GOOS}_${finalAttrs.GOARCH}
155 ''}
156 '');
157
158 installPhase = ''
159 runHook preInstall
160 mkdir -p $GOROOT_FINAL
161 cp -a bin pkg src lib misc api doc go.env $GOROOT_FINAL
162 mkdir -p $out/bin
163 ln -s $GOROOT_FINAL/bin/* $out/bin
164 runHook postInstall
165 '';
166
167 disallowedReferences = [ goBootstrap ];
168
169 passthru = {
170 inherit goBootstrap skopeoTest;
171 tests = {
172 skopeo = testers.testVersion { package = skopeoTest; };
173 version = testers.testVersion {
174 package = finalAttrs.finalPackage;
175 command = "go version";
176 version = "go${finalAttrs.version}";
177 };
178 };
179 };
180
181 meta = with lib; {
182 changelog = "https://go.dev/doc/devel/release#go${lib.versions.majorMinor finalAttrs.version}";
183 description = "The Go Programming language";
184 homepage = "https://go.dev/";
185 license = licenses.bsd3;
186 maintainers = teams.golang.members;
187 platforms = platforms.darwin ++ platforms.linux;
188 mainProgram = "go";
189 };
190})