nixpkgs/pkgs/build-support/rust/default.nix
Justin Humm b66ef28841 buildRustPackage, fetchcargo: optionally use vendor config from cargo-vendor
By setting useRealVendorConfig explicitly to true, the actual (slightly
modified) config generated by cargo-vendor is used.

This solves a problem, where the static vendor config in
pkgs/build-support/rust/default.nix would not sufficiently replace all
crates Cargo is looking for.

As useRealVendorConfig (and writeVendorConfig in fetchcargo) default to
false, there should be no breakage in existing cargoSha256 hashes.

Nethertheless, imho using this new feature should become standard. A
possible deprecation path could be:

- introduce this patch
- set useRealVendorConfig explicitly to false whereever cargoSha256 is
  set but migration is not wanted yet.
- after some time, let writeVendorConfig default to true
- when useRealVendorConfig is true everywhere cargoSha256 is set and
  enough time is passed, `assert cargoVendorDir == null ->
  useRealVendorConfig;`, remove old behaviour
- after some time, remove all appearences of useRealVendorConfig and the
  parameter itself
2018-09-11 23:44:14 +02:00

115 lines
2.9 KiB
Nix

{ stdenv, cacert, git, rust, cargo-vendor }:
let
fetchcargo = import ./fetchcargo.nix {
inherit stdenv cacert git rust cargo-vendor;
};
in
{ name, cargoSha256 ? "unset"
, src ? null
, srcs ? null
, cargoPatches ? []
, patches ? []
, sourceRoot ? null
, logLevel ? ""
, buildInputs ? []
, cargoUpdateHook ? ""
, cargoDepsHook ? ""
, cargoBuildFlags ? []
, cargoVendorDir ? null
# This tells cargo-vendor to include a Cargo config file in the fixed-output
# derivation. This is desirable in every case, so please set it to true.
# Eventually this will default to true and even later this option and the old
# behaviour will be removed.
, useRealVendorConfig ? false
, ... } @ args:
assert cargoVendorDir == null -> cargoSha256 != "unset";
assert cargoVendorDir != null -> !useRealVendorConfig;
let
cargoDeps = if cargoVendorDir == null
then fetchcargo {
inherit name src srcs sourceRoot cargoUpdateHook;
patches = cargoPatches;
sha256 = cargoSha256;
writeVendorConfig = useRealVendorConfig;
}
else null;
setupVendorDir = if cargoVendorDir == null
then ''
unpackFile "$cargoDeps"
cargoDepsCopy=$(stripHash $(basename $cargoDeps))
chmod -R +w "$cargoDepsCopy"
''
else ''
cargoDepsCopy="$sourceRoot/${cargoVendorDir}"
'';
in stdenv.mkDerivation (args // {
inherit cargoDeps;
patchRegistryDeps = ./patch-registry-deps;
buildInputs = [ cacert git rust.cargo rust.rustc ] ++ buildInputs;
patches = cargoPatches ++ patches;
configurePhase = args.configurePhase or ''
runHook preConfigure
# noop
runHook postConfigure
'';
postUnpack = ''
eval "$cargoDepsHook"
${setupVendorDir}
mkdir .cargo
'' + (if useRealVendorConfig then ''
sed "s|directory = \".*\"|directory = \"$(pwd)/$cargoDepsCopy\"|g" \
"$(pwd)/$cargoDepsCopy/.cargo/config" > .cargo/config
'' else ''
cat >.cargo/config <<-EOF
[source.crates-io]
registry = 'https://github.com/rust-lang/crates.io-index'
replace-with = 'vendored-sources'
[source.vendored-sources]
directory = '$(pwd)/$cargoDepsCopy'
EOF
'') + ''
unset cargoDepsCopy
export RUST_LOG=${logLevel}
'' + (args.postUnpack or "");
buildPhase = with builtins; args.buildPhase or ''
runHook preBuild
echo "Running cargo build --release ${concatStringsSep " " cargoBuildFlags}"
cargo build --release --frozen ${concatStringsSep " " cargoBuildFlags}
runHook postBuild
'';
checkPhase = args.checkPhase or ''
runHook preCheck
echo "Running cargo test"
cargo test
runHook postCheck
'';
doCheck = args.doCheck or true;
installPhase = args.installPhase or ''
runHook preInstall
mkdir -p $out/bin
find target/release -maxdepth 1 -executable -type f -exec cp "{}" $out/bin \;
runHook postInstall
'';
passthru = { inherit cargoDeps; } // (args.passthru or {});
})