1 { lib, stdenv, pkgsBuildHost, pkgsHostHost
2 , file, curl, pkg-config, python3, openssl, cmake, zlib
3 , installShellFiles, makeWrapper, rustPlatform, rustc
4 , CoreFoundation, Security
5 , auditable ? !cargo-auditable.meta.broken
10 rustPlatform.buildRustPackage.override {
11 cargo-auditable = cargo-auditable.bootstrap;
14 inherit (rustc) version src;
16 # the rust source tarball already has all the dependencies vendored, no need to fetch them again
17 cargoVendorDir = "vendor";
18 buildAndTestSubdir = "src/tools/cargo";
24 inherit (rustc) tests;
27 # Upstream rustc still assumes that musl = static[1]. The fix for
28 # this is to disable crt-static by default for non-static musl
31 # For every package apart from Cargo, we can fix this by just
32 # patching rustc to not have crt-static by default. But Cargo is
33 # built with the upstream bootstrap binary for rustc, which we can't
34 # easily patch. This means we need to find another way to make sure
35 # crt-static is not used during the build of pkgsMusl.cargo.
37 # By default, Cargo doesn't apply RUSTFLAGS when building build.rs
38 # if --target is passed, so the only good way to set -crt-static for
39 # build.rs files used in the Cargo build is to use the unstable
40 # -Zhost-config Cargo feature. This allows us to specify flags that
41 # should be passed to rustc when building for the build platform.
42 # We also need to use -Ztarget-applies-to-host, because using
43 # -Zhost-config requires it.
45 # When doing this, we also have to specify the linker, or cargo
46 # won't pass a -C linker= argument to rustc. This will make rustc
47 # try to use its default value of "cc", which won't be available
48 # when cross-compiling.
50 # [1]: https://github.com/rust-lang/compiler-team/issues/422
51 postPatch = lib.optionalString (with stdenv.buildPlatform; isMusl && !isStatic) ''
53 cat <<EOF >> .cargo/config
55 rustflags = "-C target-feature=-crt-static"
56 linker = "${pkgsBuildHost.stdenv.cc}/bin/${pkgsBuildHost.stdenv.cc.targetPrefix}cc"
59 target-applies-to-host = true
63 # changes hash of vendor directory otherwise
64 dontUpdateAutotoolsGnuConfigScripts = true;
67 pkg-config cmake installShellFiles makeWrapper
68 (lib.getDev pkgsHostHost.curl)
71 buildInputs = [ file curl python3 openssl zlib ]
72 ++ lib.optionals stdenv.isDarwin [ CoreFoundation Security ];
74 # cargo uses git-rs which is made for a version of libgit2 from recent master that
75 # is not compatible with the current version in nixpkgs.
76 #LIBGIT2_SYS_USE_PKG_CONFIG = 1;
78 # fixes: the cargo feature `edition` requires a nightly version of Cargo, but this is the `stable` channel
82 wrapProgram "$out/bin/cargo" --suffix PATH : "${rustc}/bin"
84 installManPage src/tools/cargo/src/etc/man/*
86 installShellCompletion --bash --name cargo \
87 src/tools/cargo/src/etc/cargo.bashcomp.sh
89 installShellCompletion --zsh src/tools/cargo/src/etc/_cargo
93 # Disable cross compilation tests
94 export CFG_DISABLE_CROSS_TESTS=1
98 # Disable check phase as there are failures (4 tests fail)
101 doInstallCheck = !stdenv.hostPlatform.isStatic &&
102 stdenv.hostPlatform.parsed.kernel.execFormat == lib.systems.parse.execFormats.elf;
103 installCheckPhase = ''
104 runHook preInstallCheck
105 readelf -a $out/bin/.cargo-wrapped | grep -F 'Shared library: [libcurl.so'
106 runHook postInstallCheck
110 homepage = "https://crates.io";
111 description = "Downloads your Rust project's dependencies and builds your project";
112 maintainers = teams.rust.members;
113 license = [ licenses.mit licenses.asl20 ];
114 platforms = platforms.unix;
115 # https://github.com/alexcrichton/nghttp2-rs/issues/2
116 broken = stdenv.hostPlatform.isx86 && stdenv.buildPlatform != stdenv.hostPlatform;
119 // lib.optionalAttrs (stdenv.buildPlatform.rust.rustcTarget != stdenv.hostPlatform.rust.rustcTarget) {
120 HOST_PKG_CONFIG_PATH="${pkgsBuildBuild.pkg-config}/bin/pkg-config";