Add cargoHelperFunctionsHook

* It will automatically capture and log all `cargo` invocations
This commit is contained in:
Ivan Petkov 2022-07-23 10:04:55 -07:00
parent 7fc1a8fec3
commit bce972e03b
No known key found for this signature in database
GPG Key ID: BB6F9EFC065832B6
8 changed files with 31 additions and 15 deletions

View File

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* **Breaking**: the `--workspace` flag is no longer set for all cargo commands
by default. The previous behavior can be recovered by setting `cargoExtraArgs
= "--workspace";` in any derivation.
* All cargo invocations made during the build are automatically logged
## [0.5.1] - 2022-07-20

View File

@ -527,6 +527,7 @@ environment variables during the build, you can bring them back via
The `cargo` package is automatically appended as a native build input to any
other `nativeBuildInputs` specified by the caller, along with the following
hooks:
* `cargoHelperFunctionsHook`
* `configureCargoCommonVarsHook`
* `configureCargoVendoredDepsHook`
* `inheritCargoArtifactsHook`
@ -725,6 +726,16 @@ lib.writeTOML "foo.toml" { foo.bar = "baz"; }
## Hooks
### `cargoHelperFunctionsHook`
Defines helper functions for internal use. It is probably not a great idea to
depend on these directly as their behavior can change at any time, but it is
worth documenting them just in case:
* Defines a `cargo()` function which will immediately invoke the `cargo` command
found on the `$PATH` after echoing the exact arguments that were passed in.
Useful for automatically logging all cargo invocations to the log.
### `configureCargoCommonVarsHook`
Defines `configureCargoCommonVars()` which will set various common cargo-related

View File

@ -13,7 +13,7 @@ let
${cargoBuildCommand} --message-format json-render-diagnostics ${cargoExtraArgs} >"$cargoBuildLog"
'';
defaultInstallPhaseCommand = ''
installPhaseCommand = args.installPhaseCommand or ''
if [ -n "$cargoBuildLog" -a -f "$cargoBuildLog" ]; then
installFromCargoBuildLog "$out" "$cargoBuildLog"
else
@ -33,14 +33,6 @@ let
false
fi
'';
installPhaseCommand =
if args ? installPhaseCommand
then ''
echo running: ${lib.strings.escapeShellArg args.installPhaseCommand}
${args.installPhaseCommand}
''
else defaultInstallPhaseCommand;
in
(cargoBuild args).overrideAttrs (old: {
# NB: we use overrideAttrs here so that our extra additions here do not end up
@ -53,8 +45,6 @@ in
buildPhase = args.buildPhase or ''
runHook preBuild
cargo --version
echo running: ${lib.strings.escapeShellArg buildPhaseCargoCommand}
${buildPhaseCargoCommand}
runHook postBuild
'';

View File

@ -1,4 +1,5 @@
{ cargo
, cargoHelperFunctionsHook
, configureCargoCommonVarsHook
, configureCargoVendoredDepsHook
, inheritCargoArtifactsHook
@ -48,6 +49,7 @@ stdenv.mkDerivation (cleanedArgs // {
nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [
cargo
cargoHelperFunctionsHook
configureCargoCommonVarsHook
configureCargoVendoredDepsHook
inheritCargoArtifactsHook
@ -58,21 +60,18 @@ stdenv.mkDerivation (cleanedArgs // {
buildPhase = args.buildPhase or ''
runHook preBuild
cargo --version
echo running: ${lib.strings.escapeShellArg buildPhaseCargoCommand}
${buildPhaseCargoCommand}
runHook postBuild
'';
checkPhase = args.checkPhase or ''
runHook preCheck
echo running: ${lib.strings.escapeShellArg checkPhaseCargoCommand}
${checkPhaseCargoCommand}
runHook postCheck
'';
installPhase = args.installPhase or ''
runHook preInstall
echo running: ${lib.strings.escapeShellArg installPhaseCommand}
${installPhaseCommand}
runHook postInstall
'';

View File

@ -20,7 +20,6 @@ let
inherit (lib)
concatMapStrings
concatStrings
escapeShellArg
groupBy
hasPrefix
last

View File

@ -0,0 +1,8 @@
# A shell wrapper which logs any `cargo` invocation
cargo() {
# Run in a subshell to avoid polluting the environment
(
set -x
command cargo "$@"
)
}

View File

@ -0,0 +1,7 @@
{ makeSetupHook
}:
makeSetupHook
{
name = "cargoHelperFunctionsHook";
} ./cargoHelperFunctions.sh

View File

@ -9,6 +9,7 @@
# etc. scopes).
callPackage:
{
cargoHelperFunctionsHook = callPackage ./cargoHelperFunctionsHook.nix { };
configureCargoCommonVarsHook = callPackage ./configureCargoCommonVars.nix { };
configureCargoVendoredDepsHook = callPackage ./configureCargoVendoredDeps.nix { };
inheritCargoArtifactsHook = callPackage ./inheritCargoArtifacts.nix { };