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