cargo, cargoSnapshot: add rustc runtime dependency
It turns out that cargo implicitly depends on rustc at runtime: even
`cargo help` will fail if rustc is not in the PATH.
This means that we need to wrap the cargo binary to add rustc to PATH.
However, I have opted into doing something slightly unusual: instead of
tying down a specific cargo to use a specific rustc (i.e., wrap cargo so
that "${rustc}/bin" is prefixed into PATH), instead I'm adding the rustc
used to build cargo as a fallback rust compiler (i.e., wrap cargo so
that "${rustc}/bin" is suffixed into PATH). This means that cargo will
prefer to use a rust compiler that is in the default path, but fallback
into the one used to build cargo only if there wasn't any rust compiler
in the default path.
The reason I'm doing this is that otherwise it could cause unexpected
effects. For example, if you had a build environment with the
rustcMaster and cargo derivations, you would expect cargo to use
rustcMaster to compile your project (since rustcMaster would be the only
compiler available in $PATH), but this wouldn't happen if we tied down
cargo to use the rustc that was used to compile it (because the default
cargo derivation gets compiled with the stable rust compiler).
That said, I have slightly modified makeRustPlatform so that a rust
platform will always use the rust compiler that was used to build cargo,
because this prevents mistakenly depending on two different versions of
the rust compiler (stable and unstable) in the same rust platform,
something which is usually undesirable.
Fixes #11053
2015-11-17 18:32:03 +00:00
|
|
|
{ stdenv, cacert, git, cargo, rustRegistry }:
|
2015-05-29 18:35:31 +01:00
|
|
|
{ name, depsSha256
|
|
|
|
, src ? null
|
|
|
|
, srcs ? null
|
|
|
|
, sourceRoot ? null
|
2016-05-28 11:46:16 +01:00
|
|
|
, logLevel ? "warn"
|
2015-05-29 18:35:31 +01:00
|
|
|
, buildInputs ? []
|
|
|
|
, cargoUpdateHook ? ""
|
|
|
|
, ... } @ args:
|
2014-10-10 15:59:37 +01:00
|
|
|
|
|
|
|
let
|
|
|
|
fetchDeps = import ./fetchcargo.nix {
|
cargo, cargoSnapshot: add rustc runtime dependency
It turns out that cargo implicitly depends on rustc at runtime: even
`cargo help` will fail if rustc is not in the PATH.
This means that we need to wrap the cargo binary to add rustc to PATH.
However, I have opted into doing something slightly unusual: instead of
tying down a specific cargo to use a specific rustc (i.e., wrap cargo so
that "${rustc}/bin" is prefixed into PATH), instead I'm adding the rustc
used to build cargo as a fallback rust compiler (i.e., wrap cargo so
that "${rustc}/bin" is suffixed into PATH). This means that cargo will
prefer to use a rust compiler that is in the default path, but fallback
into the one used to build cargo only if there wasn't any rust compiler
in the default path.
The reason I'm doing this is that otherwise it could cause unexpected
effects. For example, if you had a build environment with the
rustcMaster and cargo derivations, you would expect cargo to use
rustcMaster to compile your project (since rustcMaster would be the only
compiler available in $PATH), but this wouldn't happen if we tied down
cargo to use the rustc that was used to compile it (because the default
cargo derivation gets compiled with the stable rust compiler).
That said, I have slightly modified makeRustPlatform so that a rust
platform will always use the rust compiler that was used to build cargo,
because this prevents mistakenly depending on two different versions of
the rust compiler (stable and unstable) in the same rust platform,
something which is usually undesirable.
Fixes #11053
2015-11-17 18:32:03 +00:00
|
|
|
inherit stdenv cacert git cargo rustRegistry;
|
2014-10-10 15:59:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
cargoDeps = fetchDeps {
|
2015-05-29 18:35:31 +01:00
|
|
|
inherit name src srcs sourceRoot cargoUpdateHook;
|
2014-10-10 15:59:37 +01:00
|
|
|
sha256 = depsSha256;
|
|
|
|
};
|
|
|
|
|
|
|
|
in stdenv.mkDerivation (args // {
|
2015-04-23 19:15:47 +01:00
|
|
|
inherit cargoDeps rustRegistry;
|
2014-10-10 15:59:37 +01:00
|
|
|
|
2015-04-23 15:37:52 +01:00
|
|
|
patchRegistryDeps = ./patch-registry-deps;
|
|
|
|
|
cargo, cargoSnapshot: add rustc runtime dependency
It turns out that cargo implicitly depends on rustc at runtime: even
`cargo help` will fail if rustc is not in the PATH.
This means that we need to wrap the cargo binary to add rustc to PATH.
However, I have opted into doing something slightly unusual: instead of
tying down a specific cargo to use a specific rustc (i.e., wrap cargo so
that "${rustc}/bin" is prefixed into PATH), instead I'm adding the rustc
used to build cargo as a fallback rust compiler (i.e., wrap cargo so
that "${rustc}/bin" is suffixed into PATH). This means that cargo will
prefer to use a rust compiler that is in the default path, but fallback
into the one used to build cargo only if there wasn't any rust compiler
in the default path.
The reason I'm doing this is that otherwise it could cause unexpected
effects. For example, if you had a build environment with the
rustcMaster and cargo derivations, you would expect cargo to use
rustcMaster to compile your project (since rustcMaster would be the only
compiler available in $PATH), but this wouldn't happen if we tied down
cargo to use the rustc that was used to compile it (because the default
cargo derivation gets compiled with the stable rust compiler).
That said, I have slightly modified makeRustPlatform so that a rust
platform will always use the rust compiler that was used to build cargo,
because this prevents mistakenly depending on two different versions of
the rust compiler (stable and unstable) in the same rust platform,
something which is usually undesirable.
Fixes #11053
2015-11-17 18:32:03 +00:00
|
|
|
buildInputs = [ git cargo cargo.rustc ] ++ buildInputs;
|
2014-10-10 15:59:37 +01:00
|
|
|
|
|
|
|
configurePhase = args.configurePhase or "true";
|
|
|
|
|
|
|
|
postUnpack = ''
|
2015-04-23 14:14:34 +01:00
|
|
|
echo "Using cargo deps from $cargoDeps"
|
2015-04-23 19:15:47 +01:00
|
|
|
|
|
|
|
cp -r "$cargoDeps" deps
|
2015-04-23 14:14:34 +01:00
|
|
|
chmod +w deps -R
|
|
|
|
|
2015-05-19 17:21:11 +01:00
|
|
|
# It's OK to use /dev/null as the URL because by the time we do this, cargo
|
|
|
|
# won't attempt to update the registry anymore, so the URL is more or less
|
|
|
|
# irrelevant
|
|
|
|
|
2015-04-23 19:15:47 +01:00
|
|
|
cat <<EOF > deps/config
|
|
|
|
[registry]
|
|
|
|
index = "file:///dev/null"
|
|
|
|
EOF
|
2015-04-23 14:14:34 +01:00
|
|
|
|
2015-05-19 17:21:11 +01:00
|
|
|
export CARGO_HOME="$(realpath deps)"
|
2016-05-28 11:46:16 +01:00
|
|
|
export RUST_LOG=${logLevel}
|
2015-04-23 01:43:11 +01:00
|
|
|
|
2015-05-19 17:21:11 +01:00
|
|
|
# Let's find out which $indexHash cargo uses for file:///dev/null
|
2015-07-24 19:46:14 +01:00
|
|
|
(cd $sourceRoot && cargo fetch &>/dev/null) || true
|
2015-05-19 17:21:11 +01:00
|
|
|
cd deps
|
|
|
|
indexHash="$(basename $(echo registry/index/*))"
|
2015-04-23 19:15:47 +01:00
|
|
|
|
2015-05-19 17:21:11 +01:00
|
|
|
echo "Using indexHash '$indexHash'"
|
|
|
|
|
|
|
|
rm -rf -- "registry/cache/$indexHash" \
|
|
|
|
"registry/index/$indexHash"
|
|
|
|
|
|
|
|
mv registry/cache/HASH "registry/cache/$indexHash"
|
|
|
|
|
|
|
|
echo "Using rust registry from $rustRegistry"
|
|
|
|
ln -s "$rustRegistry" "registry/index/$indexHash"
|
2015-04-23 19:15:47 +01:00
|
|
|
|
|
|
|
# Retrieved the Cargo.lock file which we saved during the fetch
|
2015-05-19 17:21:11 +01:00
|
|
|
cd ..
|
2015-04-23 19:15:47 +01:00
|
|
|
mv deps/Cargo.lock $sourceRoot/
|
2015-04-23 01:43:11 +01:00
|
|
|
|
2015-04-23 19:15:47 +01:00
|
|
|
(
|
|
|
|
cd $sourceRoot
|
2015-04-23 01:43:11 +01:00
|
|
|
|
2014-10-10 15:59:37 +01:00
|
|
|
cargo fetch
|
2015-04-23 01:43:11 +01:00
|
|
|
cargo clean
|
2014-10-10 15:59:37 +01:00
|
|
|
)
|
|
|
|
'' + (args.postUnpack or "");
|
|
|
|
|
|
|
|
prePatch = ''
|
2015-04-23 15:37:52 +01:00
|
|
|
# Patch registry dependencies, using the scripts in $patchRegistryDeps
|
|
|
|
(
|
|
|
|
set -euo pipefail
|
2014-10-10 15:59:37 +01:00
|
|
|
|
2016-02-16 10:55:25 +00:00
|
|
|
cd $NIX_BUILD_TOP/deps/registry/src/*
|
2015-04-23 19:15:47 +01:00
|
|
|
|
2015-04-23 15:37:52 +01:00
|
|
|
for script in $patchRegistryDeps/*; do
|
|
|
|
# Run in a subshell so that directory changes and shell options don't
|
|
|
|
# affect any following commands
|
|
|
|
|
|
|
|
( . $script)
|
|
|
|
done
|
|
|
|
)
|
2014-10-10 15:59:37 +01:00
|
|
|
'' + (args.prePatch or "");
|
|
|
|
|
|
|
|
buildPhase = args.buildPhase or ''
|
2015-04-21 19:34:26 +01:00
|
|
|
echo "Running cargo build --release"
|
2014-10-10 15:59:37 +01:00
|
|
|
cargo build --release
|
|
|
|
'';
|
|
|
|
|
2015-04-21 19:34:26 +01:00
|
|
|
checkPhase = args.checkPhase or ''
|
|
|
|
echo "Running cargo test"
|
|
|
|
cargo test
|
|
|
|
'';
|
|
|
|
|
|
|
|
doCheck = args.doCheck or true;
|
|
|
|
|
2014-10-10 15:59:37 +01:00
|
|
|
installPhase = args.installPhase or ''
|
|
|
|
mkdir -p $out/bin
|
|
|
|
for f in $(find target/release -maxdepth 1 -type f); do
|
|
|
|
cp $f $out/bin
|
|
|
|
done;
|
|
|
|
'';
|
|
|
|
})
|