From 2d9788eb8b49339c8d0800ecc6a469ff24115e84 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Sun, 26 Dec 2021 18:06:19 -0800 Subject: [PATCH] Add copyCargoTargetToOutputHook --- lib/buildWithCargo.nix | 19 +++++++++++++++++-- pkgs/copyCargoTargetToOutputHook.sh | 21 +++++++++++++++++++++ pkgs/default.nix | 5 +++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 pkgs/copyCargoTargetToOutputHook.sh diff --git a/lib/buildWithCargo.nix b/lib/buildWithCargo.nix index 25b102c..fe1dc7c 100644 --- a/lib/buildWithCargo.nix +++ b/lib/buildWithCargo.nix @@ -1,23 +1,38 @@ { cargo , configureCargoCommonVarsHook , configureCargoVendoredDepsHook +, copyCargoTargetToOutputHook , lib , stdenv }: -args@{ nativeBuildInputs ? [ ], ... }: +{ doCopyTarget ? true +, doCopyTargetToSeparateOutput ? doCopyTarget +, nativeBuildInputs ? [ ] +, outputs ? [ "out" ] +, ... +}@args: stdenv.mkDerivation (args // { + inherit + doCopyTarget + doCopyTargetToSeparateOutput; + nativeBuildInputs = nativeBuildInputs ++ [ cargo configureCargoCommonVarsHook configureCargoVendoredDepsHook + copyCargoTargetToOutputHook ]; + outputs = outputs ++ lib.optional (doCopyTarget && doCopyTargetToSeparateOutput) "target"; + buildPhase = '' cargo check --release ''; installPhase = '' - touch $out + runHook preInstall + mkdir -p $out + runHook postInstall ''; }) diff --git a/pkgs/copyCargoTargetToOutputHook.sh b/pkgs/copyCargoTargetToOutputHook.sh new file mode 100644 index 0000000..dbcbb30 --- /dev/null +++ b/pkgs/copyCargoTargetToOutputHook.sh @@ -0,0 +1,21 @@ +copyCargoTargetToOutput() { + echo "Executing copyCargoTargetToOutput" + trap "echo Finished copyCargoTargetToOutput" RETURN + + local target_dir=${CARGO_TARGET_DIR:-target} + + if [ "1" != "${doCopyTarget}" ]; then + return + fi + + if [ "1" = "${doCopyTargetToSeparateOutput}" ]; then + local dest="${target}" + else + mkdir -p "${out}" + local dest="${out}/target" + fi + + mv "${target_dir}" "${dest}" +} + +postInstallHooks+=(copyCargoTargetToOutput) diff --git a/pkgs/default.nix b/pkgs/default.nix index d749198..e7aed75 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -10,4 +10,9 @@ { name = "configureCargoVendoredDepsHook"; } ./configureCargoVendoredDepsHook.sh; + + copyCargoTargetToOutputHook = makeSetupHook + { + name = "copyCargoTargetToOutputHook"; + } ./copyCargoTargetToOutputHook.sh; }