nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib, stdenv, removeReferencesTo, pkgsBuildBuild, pkgsBuildHost, pkgsBuildTarget
2, llvmShared, llvmSharedForBuild, llvmSharedForHost, llvmSharedForTarget, llvmPackages
3, fetchurl, file, python3
4, darwin, cmake, rust, rustPlatform
5, pkg-config, openssl
6, libiconv
7, which, libffi
8, withBundledLLVM ? false
9, enableRustcDev ? true
10, version
11, sha256
12, patches ? []
13}:
14
15let
16 inherit (lib) optionals optional optionalString concatStringsSep;
17 inherit (darwin.apple_sdk.frameworks) Security;
18in stdenv.mkDerivation rec {
19 pname = "rustc";
20 inherit version;
21
22 src = fetchurl {
23 url = "https://static.rust-lang.org/dist/rustc-${version}-src.tar.gz";
24 inherit sha256;
25 };
26
27 __darwinAllowLocalNetworking = true;
28
29 # rustc complains about modified source files otherwise
30 dontUpdateAutotoolsGnuConfigScripts = true;
31
32 # Running the default `strip -S` command on Darwin corrupts the
33 # .rlib files in "lib/".
34 #
35 # See https://github.com/NixOS/nixpkgs/pull/34227
36 #
37 # Running `strip -S` when cross compiling can harm the cross rlibs.
38 # See: https://github.com/NixOS/nixpkgs/pull/56540#issuecomment-471624656
39 stripDebugList = [ "bin" ];
40
41 NIX_LDFLAGS = toString (
42 # when linking stage1 libstd: cc: undefined reference to `__cxa_begin_catch'
43 optional (stdenv.isLinux && !withBundledLLVM) "--push-state --as-needed -lstdc++ --pop-state"
44 ++ optional (stdenv.isDarwin && !withBundledLLVM) "-lc++"
45 ++ optional stdenv.isDarwin "-rpath ${llvmSharedForHost}/lib");
46
47 # Increase codegen units to introduce parallelism within the compiler.
48 RUSTFLAGS = "-Ccodegen-units=10";
49
50 # We need rust to build rust. If we don't provide it, configure will try to download it.
51 # Reference: https://github.com/rust-lang/rust/blob/master/src/bootstrap/configure.py
52 configureFlags = let
53 setBuild = "--set=target.${rust.toRustTarget stdenv.buildPlatform}";
54 setHost = "--set=target.${rust.toRustTarget stdenv.hostPlatform}";
55 setTarget = "--set=target.${rust.toRustTarget stdenv.targetPlatform}";
56 ccForBuild = "${pkgsBuildBuild.targetPackages.stdenv.cc}/bin/${pkgsBuildBuild.targetPackages.stdenv.cc.targetPrefix}cc";
57 cxxForBuild = "${pkgsBuildBuild.targetPackages.stdenv.cc}/bin/${pkgsBuildBuild.targetPackages.stdenv.cc.targetPrefix}c++";
58 ccForHost = "${pkgsBuildHost.targetPackages.stdenv.cc}/bin/${pkgsBuildHost.targetPackages.stdenv.cc.targetPrefix}cc";
59 cxxForHost = "${pkgsBuildHost.targetPackages.stdenv.cc}/bin/${pkgsBuildHost.targetPackages.stdenv.cc.targetPrefix}c++";
60 ccForTarget = "${pkgsBuildTarget.targetPackages.stdenv.cc}/bin/${pkgsBuildTarget.targetPackages.stdenv.cc.targetPrefix}cc";
61 cxxForTarget = "${pkgsBuildTarget.targetPackages.stdenv.cc}/bin/${pkgsBuildTarget.targetPackages.stdenv.cc.targetPrefix}c++";
62 in [
63 "--release-channel=stable"
64 "--set=build.rustc=${rustPlatform.rust.rustc}/bin/rustc"
65 "--set=build.cargo=${rustPlatform.rust.cargo}/bin/cargo"
66 "--enable-rpath"
67 "--enable-vendor"
68 "--build=${rust.toRustTargetSpec stdenv.buildPlatform}"
69 "--host=${rust.toRustTargetSpec stdenv.hostPlatform}"
70 # std is built for all platforms in --target. When building a cross-compiler
71 # we need to add the host platform as well so rustc can compile build.rs
72 # scripts.
73 "--target=${concatStringsSep "," ([
74 (rust.toRustTargetSpec stdenv.targetPlatform)
75 ] ++ optionals (stdenv.hostPlatform != stdenv.targetPlatform) [
76 (rust.toRustTargetSpec stdenv.hostPlatform)
77 ])}"
78
79 "${setBuild}.cc=${ccForBuild}"
80 "${setHost}.cc=${ccForHost}"
81 "${setTarget}.cc=${ccForTarget}"
82
83 "${setBuild}.linker=${ccForBuild}"
84 "${setHost}.linker=${ccForHost}"
85 "${setTarget}.linker=${ccForTarget}"
86
87 "${setBuild}.cxx=${cxxForBuild}"
88 "${setHost}.cxx=${cxxForHost}"
89 "${setTarget}.cxx=${cxxForTarget}"
90 ] ++ optionals (!withBundledLLVM) [
91 "--enable-llvm-link-shared"
92 "${setBuild}.llvm-config=${llvmSharedForBuild.dev}/bin/llvm-config"
93 "${setHost}.llvm-config=${llvmSharedForHost.dev}/bin/llvm-config"
94 "${setTarget}.llvm-config=${llvmSharedForTarget.dev}/bin/llvm-config"
95 ] ++ optionals (stdenv.isLinux && !stdenv.targetPlatform.isRedox) [
96 "--enable-profiler" # build libprofiler_builtins
97 ] ++ optionals stdenv.buildPlatform.isMusl [
98 "${setBuild}.musl-root=${pkgsBuildBuild.targetPackages.stdenv.cc.libc}"
99 ] ++ optionals stdenv.hostPlatform.isMusl [
100 "${setHost}.musl-root=${pkgsBuildHost.targetPackages.stdenv.cc.libc}"
101 ] ++ optionals stdenv.targetPlatform.isMusl [
102 "${setTarget}.musl-root=${pkgsBuildTarget.targetPackages.stdenv.cc.libc}"
103 ] ++ optionals (stdenv.isDarwin && stdenv.isx86_64) [
104 # https://github.com/rust-lang/rust/issues/92173
105 "--set rust.jemalloc"
106 ];
107
108 # The bootstrap.py will generated a Makefile that then executes the build.
109 # The BOOTSTRAP_ARGS used by this Makefile must include all flags to pass
110 # to the bootstrap builder.
111 postConfigure = ''
112 substituteInPlace Makefile \
113 --replace 'BOOTSTRAP_ARGS :=' 'BOOTSTRAP_ARGS := --jobs $(NIX_BUILD_CORES)'
114 '';
115
116 # the rust build system complains that nix alters the checksums
117 dontFixLibtool = true;
118
119 inherit patches;
120
121 postPatch = ''
122 patchShebangs src/etc
123
124 ${optionalString (!withBundledLLVM) "rm -rf src/llvm"}
125
126 # Fix the configure script to not require curl as we won't use it
127 sed -i configure \
128 -e '/probe_need CFG_CURL curl/d'
129
130 # Useful debugging parameter
131 # export VERBOSE=1
132 '';
133
134 # rustc unfortunately needs cmake to compile llvm-rt but doesn't
135 # use it for the normal build. This disables cmake in Nix.
136 dontUseCmakeConfigure = true;
137
138 nativeBuildInputs = [
139 file python3 rustPlatform.rust.rustc cmake
140 which libffi removeReferencesTo pkg-config
141 ];
142
143 buildInputs = [ openssl ]
144 ++ optionals stdenv.isDarwin [ libiconv Security ]
145 ++ optional (!withBundledLLVM) llvmShared;
146
147 outputs = [ "out" "man" "doc" ];
148 setOutputFlags = false;
149
150 postInstall = lib.optionalString enableRustcDev ''
151 # install rustc-dev components. Necessary to build rls, clippy...
152 python x.py dist rustc-dev
153 tar xf build/dist/rustc-dev*tar.gz
154 cp -r rustc-dev*/rustc-dev*/lib/* $out/lib/
155 rm $out/lib/rustlib/install.log
156 for m in $out/lib/rustlib/manifest-rust*
157 do
158 sort --output=$m < $m
159 done
160
161 '' + ''
162 # remove references to llvm-config in lib/rustlib/x86_64-unknown-linux-gnu/codegen-backends/librustc_codegen_llvm-llvm.so
163 # and thus a transitive dependency on ncurses
164 find $out/lib -name "*.so" -type f -exec remove-references-to -t ${llvmShared} '{}' '+'
165
166 # remove uninstall script that doesn't really make sense for Nix.
167 rm $out/lib/rustlib/uninstall.sh
168 '';
169
170 configurePlatforms = [];
171
172 # https://github.com/NixOS/nixpkgs/pull/21742#issuecomment-272305764
173 # https://github.com/rust-lang/rust/issues/30181
174 # enableParallelBuilding = false;
175
176 setupHooks = ./setup-hook.sh;
177
178 requiredSystemFeatures = [ "big-parallel" ];
179
180 passthru = {
181 llvm = llvmShared;
182 inherit llvmPackages;
183 };
184
185 meta = with lib; {
186 homepage = "https://www.rust-lang.org/";
187 description = "A safe, concurrent, practical language";
188 maintainers = with maintainers; [ madjar cstrahan globin havvy ];
189 license = [ licenses.mit licenses.asl20 ];
190 platforms = platforms.linux ++ platforms.darwin;
191 };
192}