1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-15 06:52:43 +01:00

Merge pull request #14057 from obsidiansystems/derived-path-json

Modernize and test derived path JSON
This commit is contained in:
John Ericson 2025-09-24 18:08:06 -04:00 committed by GitHub
commit 9f26c20ebd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 300 additions and 69 deletions

View file

@ -2,8 +2,7 @@
#include "nix/store/derivations.hh"
#include "nix/store/store-api.hh"
#include "nix/util/comparator.hh"
#include <nlohmann/json.hpp>
#include "nix/util/json-utils.hh"
#include <optional>
@ -19,59 +18,6 @@ GENERATE_CMP_EXT(, std::strong_ordering, SingleDerivedPathBuilt, *me->drvPath, m
GENERATE_EQUAL(, DerivedPathBuilt ::, DerivedPathBuilt, *me->drvPath, me->outputs);
GENERATE_ONE_CMP(, bool, DerivedPathBuilt ::, <, DerivedPathBuilt, *me->drvPath, me->outputs);
nlohmann::json DerivedPath::Opaque::toJSON(const StoreDirConfig & store) const
{
return store.printStorePath(path);
}
nlohmann::json SingleDerivedPath::Built::toJSON(Store & store) const
{
nlohmann::json res;
res["drvPath"] = drvPath->toJSON(store);
// Fallback for the input-addressed derivation case: We expect to always be
// able to print the output paths, so lets do it
// FIXME try-resolve on drvPath
const auto outputMap = store.queryPartialDerivationOutputMap(resolveDerivedPath(store, *drvPath));
res["output"] = output;
auto outputPathIter = outputMap.find(output);
if (outputPathIter == outputMap.end())
res["outputPath"] = nullptr;
else if (std::optional p = outputPathIter->second)
res["outputPath"] = store.printStorePath(*p);
else
res["outputPath"] = nullptr;
return res;
}
nlohmann::json DerivedPath::Built::toJSON(Store & store) const
{
nlohmann::json res;
res["drvPath"] = drvPath->toJSON(store);
// Fallback for the input-addressed derivation case: We expect to always be
// able to print the output paths, so lets do it
// FIXME try-resolve on drvPath
const auto outputMap = store.queryPartialDerivationOutputMap(resolveDerivedPath(store, *drvPath));
for (const auto & [output, outputPathOpt] : outputMap) {
if (!outputs.contains(output))
continue;
if (outputPathOpt)
res["outputs"][output] = store.printStorePath(*outputPathOpt);
else
res["outputs"][output] = nullptr;
}
return res;
}
nlohmann::json SingleDerivedPath::toJSON(Store & store) const
{
return std::visit([&](const auto & buildable) { return buildable.toJSON(store); }, raw());
}
nlohmann::json DerivedPath::toJSON(Store & store) const
{
return std::visit([&](const auto & buildable) { return buildable.toJSON(store); }, raw());
}
std::string DerivedPath::Opaque::to_string(const StoreDirConfig & store) const
{
return store.printStorePath(path);
@ -273,3 +219,77 @@ const StorePath & DerivedPath::getBaseStorePath() const
}
} // namespace nix
namespace nlohmann {
void adl_serializer<SingleDerivedPath::Opaque>::to_json(json & json, const SingleDerivedPath::Opaque & o)
{
json = o.path;
}
SingleDerivedPath::Opaque adl_serializer<SingleDerivedPath::Opaque>::from_json(const json & json)
{
return SingleDerivedPath::Opaque{json};
}
void adl_serializer<SingleDerivedPath::Built>::to_json(json & json, const SingleDerivedPath::Built & sdpb)
{
json = {
{"drvPath", *sdpb.drvPath},
{"output", sdpb.output},
};
}
void adl_serializer<DerivedPath::Built>::to_json(json & json, const DerivedPath::Built & dbp)
{
json = {
{"drvPath", *dbp.drvPath},
{"outputs", dbp.outputs},
};
}
SingleDerivedPath::Built adl_serializer<SingleDerivedPath::Built>::from_json(const json & json0)
{
auto & json = getObject(json0);
return {
.drvPath = make_ref<SingleDerivedPath>(static_cast<SingleDerivedPath>(valueAt(json, "drvPath"))),
.output = getString(valueAt(json, "output")),
};
}
DerivedPath::Built adl_serializer<DerivedPath::Built>::from_json(const json & json0)
{
auto & json = getObject(json0);
return {
.drvPath = make_ref<SingleDerivedPath>(static_cast<SingleDerivedPath>(valueAt(json, "drvPath"))),
.outputs = adl_serializer<OutputsSpec>::from_json(valueAt(json, "outputs")),
};
}
void adl_serializer<SingleDerivedPath>::to_json(json & json, const SingleDerivedPath & sdp)
{
std::visit([&](const auto & buildable) { json = buildable; }, sdp.raw());
}
void adl_serializer<DerivedPath>::to_json(json & json, const DerivedPath & sdp)
{
std::visit([&](const auto & buildable) { json = buildable; }, sdp.raw());
}
SingleDerivedPath adl_serializer<SingleDerivedPath>::from_json(const json & json)
{
if (json.is_string())
return static_cast<SingleDerivedPath::Opaque>(json);
else
return static_cast<SingleDerivedPath::Built>(json);
}
DerivedPath adl_serializer<DerivedPath>::from_json(const json & json)
{
if (json.is_string())
return static_cast<DerivedPath::Opaque>(json);
else
return static_cast<DerivedPath::Built>(json);
}
} // namespace nlohmann

View file

@ -5,6 +5,7 @@
#include "nix/store/outputs-spec.hh"
#include "nix/util/configuration.hh"
#include "nix/util/ref.hh"
#include "nix/util/json-impls.hh"
#include <variant>
@ -14,9 +15,6 @@ namespace nix {
struct StoreDirConfig;
// TODO stop needing this, `toJSON` below should be pure
class Store;
/**
* An opaque derived path.
*
@ -30,7 +28,6 @@ struct DerivedPathOpaque
std::string to_string(const StoreDirConfig & store) const;
static DerivedPathOpaque parse(const StoreDirConfig & store, std::string_view);
nlohmann::json toJSON(const StoreDirConfig & store) const;
bool operator==(const DerivedPathOpaque &) const = default;
auto operator<=>(const DerivedPathOpaque &) const = default;
@ -80,7 +77,6 @@ struct SingleDerivedPathBuilt
ref<const SingleDerivedPath> drvPath,
OutputNameView outputs,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
nlohmann::json toJSON(Store & store) const;
bool operator==(const SingleDerivedPathBuilt &) const noexcept;
std::strong_ordering operator<=>(const SingleDerivedPathBuilt &) const noexcept;
@ -153,7 +149,6 @@ struct SingleDerivedPath : _SingleDerivedPathRaw
const StoreDirConfig & store,
std::string_view,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
nlohmann::json toJSON(Store & store) const;
};
static inline ref<SingleDerivedPath> makeConstantStorePathRef(StorePath drvPath)
@ -208,7 +203,6 @@ struct DerivedPathBuilt
ref<const SingleDerivedPath>,
std::string_view,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
nlohmann::json toJSON(Store & store) const;
bool operator==(const DerivedPathBuilt &) const noexcept;
// TODO libc++ 16 (used by darwin) missing `std::set::operator <=>`, can't do yet.
@ -287,8 +281,6 @@ struct DerivedPath : _DerivedPathRaw
* Convert a `SingleDerivedPath` to a `DerivedPath`.
*/
static DerivedPath fromSingle(const SingleDerivedPath &);
nlohmann::json toJSON(Store & store) const;
};
typedef std::vector<DerivedPath> DerivedPaths;
@ -305,3 +297,9 @@ typedef std::vector<DerivedPath> DerivedPaths;
void drvRequireExperiment(
const SingleDerivedPath & drv, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
} // namespace nix
JSON_IMPL(nix::SingleDerivedPath::Opaque)
JSON_IMPL(nix::SingleDerivedPath::Built)
JSON_IMPL(nix::SingleDerivedPath)
JSON_IMPL(nix::DerivedPath::Built)
JSON_IMPL(nix::DerivedPath)