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