From c515bc66f1d8941290ef448eea4661b741a8fcc7 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 13 Mar 2025 18:52:29 +0100 Subject: [PATCH] Provide a structured JSON serialisation of hashes --- src/libstore/unix/build/local-derivation-goal.cc | 4 ++-- src/libutil/hash.cc | 11 +++++++++++ src/libutil/hash.hh | 6 ++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/libstore/unix/build/local-derivation-goal.cc b/src/libstore/unix/build/local-derivation-goal.cc index ec06c2044..cb3d4a04f 100644 --- a/src/libstore/unix/build/local-derivation-goal.cc +++ b/src/libstore/unix/build/local-derivation-goal.cc @@ -2659,8 +2659,8 @@ SingleDrvOutputs LocalDerivationGoal::registerOutputs() act->result(resHashMismatch, { {"storePath", worker.store.printStorePath(drvPath)}, - {"wanted", wanted.to_string(HashFormat::SRI, true)}, - {"got", got.to_string(HashFormat::SRI, true)}, + {"wanted", wanted}, + {"got", got}, }); } if (!newInfo0.references.empty()) { diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index b69dec685..9668800af 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -14,6 +14,8 @@ #include #include +#include + #include namespace nix { @@ -456,4 +458,13 @@ std::string_view printHashAlgo(HashAlgorithm ha) } } +void to_json(nlohmann::json & json, const Hash & hash) +{ + json = nlohmann::json::object( + { + {"algo", printHashAlgo(hash.algo)}, + {"base16", hash.to_string(HashFormat::Base16, false)}, + }); +} + } diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh index dc95b9f2f..3ef7e8b14 100644 --- a/src/libutil/hash.hh +++ b/src/libutil/hash.hh @@ -5,6 +5,8 @@ #include "serialise.hh" #include "file-system.hh" +#include + namespace nix { @@ -209,6 +211,10 @@ std::optional parseHashAlgoOpt(std::string_view s); */ std::string_view printHashAlgo(HashAlgorithm ha); +/** + * Write a JSON serialisation of the format `{"algo":"","base16":""}`. + */ +void to_json(nlohmann::json & json, const Hash & hash); union Ctx;