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