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