crane/lib/downloadCargoPackageFromGit.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

82 lines
1.7 KiB
Nix
Raw Normal View History

{ lib
, pkgsBuildBuild
2022-02-08 07:03:12 +03:00
}:
let
inherit (pkgsBuildBuild)
cargo
fetchgit
jq
stdenv;
craneUtils = pkgsBuildBuild.callPackage ../pkgs/crane-utils { };
in
{ git
2022-02-08 07:03:12 +03:00
, rev
, ref ? null
, sha256 ? null
, allRefs ? ref == null
2022-10-24 02:20:22 +03:00
}:
2022-02-08 07:03:12 +03:00
let
maybeRef = lib.optionalAttrs (ref != null) { inherit ref; };
repo =
if sha256 == null then
builtins.fetchGit
(maybeRef // {
inherit allRefs rev;
url = git;
submodules = true;
})
else
fetchgit {
inherit rev sha256;
url = git;
fetchSubmodules = true;
};
2022-02-08 07:03:12 +03:00
in
stdenv.mkDerivation {
name = "cargo-git";
src = repo;
dontConfigure = true;
dontBuild = true;
nativeBuildInputs = [
cargo
craneUtils
jq
];
installPhase = ''
runHook preInstall
mkdir -p $out
declare -A existing_crates
find "$(pwd)" -name Cargo.toml | while read -r cargoToml; do
local crate=$(
cargo metadata --format-version 1 --no-deps --manifest-path "$cargoToml" |
jq -r '.packages[] | select(.manifest_path == "'"$cargoToml"'") | "\(.name)-\(.version)"'
)
if [ -n "$crate" ]; then
if [[ -n "''${existing_crates["$crate"]}" ]]; then
>&2 echo "warning: skipping duplicate package $crate found at $cargoToml"
continue
fi
local dest="$out/$crate"
cp -rL "$(dirname "$cargoToml")" "$dest"
chmod +w "$dest"
echo '{"files":{}, "package":null}' > "$dest/.cargo-checksum.json"
crane-resolve-workspace-inheritance "$cargoToml" > "$dest/Cargo.toml.resolved" &&
mv "$dest/Cargo.toml"{.resolved,}
existing_crates["$crate"]='1'
fi
done
runHook postInstall
'';
}