1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 06:31:14 +02:00
nix/src/libstore/config-parse.cc
John Ericson fe8b42df23 New store settings system
Motivation:

See the linked issues for details.

The most notable user-relevant bits are:

- This cleans up the `MountedSSHStore`: decomposed into its orthogonal parts

- This brings us pretty close to being able to then implement a JSON-based config.
   - Store query parameters can be JSON
   - Stores can entirely be specified via JSON objects, but this is not yet hooked up to anything.

Also behind the scenes have these benefits:

1. The docs are moved out of the headers, good for less rebuilding when they changes
2. Stores are always constructed from store configs
3. Use JSON, avoid custom serializers

Context:

Part of #11106

Co-Authored-By: Robert Hensing <robert@roberthensing.nl>
Co-authored-by: Sergei Zimmerman <145775305+xokdvium@users.noreply.github.com>
2025-05-23 16:49:57 -04:00

71 lines
2.5 KiB
C++

#include <nlohmann/json.hpp>
#include "nix/store/config-parse.hh"
#include "nix/util/json-utils.hh"
#include "nix/util/util.hh"
namespace nix::config {
};
namespace nlohmann {
using namespace nix::config;
SettingDescription adl_serializer<SettingDescription>::from_json(const json & json)
{
auto & obj = getObject(json);
return {
.description = getString(valueAt(obj, "description")),
.experimentalFeature = valueAt(obj, "experimentalFeature").get<std::optional<Xp>>(),
.info = [&]() -> decltype(SettingDescription::info) {
if (auto documentDefault = optionalValueAt(obj, "documentDefault")) {
return SettingDescription::Single{
.defaultValue = *documentDefault ? (std::optional<nlohmann::json>{valueAt(obj, "defaultValue")})
: (std::optional<nlohmann::json>{}),
};
} else {
auto & subObj = getObject(valueAt(obj, "subSettings"));
return SettingDescription::Sub{
.nullable = valueAt(subObj, "nullable"),
.map = valueAt(subObj, "map"),
};
}
}(),
};
}
void adl_serializer<SettingDescription>::to_json(json & obj, SettingDescription sd)
{
obj.emplace("description", sd.description);
// obj.emplace("aliases", sd.aliases);
obj.emplace("experimentalFeature", sd.experimentalFeature);
std::visit(
overloaded{
[&](const SettingDescription::Single & single) {
// Indicate the default value is JSON, rather than a legacy setting
// boolean or string.
//
// TODO remove if we no longer have the legacy setting system / the
// code handling doc rendering of the settings is decoupled.
obj.emplace("isJson", true);
// Cannot just use `null` because the default value might itself be
// `null`.
obj.emplace("documentDefault", single.defaultValue.has_value());
if (single.defaultValue.has_value())
obj.emplace("defaultValue", *single.defaultValue);
},
[&](const SettingDescription::Sub & sub) {
json subJson;
subJson.emplace("nullable", sub.nullable);
subJson.emplace("map", sub.map);
obj.emplace("subSettings", std::move(subJson));
},
},
sd.info);
}
}