crane/lib/downloadCargoPackageFromGit.nix
Jörg Thalheim 16f5732c14
allow to pass outputHashes to crane (#266)
This makes it possible to evaluate crane in a nixos test without network
as well as allow to backup all fetched input derivations properly in a
binary cache, whereas fetchGit will fallback to downloading from a
repository, which also requires a `git` binary to be present.

Co-authored-by: Ivan Petkov <ivanppetkov@gmail.com>
2023-09-22 04:08:53 +00:00

67 lines
1.4 KiB
Nix

{ cargo
, craneUtils
, jq
, lib
, fetchgit
, runCommand
}:
{ git
, rev
, ref ? null
, sha256 ? null
, allRefs ? ref == null
}:
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;
};
deps = {
nativeBuildInputs = [
cargo
craneUtils
jq
];
};
in
runCommand "cargo-git" deps ''
mkdir -p $out
existing_crates=()
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 [[ " ''${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")
fi
done < <(find ${repo} -name Cargo.toml)
''