Skip to content

Commit

Permalink
rustPlatform.importCargoLock: support lockfile v4 escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
TomaSajt authored and JohnRTitor committed Jan 8, 2025
1 parent 1db27e9 commit 0bfdb03
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pkgs/build-support/rust/import-cargo-lock.nix
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ let

parsedLockFile = builtins.fromTOML lockFileContents;

# lockfile v1 and v2 don't have the `version` key, so assume v2
# we can implement more fine-grained detection later, if needed
lockFileVersion = parsedLockFile.version or 2;

packages = parsedLockFile.package;

# There is no source attribute for the source package itself. But
Expand Down Expand Up @@ -202,11 +206,20 @@ let
# Cargo is happy with empty metadata.
printf '{"files":{},"package":null}' > "$out/.cargo-checksum.json"
${lib.optionalString (gitParts ? type) ''
gitPartsValue=${lib.escapeShellArg gitParts.value}
# starting with lockfile version v4 the git source url contains encoded query parameters
# our regex parser does not know how to unescape them to get the actual value, so we do it here
${lib.optionalString (lockFileVersion >= 4) ''
gitPartsValue=$(${lib.getExe python3Packages.python} -c "import sys, urllib.parse; print(urllib.parse.unquote(sys.argv[1]))" "$gitPartsValue")
''}
''}
# Set up configuration for the vendor directory.
cat > $out/.cargo-config <<EOF
[source."${gitParts.url}${lib.optionalString (gitParts ? type) "?${gitParts.type}=${gitParts.value}"}"]
[source."${pkg.source}"]
git = "${gitParts.url}"
${lib.optionalString (gitParts ? type) "${gitParts.type} = \"${gitParts.value}\""}
${lib.optionalString (gitParts ? type) "${gitParts.type} = \"$gitPartsValue\""}
replace-with = "vendored-sources"
EOF
''
Expand Down

0 comments on commit 0bfdb03

Please sign in to comment.