1{
2 lib,
3 stdenv,
4 fetchurl,
5 tzdata,
6 replaceVars,
7 iana-etc,
8 mailcap,
9 buildPackages,
10 pkgsBuildTarget,
11 targetPackages,
12 testers,
13 skopeo,
14 buildGo124Module,
15}:
16
17let
18 goBootstrap = buildPackages.callPackage ./bootstrap122.nix { };
19
20 skopeoTest = skopeo.override { buildGoModule = buildGo124Module; };
21
22 # We need a target compiler which is still runnable at build time,
23 # to handle the cross-building case where build != host == target
24 targetCC = pkgsBuildTarget.targetPackages.stdenv.cc;
25
26 isCross = stdenv.buildPlatform != stdenv.targetPlatform;
27in
28stdenv.mkDerivation (finalAttrs: {
29 pname = "go";
30 version = "1.24.7";
31
32 src = fetchurl {
33 url = "https://go.dev/dl/go${finalAttrs.version}.src.tar.gz";
34 hash = "sha256-Ko9Q2w+IgDYHxQ1+qINNy3vUg8a0KKkeNg/fhiS0ZGQ=";
35 };
36
37 strictDeps = true;
38 buildInputs =
39 [ ]
40 ++ lib.optionals stdenv.hostPlatform.isLinux [ stdenv.cc.libc.out ]
41 ++ lib.optionals (stdenv.hostPlatform.libc == "glibc") [ stdenv.cc.libc.static ];
42
43 depsBuildTarget = lib.optional isCross targetCC;
44
45 depsTargetTarget = lib.optional stdenv.targetPlatform.isMinGW targetPackages.threads.package;
46
47 postPatch = ''
48 patchShebangs .
49 '';
50
51 patches = [
52 (replaceVars ./iana-etc-1.17.patch {
53 iana = iana-etc;
54 })
55 # Patch the mimetype database location which is missing on NixOS.
56 # but also allow static binaries built with NixOS to run outside nix
57 (replaceVars ./mailcap-1.17.patch {
58 inherit mailcap;
59 })
60 # prepend the nix path to the zoneinfo files but also leave the original value for static binaries
61 # that run outside a nix server
62 (replaceVars ./tzdata-1.19.patch {
63 inherit tzdata;
64 })
65 ./remove-tools-1.11.patch
66 ./go_no_vendor_checks-1.23.patch
67 ];
68
69 inherit (stdenv.targetPlatform.go) GOOS GOARCH GOARM;
70 # GOHOSTOS/GOHOSTARCH must match the building system, not the host system.
71 # Go will nevertheless build a for host system that we will copy over in
72 # the install phase.
73 GOHOSTOS = stdenv.buildPlatform.go.GOOS;
74 GOHOSTARCH = stdenv.buildPlatform.go.GOARCH;
75
76 # {CC,CXX}_FOR_TARGET must be only set for cross compilation case as go expect those
77 # to be different from CC/CXX
78 CC_FOR_TARGET = if isCross then "${targetCC}/bin/${targetCC.targetPrefix}cc" else null;
79 CXX_FOR_TARGET = if isCross then "${targetCC}/bin/${targetCC.targetPrefix}c++" else null;
80
81 GO386 = "softfloat"; # from Arch: don't assume sse2 on i686
82 # Wasi does not support CGO
83 # ppc64/linux CGO is incomplete/borked, and will likely not receive any further improvements
84 # https://github.com/golang/go/issues/8912
85 # https://github.com/golang/go/issues/13192
86 CGO_ENABLED =
87 if
88 (
89 stdenv.targetPlatform.isWasi
90 || (stdenv.targetPlatform.isPower64 && stdenv.targetPlatform.isBigEndian)
91 )
92 then
93 0
94 else
95 1;
96
97 GOROOT_BOOTSTRAP = "${goBootstrap}/share/go";
98
99 buildPhase = ''
100 runHook preBuild
101 export GOCACHE=$TMPDIR/go-cache
102
103 export PATH=$(pwd)/bin:$PATH
104
105 ${lib.optionalString isCross ''
106 # Independent from host/target, CC should produce code for the building system.
107 # We only set it when cross-compiling.
108 export CC=${buildPackages.stdenv.cc}/bin/cc
109 ''}
110 ulimit -a
111
112 pushd src
113 ./make.bash
114 popd
115 runHook postBuild
116 '';
117
118 preInstall = ''
119 # Contains the wrong perl shebang when cross compiling,
120 # since it is not used for anything we can deleted as well.
121 rm src/regexp/syntax/make_perl_groups.pl
122 ''
123 + (
124 if (stdenv.buildPlatform.system != stdenv.hostPlatform.system) then
125 ''
126 mv bin/*_*/* bin
127 rmdir bin/*_*
128 ${lib.optionalString
129 (!(finalAttrs.GOHOSTARCH == finalAttrs.GOARCH && finalAttrs.GOOS == finalAttrs.GOHOSTOS))
130 ''
131 rm -rf pkg/${finalAttrs.GOHOSTOS}_${finalAttrs.GOHOSTARCH} pkg/tool/${finalAttrs.GOHOSTOS}_${finalAttrs.GOHOSTARCH}
132 ''
133 }
134 ''
135 else
136 lib.optionalString (stdenv.hostPlatform.system != stdenv.targetPlatform.system) ''
137 rm -rf bin/*_*
138 ${lib.optionalString
139 (!(finalAttrs.GOHOSTARCH == finalAttrs.GOARCH && finalAttrs.GOOS == finalAttrs.GOHOSTOS))
140 ''
141 rm -rf pkg/${finalAttrs.GOOS}_${finalAttrs.GOARCH} pkg/tool/${finalAttrs.GOOS}_${finalAttrs.GOARCH}
142 ''
143 }
144 ''
145 );
146
147 installPhase = ''
148 runHook preInstall
149 mkdir -p $out/share/go
150 cp -a bin pkg src lib misc api doc go.env VERSION $out/share/go
151 mkdir -p $out/bin
152 ln -s $out/share/go/bin/* $out/bin
153 runHook postInstall
154 '';
155
156 disallowedReferences = [ goBootstrap ];
157
158 passthru = {
159 inherit goBootstrap skopeoTest;
160 tests = {
161 skopeo = testers.testVersion { package = skopeoTest; };
162 version = testers.testVersion {
163 package = finalAttrs.finalPackage;
164 command = "go version";
165 version = "go${finalAttrs.version}";
166 };
167 };
168 };
169
170 meta = with lib; {
171 changelog = "https://go.dev/doc/devel/release#go${lib.versions.majorMinor finalAttrs.version}";
172 description = "Go Programming language";
173 homepage = "https://go.dev/";
174 license = licenses.bsd3;
175 teams = [ teams.golang ];
176 platforms = platforms.darwin ++ platforms.linux ++ platforms.wasi ++ platforms.freebsd;
177 badPlatforms = [
178 # Support for big-endian POWER < 8 was dropped in 1.9, but POWER8 users have less of a reason to run in big-endian mode than pre-POWER8 ones
179 # So non-LE ppc64 is effectively unsupported, and Go SIGILLs on affordable ppc64 hardware
180 # https://github.com/golang/go/issues/19074 - Dropped support for big-endian POWER < 8, with community pushback
181 # https://github.com/golang/go/issues/73349 - upstream will not accept submissions to fix this
182 "powerpc64-linux"
183 ];
184 mainProgram = "go";
185 };
186})