2022-10-10 00:55:36 +03:00
|
|
|
{ buildDepsOnly
|
|
|
|
, crateNameFromCargoToml
|
2022-01-04 01:22:08 +03:00
|
|
|
, installFromCargoBuildLogHook
|
2022-01-04 20:48:22 +03:00
|
|
|
, lib
|
2022-10-10 00:55:36 +03:00
|
|
|
, mkCargoDerivation
|
2022-09-25 02:45:56 +03:00
|
|
|
, removeReferencesToVendoredSourcesHook
|
2022-10-10 00:55:36 +03:00
|
|
|
, vendorCargoDeps
|
2022-01-04 01:22:08 +03:00
|
|
|
}:
|
|
|
|
|
2022-07-23 21:10:45 +03:00
|
|
|
{ cargoBuildCommand ? "cargoWithProfile build"
|
2023-08-27 22:17:58 +03:00
|
|
|
, cargoExtraArgs ? "--locked"
|
2022-10-10 00:55:36 +03:00
|
|
|
, cargoTestCommand ? "cargoWithProfile test"
|
|
|
|
, cargoTestExtraArgs ? ""
|
2022-01-04 01:22:08 +03:00
|
|
|
, ...
|
|
|
|
}@args:
|
2022-01-04 20:48:22 +03:00
|
|
|
let
|
2023-04-11 04:37:58 +03:00
|
|
|
inherit (builtins) removeAttrs;
|
|
|
|
|
2022-10-10 00:55:36 +03:00
|
|
|
crateName = crateNameFromCargoToml args;
|
2023-04-11 04:37:58 +03:00
|
|
|
cleanedArgs = removeAttrs args [
|
2022-10-10 00:55:36 +03:00
|
|
|
"cargoBuildCommand"
|
|
|
|
"cargoExtraArgs"
|
|
|
|
"cargoTestCommand"
|
|
|
|
"cargoTestExtraArgs"
|
2023-09-22 07:08:53 +03:00
|
|
|
"outputHashes"
|
2022-10-10 00:55:36 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
# Avoid recomputing values when passing args down
|
|
|
|
memoizedArgs = {
|
|
|
|
pname = args.pname or crateName.pname;
|
|
|
|
version = args.version or crateName.version;
|
|
|
|
cargoVendorDir = args.cargoVendorDir or (vendorCargoDeps args);
|
|
|
|
};
|
|
|
|
in
|
|
|
|
mkCargoDerivation (cleanedArgs // memoizedArgs // {
|
|
|
|
doCheck = args.doCheck or true;
|
|
|
|
doInstallCargoArtifacts = args.doInstallCargoArtifacts or false;
|
|
|
|
|
2023-04-11 04:37:58 +03:00
|
|
|
cargoArtifacts = args.cargoArtifacts or (
|
2023-06-07 08:38:53 +03:00
|
|
|
buildDepsOnly (args // memoizedArgs // {
|
2023-09-24 02:23:33 +03:00
|
|
|
# NB: we intentionally don't run any caller-provided hooks here since they might fail
|
|
|
|
# if they require any files that have been omitted by the source dummification.
|
|
|
|
# However, we still _do_ want to run the installation hook with the actual artifacts
|
|
|
|
installPhase = "prepareAndInstallCargoArtifactsDir";
|
2023-06-07 08:38:53 +03:00
|
|
|
})
|
2023-04-11 04:37:58 +03:00
|
|
|
);
|
2022-10-10 00:55:36 +03:00
|
|
|
|
2022-01-04 20:48:22 +03:00
|
|
|
buildPhaseCargoCommand = args.buildPhaseCargoCommand or ''
|
|
|
|
cargoBuildLog=$(mktemp cargoBuildLogXXXX.json)
|
|
|
|
${cargoBuildCommand} --message-format json-render-diagnostics ${cargoExtraArgs} >"$cargoBuildLog"
|
|
|
|
'';
|
|
|
|
|
2022-10-10 00:55:36 +03:00
|
|
|
checkPhaseCargoCommand = args.checkPhaseCargoCommand or ''
|
|
|
|
${cargoTestCommand} ${cargoExtraArgs} ${cargoTestExtraArgs}
|
|
|
|
'';
|
|
|
|
|
2022-07-23 20:04:55 +03:00
|
|
|
installPhaseCommand = args.installPhaseCommand or ''
|
2022-01-04 20:48:22 +03:00
|
|
|
if [ -n "$cargoBuildLog" -a -f "$cargoBuildLog" ]; then
|
|
|
|
installFromCargoBuildLog "$out" "$cargoBuildLog"
|
|
|
|
else
|
|
|
|
echo ${lib.strings.escapeShellArg ''
|
|
|
|
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
$cargoBuildLog is either undefined or does not point to a valid file location!
|
|
|
|
By default `buildPackage` will capture cargo's output and use it to determine which binaries
|
|
|
|
should be installed (instead of just guessing based on what is present in cargo's target directory).
|
|
|
|
If you are overriding the derivation with a custom build step, you have two options:
|
|
|
|
1. override `installPhaseCommand` with the appropriate installation steps
|
|
|
|
2. ensure that cargo's build log is captured in a file and point $cargoBuildLog at it
|
|
|
|
At a minimum, the latter option can be achieved with running:
|
|
|
|
cargoBuildLog=$(mktemp cargoBuildLogXXXX.json)
|
2022-07-23 21:10:45 +03:00
|
|
|
cargo build --release --message-format json-render-diagnostics >"$cargoBuildLog"
|
2022-01-04 20:48:22 +03:00
|
|
|
''}
|
|
|
|
|
|
|
|
false
|
|
|
|
fi
|
|
|
|
'';
|
2022-01-04 01:22:08 +03:00
|
|
|
|
2022-10-10 00:55:36 +03:00
|
|
|
nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [
|
2024-08-23 02:24:53 +03:00
|
|
|
# NB: avoid adding any non-hook packages here. Doing so will end up
|
|
|
|
# changing PKG_CONFIG_PATH and cause rebuilds of `*-sys` crates.
|
2022-01-04 01:22:08 +03:00
|
|
|
installFromCargoBuildLogHook
|
2022-09-25 02:45:56 +03:00
|
|
|
removeReferencesToVendoredSourcesHook
|
2022-01-04 01:22:08 +03:00
|
|
|
];
|
|
|
|
})
|