From f2ce346679a6237499d0a40ecf871130d2c34e95 Mon Sep 17 00:00:00 2001 From: Kamen Mladenov Date: Mon, 17 Feb 2025 16:27:55 +0200 Subject: feat(zkvmLib): Handle duplicate entries in generated Cargo.lock file --- zkvmLib.nix | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/zkvmLib.nix b/zkvmLib.nix index f08cf12..c982e93 100644 --- a/zkvmLib.nix +++ b/zkvmLib.nix @@ -7,14 +7,72 @@ pkgs: guest: let fileset = ./.; }; - installPhase = '' + installPhase = let + # Since we're concatenating Cargo.lock files, duplicate package entries + # are inevitable and cargo crashes when it encounters them. + # We'll manually remove all duplicates and cargo will be happy. + # This is a disgusting hack, but it's the best I've come up with. + removeDuplicates = '' + BEGIN { + unique = 1 + } + + /^\[\[package\]\]/ { unique = 0; next } + + /^name = / { + match($0, /".*"/) + name = substr($0, RSTART + 1, RLENGTH - 2) + next + } + + name && /^version = / { + match($0, /".*"/) + version = substr($0, RSTART + 1, RLENGTH - 2) + next + } + + version && /^source = / { + match($0, /".*"/) + source = substr($0, RSTART + 1, RLENGTH - 2) + next + } + + source && /^checksum = / { + match($0, /".*"/) + checksum = substr($0, RSTART + 1, RLENGTH - 2) + next + } + + name && !unique { + unique = (index(versions[name], version) == 0) || + (source && index(sources[name], source) == 0) || + (checksum && index(checksums[name], checksum) == 0) + + if (unique) { + versions[name] = versions[name] version + sources[name] = sources[name] source + checksums[name] = checksums[name] checksum + + print "[[package]]" + print "name = \"" name "\"" + print "version = \"" version "\"" + if (source) print "source = \"" source "\"" + if (checksum) print "checksum = \"" checksum "\"" + } + name = ""; version = ""; source = ""; checksum = "" + } + + unique || /^$/ { print } + ''; + in '' mkdir -p "$out" cd zkvms/${args.pname} - cat ./host/Cargo.lock > "$out/Cargo.lock" - tail -n +4 ./guest/Cargo.lock >> "$out/Cargo.lock" - tail -n +4 ../../guests/${guest}/Cargo.lock >> "$out/Cargo.lock" + cat ./host/Cargo.lock > lockfile + tail -n +4 ./guest/Cargo.lock >> lockfile + tail -n +4 ../../guests/${guest}/Cargo.lock >> lockfile + awk '${removeDuplicates}' lockfile > "$out/Cargo.lock" ''; }; -- cgit v1.2.3