2018-02-03 23:30:45 +01:00
|
|
|
{ callPackage, fetchurl, stdenv, path, cacert, git, rust, cargo-vendor }:
|
2017-06-15 23:12:31 +02:00
|
|
|
let
|
2017-08-05 16:38:48 +02:00
|
|
|
fetchcargo = import ./fetchcargo.nix {
|
2018-02-03 23:30:45 +01:00
|
|
|
inherit stdenv cacert git rust cargo-vendor;
|
2017-08-05 16:38:48 +02:00
|
|
|
};
|
2017-06-15 23:12:31 +02:00
|
|
|
in
|
2018-02-20 10:59:26 +01:00
|
|
|
{ name, cargoSha256 ? null
|
2015-05-29 19:35:31 +02:00
|
|
|
, src ? null
|
|
|
|
, srcs ? null
|
|
|
|
, sourceRoot ? null
|
2016-05-28 15:03:59 +02:00
|
|
|
, logLevel ? ""
|
2015-05-29 19:35:31 +02:00
|
|
|
, buildInputs ? []
|
|
|
|
, cargoUpdateHook ? ""
|
2016-12-03 23:36:48 +01:00
|
|
|
, cargoDepsHook ? ""
|
2017-04-15 11:14:55 +02:00
|
|
|
, cargoBuildFlags ? []
|
2018-02-20 10:59:26 +01:00
|
|
|
|
|
|
|
, cargoVendorDir ? null
|
2015-05-29 19:35:31 +02:00
|
|
|
, ... } @ args:
|
2014-10-10 16:59:37 +02:00
|
|
|
|
2018-02-20 10:59:26 +01:00
|
|
|
assert cargoVendorDir == null -> cargoSha256 != null;
|
|
|
|
|
2014-10-10 16:59:37 +02:00
|
|
|
let
|
2017-06-14 23:36:27 +02:00
|
|
|
lib = stdenv.lib;
|
|
|
|
|
2018-02-20 10:59:26 +01:00
|
|
|
cargoDeps = if cargoVendorDir == null
|
|
|
|
then fetchcargo {
|
|
|
|
inherit name src srcs sourceRoot cargoUpdateHook;
|
|
|
|
sha256 = cargoSha256;
|
|
|
|
}
|
|
|
|
else null;
|
|
|
|
|
|
|
|
setupVendorDir = if cargoVendorDir == null
|
|
|
|
then ''
|
|
|
|
unpackFile "$cargoDeps"
|
|
|
|
cargoDepsCopy=$(stripHash $(basename $cargoDeps))
|
|
|
|
chmod -R +w "$cargoDepsCopy"
|
|
|
|
''
|
|
|
|
else ''
|
|
|
|
cargoDepsCopy="$sourceRoot/${cargoVendorDir}"
|
|
|
|
'';
|
2014-10-10 16:59:37 +02:00
|
|
|
|
|
|
|
in stdenv.mkDerivation (args // {
|
2017-08-05 16:38:48 +02:00
|
|
|
inherit cargoDeps;
|
2014-10-10 16:59:37 +02:00
|
|
|
|
2015-04-23 16:37:52 +02:00
|
|
|
patchRegistryDeps = ./patch-registry-deps;
|
|
|
|
|
2017-12-27 21:26:36 +01:00
|
|
|
buildInputs = [ cacert git rust.cargo rust.rustc ] ++ buildInputs;
|
2014-10-10 16:59:37 +02:00
|
|
|
|
2017-04-15 12:42:09 +02:00
|
|
|
configurePhase = args.configurePhase or ''
|
|
|
|
runHook preConfigure
|
|
|
|
# noop
|
|
|
|
runHook postConfigure
|
|
|
|
'';
|
2014-10-10 16:59:37 +02:00
|
|
|
|
|
|
|
postUnpack = ''
|
2016-12-03 23:36:48 +01:00
|
|
|
eval "$cargoDepsHook"
|
|
|
|
|
2018-02-20 10:59:26 +01:00
|
|
|
${setupVendorDir}
|
2017-11-20 18:02:01 +01:00
|
|
|
|
2017-08-05 16:38:48 +02:00
|
|
|
mkdir .cargo
|
|
|
|
cat >.cargo/config <<-EOF
|
|
|
|
[source.crates-io]
|
|
|
|
registry = 'https://github.com/rust-lang/crates.io-index'
|
|
|
|
replace-with = 'vendored-sources'
|
2015-04-23 20:15:47 +02:00
|
|
|
|
2017-08-05 16:38:48 +02:00
|
|
|
[source.vendored-sources]
|
2017-11-20 18:02:01 +01:00
|
|
|
directory = '$(pwd)/$cargoDepsCopy'
|
2015-04-23 20:15:47 +02:00
|
|
|
EOF
|
2015-04-23 15:14:34 +02:00
|
|
|
|
2017-11-20 18:02:01 +01:00
|
|
|
unset cargoDepsCopy
|
|
|
|
|
2016-05-28 12:46:16 +02:00
|
|
|
export RUST_LOG=${logLevel}
|
2014-10-10 16:59:37 +02:00
|
|
|
'' + (args.postUnpack or "");
|
|
|
|
|
2017-04-15 11:14:55 +02:00
|
|
|
buildPhase = with builtins; args.buildPhase or ''
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook preBuild
|
2017-04-15 11:14:55 +02:00
|
|
|
echo "Running cargo build --release ${concatStringsSep " " cargoBuildFlags}"
|
2017-08-05 16:38:48 +02:00
|
|
|
cargo build --release --frozen ${concatStringsSep " " cargoBuildFlags}
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook postBuild
|
2014-10-10 16:59:37 +02:00
|
|
|
'';
|
|
|
|
|
2015-04-21 20:34:26 +02:00
|
|
|
checkPhase = args.checkPhase or ''
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook preCheck
|
2015-04-21 20:34:26 +02:00
|
|
|
echo "Running cargo test"
|
|
|
|
cargo test
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook postCheck
|
2015-04-21 20:34:26 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
doCheck = args.doCheck or true;
|
|
|
|
|
2014-10-10 16:59:37 +02:00
|
|
|
installPhase = args.installPhase or ''
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook preInstall
|
2014-10-10 16:59:37 +02:00
|
|
|
mkdir -p $out/bin
|
2017-04-15 13:10:29 +02:00
|
|
|
find target/release -maxdepth 1 -executable -exec cp "{}" $out/bin \;
|
2017-04-15 12:42:09 +02:00
|
|
|
runHook postInstall
|
2014-10-10 16:59:37 +02:00
|
|
|
'';
|
2017-08-05 16:38:48 +02:00
|
|
|
|
2017-10-26 18:43:17 +02:00
|
|
|
passthru = { inherit cargoDeps; } // (args.passthru or {});
|
2014-10-10 16:59:37 +02:00
|
|
|
})
|