core/pkgs/build-support/rust/import-cargo-lock.nix

305 lines
9.8 KiB
Nix
Raw Normal View History

2024-06-30 08:16:52 +00:00
{
fetchgit,
fetchurl,
lib,
writers,
python3Packages,
runCommand,
cargo,
jq,
}:
2024-05-02 00:46:19 +00:00
{
# Cargo lock file
2024-06-30 08:16:52 +00:00
lockFile ? null,
2024-05-02 00:46:19 +00:00
# Cargo lock file contents as string
2024-06-30 08:16:52 +00:00
lockFileContents ? null,
2024-05-02 00:46:19 +00:00
# Allow `builtins.fetchGit` to be used to not require hashes for git dependencies
2024-06-30 08:16:52 +00:00
allowBuiltinFetchGit ? false,
2024-05-02 00:46:19 +00:00
# Additional registries to pull sources from
# { "https://<registry index URL>" = "https://<registry download URL>"; }
# where:
# - "index URL" is the "index" value of the configuration entry for that registry
# https://doc.rust-lang.org/cargo/reference/registries.html#using-an-alternate-registry
# - "download URL" is the "dl" value of its associated index configuration
# https://doc.rust-lang.org/cargo/reference/registry-index.html#index-configuration
2024-06-30 08:16:52 +00:00
extraRegistries ? { },
2024-05-02 00:46:19 +00:00
# Hashes for git dependencies.
2024-06-30 08:16:52 +00:00
outputHashes ? { },
}@args:
2024-05-02 00:46:19 +00:00
assert (lockFile == null) != (lockFileContents == null);
let
# Parse a git source into different components.
2024-06-30 08:16:52 +00:00
parseGit =
src:
2024-05-02 00:46:19 +00:00
let
parts = builtins.match ''git\+([^?]+)(\?(rev|tag|branch)=(.*))?#(.*)'' src;
type = builtins.elemAt parts 2; # rev, tag or branch
value = builtins.elemAt parts 3;
in
2024-06-30 08:16:52 +00:00
if parts == null then
null
else
{
2024-05-02 00:46:19 +00:00
url = builtins.elemAt parts 0;
sha = builtins.elemAt parts 4;
2024-06-30 08:16:52 +00:00
}
// lib.optionalAttrs (type != null) { inherit type value; };
2024-05-02 00:46:19 +00:00
# shadows args.lockFileContents
2024-06-30 08:16:52 +00:00
lockFileContents = if lockFile != null then builtins.readFile lockFile else args.lockFileContents;
2024-05-02 00:46:19 +00:00
parsedLockFile = builtins.fromTOML lockFileContents;
packages = parsedLockFile.package;
# 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)
);
2024-06-30 08:16:52 +00:00
nameGitSha =
pkg:
let
gitParts = parseGit pkg.source;
in
{
name = "${pkg.name}-${pkg.version}";
value = gitParts.sha;
};
2024-05-02 00:46:19 +00:00
# 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.
2024-06-30 08:16:52 +00:00
gitShaOutputHash = lib.mapAttrs' (
nameVer: hash:
2024-05-02 00:46:19 +00:00
let
unusedHash = throw "A hash was specified for ${nameVer}, but there is no corresponding git dependency.";
2024-06-30 08:16:52 +00:00
rev = namesGitShas.${nameVer} or unusedHash;
in
{
2024-05-02 00:46:19 +00:00
name = rev;
value = hash;
2024-06-30 08:16:52 +00:00
}
) outputHashes;
2024-05-02 00:46:19 +00:00
# We can't use the existing fetchCrate function, since it uses a
# recursive hash of the unpacked crate.
2024-06-30 08:16:52 +00:00
fetchCrate =
pkg: downloadUrl:
2024-05-02 00:46:19 +00:00
let
2024-06-30 08:16:52 +00:00
checksum =
pkg.checksum or parsedLockFile.metadata."checksum ${pkg.name} ${pkg.version} (${pkg.source})";
2024-05-02 00:46:19 +00:00
in
assert lib.assertMsg (checksum != null) ''
Package ${pkg.name} does not have a checksum.
'';
fetchurl {
name = "crate-${pkg.name}-${pkg.version}.tar.gz";
url = "${downloadUrl}/${pkg.name}/${pkg.version}/download";
sha256 = checksum;
};
registries = {
"https://github.com/rust-lang/crates.io-index" = "https://crates.io/api/v1/crates";
} // extraRegistries;
# Replaces values inherited by workspace members.
2024-06-30 08:16:52 +00:00
replaceWorkspaceValues = writers.writePython3 "replace-workspace-values" {
libraries = with python3Packages; [
tomli
tomli-w
];
flakeIgnore = [
"E501"
"W503"
];
} (builtins.readFile ./replace-workspace-values.py);
2024-05-02 00:46:19 +00:00
# Fetch and unpack a crate.
2024-06-30 08:16:52 +00:00
mkCrate =
pkg:
2024-05-02 00:46:19 +00:00
let
gitParts = parseGit pkg.source;
registryIndexUrl = lib.removePrefix "registry+" pkg.source;
in
2024-06-30 08:16:52 +00:00
if lib.hasPrefix "registry+" pkg.source && builtins.hasAttr registryIndexUrl registries then
2024-05-02 00:46:19 +00:00
let
crateTarball = fetchCrate pkg registries.${registryIndexUrl};
2024-06-30 08:16:52 +00:00
in
runCommand "${pkg.name}-${pkg.version}" { } ''
2024-05-02 00:46:19 +00:00
mkdir $out
tar xf "${crateTarball}" -C $out --strip-components=1
# Cargo is happy with largely empty metadata.
printf '{"files":{},"package":"${crateTarball.outputHash}"}' > "$out/.cargo-checksum.json"
''
2024-06-30 08:16:52 +00:00
else if gitParts != null then
2024-05-02 00:46:19 +00:00
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.
'';
tree =
if gitShaOutputHash ? ${gitParts.sha} then
fetchgit {
inherit (gitParts) url;
rev = gitParts.sha; # The commit SHA is always available.
sha256 = gitShaOutputHash.${gitParts.sha};
}
else if allowBuiltinFetchGit then
builtins.fetchGit {
inherit (gitParts) url;
rev = gitParts.sha;
allRefs = true;
submodules = true;
}
else
missingHash;
2024-06-30 08:16:52 +00:00
in
runCommand "${pkg.name}-${pkg.version}" { } ''
2024-05-02 00:46:19 +00:00
tree=${tree}
# 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`.
# 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 | \
${jq}/bin/jq -r '.packages[] | select(.name == "${pkg.name}") | .manifest_path')
fi
# 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' || :)
if [[ ! -z $crateCargoTOML ]]; then
break
fi
done
if [[ -z $crateCargoTOML ]]; then
>&2 echo "Cannot find path for crate '${pkg.name}-${pkg.version}' in the tree in: $tree"
exit 1
fi
fi
echo Found crate ${pkg.name} at $crateCargoTOML
tree=$(dirname $crateCargoTOML)
cp -prvL "$tree/" $out
chmod u+w $out
if grep -q workspace "$out/Cargo.toml"; then
chmod u+w "$out/Cargo.toml"
${replaceWorkspaceValues} "$out/Cargo.toml" "$(${cargo}/bin/cargo metadata --format-version 1 --no-deps --manifest-path $crateCargoTOML | ${jq}/bin/jq -r .workspace_root)/Cargo.toml"
fi
# 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
2024-06-30 08:16:52 +00:00
[source."${gitParts.url}${
lib.optionalString (gitParts ? type) "?${gitParts.type}=${gitParts.value}"
}"]
2024-05-02 00:46:19 +00:00
git = "${gitParts.url}"
${lib.optionalString (gitParts ? type) "${gitParts.type} = \"${gitParts.value}\""}
replace-with = "vendored-sources"
EOF
''
2024-06-30 08:16:52 +00:00
else
throw "Cannot handle crate source: ${pkg.source}";
vendorDir =
runCommand "cargo-vendor-dir"
(
if lockFile == null then
{
inherit lockFileContents;
passAsFile = [ "lockFileContents" ];
}
else
{
passthru = {
inherit lockFile;
};
}
)
''
mkdir -p $out/.cargo
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
${
if lockFile != null then
"ln -s ${lockFile} $out/Cargo.lock"
else
"cp $lockFileContentsPath $out/Cargo.lock"
}
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
cat > $out/.cargo/config <<EOF
[source.crates-io]
replace-with = "vendored-sources"
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
[source.vendored-sources]
directory = "cargo-vendor-dir"
EOF
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
declare -A keysSeen
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
for registry in ${toString (builtins.attrNames extraRegistries)}; do
cat >> $out/.cargo/config <<EOF
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
[source."$registry"]
registry = "$registry"
replace-with = "vendored-sources"
EOF
done
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
'';
2024-05-02 00:46:19 +00:00
in
2024-06-30 08:16:52 +00:00
vendorDir