From 58f4d9ecd7d64cb3b30194a4532c116f322bd927 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Tue, 2 Jul 2024 14:57:31 -0400 Subject: [PATCH] WIP get settings info out of headers, out of libutil --- .../abstract-setting-to-json.hh | 0 src/{libutil => libmain}/args.cc | 0 src/{libutil => libmain}/args.hh | 0 src/{libutil => libmain}/args/root.hh | 0 src/{libutil => libmain}/config-global.cc | 0 src/{libutil => libmain}/config-global.hh | 0 src/{libutil => libmain}/config-impl.hh | 0 src/libmain/config-upstream.cc | 40 +++++++++++++++++ src/{libutil => libmain}/config.cc | 22 --------- src/{libutil => libmain}/config.hh | 3 -- src/libstore/derived-path.hh | 1 - src/libutil/archive.cc | 23 +++++----- src/libutil/archive.hh | 11 +++++ src/libutil/config-abstract.hh | 15 +++++++ src/libutil/experimental-feature-settings.cc | 37 +++++++++++++++ src/libutil/experimental-feature-settings.hh | 45 +++++++++++++++++++ src/libutil/experimental-features.hh | 1 + src/libutil/fs-sink.cc | 11 +---- src/libutil/fs-sink.hh | 10 +++++ src/libutil/git.cc | 2 +- src/libutil/git.hh | 1 + src/libutil/hash.cc | 1 - src/libutil/logging.cc | 9 ++-- src/libutil/logging.hh | 14 +++--- src/libutil/meson.build | 3 +- 25 files changed, 184 insertions(+), 65 deletions(-) rename src/{libutil => libmain}/abstract-setting-to-json.hh (100%) rename src/{libutil => libmain}/args.cc (100%) rename src/{libutil => libmain}/args.hh (100%) rename src/{libutil => libmain}/args/root.hh (100%) rename src/{libutil => libmain}/config-global.cc (100%) rename src/{libutil => libmain}/config-global.hh (100%) rename src/{libutil => libmain}/config-impl.hh (100%) create mode 100644 src/libmain/config-upstream.cc rename src/{libutil => libmain}/config.cc (95%) rename src/{libutil => libmain}/config.hh (98%) create mode 100644 src/libutil/config-abstract.hh create mode 100644 src/libutil/experimental-feature-settings.cc create mode 100644 src/libutil/experimental-feature-settings.hh diff --git a/src/libutil/abstract-setting-to-json.hh b/src/libmain/abstract-setting-to-json.hh similarity index 100% rename from src/libutil/abstract-setting-to-json.hh rename to src/libmain/abstract-setting-to-json.hh diff --git a/src/libutil/args.cc b/src/libmain/args.cc similarity index 100% rename from src/libutil/args.cc rename to src/libmain/args.cc diff --git a/src/libutil/args.hh b/src/libmain/args.hh similarity index 100% rename from src/libutil/args.hh rename to src/libmain/args.hh diff --git a/src/libutil/args/root.hh b/src/libmain/args/root.hh similarity index 100% rename from src/libutil/args/root.hh rename to src/libmain/args/root.hh diff --git a/src/libutil/config-global.cc b/src/libmain/config-global.cc similarity index 100% rename from src/libutil/config-global.cc rename to src/libmain/config-global.cc diff --git a/src/libutil/config-global.hh b/src/libmain/config-global.hh similarity index 100% rename from src/libutil/config-global.hh rename to src/libmain/config-global.hh diff --git a/src/libutil/config-impl.hh b/src/libmain/config-impl.hh similarity index 100% rename from src/libutil/config-impl.hh rename to src/libmain/config-impl.hh diff --git a/src/libmain/config-upstream.cc b/src/libmain/config-upstream.cc new file mode 100644 index 000000000000..b39d26c2aa9f --- /dev/null +++ b/src/libmain/config-upstream.cc @@ -0,0 +1,40 @@ +namespace nix { + +#include "fs-sink.hh" +#include "logging.hh" + +struct LoggerSettings : Config +{ + Setting showTrace{ + this, false, "show-trace", + R"( + Whether Nix should print out a stack trace in case of Nix + expression evaluation errors. + )"}; +}; + +static GlobalConfig::Register r1(&restoreSinkSettings); + +struct RestoreSinkSettings : Config +{ + Setting preallocateContents{this, false, "preallocate-contents", + "Whether to preallocate files when writing objects with known size."}; +}; + +static GlobalConfig::Register rLoggerSettings(&loggerSettings); + +struct ArchiveSettings : Config +{ + Setting useCaseHack{this, + #if __APPLE__ + true, + #else + false, + #endif + "use-case-hack", + "Whether to enable a Darwin-specific hack for dealing with file name collisions."}; +}; + +static GlobalConfig::Register rArchiveSettings(&archiveSettings); + +} diff --git a/src/libutil/config.cc b/src/libmain/config.cc similarity index 95% rename from src/libutil/config.cc rename to src/libmain/config.cc index 192a4ecb9514..af80b257aab1 100644 --- a/src/libutil/config.cc +++ b/src/libmain/config.cc @@ -443,26 +443,4 @@ void OptionalPathSetting::operator =(const std::optional & v) this->assign(v); } -bool ExperimentalFeatureSettings::isEnabled(const ExperimentalFeature & feature) const -{ - auto & f = experimentalFeatures.get(); - return std::find(f.begin(), f.end(), feature) != f.end(); -} - -void ExperimentalFeatureSettings::require(const ExperimentalFeature & feature) const -{ - if (!isEnabled(feature)) - throw MissingExperimentalFeature(feature); -} - -bool ExperimentalFeatureSettings::isEnabled(const std::optional & feature) const -{ - return !feature || isEnabled(*feature); -} - -void ExperimentalFeatureSettings::require(const std::optional & feature) const -{ - if (feature) require(*feature); -} - } diff --git a/src/libutil/config.hh b/src/libmain/config.hh similarity index 98% rename from src/libutil/config.hh rename to src/libmain/config.hh index 1952ba1b8d77..616cbb1683b2 100644 --- a/src/libutil/config.hh +++ b/src/libmain/config.hh @@ -257,9 +257,6 @@ public: , documentDefault(documentDefault) { } - operator const T &() const { return value; } - operator T &() { return value; } - const T & get() const { return value; } template bool operator ==(const U & v2) const { return value == v2; } template diff --git a/src/libstore/derived-path.hh b/src/libstore/derived-path.hh index b238f844e56d..03526806af2f 100644 --- a/src/libstore/derived-path.hh +++ b/src/libstore/derived-path.hh @@ -4,7 +4,6 @@ #include "path.hh" #include "outputs-spec.hh" #include "comparator.hh" -#include "config.hh" #include diff --git a/src/libutil/archive.cc b/src/libutil/archive.cc index e2ebcda0c574..90596b288194 100644 --- a/src/libutil/archive.cc +++ b/src/libutil/archive.cc @@ -6,7 +6,6 @@ #include // for strcasecmp #include "archive.hh" -#include "config-global.hh" #include "posix-source-accessor.hh" #include "source-path.hh" #include "file-system.hh" @@ -14,21 +13,19 @@ namespace nix { -struct ArchiveSettings : Config -{ - Setting useCaseHack{this, - #if __APPLE__ - true, - #else - false, - #endif - "use-case-hack", - "Whether to enable a Darwin-specific hack for dealing with file name collisions."}; +const extern ArchiveSettings archiveSettingsDefaults = { + .useCaseHack = { + .value = +#if __APPLE__ + true +#else + false +#endif + }, }; -static ArchiveSettings archiveSettings; +ArchiveSettings archiveSettings = archiveSettingsDefaults; -static GlobalConfig::Register rArchiveSettings(&archiveSettings); PathFilter defaultPathFilter = [](const Path &) { return true; }; diff --git a/src/libutil/archive.hh b/src/libutil/archive.hh index 2da8f0cb1854..8baeb622ad78 100644 --- a/src/libutil/archive.hh +++ b/src/libutil/archive.hh @@ -2,12 +2,23 @@ ///@file #include "types.hh" +#include "config-abstract.hh" #include "serialise.hh" #include "fs-sink.hh" namespace nix { +template class R> +struct ArchiveSettings +{ + R useCaseHack; +}; + +const extern ArchiveSettings archiveSettingsDefaults; + +// FIXME: don't use a global variable. +extern ArchiveSettings archiveSettings; /** * dumpPath creates a Nix archive of the specified path. diff --git a/src/libutil/config-abstract.hh b/src/libutil/config-abstract.hh new file mode 100644 index 000000000000..d02b2bd2e2aa --- /dev/null +++ b/src/libutil/config-abstract.hh @@ -0,0 +1,15 @@ +#pragma once +///@type + +namespace nix { + +template +struct JustValue { + T value; + + operator const T &() const { return value; } + operator T &() { return value; } + const T & get() const { return value; } +}; + +} diff --git a/src/libutil/experimental-feature-settings.cc b/src/libutil/experimental-feature-settings.cc new file mode 100644 index 000000000000..d646c19bc230 --- /dev/null +++ b/src/libutil/experimental-feature-settings.cc @@ -0,0 +1,37 @@ +#include "experimental-feature-settings.hh" + +namespace nix { + +const extern ExperimentalFeatureSettings experimentalFeatureSettingsDefaults = { + { + .experimentalFeatures = { + .value = {} + }, + } +}; + +ExperimentalFeatureSettings experimentalFeatureSettings = experimentalFeatureSettingsDefaults; + +bool ExperimentalFeatureSettings::isEnabled(const ExperimentalFeature & feature) const +{ + auto & f = experimentalFeatures.get(); + return std::find(f.begin(), f.end(), feature) != f.end(); +} + +void ExperimentalFeatureSettings::require(const ExperimentalFeature & feature) const +{ + if (!isEnabled(feature)) + throw MissingExperimentalFeature(feature); +} + +bool ExperimentalFeatureSettings::isEnabled(const std::optional & feature) const +{ + return !feature || isEnabled(*feature); +} + +void ExperimentalFeatureSettings::require(const std::optional & feature) const +{ + if (feature) require(*feature); +} + +}; diff --git a/src/libutil/experimental-feature-settings.hh b/src/libutil/experimental-feature-settings.hh new file mode 100644 index 000000000000..390c6a17e127 --- /dev/null +++ b/src/libutil/experimental-feature-settings.hh @@ -0,0 +1,45 @@ +#pragma once +///@file + +#include "experimental-features.hh" + +namespace nix { + +template class R> +struct ExperimentalFeatureSettingsT +{ + R> experimentalFeatures; +}; + +struct ExperimentalFeatureSettings : ExperimentalFeatureSettingsT +{ + /** + * Check whether the given experimental feature is enabled. + */ + bool isEnabled(const ExperimentalFeature &) const; + + /** + * Require an experimental feature be enabled, throwing an error if it is + * not. + */ + void require(const ExperimentalFeature &) const; + + /** + * `std::nullopt` pointer means no feature, which means there is nothing that could be + * disabled, and so the function returns true in that case. + */ + bool isEnabled(const std::optional &) const; + + /** + * `std::nullopt` pointer means no feature, which means there is nothing that could be + * disabled, and so the function does nothing in that case. + */ + void require(const std::optional &) const; +}; + +const extern ExperimentalFeatureSettings experimentalFeatureSettingsDefaults; + +// FIXME: don't use a global variable. +extern ExperimentalFeatureSettings experimentalFeatureSettings; + +} diff --git a/src/libutil/experimental-features.hh b/src/libutil/experimental-features.hh index 1da2a3ff55d6..b2b2a94f7c09 100644 --- a/src/libutil/experimental-features.hh +++ b/src/libutil/experimental-features.hh @@ -2,6 +2,7 @@ ///@file #include "comparator.hh" +#include "config-abstract.hh" #include "error.hh" #include "json-utils.hh" #include "types.hh" diff --git a/src/libutil/fs-sink.cc b/src/libutil/fs-sink.cc index 597272ec98cd..5dd29506cd55 100644 --- a/src/libutil/fs-sink.cc +++ b/src/libutil/fs-sink.cc @@ -1,7 +1,6 @@ #include #include "error.hh" -#include "config-global.hh" #include "fs-sink.hh" #if _WIN32 @@ -58,15 +57,7 @@ void copyRecursive( } -struct RestoreSinkSettings : Config -{ - Setting preallocateContents{this, false, "preallocate-contents", - "Whether to preallocate files when writing objects with known size."}; -}; - -static RestoreSinkSettings restoreSinkSettings; - -static GlobalConfig::Register r1(&restoreSinkSettings); +RestoreSinkSettings restoreSinkSettings; void RestoreSink::createDirectory(const CanonPath & path) diff --git a/src/libutil/fs-sink.hh b/src/libutil/fs-sink.hh index cf7d34d22733..c5f72872740c 100644 --- a/src/libutil/fs-sink.hh +++ b/src/libutil/fs-sink.hh @@ -2,12 +2,20 @@ ///@file #include "types.hh" +#include "config-abstract.hh" #include "serialise.hh" #include "source-accessor.hh" #include "file-system.hh" namespace nix { +template class R> +struct RestoreSinkSettings +{ + R preallocateContents; +}; + + /** * Actions on an open regular file in the process of creating it. * @@ -23,6 +31,8 @@ struct CreateRegularFileSink : Sink virtual void preallocateContents(uint64_t size) { }; }; +// FIXME: don't use a global variable. +extern RestoreSinkSettings restoreSinkSettings; struct FileSystemObjectSink { diff --git a/src/libutil/git.cc b/src/libutil/git.cc index a6968a43eed9..40ad93895c9b 100644 --- a/src/libutil/git.cc +++ b/src/libutil/git.cc @@ -6,7 +6,7 @@ #include // for strcasecmp #include "signals.hh" -#include "config.hh" +#include "config-abstract.hh" #include "hash.hh" #include "git.hh" diff --git a/src/libutil/git.hh b/src/libutil/git.hh index 2190cc5509c6..016e253c5cd3 100644 --- a/src/libutil/git.hh +++ b/src/libutil/git.hh @@ -6,6 +6,7 @@ #include #include "types.hh" +#include "experimental-feature-settings.hh" #include "serialise.hh" #include "hash.hh" #include "source-path.hh" diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc index 7064e96e61c3..b4684771a5cd 100644 --- a/src/libutil/hash.cc +++ b/src/libutil/hash.cc @@ -5,7 +5,6 @@ #include #include -#include "args.hh" #include "hash.hh" #include "archive.hh" #include "split.hh" diff --git a/src/libutil/logging.cc b/src/libutil/logging.cc index 55751b4cf0da..a5cc9d0c3875 100644 --- a/src/libutil/logging.cc +++ b/src/libutil/logging.cc @@ -3,7 +3,6 @@ #include "environment-variables.hh" #include "terminal.hh" #include "util.hh" -#include "config-global.hh" #include "source-path.hh" #include "position.hh" @@ -14,9 +13,13 @@ namespace nix { -LoggerSettings loggerSettings; +const extern LoggerSettings loggerSettingsDefaults = { + .showTrace = { + .value = false + }, +}; -static GlobalConfig::Register rLoggerSettings(&loggerSettings); +LoggerSettings loggerSettings = loggerSettingsDefaults; static thread_local ActivityId curActivity = 0; diff --git a/src/libutil/logging.hh b/src/libutil/logging.hh index 9e81132e3541..1e3ab57d303e 100644 --- a/src/libutil/logging.hh +++ b/src/libutil/logging.hh @@ -3,7 +3,7 @@ #include "types.hh" #include "error.hh" -#include "config.hh" +#include "config-abstract.hh" #include @@ -40,17 +40,13 @@ typedef enum { typedef uint64_t ActivityId; -struct LoggerSettings : Config +template class R> +struct LoggerSettings { - Setting showTrace{ - this, false, "show-trace", - R"( - Whether Nix should print out a stack trace in case of Nix - expression evaluation errors. - )"}; + R showTrace; }; -extern LoggerSettings loggerSettings; +extern LoggerSettings loggerSettings; class Logger { diff --git a/src/libutil/meson.build b/src/libutil/meson.build index 099c0c65fdce..bd4d0b2ae93b 100644 --- a/src/libutil/meson.build +++ b/src/libutil/meson.build @@ -162,7 +162,6 @@ sources = files( 'compression.cc', 'compute-levels.cc', 'config.cc', - 'config-global.cc', 'current-process.cc', 'english.cc', 'environment-variables.cc', @@ -215,7 +214,7 @@ headers = [config_h] + files( 'compute-levels.hh', 'config-global.hh', 'config-impl.hh', - 'config.hh', + 'config-abstract.hh', 'current-process.hh', 'english.hh', 'environment-variables.hh',