1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-28 13:11:00 +01:00

Merge pull request #14632 from NixOS/path-setting

Add `Setting<std::filesystem::path>` and `Setting<std::optional<std::filesystem::path>>` specializations
This commit is contained in:
John Ericson 2025-11-27 00:29:28 +00:00 committed by GitHub
commit 35492fe94a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 50 additions and 12 deletions

View file

@ -433,6 +433,42 @@ std::string BaseSetting<StringMap>::to_string() const
[](const auto & kvpair) { return kvpair.first + "=" + kvpair.second; });
}
static Path parsePath(const AbstractSetting & s, const std::string & str)
{
if (str == "")
throw UsageError("setting '%s' is a path and paths cannot be empty", s.name);
else
return canonPath(str);
}
template<>
std::filesystem::path BaseSetting<std::filesystem::path>::parse(const std::string & str) const
{
return parsePath(*this, str);
}
template<>
std::string BaseSetting<std::filesystem::path>::to_string() const
{
return value.string();
}
template<>
std::optional<std::filesystem::path>
BaseSetting<std::optional<std::filesystem::path>>::parse(const std::string & str) const
{
if (str == "")
return std::nullopt;
else
return parsePath(*this, str);
}
template<>
std::string BaseSetting<std::optional<std::filesystem::path>>::to_string() const
{
return value ? value->string() : "";
}
template class BaseSetting<int>;
template class BaseSetting<unsigned int>;
template class BaseSetting<long>;
@ -445,14 +481,8 @@ template class BaseSetting<Strings>;
template class BaseSetting<StringSet>;
template class BaseSetting<StringMap>;
template class BaseSetting<std::set<ExperimentalFeature>>;
static Path parsePath(const AbstractSetting & s, const std::string & str)
{
if (str == "")
throw UsageError("setting '%s' is a path and paths cannot be empty", s.name);
else
return canonPath(str);
}
template class BaseSetting<std::filesystem::path>;
template class BaseSetting<std::optional<std::filesystem::path>>;
PathSetting::PathSetting(
Config * options,

View file

@ -16,6 +16,7 @@
#include "nix/util/configuration.hh"
#include "nix/util/args.hh"
#include "nix/util/logging.hh"
#include "nix/util/file-path.hh"
namespace nix {
@ -134,6 +135,8 @@ DECLARE_CONFIG_SERIALISER(Strings)
DECLARE_CONFIG_SERIALISER(StringSet)
DECLARE_CONFIG_SERIALISER(StringMap)
DECLARE_CONFIG_SERIALISER(std::set<ExperimentalFeature>)
DECLARE_CONFIG_SERIALISER(std::filesystem::path)
DECLARE_CONFIG_SERIALISER(std::optional<std::filesystem::path>)
template<typename T>
T BaseSetting<T>::parse(const std::string & str) const

View file

@ -5,6 +5,7 @@
#include "nix/util/types.hh"
#include "nix/util/os-string.hh"
#include "nix/util/json-non-null.hh"
namespace nix {
@ -53,4 +54,8 @@ std::optional<std::filesystem::path> maybePath(PathView path);
std::filesystem::path pathNG(PathView path);
template<>
struct json_avoids_null<std::filesystem::path> : std::true_type
{};
} // namespace nix

View file

@ -54,9 +54,9 @@ struct LoggerSettings : Config
expression evaluation errors.
)"};
Setting<Path> jsonLogPath{
Setting<std::optional<std::filesystem::path>> jsonLogPath{
this,
"",
{},
"json-log-path",
R"(
A file or unix socket to which JSON records of Nix's log output are

View file

@ -367,10 +367,10 @@ std::unique_ptr<Logger> makeJSONLogger(const std::filesystem::path & path, bool
void applyJSONLogger()
{
if (!loggerSettings.jsonLogPath.get().empty()) {
if (auto & opt = loggerSettings.jsonLogPath.get()) {
try {
std::vector<std::unique_ptr<Logger>> loggers;
loggers.push_back(makeJSONLogger(std::filesystem::path(loggerSettings.jsonLogPath.get()), false));
loggers.push_back(makeJSONLogger(*opt, false));
try {
logger = makeTeeLogger(std::move(logger), std::move(loggers));
} catch (...) {