1{ lib, stdenv, makeWrapper, wrapRustc, bash, curl, darwin, zlib
2, autoPatchelfHook, gcc
3, version
4, src
5, platform
6, versionType
7}:
8
9let
10 inherit (lib) optionalString;
11 inherit (darwin.apple_sdk.frameworks) Security;
12
13 bootstrapping = versionType == "bootstrap";
14
15 installComponents
16 = "rustc,rust-std-${platform}"
17 + (optionalString bootstrapping ",cargo")
18 ;
19in
20
21rec {
22 rustc-unwrapped = stdenv.mkDerivation {
23 pname = "rustc-${versionType}";
24
25 inherit version;
26 inherit src;
27
28 meta = with lib; {
29 homepage = "https://www.rust-lang.org/";
30 sourceProvenance = with sourceTypes; [ binaryNativeCode ];
31 description = "A safe, concurrent, practical language";
32 maintainers = with maintainers; [ qknight ];
33 license = [ licenses.mit licenses.asl20 ];
34 };
35
36 nativeBuildInputs = lib.optional (!stdenv.isDarwin) autoPatchelfHook;
37 buildInputs = [ bash ]
38 ++ lib.optionals (!stdenv.isDarwin) [ gcc.cc.lib zlib ]
39 ++ lib.optional stdenv.isDarwin Security;
40
41 postPatch = ''
42 patchShebangs .
43 '';
44
45 installPhase = ''
46 ./install.sh --prefix=$out \
47 --components=${installComponents}
48
49 # Do NOT, I repeat, DO NOT use `wrapProgram` on $out/bin/rustc
50 # (or similar) here. It causes strange effects where rustc loads
51 # the wrong libraries in a bootstrap-build causing failures that
52 # are very hard to track down. For details, see
53 # https://github.com/rust-lang/rust/issues/34722#issuecomment-232164943
54 '';
55
56 # The strip tool in cctools 973.0.1 and up appears to break rlibs in the
57 # binaries. The lib.rmeta object inside the ar archive should contain an
58 # .rmeta section, but it is removed. Luckily, this doesn't appear to be an
59 # issue for Rust builds produced by Nix.
60 dontStrip = true;
61
62 setupHooks = ./setup-hook.sh;
63 };
64
65 rustc = wrapRustc rustc-unwrapped;
66
67 cargo = stdenv.mkDerivation {
68 pname = "cargo-${versionType}";
69
70 inherit version;
71 inherit src;
72
73 meta = with lib; {
74 homepage = "https://doc.rust-lang.org/cargo/";
75 sourceProvenance = with sourceTypes; [ binaryNativeCode ];
76 description = "The Rust package manager";
77 maintainers = with maintainers; [ qknight ];
78 license = [ licenses.mit licenses.asl20 ];
79 };
80
81 nativeBuildInputs = [ makeWrapper ]
82 ++ lib.optional (!stdenv.isDarwin) autoPatchelfHook;
83 buildInputs = [ bash ]
84 ++ lib.optional (!stdenv.isDarwin) gcc.cc.lib
85 ++ lib.optional stdenv.isDarwin Security;
86
87 postPatch = ''
88 patchShebangs .
89 '';
90
91 installPhase = ''
92 patchShebangs ./install.sh
93 ./install.sh --prefix=$out \
94 --components=cargo
95
96 wrapProgram "$out/bin/cargo" \
97 --suffix PATH : "${rustc}/bin"
98 '';
99 };
100}