Handle final handling for old lock files with improper narHash fields

This fixes the error

  '{"__final":true,"lastModified":1686592866,"narHash":"sha256-riGg89eWhXJcPNrQGcSwTEEm7CGxWC06oSX44hajeMw","owner":"nixos","repo":"nixpkgs","rev":"0eeebd64de89e4163f4d3cf34ffe925a5cf67a05","type":"github"}' resulted in different input
  '{"__final":true,"lastModified":1686592866,"narHash":"sha256-riGg89eWhXJcPNrQGcSwTEEm7CGxWC06oSX44hajeMw=","owner":"nixos","repo":"nixpkgs","rev":"0eeebd64de89e4163f4d3cf34ffe925a5cf67a05","type":"github"}'

in flake-regressions/tests/nix-community/patsh/0.2.1 (note the lack of
a trailing '=' in the NAR hash in the lock file).
This commit is contained in:
Eelco Dolstra 2024-11-01 15:34:48 +01:00
parent 4fcd458512
commit 5c49d0b5d2

View File

@ -236,9 +236,22 @@ void InputScheme::checkLocks(const Input & specified, const Input & final) const
final.to_string(), *prevRevCount);
}
if (specified.isFinal() && specified.attrs != final.attrs)
throw Error("fetching final input '%s' resulted in different input '%s'",
attrsToJSON(specified.attrs), attrsToJSON(final.attrs));
/* If the original input is final, then the result must be the
same (i.e. cannot remove, add or change fields. */
if (specified.isFinal()) {
/* Backwards compatibility hack: we had some lock files in the
past that 'narHash' fields with incorrect base-64
formatting (lacking the trailing '=', e.g. 'sha256-ri...Mw'
instead of ''sha256-ri...Mw='). So fix */
auto specified2 = specified;
if (auto prevNarHash = specified2.getNarHash())
specified2.attrs.insert_or_assign("narHash", prevNarHash->to_string(HashFormat::SRI, true));
if (specified2.attrs != final.attrs)
throw Error("fetching final input '%s' resulted in different input '%s'",
attrsToJSON(specified2.attrs), attrsToJSON(final.attrs));
}
}
std::pair<ref<SourceAccessor>, Input> Input::getAccessor(ref<Store> store) const