2021-05-08 08:40:34 +03:00
|
|
|
{ fetchgit, fetchurl, lib, runCommand, cargo, jq }:
|
|
|
|
|
|
|
|
{
|
|
|
|
# Cargo lock file
|
2021-09-10 20:09:29 +03:00
|
|
|
lockFile ? null
|
|
|
|
|
|
|
|
# Cargo lock file contents as string
|
|
|
|
, lockFileContents ? null
|
2021-05-08 08:40:34 +03:00
|
|
|
|
|
|
|
# Hashes for git dependencies.
|
|
|
|
, outputHashes ? {}
|
2021-09-10 20:09:29 +03:00
|
|
|
} @ args:
|
|
|
|
|
|
|
|
assert (lockFile == null) != (lockFileContents == null);
|
2021-05-08 08:40:34 +03:00
|
|
|
|
|
|
|
let
|
|
|
|
# Parse a git source into different components.
|
|
|
|
parseGit = src:
|
|
|
|
let
|
2021-09-10 20:30:05 +03:00
|
|
|
parts = builtins.match ''git\+([^?]+)(\?(rev|tag|branch)=(.*))?#(.*)'' src;
|
|
|
|
type = builtins.elemAt parts 2; # rev, tag or branch
|
|
|
|
value = builtins.elemAt parts 3;
|
2021-05-08 08:40:34 +03:00
|
|
|
in
|
|
|
|
if parts == null then null
|
|
|
|
else {
|
|
|
|
url = builtins.elemAt parts 0;
|
2021-09-10 20:30:05 +03:00
|
|
|
sha = builtins.elemAt parts 4;
|
|
|
|
} // lib.optionalAttrs (type != null) { inherit type value; };
|
2021-05-08 08:40:34 +03:00
|
|
|
|
2021-09-10 20:09:29 +03:00
|
|
|
# shadows args.lockFileContents
|
|
|
|
lockFileContents =
|
|
|
|
if lockFile != null
|
|
|
|
then builtins.readFile lockFile
|
|
|
|
else args.lockFileContents;
|
|
|
|
|
|
|
|
packages = (builtins.fromTOML lockFileContents).package;
|
2021-05-08 08:40:34 +03:00
|
|
|
|
|
|
|
# There is no source attribute for the source package itself. But
|
|
|
|
# since we do not want to vendor the source package anyway, we can
|
|
|
|
# safely skip it.
|
|
|
|
depPackages = (builtins.filter (p: p ? "source") packages);
|
|
|
|
|
|
|
|
# Create dependent crates from packages.
|
|
|
|
#
|
|
|
|
# Force evaluation of the git SHA -> hash mapping, so that an error is
|
|
|
|
# thrown if there are stale hashes. We cannot rely on gitShaOutputHash
|
|
|
|
# being evaluated otherwise, since there could be no git dependencies.
|
|
|
|
depCrates = builtins.deepSeq (gitShaOutputHash) (builtins.map mkCrate depPackages);
|
|
|
|
|
|
|
|
# Map package name + version to git commit SHA for packages with a git source.
|
|
|
|
namesGitShas = builtins.listToAttrs (
|
|
|
|
builtins.map nameGitSha (builtins.filter (pkg: lib.hasPrefix "git+" pkg.source) depPackages)
|
|
|
|
);
|
|
|
|
|
|
|
|
nameGitSha = pkg: let gitParts = parseGit pkg.source; in {
|
|
|
|
name = "${pkg.name}-${pkg.version}";
|
|
|
|
value = gitParts.sha;
|
|
|
|
};
|
|
|
|
|
|
|
|
# Convert the attrset provided through the `outputHashes` argument to a
|
|
|
|
# a mapping from git commit SHA -> output hash.
|
|
|
|
#
|
|
|
|
# There may be multiple different packages with different names
|
|
|
|
# originating from the same git repository (typically a Cargo
|
|
|
|
# workspace). By using the git commit SHA as a universal identifier,
|
|
|
|
# the user does not have to specify the output hash for every package
|
|
|
|
# individually.
|
|
|
|
gitShaOutputHash = lib.mapAttrs' (nameVer: hash:
|
|
|
|
let
|
|
|
|
unusedHash = throw "A hash was specified for ${nameVer}, but there is no corresponding git dependency.";
|
|
|
|
rev = namesGitShas.${nameVer} or unusedHash; in {
|
|
|
|
name = rev;
|
|
|
|
value = hash;
|
|
|
|
}) outputHashes;
|
|
|
|
|
|
|
|
# We can't use the existing fetchCrate function, since it uses a
|
|
|
|
# recursive hash of the unpacked crate.
|
2021-07-17 18:02:45 +03:00
|
|
|
fetchCrate = pkg:
|
|
|
|
assert lib.assertMsg (pkg ? checksum) ''
|
|
|
|
Package ${pkg.name} does not have a checksum.
|
|
|
|
Please note that the Cargo.lock format where checksums used to be listed
|
|
|
|
under [metadata] is not supported.
|
|
|
|
If that is the case, running `cargo update` with a recent toolchain will
|
|
|
|
automatically update the format along with the crate's depenendencies.
|
|
|
|
'';
|
|
|
|
fetchurl {
|
|
|
|
name = "crate-${pkg.name}-${pkg.version}.tar.gz";
|
|
|
|
url = "https://crates.io/api/v1/crates/${pkg.name}/${pkg.version}/download";
|
|
|
|
sha256 = pkg.checksum;
|
|
|
|
};
|
2021-05-08 08:40:34 +03:00
|
|
|
|
|
|
|
# Fetch and unpack a crate.
|
|
|
|
mkCrate = pkg:
|
|
|
|
let
|
|
|
|
gitParts = parseGit pkg.source;
|
|
|
|
in
|
|
|
|
if pkg.source == "registry+https://github.com/rust-lang/crates.io-index" then
|
|
|
|
let
|
|
|
|
crateTarball = fetchCrate pkg;
|
|
|
|
in runCommand "${pkg.name}-${pkg.version}" {} ''
|
|
|
|
mkdir $out
|
|
|
|
tar xf "${crateTarball}" -C $out --strip-components=1
|
|
|
|
|
|
|
|
# Cargo is happy with largely empty metadata.
|
|
|
|
printf '{"files":{},"package":"${pkg.checksum}"}' > "$out/.cargo-checksum.json"
|
|
|
|
''
|
|
|
|
else if gitParts != null then
|
|
|
|
let
|
|
|
|
missingHash = throw ''
|
|
|
|
No hash was found while vendoring the git dependency ${pkg.name}-${pkg.version}. You can add
|
|
|
|
a hash through the `outputHashes` argument of `importCargoLock`:
|
|
|
|
|
|
|
|
outputHashes = {
|
|
|
|
"${pkg.name}-${pkg.version}" = "<hash>";
|
|
|
|
};
|
|
|
|
|
|
|
|
If you use `buildRustPackage`, you can add this attribute to the `cargoLock`
|
|
|
|
attribute set.
|
|
|
|
'';
|
|
|
|
sha256 = gitShaOutputHash.${gitParts.sha} or missingHash;
|
|
|
|
tree = fetchgit {
|
|
|
|
inherit sha256;
|
|
|
|
inherit (gitParts) url;
|
|
|
|
rev = gitParts.sha; # The commit SHA is always available.
|
|
|
|
};
|
|
|
|
in runCommand "${pkg.name}-${pkg.version}" {} ''
|
|
|
|
tree=${tree}
|
|
|
|
|
2021-10-24 02:01:07 +03:00
|
|
|
# If the target package is in a workspace, or if it's the top-level
|
|
|
|
# crate, we should find the crate path using `cargo metadata`.
|
2022-03-16 00:26:21 +03:00
|
|
|
# Some packages do not have a Cargo.toml at the top-level,
|
|
|
|
# but only in nested directories.
|
|
|
|
# Only check the top-level Cargo.toml, if it actually exists
|
|
|
|
if [[ -f $tree/Cargo.toml ]]; then
|
|
|
|
crateCargoTOML=$(${cargo}/bin/cargo metadata --format-version 1 --no-deps --manifest-path $tree/Cargo.toml | \
|
2021-10-24 02:01:07 +03:00
|
|
|
${jq}/bin/jq -r '.packages[] | select(.name == "${pkg.name}") | .manifest_path')
|
2022-03-16 00:26:21 +03:00
|
|
|
fi
|
2021-10-24 02:01:07 +03:00
|
|
|
|
|
|
|
# If the repository is not a workspace the package might be in a subdirectory.
|
|
|
|
if [[ -z $crateCargoTOML ]]; then
|
|
|
|
for manifest in $(find $tree -name "Cargo.toml"); do
|
|
|
|
echo Looking at $manifest
|
|
|
|
crateCargoTOML=$(${cargo}/bin/cargo metadata --format-version 1 --no-deps --manifest-path "$manifest" | ${jq}/bin/jq -r '.packages[] | select(.name == "${pkg.name}") | .manifest_path' || :)
|
2021-05-08 08:40:34 +03:00
|
|
|
if [[ ! -z $crateCargoTOML ]]; then
|
2021-10-24 02:01:07 +03:00
|
|
|
break
|
2021-05-08 08:40:34 +03:00
|
|
|
fi
|
2021-10-24 02:01:07 +03:00
|
|
|
done
|
|
|
|
|
|
|
|
if [[ -z $crateCargoTOML ]]; then
|
|
|
|
>&2 echo "Cannot find path for crate '${pkg.name}-${pkg.version}' in the tree in: $tree"
|
|
|
|
exit 1
|
|
|
|
fi
|
2021-05-08 08:40:34 +03:00
|
|
|
fi
|
|
|
|
|
2021-10-24 02:01:07 +03:00
|
|
|
echo Found crate ${pkg.name} at $crateCargoTOML
|
|
|
|
tree=$(dirname $crateCargoTOML)
|
|
|
|
|
2021-05-08 08:40:34 +03:00
|
|
|
cp -prvd "$tree/" $out
|
|
|
|
chmod u+w $out
|
|
|
|
|
|
|
|
# Cargo is happy with empty metadata.
|
|
|
|
printf '{"files":{},"package":null}' > "$out/.cargo-checksum.json"
|
|
|
|
|
|
|
|
# Set up configuration for the vendor directory.
|
|
|
|
cat > $out/.cargo-config <<EOF
|
|
|
|
[source."${gitParts.url}"]
|
|
|
|
git = "${gitParts.url}"
|
2021-09-10 20:30:05 +03:00
|
|
|
${lib.optionalString (gitParts ? type) "${gitParts.type} = \"${gitParts.value}\""}
|
2021-05-08 08:40:34 +03:00
|
|
|
replace-with = "vendored-sources"
|
|
|
|
EOF
|
|
|
|
''
|
|
|
|
else throw "Cannot handle crate source: ${pkg.source}";
|
|
|
|
|
2021-09-10 20:09:29 +03:00
|
|
|
vendorDir = runCommand "cargo-vendor-dir" (lib.optionalAttrs (lockFile == null) {
|
|
|
|
inherit lockFileContents;
|
|
|
|
passAsFile = [ "lockFileContents" ];
|
|
|
|
}) ''
|
2021-05-08 08:40:34 +03:00
|
|
|
mkdir -p $out/.cargo
|
|
|
|
|
2021-09-10 20:09:29 +03:00
|
|
|
${
|
|
|
|
if lockFile != null
|
|
|
|
then "ln -s ${lockFile} $out/Cargo.lock"
|
|
|
|
else "cp $lockFileContentsPath $out/Cargo.lock"
|
|
|
|
}
|
2021-05-08 08:40:34 +03:00
|
|
|
|
|
|
|
cat > $out/.cargo/config <<EOF
|
|
|
|
[source.crates-io]
|
|
|
|
replace-with = "vendored-sources"
|
|
|
|
|
|
|
|
[source.vendored-sources]
|
|
|
|
directory = "cargo-vendor-dir"
|
|
|
|
EOF
|
|
|
|
|
|
|
|
declare -A keysSeen
|
|
|
|
|
|
|
|
for crate in ${toString depCrates}; do
|
|
|
|
# Link the crate directory, removing the output path hash from the destination.
|
|
|
|
ln -s "$crate" $out/$(basename "$crate" | cut -c 34-)
|
|
|
|
|
|
|
|
if [ -e "$crate/.cargo-config" ]; then
|
|
|
|
key=$(sed 's/\[source\."\(.*\)"\]/\1/; t; d' < "$crate/.cargo-config")
|
|
|
|
if [[ -z ''${keysSeen[$key]} ]]; then
|
|
|
|
keysSeen[$key]=1
|
|
|
|
cat "$crate/.cargo-config" >> $out/.cargo/config
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
vendorDir
|