From d7c37324bb77f8348a11f7fbf168fc1a085f2505 Mon Sep 17 00:00:00 2001 From: Alois Wohlschlager Date: Sat, 24 Aug 2024 14:39:45 +0200 Subject: [PATCH 1/2] libstore: declare SandboxMode JSON serialisation in the header The JSON serialisation should be declared in the header so that all translation units can see it when needed, even though it seems that it has not been used anywhere else so far. Unfortunately, this means we cannot use the NLOHMANN_JSON_SERIALIZE_ENUM convenience macro, since it uses a slightly different signature, but the code is not too bad either. Change-Id: I6e2851b250e0b53114d2fecb8011ff1ea9379d0f --- src/libstore/globals.cc | 30 +++++++++++++++++++++++++----- src/libstore/globals.hh | 3 +++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index c114e22dc..ffc2543ef 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -269,11 +269,31 @@ Path Settings::getDefaultSSLCertFile() const std::string nixVersion = PACKAGE_VERSION; -NLOHMANN_JSON_SERIALIZE_ENUM(SandboxMode, { - {SandboxMode::smEnabled, true}, - {SandboxMode::smRelaxed, "relaxed"}, - {SandboxMode::smDisabled, false}, -}); +void to_json(nlohmann::json & j, const SandboxMode & e) +{ + if (e == SandboxMode::smEnabled) { + j = true; + } else if (e == SandboxMode::smRelaxed) { + j = "relaxed"; + } else if (e == SandboxMode::smDisabled) { + j = false; + } else { + abort(); + } +} + +void from_json(const nlohmann::json & j, SandboxMode & e) +{ + if (j == true) { + e = SandboxMode::smEnabled; + } else if (j == "relaxed") { + e = SandboxMode::smRelaxed; + } else if (j == false) { + e = SandboxMode::smDisabled; + } else { + throw Error("Invalid sandbox mode '%s'", std::string(j)); + } +} template<> SandboxMode BaseSetting::parse(const std::string & str, const ApplyConfigOptions & options) const { diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh index 51550b2c3..ca2a917ed 100644 --- a/src/libstore/globals.hh +++ b/src/libstore/globals.hh @@ -14,6 +14,9 @@ namespace nix { typedef enum { smEnabled, smRelaxed, smDisabled } SandboxMode; +void to_json(nlohmann::json & j, const SandboxMode & e); +void from_json(const nlohmann::json & j, SandboxMode & e); + struct MaxBuildJobsSetting : public BaseSetting { MaxBuildJobsSetting(Config * options, From 63ee2cdda36e48f932dd5a1ae7f35f85687ce1f5 Mon Sep 17 00:00:00 2001 From: Alois Wohlschlager Date: Tue, 20 Aug 2024 19:36:49 +0200 Subject: [PATCH 2/2] libfetchers: serialise accept-flake-config properly The AcceptFlakeConfig type used was missing its JSON serialisation definition, so it was incorrectly serialised as an integer, ending up that way for example in the nix.conf manual page. Declare a proper serialisation. Change-Id: If8ec210f9d4dd42fe480c4e97d0a4920eb66a01e --- src/libfetchers/fetch-settings.cc | 26 ++++++++++++++++++++++++++ src/libfetchers/fetch-settings.hh | 3 +++ 2 files changed, 29 insertions(+) diff --git a/src/libfetchers/fetch-settings.cc b/src/libfetchers/fetch-settings.cc index 007f2725f..b278835ad 100644 --- a/src/libfetchers/fetch-settings.cc +++ b/src/libfetchers/fetch-settings.cc @@ -7,6 +7,32 @@ namespace nix { +void to_json(nlohmann::json & j, const AcceptFlakeConfig & e) +{ + if (e == AcceptFlakeConfig::False) { + j = false; + } else if (e == AcceptFlakeConfig::Ask) { + j = "ask"; + } else if (e == AcceptFlakeConfig::True) { + j = true; + } else { + abort(); + } +} + +void from_json(const nlohmann::json & j, AcceptFlakeConfig & e) +{ + if (j == false) { + e = AcceptFlakeConfig::False; + } else if (j == "ask") { + e = AcceptFlakeConfig::Ask; + } else if (j == true) { + e = AcceptFlakeConfig::True; + } else { + throw Error("Invalid accept-flake-config value '%s'", std::string(j)); + } +} + template<> AcceptFlakeConfig BaseSetting::parse(const std::string & str, const ApplyConfigOptions & options) const { if (str == "true") return AcceptFlakeConfig::True; diff --git a/src/libfetchers/fetch-settings.hh b/src/libfetchers/fetch-settings.hh index 93123463c..0bdc707ec 100644 --- a/src/libfetchers/fetch-settings.hh +++ b/src/libfetchers/fetch-settings.hh @@ -13,6 +13,9 @@ namespace nix { enum class AcceptFlakeConfig { False, Ask, True }; +void to_json(nlohmann::json & j, const AcceptFlakeConfig & e); +void from_json(const nlohmann::json & j, AcceptFlakeConfig & e); + struct FetchSettings : public Config { FetchSettings();