From 5c49d0b5d235e1cbec31b3225c338781f6c7c506 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 1 Nov 2024 15:34:48 +0100 Subject: [PATCH] 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). --- src/libfetchers/fetchers.cc | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/libfetchers/fetchers.cc b/src/libfetchers/fetchers.cc index cea6e43ae..cce1971ff 100644 --- a/src/libfetchers/fetchers.cc +++ b/src/libfetchers/fetchers.cc @@ -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, Input> Input::getAccessor(ref store) const