1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-03 06:11:46 +02:00

Provide a structured JSON serialisation of hashes

This commit is contained in:
Eelco Dolstra 2025-03-13 18:52:29 +01:00
parent 2a2af3f72f
commit c515bc66f1
3 changed files with 19 additions and 2 deletions

View file

@ -2659,8 +2659,8 @@ SingleDrvOutputs LocalDerivationGoal::registerOutputs()
act->result(resHashMismatch, act->result(resHashMismatch,
{ {
{"storePath", worker.store.printStorePath(drvPath)}, {"storePath", worker.store.printStorePath(drvPath)},
{"wanted", wanted.to_string(HashFormat::SRI, true)}, {"wanted", wanted},
{"got", got.to_string(HashFormat::SRI, true)}, {"got", got},
}); });
} }
if (!newInfo0.references.empty()) { if (!newInfo0.references.empty()) {

View file

@ -14,6 +14,8 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <nlohmann/json.hpp>
#include <sodium.h> #include <sodium.h>
namespace nix { 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)},
});
}
} }

View file

@ -5,6 +5,8 @@
#include "serialise.hh" #include "serialise.hh"
#include "file-system.hh" #include "file-system.hh"
#include <nlohmann/json_fwd.hpp>
namespace nix { namespace nix {
@ -209,6 +211,10 @@ std::optional<HashAlgorithm> parseHashAlgoOpt(std::string_view s);
*/ */
std::string_view printHashAlgo(HashAlgorithm ha); std::string_view printHashAlgo(HashAlgorithm ha);
/**
* Write a JSON serialisation of the format `{"algo":"<sha1|...>","base16":"<hex>"}`.
*/
void to_json(nlohmann::json & json, const Hash & hash);
union Ctx; union Ctx;