diff --git a/src/libcmd/command.cc b/src/libcmd/command.cc index d57b76411..988df233d 100644 --- a/src/libcmd/command.cc +++ b/src/libcmd/command.cc @@ -4,6 +4,7 @@ #include "nix/cmd/command.hh" #include "nix/cmd/legacy.hh" #include "nix/cmd/markdown.hh" +#include "nix/main/shared.hh" #include "nix/store/globals.hh" #include "nix/store/store-open.hh" #include "nix/store/local-fs-store.hh" @@ -75,7 +76,7 @@ ref StoreConfigCommand::getStoreConfig() ref StoreConfigCommand::createStoreConfig() { - return resolveStoreConfig(StoreReference{settings.storeUri.get()}); + return resolveStoreConfig(settings, StoreReference{settings.storeUri.get()}); } void StoreConfigCommand::run() @@ -129,7 +130,7 @@ CopyCommand::CopyCommand() ref CopyCommand::createStoreConfig() { - return !srcUri ? StoreCommand::createStoreConfig() : resolveStoreConfig(StoreReference{*srcUri}); + return !srcUri ? StoreCommand::createStoreConfig() : resolveStoreConfig(settings, StoreReference{*srcUri}); } ref CopyCommand::getDstStore() @@ -137,7 +138,7 @@ ref CopyCommand::getDstStore() if (!srcUri && !dstUri) throw UsageError("you must pass '--from' and/or '--to'"); - return !dstUri ? openStore() : openStore(StoreReference{*dstUri}); + return !dstUri ? openStore(settings) : openStore(settings, StoreReference{*dstUri}); } EvalCommand::EvalCommand() @@ -159,7 +160,7 @@ EvalCommand::~EvalCommand() ref EvalCommand::getEvalStore() { if (!evalStore) - evalStore = evalStoreUrl ? openStore(StoreReference{*evalStoreUrl}) : getStore(); + evalStore = evalStoreUrl ? openStore(settings, StoreReference{*evalStoreUrl}) : getStore(); return ref(evalStore); } diff --git a/src/libcmd/common-eval-args.cc b/src/libcmd/common-eval-args.cc index 7e4bd7a16..2f35c526a 100644 --- a/src/libcmd/common-eval-args.cc +++ b/src/libcmd/common-eval-args.cc @@ -19,12 +19,12 @@ namespace nix { -fetchers::Settings fetchSettings; +fetchers::Settings fetchSettings{settings}; static GlobalConfig::Register rFetchSettings(&fetchSettings); EvalSettings evalSettings{ - settings.readOnlyMode, + settings, { { "flake", @@ -135,7 +135,7 @@ MixEvalArgs::MixEvalArgs() fetchers::overrideRegistry(from.input, to.input, extraAttrs); }}, .completer = {[&](AddCompletions & completions, size_t, std::string_view prefix) { - completeFlakeRef(completions, openStore(), prefix); + completeFlakeRef(completions, openStore(settings), prefix); }}, }); diff --git a/src/libcmd/get-build-log.cc b/src/libcmd/get-build-log.cc index 8bbbf5899..75594e04c 100644 --- a/src/libcmd/get-build-log.cc +++ b/src/libcmd/get-build-log.cc @@ -4,9 +4,9 @@ namespace nix { -std::string fetchBuildLog(ref store, const StorePath & path, std::string_view what) +std::string fetchBuildLog(Settings & settings, ref store, const StorePath & path, std::string_view what) { - auto subs = getDefaultSubstituters(); + auto subs = getDefaultSubstituters(settings); subs.push_front(store); diff --git a/src/libcmd/include/nix/cmd/get-build-log.hh b/src/libcmd/include/nix/cmd/get-build-log.hh index 81d3a0dd3..ae42325c8 100644 --- a/src/libcmd/include/nix/cmd/get-build-log.hh +++ b/src/libcmd/include/nix/cmd/get-build-log.hh @@ -8,6 +8,8 @@ namespace nix { +class Settings; + /** * Fetch the build log for a store path, searching the store and its * substituters. @@ -18,6 +20,6 @@ namespace nix { * @return The build log content. * @throws Error if the build log is not available. */ -std::string fetchBuildLog(ref store, const StorePath & path, std::string_view what); +std::string fetchBuildLog(Settings & settings, ref store, const StorePath & path, std::string_view what); } // namespace nix diff --git a/src/libcmd/include/nix/cmd/installable-attr-path.hh b/src/libcmd/include/nix/cmd/installable-attr-path.hh index 474bb358e..8deee4a37 100644 --- a/src/libcmd/include/nix/cmd/installable-attr-path.hh +++ b/src/libcmd/include/nix/cmd/installable-attr-path.hh @@ -1,7 +1,6 @@ #pragma once ///@file -#include "nix/store/globals.hh" #include "nix/cmd/installable-value.hh" #include "nix/store/outputs-spec.hh" #include "nix/cmd/command.hh" diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index ffb14c42e..8fcc23f2f 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -564,7 +564,7 @@ ProcessLineResult NixRepl::processLine(std::string line) settings.readOnlyMode = true; Finally roModeReset([&]() { settings.readOnlyMode = false; }); RunPager pager; - auto log = fetchBuildLog(state->store, drvPath, drvPathRaw); + auto log = fetchBuildLog(settings, state->store, drvPath, drvPathRaw); logger->writeToStdout(log); } else { runNix("nix-shell", {drvPathRaw}); diff --git a/src/libexpr-c/nix_api_expr.cc b/src/libexpr-c/nix_api_expr.cc index 0dd9fa0a5..52648bb8b 100644 --- a/src/libexpr-c/nix_api_expr.cc +++ b/src/libexpr-c/nix_api_expr.cc @@ -132,9 +132,8 @@ nix_eval_state_builder * nix_eval_state_builder_new(nix_c_context * context, Sto return unsafe_new_with_self([&](auto * self) { return nix_eval_state_builder{ .store = nix::ref(store->ptr), - .settings = nix::EvalSettings{/* &bool */ self->readOnlyMode}, - .fetchSettings = nix::fetchers::Settings{}, - .readOnlyMode = true, + .settings = nix::EvalSettings{cStoreSettings}, + .fetchSettings = nix::fetchers::Settings{cStoreSettings}, }; }); } @@ -153,8 +152,7 @@ nix_err nix_eval_state_builder_load(nix_c_context * context, nix_eval_state_buil if (context) context->last_err_code = NIX_OK; try { - // TODO: load in one go? - builder->settings.readOnlyMode = nix::settings.readOnlyMode; + loadConfFile(cStoreSettings); loadConfFile(builder->settings); loadConfFile(builder->fetchSettings); } diff --git a/src/libexpr-c/nix_api_expr_internal.h b/src/libexpr-c/nix_api_expr_internal.h index 07c7a2194..0e5389e37 100644 --- a/src/libexpr-c/nix_api_expr_internal.h +++ b/src/libexpr-c/nix_api_expr_internal.h @@ -16,8 +16,6 @@ struct nix_eval_state_builder nix::EvalSettings settings; nix::fetchers::Settings fetchSettings; nix::LookupPath lookupPath; - // TODO: make an EvalSettings setting own this instead? - bool readOnlyMode; }; struct EvalState diff --git a/src/libexpr-test-support/include/nix/expr/tests/libexpr.hh b/src/libexpr-test-support/include/nix/expr/tests/libexpr.hh index 658a6ffe0..7150096e6 100644 --- a/src/libexpr-test-support/include/nix/expr/tests/libexpr.hh +++ b/src/libexpr-test-support/include/nix/expr/tests/libexpr.hh @@ -26,18 +26,18 @@ public: } protected: - LibExprTest(ref store, auto && makeEvalSettings) - : LibStoreTest() - , evalSettings(makeEvalSettings(readOnlyMode)) + LibExprTest(auto && makeEvalSettings, auto &&... args) + : LibStoreTest(args...) + , evalSettings(makeEvalSettings(settings)) , state({}, store, fetchSettings, evalSettings, nullptr) { } LibExprTest() - : LibExprTest(openStore("dummy://"), [](bool & readOnlyMode) { - EvalSettings settings{readOnlyMode}; - settings.nixPath = {}; - return settings; + : LibExprTest([](Settings & settings) { + EvalSettings evalSettings{settings}; + evalSettings.nixPath = {}; + return evalSettings; }) { } @@ -66,8 +66,8 @@ protected: } bool readOnlyMode = true; - fetchers::Settings fetchSettings{}; - EvalSettings evalSettings{readOnlyMode}; + fetchers::Settings fetchSettings{settings}; + EvalSettings evalSettings{settings}; EvalState state; }; diff --git a/src/libexpr-tests/eval.cc b/src/libexpr-tests/eval.cc index 7562a9da2..3bcef1e82 100644 --- a/src/libexpr-tests/eval.cc +++ b/src/libexpr-tests/eval.cc @@ -179,12 +179,15 @@ class PureEvalTest : public LibExprTest { public: PureEvalTest() - : LibExprTest(openStore("dummy://", {{"read-only", "false"}}), [](bool & readOnlyMode) { - EvalSettings settings{readOnlyMode}; - settings.pureEval = true; - settings.restrictEval = true; - return settings; - }) + : LibExprTest{ + [](auto & settings) { + EvalSettings evalSettings{settings}; + evalSettings.pureEval = true; + evalSettings.restrictEval = true; + return evalSettings; + }, + [](auto & settings) { return openStore(settings, "dummy://", {{"read-only", "false"}}); }, + } { } }; diff --git a/src/libexpr/eval-cache.cc b/src/libexpr/eval-cache.cc index 4f6c5fe8e..77eebbf73 100644 --- a/src/libexpr/eval-cache.cc +++ b/src/libexpr/eval-cache.cc @@ -63,7 +63,7 @@ struct AttrDb SymbolTable & symbols; - AttrDb(const StoreDirConfig & cfg, const Hash & fingerprint, SymbolTable & symbols) + AttrDb(bool useSQLiteWAL, const StoreDirConfig & cfg, const Hash & fingerprint, SymbolTable & symbols) : cfg(cfg) , _state(std::make_unique>()) , symbols(symbols) @@ -75,7 +75,7 @@ struct AttrDb auto dbPath = cacheDir / (fingerprint.to_string(HashFormat::Base16, false) + ".sqlite"); - state->db = SQLite(dbPath, {.useWAL = settings.useSQLiteWAL}); + state->db = SQLite(dbPath, {.useWAL = useSQLiteWAL}); state->db.isCache(); state->db.exec(schema); @@ -287,10 +287,11 @@ struct AttrDb } }; -static std::shared_ptr makeAttrDb(const StoreDirConfig & cfg, const Hash & fingerprint, SymbolTable & symbols) +static std::shared_ptr +makeAttrDb(bool useSQLiteWAL, const StoreDirConfig & cfg, const Hash & fingerprint, SymbolTable & symbols) { try { - return std::make_shared(cfg, fingerprint, symbols); + return std::make_shared(useSQLiteWAL, cfg, fingerprint, symbols); } catch (SQLiteError &) { ignoreExceptionExceptInterrupt(); return nullptr; @@ -299,7 +300,8 @@ static std::shared_ptr makeAttrDb(const StoreDirConfig & cfg, const Hash EvalCache::EvalCache( std::optional> useCache, EvalState & state, RootLoader rootLoader) - : db(useCache ? makeAttrDb(*state.store, *useCache, state.symbols) : nullptr) + : db(useCache ? makeAttrDb(state.store->config.settings.useSQLiteWAL, *state.store, *useCache, state.symbols) + : nullptr) , state(state) , rootLoader(rootLoader) { @@ -707,7 +709,7 @@ StorePath AttrCursor::forceDerivation() auto aDrvPath = getAttr(root->state.s.drvPath); auto drvPath = root->state.store->parseStorePath(aDrvPath->getString()); drvPath.requireDerivation(); - if (!root->state.store->isValidPath(drvPath) && !settings.readOnlyMode) { + if (!root->state.store->isValidPath(drvPath) && !root->state.store->config.settings.readOnlyMode) { /* The eval cache contains 'drvPath', but the actual path has been garbage-collected. So force it to be regenerated. */ aDrvPath->forceValue(); diff --git a/src/libexpr/eval-settings.cc b/src/libexpr/eval-settings.cc index 286d588b0..773133d63 100644 --- a/src/libexpr/eval-settings.cc +++ b/src/libexpr/eval-settings.cc @@ -48,8 +48,8 @@ Strings EvalSettings::parseNixPath(const std::string & s) return res; } -EvalSettings::EvalSettings(bool & readOnlyMode, EvalSettings::LookupPathHooks lookupPathHooks) - : readOnlyMode{readOnlyMode} +EvalSettings::EvalSettings(nix::Settings & storeSettings, EvalSettings::LookupPathHooks lookupPathHooks) + : storeSettings{storeSettings} , lookupPathHooks{lookupPathHooks} { auto var = getEnv("NIX_ABORT_ON_WARN"); @@ -57,7 +57,7 @@ EvalSettings::EvalSettings(bool & readOnlyMode, EvalSettings::LookupPathHooks lo builtinsAbortOnWarn = true; } -Strings EvalSettings::getDefaultNixPath() +Strings EvalSettings::getDefaultNixPath(nix::Settings & settings) { Strings res; auto add = [&](const std::filesystem::path & p, const std::string & s = std::string()) { @@ -70,7 +70,7 @@ Strings EvalSettings::getDefaultNixPath() } }; - add(std::filesystem::path{getNixDefExpr()} / "channels"); + add(std::filesystem::path{getNixDefExpr(settings)} / "channels"); auto profilesDirOpts = settings.getProfileDirsOptions(); add(rootChannelsDir(profilesDirOpts) / "nixpkgs", "nixpkgs"); add(rootChannelsDir(profilesDirOpts)); @@ -101,10 +101,10 @@ std::string EvalSettings::resolvePseudoUrl(std::string_view url) const std::string & EvalSettings::getCurrentSystem() const { const auto & evalSystem = currentSystem.get(); - return evalSystem != "" ? evalSystem : settings.thisSystem.get(); + return evalSystem != "" ? evalSystem : storeSettings.thisSystem.get(); } -std::filesystem::path getNixDefExpr() +std::filesystem::path getNixDefExpr(const Settings & settings) { return settings.useXDGBaseDirectories ? getStateDir() / "defexpr" : getHome() / ".nix-defexpr"; } diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index d9d8a986a..01eb971fa 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -15,6 +15,7 @@ #include "nix/store/filetransfer.hh" #include "nix/expr/function-trace.hh" #include "nix/store/profiles.hh" +#include "nix/store/globals.hh" #include "nix/expr/print.hh" #include "nix/fetchers/filtering-source-accessor.hh" #include "nix/util/memory-source-accessor.hh" @@ -330,7 +331,7 @@ EvalState::EvalState( lookupPath.elements.emplace_back(LookupPath::Elem::parse(i)); } if (!settings.restrictEval) { - for (auto & i : EvalSettings::getDefaultNixPath()) { + for (auto & i : EvalSettings::getDefaultNixPath(store->config.settings)) { lookupPath.elements.emplace_back(LookupPath::Elem::parse(i)); } } @@ -2508,7 +2509,7 @@ StorePath EvalState::copyPathToStore(NixStringContext & context, const SourcePat fetchSettings, *store, path.resolveSymlinks(SymlinkResolution::Ancestors), - settings.readOnlyMode ? FetchMode::DryRun : FetchMode::Copy, + settings.storeSettings.readOnlyMode ? FetchMode::DryRun : FetchMode::Copy, path.baseName(), ContentAddressMethod::Raw::NixArchive, nullptr, diff --git a/src/libexpr/include/nix/expr/eval-settings.hh b/src/libexpr/include/nix/expr/eval-settings.hh index 5dbef9272..ca4b23197 100644 --- a/src/libexpr/include/nix/expr/eval-settings.hh +++ b/src/libexpr/include/nix/expr/eval-settings.hh @@ -9,9 +9,15 @@ namespace nix { class EvalState; struct PrimOp; +class Settings; struct EvalSettings : Config { + /** + * Reference to the "parent" store-layer settings. + */ + nix::Settings & storeSettings; + /** * Function used to interpret look path entries of a given scheme. * @@ -38,11 +44,9 @@ struct EvalSettings : Config */ using LookupPathHooks = std::map>; - EvalSettings(bool & readOnlyMode, LookupPathHooks lookupPathHooks = {}); + EvalSettings(nix::Settings & settings, LookupPathHooks lookupPathHooks = {}); - bool & readOnlyMode; - - static Strings getDefaultNixPath(); + static Strings getDefaultNixPath(nix::Settings & settings); static bool isPseudoUrl(std::string_view s); @@ -366,6 +370,6 @@ struct EvalSettings : Config /** * Conventionally part of the default nix path in impure mode. */ -std::filesystem::path getNixDefExpr(); +std::filesystem::path getNixDefExpr(const Settings & settings); } // namespace nix diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index b7b80be66..fc26c93ae 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1819,8 +1819,8 @@ static void derivationStrictInternal(EvalState & state, std::string_view drvName Unless we are in read-only mode, that is, in which case we do not write anything. Users commonly do this to speed up evaluation in contexts where they don't actually want to build anything. */ - auto drvPath = - settings.readOnlyMode ? computeStorePath(*state.store, drv) : state.store->writeDerivation(drv, state.repair); + auto drvPath = state.settings.storeSettings.readOnlyMode ? computeStorePath(*state.store, drv) + : state.store->writeDerivation(drv, state.repair); auto drvPathS = state.store->printStorePath(drvPath); printMsg(lvlChatty, "instantiated '%1%' -> '%2%'", drvName, drvPathS); @@ -1935,7 +1935,7 @@ static void prim_storePath(EvalState & state, const PosIdx pos, Value ** args, V if (!state.store->isInStore(path.abs())) state.error("path '%1%' is not in the Nix store", path).atPos(pos).debugThrow(); auto path2 = state.store->toStorePath(path.abs()).first; - if (!settings.readOnlyMode) + if (!state.settings.storeSettings.readOnlyMode) state.store->ensurePath(path2); context.insert(NixStringContextElem::Opaque{.path = path2}); v.mkString(path.abs(), context, state.mem); @@ -2676,23 +2676,24 @@ static void prim_toFile(EvalState & state, const PosIdx pos, Value ** args, Valu .debugThrow(); } - auto storePath = settings.readOnlyMode ? state.store->makeFixedOutputPathFromCA( - name, - TextInfo{ - .hash = hashString(HashAlgorithm::SHA256, contents), - .references = std::move(refs), - }) - : ({ - StringSource s{contents}; - state.store->addToStoreFromDump( - s, - name, - FileSerialisationMethod::Flat, - ContentAddressMethod::Raw::Text, - HashAlgorithm::SHA256, - refs, - state.repair); - }); + auto storePath = state.settings.storeSettings.readOnlyMode + ? state.store->makeFixedOutputPathFromCA( + name, + TextInfo{ + .hash = hashString(HashAlgorithm::SHA256, contents), + .references = std::move(refs), + }) + : ({ + StringSource s{contents}; + state.store->addToStoreFromDump( + s, + name, + FileSerialisationMethod::Flat, + ContentAddressMethod::Raw::Text, + HashAlgorithm::SHA256, + refs, + state.repair); + }); /* Note: we don't need to add `context' to the context of the result, since `storePath' itself has references to the paths @@ -2836,23 +2837,24 @@ static void addPath( if (!expectedHash || !state.store->isValidPath(*expectedStorePath)) { // FIXME: support refs in fetchToStore()? - auto dstPath = refs.empty() ? fetchToStore( - state.fetchSettings, - *state.store, - path.resolveSymlinks(), - settings.readOnlyMode ? FetchMode::DryRun : FetchMode::Copy, - name, - method, - filter.get(), - state.repair) - : state.store->addToStore( - name, - path.resolveSymlinks(), - method, - HashAlgorithm::SHA256, - refs, - filter ? *filter.get() : defaultPathFilter, - state.repair); + auto dstPath = refs.empty() + ? fetchToStore( + state.fetchSettings, + *state.store, + path.resolveSymlinks(), + state.settings.storeSettings.readOnlyMode ? FetchMode::DryRun : FetchMode::Copy, + name, + method, + filter.get(), + state.repair) + : state.store->addToStore( + name, + path.resolveSymlinks(), + method, + HashAlgorithm::SHA256, + refs, + filter ? *filter.get() : defaultPathFilter, + state.repair); if (expectedHash && expectedStorePath != dstPath) state.error("store path mismatch in (possibly filtered) path added from '%s'", path) .atPos(pos) diff --git a/src/libexpr/primops/context.cc b/src/libexpr/primops/context.cc index 70c13e298..a8bee3dcb 100644 --- a/src/libexpr/primops/context.cc +++ b/src/libexpr/primops/context.cc @@ -271,7 +271,7 @@ static void prim_appendContext(EvalState & state, const PosIdx pos, Value ** arg if (!state.store->isStorePath(name)) state.error("context key '%s' is not a store path", name).atPos(i.pos).debugThrow(); auto namePath = state.store->parseStorePath(name); - if (!settings.readOnlyMode) + if (!state.settings.storeSettings.readOnlyMode) state.store->ensurePath(namePath); state.forceAttrs(*i.value, i.pos, "while evaluating the value of a string context"); diff --git a/src/libexpr/primops/fetchClosure.cc b/src/libexpr/primops/fetchClosure.cc index 21f9c3c07..71e67137d 100644 --- a/src/libexpr/primops/fetchClosure.cc +++ b/src/libexpr/primops/fetchClosure.cc @@ -209,7 +209,7 @@ static void prim_fetchClosure(EvalState & state, const PosIdx pos, Value ** args {.msg = HintFmt("'fetchClosure' does not support URL query parameters (in '%s')", *fromStoreUrl), .pos = state.positions[pos]}); - auto fromStore = openStore(std::move(storeRef)); + auto fromStore = openStore(state.settings.storeSettings, std::move(storeRef)); if (toPath) runFetchClosureWithRewrite(state, pos, *fromStore, *fromPath, *toPath, v); diff --git a/src/libfetchers-c/nix_api_fetchers.cc b/src/libfetchers-c/nix_api_fetchers.cc index 7fefedb0c..be54fc912 100644 --- a/src/libfetchers-c/nix_api_fetchers.cc +++ b/src/libfetchers-c/nix_api_fetchers.cc @@ -1,13 +1,14 @@ #include "nix_api_fetchers.h" #include "nix_api_fetchers_internal.hh" #include "nix_api_util_internal.h" +#include "nix_api_store_internal.h" extern "C" { nix_fetchers_settings * nix_fetchers_settings_new(nix_c_context * context) { try { - auto fetchersSettings = nix::make_ref(nix::fetchers::Settings{}); + auto fetchersSettings = nix::make_ref(nix::fetchers::Settings{cStoreSettings}); return new nix_fetchers_settings{ .settings = fetchersSettings, }; diff --git a/src/libfetchers-tests/access-tokens.cc b/src/libfetchers-tests/access-tokens.cc index 7127434db..57d638147 100644 --- a/src/libfetchers-tests/access-tokens.cc +++ b/src/libfetchers-tests/access-tokens.cc @@ -1,9 +1,12 @@ #include #include +#include "nix/util/json-utils.hh" +#include "nix/store/globals.hh" #include "nix/fetchers/fetchers.hh" #include "nix/fetchers/fetch-settings.hh" -#include "nix/util/json-utils.hh" + +#include "nix/store/tests/test-main.hh" #include "nix/util/tests/characterization.hh" namespace nix::fetchers { @@ -25,7 +28,8 @@ public: TEST_F(AccessKeysTest, singleOrgGitHub) { - fetchers::Settings fetchSettings = fetchers::Settings{}; + auto settings = getTestSettings(); + fetchers::Settings fetchSettings = fetchers::Settings{settings}; fetchSettings.accessTokens.get().insert({"github.com/a", "token"}); auto i = Input::fromURL(fetchSettings, "github:a/b"); @@ -35,7 +39,8 @@ TEST_F(AccessKeysTest, singleOrgGitHub) TEST_F(AccessKeysTest, nonMatches) { - fetchers::Settings fetchSettings = fetchers::Settings{}; + auto settings = getTestSettings(); + fetchers::Settings fetchSettings = fetchers::Settings{settings}; fetchSettings.accessTokens.get().insert({"github.com", "token"}); auto i = Input::fromURL(fetchSettings, "gitlab:github.com/evil"); @@ -45,7 +50,8 @@ TEST_F(AccessKeysTest, nonMatches) TEST_F(AccessKeysTest, noPartialMatches) { - fetchers::Settings fetchSettings = fetchers::Settings{}; + auto settings = getTestSettings(); + fetchers::Settings fetchSettings = fetchers::Settings{settings}; fetchSettings.accessTokens.get().insert({"github.com/partial", "token"}); auto i = Input::fromURL(fetchSettings, "github:partial-match/repo"); @@ -55,7 +61,8 @@ TEST_F(AccessKeysTest, noPartialMatches) TEST_F(AccessKeysTest, repoGitHub) { - fetchers::Settings fetchSettings = fetchers::Settings{}; + auto settings = getTestSettings(); + fetchers::Settings fetchSettings = fetchers::Settings{settings}; fetchSettings.accessTokens.get().insert({"github.com", "token"}); fetchSettings.accessTokens.get().insert({"github.com/a/b", "another_token"}); fetchSettings.accessTokens.get().insert({"github.com/a/c", "yet_another_token"}); @@ -73,7 +80,8 @@ TEST_F(AccessKeysTest, repoGitHub) TEST_F(AccessKeysTest, multipleGitLab) { - fetchers::Settings fetchSettings = fetchers::Settings{}; + auto settings = getTestSettings(); + fetchers::Settings fetchSettings = fetchers::Settings{settings}; fetchSettings.accessTokens.get().insert({"gitlab.com", "token"}); fetchSettings.accessTokens.get().insert({"gitlab.com/a/b", "another_token"}); auto i = Input::fromURL(fetchSettings, "gitlab:a/b"); @@ -87,7 +95,8 @@ TEST_F(AccessKeysTest, multipleGitLab) TEST_F(AccessKeysTest, multipleSourceHut) { - fetchers::Settings fetchSettings = fetchers::Settings{}; + auto settings = getTestSettings(); + fetchers::Settings fetchSettings = fetchers::Settings{settings}; fetchSettings.accessTokens.get().insert({"git.sr.ht", "token"}); fetchSettings.accessTokens.get().insert({"git.sr.ht/~a/b", "another_token"}); auto i = Input::fromURL(fetchSettings, "sourcehut:a/b"); diff --git a/src/libfetchers-tests/git.cc b/src/libfetchers-tests/git.cc index abc3dd74c..8ddc1ca36 100644 --- a/src/libfetchers-tests/git.cc +++ b/src/libfetchers-tests/git.cc @@ -5,6 +5,8 @@ #include "nix/fetchers/fetchers.hh" #include "nix/fetchers/git-utils.hh" +#include "nix/store/tests/test-main.hh" + #include #include @@ -180,13 +182,15 @@ TEST_F(GitTest, submodulePeriodSupport) // 6) Commit the addition in super commitAll(super.get(), "Add submodule with branch='.'"); - auto store = [] { - auto cfg = make_ref(StoreReference::Params{}); + auto settings = getTestSettings(); + + auto store = [&] { + auto cfg = make_ref(settings, StoreReference::Params{}); cfg->readOnly = false; return cfg->openStore(); }(); - auto settings = fetchers::Settings{}; + auto fetchSettings = fetchers::Settings{settings}; auto input = fetchers::Input::fromAttrs( settings, { @@ -196,7 +200,7 @@ TEST_F(GitTest, submodulePeriodSupport) {"ref", "main"}, }); - auto [accessor, i] = input.getAccessor(settings, *store); + auto [accessor, i] = input.getAccessor(fetchSettings, *store); ASSERT_EQ(accessor->readFile(CanonPath("deps/sub/lib.txt")), "hello from submodule\n"); } diff --git a/src/libfetchers-tests/input.cc b/src/libfetchers-tests/input.cc index faff55f2c..aa4558040 100644 --- a/src/libfetchers-tests/input.cc +++ b/src/libfetchers-tests/input.cc @@ -1,7 +1,11 @@ +#include "nix/store/globals.hh" + #include "nix/fetchers/fetch-settings.hh" #include "nix/fetchers/attrs.hh" #include "nix/fetchers/fetchers.hh" +#include "nix/store/tests/test-main.hh" + #include #include @@ -23,7 +27,8 @@ class InputFromAttrsTest : public ::testing::WithParamInterfacedb = SQLite(dbPath, {.useWAL = nix::settings.useSQLiteWAL}); + state->db = SQLite(dbPath, {.useWAL = settings.storeSettings.useSQLiteWAL}); state->db.isCache(); state->db.exec(schema); diff --git a/src/libfetchers/fetch-settings.cc b/src/libfetchers/fetch-settings.cc index f92b94a0b..d1a45409e 100644 --- a/src/libfetchers/fetch-settings.cc +++ b/src/libfetchers/fetch-settings.cc @@ -2,6 +2,9 @@ namespace nix::fetchers { -Settings::Settings() {} +Settings::Settings(const nix::Settings & storeSettings) + : storeSettings(storeSettings) +{ +} } // namespace nix::fetchers diff --git a/src/libfetchers/fetch-to-store.cc b/src/libfetchers/fetch-to-store.cc index e2b06d8ca..bb36ff9c2 100644 --- a/src/libfetchers/fetch-to-store.cc +++ b/src/libfetchers/fetch-to-store.cc @@ -76,8 +76,8 @@ std::pair fetchToStore2( auto [storePath, hash] = mode == FetchMode::DryRun ? [&]() { - auto [storePath, hash] = - store.computeStorePath(name, path, method, HashAlgorithm::SHA256, {}, filter2); + auto [storePath, hash] = store.computeStorePath( + store.config.settings, name, path, method, HashAlgorithm::SHA256, {}, filter2); debug( "hashed '%s' to '%s' (hash '%s')", path, diff --git a/src/libfetchers/include/nix/fetchers/fetch-settings.hh b/src/libfetchers/include/nix/fetchers/fetch-settings.hh index 2ab215a68..6db1b744c 100644 --- a/src/libfetchers/include/nix/fetchers/fetch-settings.hh +++ b/src/libfetchers/include/nix/fetchers/fetch-settings.hh @@ -13,9 +13,10 @@ namespace nix { +class Settings; struct GitRepo; -} +} // namespace nix namespace nix::fetchers { @@ -23,7 +24,12 @@ struct Cache; struct Settings : public Config { - Settings(); + /** + * Reference to the "parent" store-layer settings. + */ + const nix::Settings & storeSettings; + + Settings(const nix::Settings & storeSettings); Setting accessTokens{ this, diff --git a/src/libflake-tests/flakeref.cc b/src/libflake-tests/flakeref.cc index eb8b56ea2..2fc12c16f 100644 --- a/src/libflake-tests/flakeref.cc +++ b/src/libflake-tests/flakeref.cc @@ -3,6 +3,7 @@ #include #include +#include "nix/store/globals.hh" #include "nix/fetchers/fetch-settings.hh" #include "nix/flake/flakeref.hh" #include "nix/fetchers/attrs.hh" @@ -19,7 +20,8 @@ TEST(parseFlakeRef, path) { experimentalFeatureSettings.experimentalFeatures.get().insert(Xp::Flakes); - fetchers::Settings fetchSettings; + Settings settings; + fetchers::Settings fetchSettings{settings}; { auto s = "/foo/bar"; @@ -69,7 +71,8 @@ TEST(parseFlakeRef, GitArchiveInput) { experimentalFeatureSettings.experimentalFeatures.get().insert(Xp::Flakes); - fetchers::Settings fetchSettings; + Settings settings; + fetchers::Settings fetchSettings{settings}; { auto s = "github:foo/bar/branch%23"; // branch name with `#` @@ -112,7 +115,8 @@ class InputFromURLTest : public ::testing::WithParamInterface fun); + /** * Don't forget to call initPlugins() after settings are initialized! * @param loadConfig Whether to load configuration from `nix.conf`, `NIX_CONFIG`, etc. May be disabled for unit tests. diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index 44b5914b8..e19d19b0f 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -31,12 +31,17 @@ #include "nix/util/exit.hh" #include "nix/util/strings.hh" +#include "nix/util/config-global.hh" #include "main-config-private.hh" #include "nix/expr/config.hh" namespace nix { +Settings settings; + +static GlobalConfig::Register rSettings(&settings); + char ** savedArgv; static bool gcWarning = true; @@ -335,8 +340,8 @@ void printVersion(const std::string & programName) std::cout << "System configuration file: " << nixConfFile() << "\n"; std::cout << "User configuration files: " << os_string_to_string(ExecutablePath{.directories = nixUserConfFiles()}.render()) << "\n"; - std::cout << "Store directory: " << resolveStoreConfig(StoreReference{settings.storeUri.get()})->storeDir - << "\n"; + std::cout << "Store directory: " + << resolveStoreConfig(settings, StoreReference{settings.storeUri.get()})->storeDir << "\n"; std::cout << "State directory: " << settings.nixStateDir << "\n"; } throw Exit(); diff --git a/src/libstore-c/nix_api_store.cc b/src/libstore-c/nix_api_store.cc index eec7f34e8..a81b19a2e 100644 --- a/src/libstore-c/nix_api_store.cc +++ b/src/libstore-c/nix_api_store.cc @@ -18,6 +18,9 @@ extern "C" { +// FIXME: This is going to conflict with the one in libmain. +nix::Settings cStoreSettings; + nix_err nix_libstore_init(nix_c_context * context) { if (context) @@ -46,7 +49,7 @@ Store * nix_store_open(nix_c_context * context, const char * uri, const char *** std::string uri_str = uri ? uri : ""; if (uri_str.empty()) - return new Store{nix::openStore()}; + return new Store{nix::openStore(cStoreSettings)}; auto storeRef = nix::StoreReference::parse(uri_str); @@ -55,7 +58,7 @@ Store * nix_store_open(nix_c_context * context, const char * uri, const char *** storeRef.params[params[i][0]] = params[i][1]; } } - return new Store{nix::openStore(std::move(storeRef))}; + return new Store{nix::openStore(cStoreSettings, std::move(storeRef))}; } NIXC_CATCH_ERRS_NULL } @@ -312,8 +315,8 @@ StorePath * nix_add_derivation(nix_c_context * context, Store * store, nix_deriv without actually writing the derivation if this setting is set, but it was that way already, so we are doing this for back-compat for now. */ - auto ret = nix::settings.readOnlyMode ? nix::computeStorePath(*store->ptr, derivation->drv) - : store->ptr->writeDerivation(derivation->drv, nix::NoRepair); + auto ret = cStoreSettings.readOnlyMode ? nix::computeStorePath(*store->ptr, derivation->drv) + : store->ptr->writeDerivation(derivation->drv, nix::NoRepair); return new StorePath{ret}; } diff --git a/src/libstore-c/nix_api_store_internal.h b/src/libstore-c/nix_api_store_internal.h index 712d96488..d211b9839 100644 --- a/src/libstore-c/nix_api_store_internal.h +++ b/src/libstore-c/nix_api_store_internal.h @@ -5,6 +5,8 @@ extern "C" { +extern nix::Settings cStoreSettings; + struct Store { nix::ref ptr; diff --git a/src/libstore-test-support/https-store.cc b/src/libstore-test-support/https-store.cc index 5d5238958..4214f656b 100644 --- a/src/libstore-test-support/https-store.cc +++ b/src/libstore-test-support/https-store.cc @@ -1,4 +1,5 @@ #include "nix/store/tests/https-store.hh" +#include "nix/store/tests/test-main.hh" #include @@ -36,9 +37,9 @@ void HttpsBinaryCacheStoreTest::SetUp() cacheDir = tmpDir / "cache"; delTmpDir = std::make_unique(tmpDir); - localCacheStore = - make_ref("file", cacheDir.string(), LocalBinaryCacheStoreConfig::Params{}) - ->openStore(); + localCacheStore = make_ref( + settings, "file", cacheDir.string(), LocalBinaryCacheStoreConfig::Params{}) + ->openStore(); caCert = tmpDir / "ca.crt"; caKey = tmpDir / "ca.key"; @@ -121,6 +122,7 @@ std::vector HttpsBinaryCacheStoreMtlsTest::serverArgs() ref HttpsBinaryCacheStoreTest::makeConfig() { auto res = make_ref( + settings, ParsedURL{ .scheme = "https", .authority = diff --git a/src/libstore-test-support/include/nix/store/tests/https-store.hh b/src/libstore-test-support/include/nix/store/tests/https-store.hh index 3aa2cd345..2933c9741 100644 --- a/src/libstore-test-support/include/nix/store/tests/https-store.hh +++ b/src/libstore-test-support/include/nix/store/tests/https-store.hh @@ -12,6 +12,8 @@ #include "nix/util/file-system.hh" #include "nix/util/processes.hh" +#include "nix/store/tests/test-main.hh" + namespace nix::testing { class TestHttpBinaryCacheStoreConfig; @@ -42,9 +44,9 @@ public: class TestHttpBinaryCacheStoreConfig : public HttpBinaryCacheStoreConfig { public: - TestHttpBinaryCacheStoreConfig(ParsedURL url, const Store::Config::Params & params) - : StoreConfig(params) - , HttpBinaryCacheStoreConfig(url, params) + TestHttpBinaryCacheStoreConfig(nix::Settings & settings, ParsedURL url, const Store::Config::Params & params) + : StoreConfig(settings, params) + , HttpBinaryCacheStoreConfig(settings, url, params) { } @@ -56,6 +58,8 @@ class HttpsBinaryCacheStoreTest : public virtual LibStoreNetworkTest std::unique_ptr delTmpDir; public: + Settings settings = getTestSettings(); + static void SetUpTestSuite() { initLibStore(/*loadConfig=*/false); diff --git a/src/libstore-test-support/include/nix/store/tests/libstore.hh b/src/libstore-test-support/include/nix/store/tests/libstore.hh index d79b55312..65260f6ca 100644 --- a/src/libstore-test-support/include/nix/store/tests/libstore.hh +++ b/src/libstore-test-support/include/nix/store/tests/libstore.hh @@ -7,6 +7,7 @@ #include "nix/store/store-api.hh" #include "nix/store/store-open.hh" #include "nix/store/globals.hh" +#include "nix/store/tests/test-main.hh" namespace nix { @@ -18,14 +19,16 @@ public: initLibStore(false); } + Settings settings = getTestSettings(); + protected: - LibStoreTest(ref store) - : store(std::move(store)) + LibStoreTest(auto && makeStore) + : store(makeStore(settings)) { } LibStoreTest() - : LibStoreTest(openStore("dummy://")) + : LibStoreTest([](auto & settings) { return openStore(settings, "dummy://"); }) { } diff --git a/src/libstore-test-support/include/nix/store/tests/test-main.hh b/src/libstore-test-support/include/nix/store/tests/test-main.hh index 3a1897469..3f6eb9fcf 100644 --- a/src/libstore-test-support/include/nix/store/tests/test-main.hh +++ b/src/libstore-test-support/include/nix/store/tests/test-main.hh @@ -4,6 +4,13 @@ namespace nix { +class Settings; + +/** + * Get a Settings object configured appropriately for unit testing. + */ +Settings getTestSettings(); + /** * Call this for a GTest test suite that will including performing Nix * builds, before running tests. diff --git a/src/libstore-test-support/test-main.cc b/src/libstore-test-support/test-main.cc index 7c4a56ecc..5d246f4ed 100644 --- a/src/libstore-test-support/test-main.cc +++ b/src/libstore-test-support/test-main.cc @@ -7,12 +7,9 @@ namespace nix { -int testMainForBuidingPre(int argc, char ** argv) +Settings getTestSettings() { - if (argc > 1 && std::string_view(argv[1]) == "__build-remote") { - printError("test-build-remote: not supported in libexpr unit tests"); - return EXIT_FAILURE; - } + Settings settings; // Disable build hook. We won't be testing remote builds in these unit tests. If we do, fix the above build hook. settings.getWorkerSettings().buildHook = {}; @@ -41,6 +38,16 @@ int testMainForBuidingPre(int argc, char ** argv) setEnv("_NIX_TEST_NO_SANDBOX", "1"); #endif + return settings; +} + +int testMainForBuidingPre(int argc, char ** argv) +{ + if (argc > 1 && std::string_view(argv[1]) == "__build-remote") { + printError("test-build-remote: not supported in libexpr unit tests"); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; } diff --git a/src/libstore-tests/derivation-advanced-attrs.cc b/src/libstore-tests/derivation-advanced-attrs.cc index 16988613c..a7808a952 100644 --- a/src/libstore-tests/derivation-advanced-attrs.cc +++ b/src/libstore-tests/derivation-advanced-attrs.cc @@ -193,7 +193,7 @@ TYPED_TEST(DerivationAdvancedAttrsBothTest, advancedAttributes_defaults) EXPECT_EQ(options, advancedAttributes_defaults); - EXPECT_EQ(options.substitutesAllowed(settings.getWorkerSettings()), true); + EXPECT_EQ(options.substitutesAllowed(this->settings.getWorkerSettings()), true); EXPECT_EQ(options.useUidRange(got), false); }); }; @@ -241,7 +241,7 @@ TYPED_TEST(DerivationAdvancedAttrsBothTest, advancedAttributes) EXPECT_EQ(options, expected); - EXPECT_EQ(options.substitutesAllowed(settings.getWorkerSettings()), false); + EXPECT_EQ(options.substitutesAllowed(this->settings.getWorkerSettings()), false); EXPECT_EQ(options.useUidRange(got), true); }); }; @@ -333,7 +333,7 @@ TYPED_TEST(DerivationAdvancedAttrsBothTest, advancedAttributes_structuredAttrs_d EXPECT_EQ(options, advancedAttributes_structuredAttrs_defaults); - EXPECT_EQ(options.substitutesAllowed(settings.getWorkerSettings()), true); + EXPECT_EQ(options.substitutesAllowed(this->settings.getWorkerSettings()), true); EXPECT_EQ(options.useUidRange(got), false); }); }; @@ -398,7 +398,7 @@ TYPED_TEST(DerivationAdvancedAttrsBothTest, advancedAttributes_structuredAttrs) EXPECT_EQ(options, expected); - EXPECT_EQ(options.substitutesAllowed(settings.getWorkerSettings()), false); + EXPECT_EQ(options.substitutesAllowed(this->settings.getWorkerSettings()), false); EXPECT_EQ(options.useUidRange(got), true); }); }; diff --git a/src/libstore-tests/derivation/invariants.cc b/src/libstore-tests/derivation/invariants.cc index 115d5bc4b..3e012f2fe 100644 --- a/src/libstore-tests/derivation/invariants.cc +++ b/src/libstore-tests/derivation/invariants.cc @@ -16,11 +16,11 @@ class FillInOutputPathsTest : public LibStoreTest, public JsonCharacterizationTe protected: FillInOutputPathsTest() - : LibStoreTest([]() { - auto config = make_ref(DummyStoreConfig::Params{}); + : LibStoreTest([](auto & settings) { + auto config = make_ref(settings, DummyStoreConfig::Params{}); config->readOnly = false; return config->openDummyStore(); - }()) + }) { } diff --git a/src/libstore-tests/dummy-store.cc b/src/libstore-tests/dummy-store.cc index 4a12dcf78..773f5a623 100644 --- a/src/libstore-tests/dummy-store.cc +++ b/src/libstore-tests/dummy-store.cc @@ -7,6 +7,7 @@ #include "nix/store/realisation.hh" #include "nix/util/tests/json-characterization.hh" +#include "nix/store/tests/test-main.hh" namespace nix { @@ -31,8 +32,10 @@ TEST(DummyStore, realisation_read) { initLibStore(/*loadConfig=*/false); - auto store = [] { - auto cfg = make_ref(StoreReference::Params{}); + auto settings = getTestSettings(); + + auto store = [&] { + auto cfg = make_ref(settings, StoreReference::Params{}); cfg->readOnly = false; return cfg->openDummyStore(); }(); @@ -73,9 +76,10 @@ TEST_P(DummyStoreJsonTest, from_json) using namespace nlohmann; /* Cannot use `readJsonTest` because need to dereference the stores for equality. */ + auto settings = getTestSettings(); readTest(Path{name} + ".json", [&](const auto & encodedRaw) { auto encoded = json::parse(encodedRaw); - ref decoded = adl_serializer>::from_json(encoded); + ref decoded = adl_serializer>::from_json(settings, encoded); ASSERT_EQ(*decoded, *expected); }); } @@ -88,12 +92,13 @@ TEST_P(DummyStoreJsonTest, to_json) INSTANTIATE_TEST_SUITE_P(DummyStoreJSON, DummyStoreJsonTest, [] { initLibStore(false); - auto writeCfg = make_ref(DummyStore::Config::Params{}); + auto settings = getTestSettings(); + auto writeCfg = make_ref(settings, DummyStore::Config::Params{}); writeCfg->readOnly = false; return ::testing::Values( std::pair{ "empty", - make_ref(DummyStore::Config::Params{})->openDummyStore(), + make_ref(settings, DummyStore::Config::Params{})->openDummyStore(), }, std::pair{ "one-flat-file", diff --git a/src/libstore-tests/http-binary-cache-store.cc b/src/libstore-tests/http-binary-cache-store.cc index 33369cd9b..92e1f42d1 100644 --- a/src/libstore-tests/http-binary-cache-store.cc +++ b/src/libstore-tests/http-binary-cache-store.cc @@ -1,38 +1,44 @@ #include #include +#include "nix/store/globals.hh" #include "nix/store/http-binary-cache-store.hh" #include "nix/store/tests/https-store.hh" #include "nix/util/fs-sink.hh" +#include "nix/store/tests/test-main.hh" namespace nix { TEST(HttpBinaryCacheStore, constructConfig) { - HttpBinaryCacheStoreConfig config{"http", "foo.bar.baz", {}}; + auto settings = getTestSettings(); + HttpBinaryCacheStoreConfig config{settings, "http", "foo.bar.baz", {}}; EXPECT_EQ(config.cacheUri.to_string(), "http://foo.bar.baz"); } TEST(HttpBinaryCacheStore, constructConfigNoTrailingSlash) { - HttpBinaryCacheStoreConfig config{"https", "foo.bar.baz/a/b/", {}}; + auto settings = getTestSettings(); + HttpBinaryCacheStoreConfig config{settings, "https", "foo.bar.baz/a/b/", {}}; EXPECT_EQ(config.cacheUri.to_string(), "https://foo.bar.baz/a/b"); } TEST(HttpBinaryCacheStore, constructConfigWithParams) { + auto settings = getTestSettings(); StoreConfig::Params params{{"compression", "xz"}}; - HttpBinaryCacheStoreConfig config{"https", "foo.bar.baz/a/b/", params}; + HttpBinaryCacheStoreConfig config{settings, "https", "foo.bar.baz/a/b/", params}; EXPECT_EQ(config.cacheUri.to_string(), "https://foo.bar.baz/a/b"); EXPECT_EQ(config.getReference().params, params); } TEST(HttpBinaryCacheStore, constructConfigWithParamsAndUrlWithParams) { + auto settings = getTestSettings(); StoreConfig::Params params{{"compression", "xz"}}; - HttpBinaryCacheStoreConfig config{"https", "foo.bar.baz/a/b?some-param=some-value", params}; + HttpBinaryCacheStoreConfig config{settings, "https", "foo.bar.baz/a/b?some-param=some-value", params}; EXPECT_EQ(config.cacheUri.to_string(), "https://foo.bar.baz/a/b?some-param=some-value"); EXPECT_EQ(config.getReference().params, params); } diff --git a/src/libstore-tests/legacy-ssh-store.cc b/src/libstore-tests/legacy-ssh-store.cc index d60ecc424..b92e99df1 100644 --- a/src/libstore-tests/legacy-ssh-store.cc +++ b/src/libstore-tests/legacy-ssh-store.cc @@ -1,12 +1,16 @@ #include +#include "nix/store/globals.hh" #include "nix/store/legacy-ssh-store.hh" +#include "nix/store/tests/test-main.hh" namespace nix { TEST(LegacySSHStore, constructConfig) { + auto settings = getTestSettings(); LegacySSHStoreConfig config( + settings, "ssh", "me@localhost:2222", StoreConfig::Params{ diff --git a/src/libstore-tests/local-binary-cache-store.cc b/src/libstore-tests/local-binary-cache-store.cc index 01f514e89..d771cd70a 100644 --- a/src/libstore-tests/local-binary-cache-store.cc +++ b/src/libstore-tests/local-binary-cache-store.cc @@ -1,12 +1,14 @@ #include +#include "nix/store/globals.hh" #include "nix/store/local-binary-cache-store.hh" namespace nix { TEST(LocalBinaryCacheStore, constructConfig) { - LocalBinaryCacheStoreConfig config{"local", "/foo/bar/baz", {}}; + Settings settings; + LocalBinaryCacheStoreConfig config{settings, "local", "/foo/bar/baz", {}}; EXPECT_EQ(config.binaryCacheDir, "/foo/bar/baz"); } diff --git a/src/libstore-tests/local-overlay-store.cc b/src/libstore-tests/local-overlay-store.cc index 175e5d0f4..9bdbbe723 100644 --- a/src/libstore-tests/local-overlay-store.cc +++ b/src/libstore-tests/local-overlay-store.cc @@ -1,12 +1,16 @@ #include +#include "nix/store/globals.hh" #include "nix/store/local-overlay-store.hh" +#include "nix/store/tests/test-main.hh" namespace nix { TEST(LocalOverlayStore, constructConfig_rootQueryParam) { + auto settings = getTestSettings(); LocalOverlayStoreConfig config{ + settings, "local-overlay", "", { @@ -22,7 +26,8 @@ TEST(LocalOverlayStore, constructConfig_rootQueryParam) TEST(LocalOverlayStore, constructConfig_rootPath) { - LocalOverlayStoreConfig config{"local-overlay", "/foo/bar", {}}; + auto settings = getTestSettings(); + LocalOverlayStoreConfig config{settings, "local-overlay", "/foo/bar", {}}; EXPECT_EQ(config.rootDir.get(), std::optional{"/foo/bar"}); } diff --git a/src/libstore-tests/local-store.cc b/src/libstore-tests/local-store.cc index d00888897..40d762231 100644 --- a/src/libstore-tests/local-store.cc +++ b/src/libstore-tests/local-store.cc @@ -1,18 +1,16 @@ #include +#include "nix/store/globals.hh" #include "nix/store/local-store.hh" - -// Needed for template specialisations. This is not good! When we -// overhaul how store configs work, this should be fixed. -#include "nix/util/args.hh" -#include "nix/util/config-impl.hh" -#include "nix/util/abstract-setting-to-json.hh" +#include "nix/store/tests/test-main.hh" namespace nix { TEST(LocalStore, constructConfig_rootQueryParam) { + auto settings = getTestSettings(); LocalStoreConfig config{ + settings, "local", "", { @@ -28,14 +26,16 @@ TEST(LocalStore, constructConfig_rootQueryParam) TEST(LocalStore, constructConfig_rootPath) { - LocalStoreConfig config{"local", "/foo/bar", {}}; + auto settings = getTestSettings(); + LocalStoreConfig config{settings, "local", "/foo/bar", {}}; EXPECT_EQ(config.rootDir.get(), std::optional{"/foo/bar"}); } TEST(LocalStore, constructConfig_to_string) { - LocalStoreConfig config{"local", "", {}}; + auto settings = getTestSettings(); + LocalStoreConfig config{settings, "local", "", {}}; EXPECT_EQ(config.getReference().to_string(), "local"); } diff --git a/src/libstore-tests/nar-info-disk-cache.cc b/src/libstore-tests/nar-info-disk-cache.cc index 1e61dcc34..dd0d3f75d 100644 --- a/src/libstore-tests/nar-info-disk-cache.cc +++ b/src/libstore-tests/nar-info-disk-cache.cc @@ -1,11 +1,13 @@ -#include "nix/store/nar-info-disk-cache.hh" - #include #include #include "nix/store/globals.hh" #include "nix/store/sqlite.hh" #include +#include "nix/store/globals.hh" +#include "nix/store/sqlite.hh" +#include "nix/store/nar-info-disk-cache.hh" + namespace nix { TEST(NarInfoDiskCacheImpl, create_and_read) @@ -24,6 +26,8 @@ TEST(NarInfoDiskCacheImpl, create_and_read) SQLite db; SQLiteStmt getIds; + Settings settings; + { auto cache = NarInfoDiskCache::getTest( settings.getNarInfoDiskCacheSettings(), {.useWAL = settings.useSQLiteWAL}, dbPath.string()); diff --git a/src/libstore-tests/nix_api_store.cc b/src/libstore-tests/nix_api_store.cc index 9bfbf8453..cbe2e8fe7 100644 --- a/src/libstore-tests/nix_api_store.cc +++ b/src/libstore-tests/nix_api_store.cc @@ -299,7 +299,7 @@ public: nix_api_store_test_base::SetUp(); nix::experimentalFeatureSettings.set("extra-experimental-features", "ca-derivations"); - nix::settings.getWorkerSettings().substituters = {}; + cStoreSettings.getWorkerSettings().substituters = {}; store = open_local_store(); @@ -309,7 +309,7 @@ public: buffer << t.rdbuf(); // Replace the hardcoded system with the current system - std::string jsonStr = nix::replaceStrings(buffer.str(), "x86_64-linux", nix::settings.thisSystem.get()); + std::string jsonStr = nix::replaceStrings(buffer.str(), "x86_64-linux", cStoreSettings.thisSystem.get()); drv = nix_derivation_from_json(ctx, store, jsonStr.c_str()); assert_ctx_ok(); @@ -354,7 +354,7 @@ TEST_F(nix_api_store_test_base, build_from_json) { // FIXME get rid of these nix::experimentalFeatureSettings.set("extra-experimental-features", "ca-derivations"); - nix::settings.getWorkerSettings().substituters = {}; + cStoreSettings.getWorkerSettings().substituters = {}; auto * store = open_local_store(); @@ -365,7 +365,7 @@ TEST_F(nix_api_store_test_base, build_from_json) buffer << t.rdbuf(); // Replace the hardcoded system with the current system - std::string jsonStr = nix::replaceStrings(buffer.str(), "x86_64-linux", nix::settings.thisSystem.get()); + std::string jsonStr = nix::replaceStrings(buffer.str(), "x86_64-linux", cStoreSettings.thisSystem.get()); auto * drv = nix_derivation_from_json(ctx, store, jsonStr.c_str()); assert_ctx_ok(); @@ -401,7 +401,7 @@ TEST_F(nix_api_store_test_base, nix_store_realise_invalid_system) { // Test that nix_store_realise properly reports errors when the system is invalid nix::experimentalFeatureSettings.set("extra-experimental-features", "ca-derivations"); - nix::settings.getWorkerSettings().substituters = {}; + cStoreSettings.getWorkerSettings().substituters = {}; auto * store = open_local_store(); @@ -446,7 +446,7 @@ TEST_F(nix_api_store_test_base, nix_store_realise_builder_fails) { // Test that nix_store_realise properly reports errors when the builder fails nix::experimentalFeatureSettings.set("extra-experimental-features", "ca-derivations"); - nix::settings.getWorkerSettings().substituters = {}; + cStoreSettings.getWorkerSettings().substituters = {}; auto * store = open_local_store(); @@ -456,7 +456,7 @@ TEST_F(nix_api_store_test_base, nix_store_realise_builder_fails) buffer << t.rdbuf(); // Replace with current system and make builder command fail - std::string jsonStr = nix::replaceStrings(buffer.str(), "x86_64-linux", nix::settings.thisSystem.get()); + std::string jsonStr = nix::replaceStrings(buffer.str(), "x86_64-linux", cStoreSettings.thisSystem.get()); jsonStr = nix::replaceStrings(jsonStr, "echo $name foo > $out", "exit 1"); auto * drv = nix_derivation_from_json(ctx, store, jsonStr.c_str()); @@ -491,7 +491,7 @@ TEST_F(nix_api_store_test_base, nix_store_realise_builder_no_output) { // Test that nix_store_realise properly reports errors when builder succeeds but produces no output nix::experimentalFeatureSettings.set("extra-experimental-features", "ca-derivations"); - nix::settings.getWorkerSettings().substituters = {}; + cStoreSettings.getWorkerSettings().substituters = {}; auto * store = open_local_store(); @@ -501,7 +501,7 @@ TEST_F(nix_api_store_test_base, nix_store_realise_builder_no_output) buffer << t.rdbuf(); // Replace with current system and make builder succeed but not produce output - std::string jsonStr = nix::replaceStrings(buffer.str(), "x86_64-linux", nix::settings.thisSystem.get()); + std::string jsonStr = nix::replaceStrings(buffer.str(), "x86_64-linux", cStoreSettings.thisSystem.get()); jsonStr = nix::replaceStrings(jsonStr, "echo $name foo > $out", "true"); auto * drv = nix_derivation_from_json(ctx, store, jsonStr.c_str()); @@ -687,7 +687,7 @@ TEST_F(NixApiStoreTestWithRealisedPath, nix_store_realise_output_ordering) // This test uses a CA derivation with 10 outputs in randomized input order // to verify that the callback order is deterministic and alphabetical. nix::experimentalFeatureSettings.set("extra-experimental-features", "ca-derivations"); - nix::settings.getWorkerSettings().substituters = {}; + cStoreSettings.getWorkerSettings().substituters = {}; auto * store = open_local_store(); @@ -706,14 +706,14 @@ TEST_F(NixApiStoreTestWithRealisedPath, nix_store_realise_output_ordering) std::string drvJson = R"({ "version": 4, "name": "multi-output-test", - "system": ")" + nix::settings.thisSystem.get() + "system": ")" + cStoreSettings.thisSystem.get() + R"(", "builder": "/bin/sh", "args": ["-c", "echo a > $outa; echo b > $outb; echo c > $outc; echo d > $outd; echo e > $oute; echo f > $outf; echo g > $outg; echo h > $outh; echo i > $outi; echo j > $outj"], "env": { "builder": "/bin/sh", "name": "multi-output-test", - "system": ")" + nix::settings.thisSystem.get() + "system": ")" + cStoreSettings.thisSystem.get() + R"(", "outf": ")" + outf_ph + R"(", diff --git a/src/libstore-tests/s3-binary-cache-store.cc b/src/libstore-tests/s3-binary-cache-store.cc index 59090a589..62629a7fd 100644 --- a/src/libstore-tests/s3-binary-cache-store.cc +++ b/src/libstore-tests/s3-binary-cache-store.cc @@ -1,7 +1,9 @@ +#include "nix/store/globals.hh" #include "nix/store/s3-binary-cache-store.hh" #include "nix/store/http-binary-cache-store.hh" #include "nix/store/filetransfer.hh" #include "nix/store/s3-url.hh" +#include "nix/store/tests/test-main.hh" #include @@ -9,7 +11,8 @@ namespace nix { TEST(S3BinaryCacheStore, constructConfig) { - S3BinaryCacheStoreConfig config{"s3", "foobar", {}}; + auto settings = getTestSettings(); + S3BinaryCacheStoreConfig config{settings, "s3", "foobar", {}}; // The bucket name is stored as the host part of the authority in cacheUri EXPECT_EQ( @@ -22,8 +25,9 @@ TEST(S3BinaryCacheStore, constructConfig) TEST(S3BinaryCacheStore, constructConfigWithRegion) { + auto settings = getTestSettings(); Store::Config::Params params{{"region", "eu-west-1"}}; - S3BinaryCacheStoreConfig config{"s3", "my-bucket", params}; + S3BinaryCacheStoreConfig config{settings, "s3", "my-bucket", params}; EXPECT_EQ( config.cacheUri, @@ -37,7 +41,8 @@ TEST(S3BinaryCacheStore, constructConfigWithRegion) TEST(S3BinaryCacheStore, defaultSettings) { - S3BinaryCacheStoreConfig config{"s3", "test-bucket", {}}; + auto settings = getTestSettings(); + S3BinaryCacheStoreConfig config{settings, "s3", "test-bucket", {}}; EXPECT_EQ( config.cacheUri, @@ -58,11 +63,13 @@ TEST(S3BinaryCacheStore, defaultSettings) */ TEST(S3BinaryCacheStore, s3StoreConfigPreservesParameters) { + auto settings = getTestSettings(); + StringMap params; params["region"] = "eu-west-1"; params["endpoint"] = "custom.s3.com"; - S3BinaryCacheStoreConfig config("s3", "test-bucket", params); + S3BinaryCacheStoreConfig config(settings, "s3", "test-bucket", params); // The config should preserve S3-specific parameters EXPECT_EQ( @@ -93,13 +100,15 @@ TEST(S3BinaryCacheStore, s3SchemeRegistration) */ TEST(S3BinaryCacheStore, parameterFiltering) { + auto settings = getTestSettings(); + StringMap params; params["region"] = "eu-west-1"; params["endpoint"] = "minio.local"; params["want-mass-query"] = "true"; // Non-S3 store parameter params["priority"] = "10"; // Non-S3 store parameter - S3BinaryCacheStoreConfig config("s3", "test-bucket", params); + S3BinaryCacheStoreConfig config(settings, "s3", "test-bucket", params); // Only S3-specific params should be in cacheUri.query EXPECT_EQ( @@ -127,16 +136,19 @@ TEST(S3BinaryCacheStore, parameterFiltering) */ TEST(S3BinaryCacheStore, storageClassDefault) { - S3BinaryCacheStoreConfig config{"s3", "test-bucket", {}}; + auto settings = getTestSettings(); + S3BinaryCacheStoreConfig config{settings, "s3", "test-bucket", {}}; EXPECT_EQ(config.storageClass.get(), std::nullopt); } TEST(S3BinaryCacheStore, storageClassConfiguration) { + auto settings = getTestSettings(); + StringMap params; params["storage-class"] = "GLACIER"; - S3BinaryCacheStoreConfig config("s3", "test-bucket", params); + S3BinaryCacheStoreConfig config(settings, "s3", "test-bucket", params); EXPECT_EQ(config.storageClass.get(), std::optional("GLACIER")); } diff --git a/src/libstore-tests/ssh-store.cc b/src/libstore-tests/ssh-store.cc index a156da52b..c0a32d0a8 100644 --- a/src/libstore-tests/ssh-store.cc +++ b/src/libstore-tests/ssh-store.cc @@ -1,14 +1,16 @@ #include +#include "nix/store/globals.hh" #include "nix/store/ssh-store.hh" -#include "nix/util/config-impl.hh" -#include "nix/util/abstract-setting-to-json.hh" +#include "nix/store/tests/test-main.hh" namespace nix { TEST(SSHStore, constructConfig) { + auto settings = getTestSettings(); SSHStoreConfig config{ + settings, "ssh-ng", "me@localhost:2222", StoreConfig::Params{ @@ -34,7 +36,9 @@ TEST(SSHStore, constructConfig) TEST(MountedSSHStore, constructConfig) { + auto settings = getTestSettings(); MountedSSHStoreConfig config{ + settings, "mounted-ssh", "localhost", StoreConfig::Params{ diff --git a/src/libstore-tests/uds-remote-store.cc b/src/libstore-tests/uds-remote-store.cc index 415dfc4ac..cc8eca251 100644 --- a/src/libstore-tests/uds-remote-store.cc +++ b/src/libstore-tests/uds-remote-store.cc @@ -1,31 +1,38 @@ #include +#include "nix/store/globals.hh" #include "nix/store/uds-remote-store.hh" namespace nix { TEST(UDSRemoteStore, constructConfig) { - UDSRemoteStoreConfig config{"unix", "/tmp/socket", {}}; + Settings settings; + UDSRemoteStoreConfig config{settings, "unix", "/tmp/socket", {}}; EXPECT_EQ(config.path, "/tmp/socket"); } TEST(UDSRemoteStore, constructConfigWrongScheme) { - EXPECT_THROW(UDSRemoteStoreConfig("http", "/tmp/socket", {}), UsageError); + Settings settings; + EXPECT_THROW(UDSRemoteStoreConfig(settings, "http", "/tmp/socket", {}), UsageError); } TEST(UDSRemoteStore, constructConfig_to_string) { - UDSRemoteStoreConfig config{"unix", "", {}}; + Settings settings; + UDSRemoteStoreConfig config{settings, "unix", "", {}}; EXPECT_EQ(config.getReference().to_string(), "daemon"); } TEST(UDSRemoteStore, constructConfigWithParams) { + Settings settings; + StoreConfig::Params params{{"max-connections", "1"}}; - UDSRemoteStoreConfig config{"unix", "/tmp/socket", params}; + UDSRemoteStoreConfig config{settings, "unix", "/tmp/socket", params}; + auto storeReference = config.getReference(); EXPECT_EQ(storeReference.to_string(), "unix:///tmp/socket?max-connections=1"); EXPECT_EQ(storeReference.render(/*withParams=*/false), "unix:///tmp/socket"); @@ -34,8 +41,11 @@ TEST(UDSRemoteStore, constructConfigWithParams) TEST(UDSRemoteStore, constructConfigWithParamsNoPath) { + Settings settings; + StoreConfig::Params params{{"max-connections", "1"}}; - UDSRemoteStoreConfig config{"unix", "", params}; + UDSRemoteStoreConfig config{settings, "unix", "", params}; + auto storeReference = config.getReference(); EXPECT_EQ(storeReference.to_string(), "daemon?max-connections=1"); EXPECT_EQ(storeReference.render(/*withParams=*/false), "daemon"); diff --git a/src/libstore-tests/worker-substitution.cc b/src/libstore-tests/worker-substitution.cc index 3a049b7e7..1f624b88c 100644 --- a/src/libstore-tests/worker-substitution.cc +++ b/src/libstore-tests/worker-substitution.cc @@ -21,14 +21,14 @@ protected: ref substituter; WorkerSubstitutionTest() - : LibStoreTest([] { - auto config = make_ref(DummyStoreConfig::Params{}); + : LibStoreTest([](auto & settings) { + auto config = make_ref(settings, DummyStoreConfig::Params{}); config->readOnly = false; return config->openDummyStore(); - }()) + }) , dummyStore(store.dynamic_pointer_cast()) - , substituter([] { - auto config = make_ref(DummyStoreConfig::Params{}); + , substituter([this] { + auto config = make_ref(settings, DummyStoreConfig::Params{}); config->readOnly = false; config->isTrusted = true; return config->openDummyStore(); @@ -67,10 +67,10 @@ TEST_F(WorkerSubstitutionTest, singleStoreObject) HashAlgorithm::SHA256); // Snapshot the substituter (has one store object) - checkpointJson("single/substituter", substituter); + checkpointJson("single/substituter", substituter, settings); // Snapshot the destination store before (should be empty) - checkpointJson("../dummy-store/empty", dummyStore); + checkpointJson("../dummy-store/empty", dummyStore, settings); // The path should not exist in the destination store yet ASSERT_FALSE(dummyStore->isValidPath(pathInSubstituter)); @@ -92,7 +92,7 @@ TEST_F(WorkerSubstitutionTest, singleStoreObject) worker.run(goals); // Snapshot the destination store after (should match the substituter) - checkpointJson("single/substituter", dummyStore); + checkpointJson("single/substituter", dummyStore, settings); // The path should now exist in the destination store ASSERT_TRUE(dummyStore->isValidPath(pathInSubstituter)); @@ -138,10 +138,10 @@ TEST_F(WorkerSubstitutionTest, singleRootStoreObjectWithSingleDepStoreObject) StorePathSet{dependencyPath}); // Snapshot the substituter (has two store objects) - checkpointJson("with-dep/substituter", substituter); + checkpointJson("with-dep/substituter", substituter, settings); // Snapshot the destination store before (should be empty) - checkpointJson("../dummy-store/empty", dummyStore); + checkpointJson("../dummy-store/empty", dummyStore, settings); // Neither path should exist in the destination store yet ASSERT_FALSE(dummyStore->isValidPath(dependencyPath)); @@ -164,7 +164,7 @@ TEST_F(WorkerSubstitutionTest, singleRootStoreObjectWithSingleDepStoreObject) worker.run(goals); // Snapshot the destination store after (should match the substituter) - checkpointJson("with-dep/substituter", dummyStore); + checkpointJson("with-dep/substituter", dummyStore, settings); // Both paths should now exist in the destination store ASSERT_TRUE(dummyStore->isValidPath(dependencyPath)); @@ -196,7 +196,7 @@ TEST_F(WorkerSubstitutionTest, floatingDerivationOutput) auto drvPath = dummyStore->writeDerivation(drv); // Snapshot the destination store before - checkpointJson("ca-drv/store-before", dummyStore); + checkpointJson("ca-drv/store-before", dummyStore, settings); // Compute the hash modulo of the derivation // For CA floating derivations, the kind is Deferred since outputs aren't known until build @@ -233,7 +233,7 @@ TEST_F(WorkerSubstitutionTest, floatingDerivationOutput) }); // Snapshot the substituter - checkpointJson("ca-drv/substituter", substituter); + checkpointJson("ca-drv/substituter", substituter, settings); // The realisation should not exist in the destination store yet DrvOutput drvOutput{drvHash, "out"}; @@ -256,7 +256,7 @@ TEST_F(WorkerSubstitutionTest, floatingDerivationOutput) worker.run(goals); // Snapshot the destination store after - checkpointJson("ca-drv/store-after", dummyStore); + checkpointJson("ca-drv/store-after", dummyStore, settings); // The output path should now exist in the destination store ASSERT_TRUE(dummyStore->isValidPath(outputPath)); @@ -351,7 +351,7 @@ TEST_F(WorkerSubstitutionTest, floatingDerivationOutputWithDepDrv) auto rootDrvPath = dummyStore->writeDerivation(rootDrv); // Snapshot the destination store before - checkpointJson("issue-11928/store-before", dummyStore); + checkpointJson("issue-11928/store-before", dummyStore, settings); // Compute the hash modulo for the root derivation auto rootHashModulo = hashDerivationModulo(*dummyStore, rootDrv, true); @@ -395,7 +395,7 @@ TEST_F(WorkerSubstitutionTest, floatingDerivationOutputWithDepDrv) // Snapshot the substituter // Note: it has realisations for both drvs, but only the root's output store object - checkpointJson("issue-11928/substituter", substituter); + checkpointJson("issue-11928/substituter", substituter, settings); // The realisations should not exist in the destination store yet ASSERT_FALSE(dummyStore->queryRealisation(depDrvOutput)); @@ -418,7 +418,7 @@ TEST_F(WorkerSubstitutionTest, floatingDerivationOutputWithDepDrv) worker.run(goals); // Snapshot the destination store after - checkpointJson("issue-11928/store-after", dummyStore); + checkpointJson("issue-11928/store-after", dummyStore, settings); // The root output path should now exist in the destination store ASSERT_TRUE(dummyStore->isValidPath(rootOutputPath)); diff --git a/src/libstore-tests/write-derivation.cc b/src/libstore-tests/write-derivation.cc index c68753823..18f837523 100644 --- a/src/libstore-tests/write-derivation.cc +++ b/src/libstore-tests/write-derivation.cc @@ -12,19 +12,22 @@ namespace { class WriteDerivationTest : public LibStoreTest { protected: - WriteDerivationTest(ref config_) - : LibStoreTest(config_->openDummyStore()) - , config(std::move(config_)) + WriteDerivationTest(auto && makeConfig) + : LibStoreTest([&](auto & settings) { + this->config = makeConfig(settings); + return config->openDummyStore(); + }) { config->readOnly = false; } WriteDerivationTest() - : WriteDerivationTest(make_ref(DummyStoreConfig::Params{})) + : WriteDerivationTest( + [](auto & settings) { return make_ref(settings, DummyStoreConfig::Params{}); }) { } - ref config; + std::shared_ptr config; }; static Derivation makeSimpleDrv() diff --git a/src/libstore/build/derivation-building-goal.cc b/src/libstore/build/derivation-building-goal.cc index e19c9cf1f..8497791b3 100644 --- a/src/libstore/build/derivation-building-goal.cc +++ b/src/libstore/build/derivation-building-goal.cc @@ -327,8 +327,8 @@ Goal::Co DerivationBuildingGoal::tryToBuild(StorePathSet inputPaths) } bool canBuildLocally = [&] { - if (drv->platform != settings.thisSystem.get() && !settings.extraPlatforms.get().count(drv->platform) - && !drv->isBuiltin()) + if (drv->platform != worker.store.config.settings.thisSystem.get() + && !worker.store.config.settings.extraPlatforms.get().count(drv->platform) && !drv->isBuiltin()) return false; if (worker.settings.maxBuildJobs.get() == 0 && !drv->isBuiltin()) @@ -377,7 +377,7 @@ Goal::Co DerivationBuildingGoal::tryToBuild(StorePathSet inputPaths) use that. If there is not, then check if we can do a "true" local build. */ - externalBuilder = settings.findExternalDerivationBuilderIfSupported(*drv); + externalBuilder = worker.store.config.settings.findExternalDerivationBuilderIfSupported(*drv); if (!externalBuilder && !canBuildLocally) { auto msg = @@ -389,12 +389,12 @@ Goal::Co DerivationBuildingGoal::tryToBuild(StorePathSet inputPaths) Magenta(worker.store.printStorePath(drvPath)), Magenta(drv->platform), concatStringsSep(", ", drvOptions.getRequiredSystemFeatures(*drv)), - Magenta(settings.thisSystem), + Magenta(worker.store.config.settings.thisSystem), concatStringsSep(", ", worker.store.Store::config.systemFeatures)); // since aarch64-darwin has Rosetta 2, this user can actually run x86_64-darwin on their hardware - // we should tell them to run the command to install Darwin 2 - if (drv->platform == "x86_64-darwin" && settings.thisSystem == "aarch64-darwin") + if (drv->platform == "x86_64-darwin" && worker.store.config.settings.thisSystem == "aarch64-darwin") msg += fmt( "\nNote: run `%s` to run programs for x86_64-darwin", Magenta( @@ -479,7 +479,8 @@ Goal::Co DerivationBuildingGoal::buildWithHook( hook->toHook.writeSide.close(); /* Create the log file and pipe. */ - std::unique_ptr logFile = std::make_unique(worker.store, drvPath, settings.getLogFileSettings()); + std::unique_ptr logFile = + std::make_unique(worker.store, drvPath, worker.store.config.settings.getLogFileSettings()); std::set fds; fds.insert(hook->fromHook.readSide.get()); @@ -655,7 +656,7 @@ Goal::Co DerivationBuildingGoal::buildLocally( std::unique_ptr logFile; auto openLogFile = [&]() { - logFile = std::make_unique(worker.store, drvPath, settings.getLogFileSettings()); + logFile = std::make_unique(worker.store, drvPath, worker.store.config.settings.getLogFileSettings()); }; auto closeLogFile = [&]() { logFile.reset(); }; @@ -756,6 +757,7 @@ Goal::Co DerivationBuildingGoal::buildLocally( } DerivationBuilderParams params{ + .settings = worker.store.config.settings, .drvPath = drvPath, .buildResult = buildResult, .drv = *drv, diff --git a/src/libstore/build/worker.cc b/src/libstore/build/worker.cc index 1069b68a1..4dbe898c0 100644 --- a/src/libstore/build/worker.cc +++ b/src/libstore/build/worker.cc @@ -22,9 +22,9 @@ Worker::Worker(Store & store, Store & evalStore) , actSubstitutions(*logger, actCopyPaths) , store(store) , evalStore(evalStore) - , settings(nix::settings.getWorkerSettings()) - , getSubstituters{[] { - return nix::settings.getWorkerSettings().useSubstitutes ? getDefaultSubstituters() : std::list>{}; + , settings(store.config.settings.getWorkerSettings()) + , getSubstituters{[&] { + return settings.useSubstitutes ? getDefaultSubstituters(store.config.settings) : std::list>{}; }} { nrLocalBuilds = 0; @@ -367,7 +367,9 @@ void Worker::run(const Goals & _topGoals) if (!children.empty() || !waitingForAWhile.empty()) waitForInput(); else if (awake.empty() && 0U == settings.maxBuildJobs) { - if (Machine::parseConfig({nix::settings.thisSystem}, nix::settings.getWorkerSettings().builders).empty()) + if (Machine::parseConfig( + {store.config.settings.thisSystem}, store.config.settings.getWorkerSettings().builders) + .empty()) throw Error( "Unable to start any build; either increase '--max-jobs' or enable remote builds.\n" "\n" diff --git a/src/libstore/common-ssh-store-config.cc b/src/libstore/common-ssh-store-config.cc index 12f187b4c..5eec1e5fa 100644 --- a/src/libstore/common-ssh-store-config.cc +++ b/src/libstore/common-ssh-store-config.cc @@ -5,14 +5,15 @@ namespace nix { -CommonSSHStoreConfig::CommonSSHStoreConfig(std::string_view scheme, std::string_view authority, const Params & params) - : CommonSSHStoreConfig(scheme, ParsedURL::Authority::parse(authority), params) +CommonSSHStoreConfig::CommonSSHStoreConfig( + nix::Settings & settings, std::string_view scheme, std::string_view authority, const Params & params) + : CommonSSHStoreConfig(settings, scheme, ParsedURL::Authority::parse(authority), params) { } CommonSSHStoreConfig::CommonSSHStoreConfig( - std::string_view scheme, const ParsedURL::Authority & authority, const Params & params) - : StoreConfig(params) + nix::Settings & settings, std::string_view scheme, const ParsedURL::Authority & authority, const Params & params) + : StoreConfig(settings, params) , authority(authority) { } diff --git a/src/libstore/daemon.cc b/src/libstore/daemon.cc index 155fe2432..4c6ac76d6 100644 --- a/src/libstore/daemon.cc +++ b/src/libstore/daemon.cc @@ -230,7 +230,7 @@ struct ClientSettings bool useSubstitutes; StringMap overrides; - void apply(TrustedFlag trusted) + void apply(Settings & settings, TrustedFlag trusted) { settings.keepFailed = keepFailed; settings.getWorkerSettings().keepGoing = keepGoing; @@ -783,7 +783,7 @@ static void performOp( // FIXME: use some setting in recursive mode. Will need to use // non-global variables. if (!recursive) - clientSettings.apply(trusted); + clientSettings.apply(store->config.settings, trusted); logger->stopWork(); break; diff --git a/src/libstore/dummy-store.cc b/src/libstore/dummy-store.cc index c452d38bf..d03022aaf 100644 --- a/src/libstore/dummy-store.cc +++ b/src/libstore/dummy-store.cc @@ -408,10 +408,10 @@ void adl_serializer::to_json(json & json, const }; } -ref adl_serializer>::from_json(const json & json) +ref adl_serializer>::from_json(Settings & settings, const json & json) { auto & obj = getObject(json); - auto cfg = make_ref(DummyStore::Config::Params{}); + auto cfg = make_ref(settings, DummyStore::Config::Params{}); const_cast(cfg->storeDir_).set(getString(valueAt(obj, "store"))); cfg->readOnly = true; return cfg; @@ -424,10 +424,11 @@ void adl_serializer::to_json(json & json, const DummyStoreConf }; } -ref adl_serializer>::from_json(const json & json) +ref adl_serializer>::from_json(Settings & settings, const json & json) { auto & obj = getObject(json); - ref res = adl_serializer>::from_json(valueAt(obj, "config"))->openDummyStore(); + ref res = + adl_serializer>::from_json(settings, valueAt(obj, "config"))->openDummyStore(); for (auto & [k, v] : getObject(valueAt(obj, "contents"))) res->contents.insert({StorePath{k}, v}); for (auto & [k, v] : getObject(valueAt(obj, "derivations"))) diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index dbc111905..088d697b1 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -79,10 +79,6 @@ LogFileSettings::LogFileSettings() { } -Settings settings; - -static GlobalConfig::Register rSettings(&settings); - Settings::Settings() : nixStateDir(canonPath(getEnvNonEmpty("NIX_STATE_DIR").value_or(NIX_STATE_DIR))) , nixDaemonSocketFile(canonPath(getEnvOsNonEmpty(OS_STR("NIX_DAEMON_SOCKET_PATH")) diff --git a/src/libstore/http-binary-cache-store.cc b/src/libstore/http-binary-cache-store.cc index c1bee215c..3ca255894 100644 --- a/src/libstore/http-binary-cache-store.cc +++ b/src/libstore/http-binary-cache-store.cc @@ -5,7 +5,6 @@ #include "nix/store/sqlite.hh" #include "nix/util/callback.hh" #include "nix/store/store-registration.hh" -#include "nix/store/globals.hh" namespace nix { @@ -21,8 +20,9 @@ StringSet HttpBinaryCacheStoreConfig::uriSchemes() } HttpBinaryCacheStoreConfig::HttpBinaryCacheStoreConfig( - std::string_view scheme, std::string_view _cacheUri, const Params & params) + nix::Settings & settings, std::string_view scheme, std::string_view _cacheUri, const Params & params) : HttpBinaryCacheStoreConfig( + settings, parseURL( std::string{scheme} + "://" + (!_cacheUri.empty() @@ -32,9 +32,10 @@ HttpBinaryCacheStoreConfig::HttpBinaryCacheStoreConfig( { } -HttpBinaryCacheStoreConfig::HttpBinaryCacheStoreConfig(ParsedURL _cacheUri, const Params & params) - : StoreConfig(params) - , BinaryCacheStoreConfig(params) +HttpBinaryCacheStoreConfig::HttpBinaryCacheStoreConfig( + nix::Settings & settings, ParsedURL _cacheUri, const Params & params) + : StoreConfig(settings, params) + , BinaryCacheStoreConfig(settings, params) , cacheUri(std::move(_cacheUri)) { while (!cacheUri.path.empty() && cacheUri.path.back() == "") @@ -66,7 +67,8 @@ HttpBinaryCacheStore::HttpBinaryCacheStore(ref config, ref , fileTransfer{fileTransfer} , config{config} { - diskCache = NarInfoDiskCache::get(settings.getNarInfoDiskCacheSettings(), {.useWAL = settings.useSQLiteWAL}); + diskCache = NarInfoDiskCache::get( + config->settings.getNarInfoDiskCacheSettings(), {.useWAL = config->settings.useSQLiteWAL}); } void HttpBinaryCacheStore::init() @@ -104,7 +106,7 @@ std::optional HttpBinaryCacheStore::getCompressionMethod(const void HttpBinaryCacheStore::maybeDisable() { auto state(_state.lock()); - if (state->enabled && settings.getWorkerSettings().tryFallback) { + if (state->enabled && config->settings.getWorkerSettings().tryFallback) { int t = 60; printError("disabling binary cache '%s' for %s seconds", config->getHumanReadableURI(), t); state->enabled = false; diff --git a/src/libstore/include/nix/store/build/derivation-builder.hh b/src/libstore/include/nix/store/build/derivation-builder.hh index a56cbe3c2..ec4608f5d 100644 --- a/src/libstore/include/nix/store/build/derivation-builder.hh +++ b/src/libstore/include/nix/store/build/derivation-builder.hh @@ -16,6 +16,8 @@ namespace nix { +class Settings; + /** * Denotes a build failure that stemmed from the builder exiting with a * failing exist status. @@ -55,6 +57,8 @@ typedef std::map PathsInChroot; // maps targe */ struct DerivationBuilderParams { + Settings & settings; + /** The path of the derivation. */ const StorePath & drvPath; diff --git a/src/libstore/include/nix/store/common-ssh-store-config.hh b/src/libstore/include/nix/store/common-ssh-store-config.hh index 0f2cea5e5..01bd3edac 100644 --- a/src/libstore/include/nix/store/common-ssh-store-config.hh +++ b/src/libstore/include/nix/store/common-ssh-store-config.hh @@ -12,8 +12,13 @@ struct CommonSSHStoreConfig : virtual StoreConfig { using StoreConfig::StoreConfig; - CommonSSHStoreConfig(std::string_view scheme, const ParsedURL::Authority & authority, const Params & params); - CommonSSHStoreConfig(std::string_view scheme, std::string_view authority, const Params & params); + CommonSSHStoreConfig( + nix::Settings & settings, + std::string_view scheme, + const ParsedURL::Authority & authority, + const Params & params); + CommonSSHStoreConfig( + nix::Settings & settings, std::string_view scheme, std::string_view authority, const Params & params); Setting sshKey{ this, "", "ssh-key", "Path to the SSH private key used to authenticate to the remote machine."}; diff --git a/src/libstore/include/nix/store/dummy-store.hh b/src/libstore/include/nix/store/dummy-store.hh index 59d1b1fdd..9b367f485 100644 --- a/src/libstore/include/nix/store/dummy-store.hh +++ b/src/libstore/include/nix/store/dummy-store.hh @@ -12,15 +12,16 @@ struct DummyStore; struct DummyStoreConfig : public std::enable_shared_from_this, virtual StoreConfig { - DummyStoreConfig(const Params & params) - : StoreConfig(params) + DummyStoreConfig(nix::Settings & settings, const Params & params) + : StoreConfig(settings, params) { // Disable caching since this a temporary in-memory store. pathInfoCacheSize = 0; } - DummyStoreConfig(std::string_view scheme, std::string_view authority, const Params & params) - : DummyStoreConfig(params) + DummyStoreConfig( + nix::Settings & settings, std::string_view scheme, std::string_view authority, const Params & params) + : DummyStoreConfig(settings, params) { if (!authority.empty()) throw UsageError("`%s` store URIs must not contain an authority part %s", scheme, authority); @@ -90,11 +91,20 @@ namespace nlohmann { template<> JSON_IMPL_INNER_TO(nix::DummyStoreConfig); + template<> -JSON_IMPL_INNER_FROM(nix::ref); +struct adl_serializer> +{ + static nix::ref from_json(nix::Settings & settings, const json & json); +}; + template<> JSON_IMPL_INNER_TO(nix::DummyStore); + template<> -JSON_IMPL_INNER_FROM(nix::ref); +struct adl_serializer> +{ + static nix::ref from_json(nix::Settings & settings, const json & json); +}; } // namespace nlohmann diff --git a/src/libstore/include/nix/store/globals.hh b/src/libstore/include/nix/store/globals.hh index 3545950c1..a4754fd89 100644 --- a/src/libstore/include/nix/store/globals.hh +++ b/src/libstore/include/nix/store/globals.hh @@ -422,9 +422,6 @@ public: ProfileDirsOptions getProfileDirsOptions() const; }; -// FIXME: don't use a global variable. -extern nix::Settings settings; - /** * Load the configuration (from `nix.conf`, `NIX_CONFIG`, etc.) into the * given configuration object. diff --git a/src/libstore/include/nix/store/http-binary-cache-store.hh b/src/libstore/include/nix/store/http-binary-cache-store.hh index 701eb431f..79c4638ee 100644 --- a/src/libstore/include/nix/store/http-binary-cache-store.hh +++ b/src/libstore/include/nix/store/http-binary-cache-store.hh @@ -17,9 +17,12 @@ struct HttpBinaryCacheStoreConfig : std::enable_shared_from_this { using CommonSSHStoreConfig::CommonSSHStoreConfig; - LegacySSHStoreConfig(std::string_view scheme, std::string_view authority, const Params & params); + LegacySSHStoreConfig( + nix::Settings & settings, std::string_view scheme, std::string_view authority, const Params & params); #ifndef _WIN32 // Hack for getting remote build log output. diff --git a/src/libstore/include/nix/store/local-binary-cache-store.hh b/src/libstore/include/nix/store/local-binary-cache-store.hh index 2846a9225..42847b59a 100644 --- a/src/libstore/include/nix/store/local-binary-cache-store.hh +++ b/src/libstore/include/nix/store/local-binary-cache-store.hh @@ -12,7 +12,8 @@ struct LocalBinaryCacheStoreConfig : std::enable_shared_from_this, { using LocalFSStoreConfig::LocalFSStoreConfig; - LocalStoreConfig(std::string_view scheme, std::string_view authority, const Params & params); + LocalStoreConfig( + nix::Settings & settings, std::string_view scheme, std::string_view authority, const Params & params); private: diff --git a/src/libstore/include/nix/store/machines.hh b/src/libstore/include/nix/store/machines.hh index 051ec1454..5cf39780f 100644 --- a/src/libstore/include/nix/store/machines.hh +++ b/src/libstore/include/nix/store/machines.hh @@ -6,6 +6,7 @@ namespace nix { +class Settings; class Store; struct Machine; @@ -67,7 +68,7 @@ struct Machine * nix::openStore(completeStoreReference()) * ``` */ - ref openStore() const; + ref openStore(Settings & settings) const; /** * Parse a machine configuration. diff --git a/src/libstore/include/nix/store/profiles.hh b/src/libstore/include/nix/store/profiles.hh index 11a3fbe89..d4364a19e 100644 --- a/src/libstore/include/nix/store/profiles.hh +++ b/src/libstore/include/nix/store/profiles.hh @@ -16,6 +16,7 @@ namespace nix { +class Settings; class StorePath; /** @@ -216,7 +217,7 @@ struct ProfileDirsOptions }; /** - * Create and return the path to a directory suitable for storing the user’s + * Create and return the path to a directory suitable for storing the user's * profiles. */ std::filesystem::path profilesDir(ProfileDirsOptions opts); diff --git a/src/libstore/include/nix/store/s3-binary-cache-store.hh b/src/libstore/include/nix/store/s3-binary-cache-store.hh index 4fa3cdcbd..f9ab6c08e 100644 --- a/src/libstore/include/nix/store/s3-binary-cache-store.hh +++ b/src/libstore/include/nix/store/s3-binary-cache-store.hh @@ -11,7 +11,8 @@ struct S3BinaryCacheStoreConfig : HttpBinaryCacheStoreConfig { using HttpBinaryCacheStoreConfig::HttpBinaryCacheStoreConfig; - S3BinaryCacheStoreConfig(std::string_view uriScheme, std::string_view bucketName, const Params & params); + S3BinaryCacheStoreConfig( + nix::Settings & settings, std::string_view uriScheme, std::string_view bucketName, const Params & params); Setting profile{ this, diff --git a/src/libstore/include/nix/store/ssh-store.hh b/src/libstore/include/nix/store/ssh-store.hh index 2c27bdb13..bcea12ff9 100644 --- a/src/libstore/include/nix/store/ssh-store.hh +++ b/src/libstore/include/nix/store/ssh-store.hh @@ -15,7 +15,8 @@ struct SSHStoreConfig : std::enable_shared_from_this, using CommonSSHStoreConfig::CommonSSHStoreConfig; using RemoteStoreConfig::RemoteStoreConfig; - SSHStoreConfig(std::string_view scheme, std::string_view authority, const Params & params); + SSHStoreConfig( + nix::Settings & settings, std::string_view scheme, std::string_view authority, const Params & params); Setting remoteProgram{ this, {"nix-daemon"}, "remote-program", "Path to the `nix-daemon` executable on the remote machine."}; @@ -39,8 +40,8 @@ struct SSHStoreConfig : std::enable_shared_from_this, struct MountedSSHStoreConfig : virtual SSHStoreConfig, virtual LocalFSStoreConfig { - MountedSSHStoreConfig(StringMap params); - MountedSSHStoreConfig(std::string_view scheme, std::string_view host, StringMap params); + MountedSSHStoreConfig(nix::Settings & settings, StringMap params); + MountedSSHStoreConfig(nix::Settings & settings, std::string_view scheme, std::string_view host, StringMap params); static const std::string name() { diff --git a/src/libstore/include/nix/store/store-api.hh b/src/libstore/include/nix/store/store-api.hh index 890f7c075..803ac76f9 100644 --- a/src/libstore/include/nix/store/store-api.hh +++ b/src/libstore/include/nix/store/store-api.hh @@ -44,6 +44,8 @@ struct NarInfoDiskCache; struct NarInfoDiskCacheSettings; class Store; +class Settings; + typedef std::map OutputPathMap; enum CheckSigsFlag : bool { NoCheckSigs = false, CheckSigs = true }; @@ -80,19 +82,32 @@ struct StoreConfigBase : Config { using Config::Config; + /** + * Global settings, which are we trying to triage and give better + * homes. Putting a reference to them on stores will help a bit with + * this, though we may find that some of the things here should + * *not* be per-store. + */ + nix::Settings & settings; + + StoreConfigBase(nix::Settings & settings) + : settings{settings} + { + } + private: /** * Compute the default Nix store directory from environment variables * (`NIX_STORE_DIR`, `NIX_STORE`) or the compile-time default. */ - static Path getDefaultNixStoreDir(); + static Path getDefaultNixStoreDir(const nix::Settings & settings); public: const PathSetting storeDir_{ this, - getDefaultNixStoreDir(), + getDefaultNixStoreDir(settings), "store", R"( Logical location of the Nix store, usually @@ -137,13 +152,13 @@ struct StoreConfig : public StoreConfigBase, public StoreDirConfig { using Params = StoreReference::Params; - StoreConfig(const Params & params); + StoreConfig(nix::Settings & settings, const Params & params); StoreConfig() = delete; virtual ~StoreConfig() {} - static StringSet getDefaultSystemFeatures(); + static StringSet getDefaultSystemFeatures(const nix::Settings & settings); /** * Documentation for this type of store. @@ -208,7 +223,7 @@ struct StoreConfig : public StoreConfigBase, public StoreDirConfig Setting systemFeatures{ this, - getDefaultSystemFeatures(), + getDefaultSystemFeatures(settings), "system-features", R"( Optional [system features](@docroot@/command-ref/conf-file.md#conf-system-features) available on the system this store uses to build derivations. diff --git a/src/libstore/include/nix/store/store-dir-config.hh b/src/libstore/include/nix/store/store-dir-config.hh index 34e928182..7572a3cde 100644 --- a/src/libstore/include/nix/store/store-dir-config.hh +++ b/src/libstore/include/nix/store/store-dir-config.hh @@ -11,6 +11,7 @@ namespace nix { +class Settings; struct SourcePath; MakeError(BadStorePath, Error); @@ -89,6 +90,7 @@ struct StoreDirConfig * path for the given file system object. */ std::pair computeStorePath( + const Settings & settings, std::string_view name, const SourcePath & path, ContentAddressMethod method = ContentAddressMethod::Raw::NixArchive, diff --git a/src/libstore/include/nix/store/store-open.hh b/src/libstore/include/nix/store/store-open.hh index ef7d81675..f3c52638d 100644 --- a/src/libstore/include/nix/store/store-open.hh +++ b/src/libstore/include/nix/store/store-open.hh @@ -15,32 +15,37 @@ namespace nix { +class Settings; + /** * @return The store config denoted by `storeURI` (slight misnomer...). */ -ref resolveStoreConfig(StoreReference && storeURI); +ref resolveStoreConfig(Settings & settings, StoreReference && storeURI); /** * @return a Store object to access the Nix store denoted by - * ‘uri’ (slight misnomer...). + * 'uri' (slight misnomer...). */ -ref openStore(StoreReference && storeURI); +ref openStore(Settings & settings, StoreReference && storeURI); /** * Opens the store at `uri`, where `uri` is in the format expected by * `StoreReference::parse` */ -ref openStore(const std::string & uri, const StoreReference::Params & extraParams = StoreReference::Params()); +ref openStore( + Settings & settings, + const std::string & uri, + const StoreReference::Params & extraParams = StoreReference::Params()); /** * Short-hand which opens the default store, according to global settings */ -ref openStore(); +ref openStore(Settings & settings); /** * @return the default substituter stores, defined by the - * ‘substituters’ option and various legacy options. + * 'substituters' option and various legacy options. */ -std::list> getDefaultSubstituters(); +std::list> getDefaultSubstituters(Settings & settings); } // namespace nix diff --git a/src/libstore/include/nix/store/store-registration.hh b/src/libstore/include/nix/store/store-registration.hh index 8b0f344ba..c47c7ed69 100644 --- a/src/libstore/include/nix/store/store-registration.hh +++ b/src/libstore/include/nix/store/store-registration.hh @@ -14,6 +14,7 @@ */ #include "nix/store/store-api.hh" +#include "nix/store/globals.hh" namespace nix { @@ -40,7 +41,10 @@ struct StoreFactory * whatever comes after `://` and before `?`. */ std::function( - std::string_view scheme, std::string_view authorityPath, const Store::Config::Params & params)> + Settings & settings, + std::string_view scheme, + std::string_view authorityPath, + const Store::Config::Params & params)> parseConfig; /** @@ -64,10 +68,13 @@ struct Implementations .doc = TConfig::doc(), .uriSchemes = TConfig::uriSchemes(), .experimentalFeature = TConfig::experimentalFeature(), - .parseConfig = ([](auto scheme, auto uri, auto & params) -> ref { - return make_ref(scheme, uri, params); + .parseConfig = ([](Settings & settings, auto scheme, auto uri, auto & params) -> ref { + return make_ref(settings, scheme, uri, params); + }), + .getConfig = ([]() -> ref { + Settings settings; + return make_ref(settings, Store::Config::Params{}); }), - .getConfig = ([]() -> ref { return make_ref(Store::Config::Params{}); }), }; auto [it, didInsert] = registered().insert({TConfig::name(), std::move(factory)}); if (!didInsert) { diff --git a/src/libstore/include/nix/store/uds-remote-store.hh b/src/libstore/include/nix/store/uds-remote-store.hh index 764e8768a..0480b31bc 100644 --- a/src/libstore/include/nix/store/uds-remote-store.hh +++ b/src/libstore/include/nix/store/uds-remote-store.hh @@ -19,9 +19,10 @@ struct UDSRemoteStoreConfig : std::enable_shared_from_this /** * @param authority is the socket path. */ - UDSRemoteStoreConfig(std::string_view scheme, std::string_view authority, const Params & params); + UDSRemoteStoreConfig( + nix::Settings & settings, std::string_view scheme, std::string_view authority, const Params & params); - UDSRemoteStoreConfig(const Params & params); + UDSRemoteStoreConfig(nix::Settings & settings, const Params & params); static const std::string name() { diff --git a/src/libstore/keys.cc b/src/libstore/keys.cc index 8b02e7a66..159aff5ad 100644 --- a/src/libstore/keys.cc +++ b/src/libstore/keys.cc @@ -4,7 +4,7 @@ namespace nix { -PublicKeys getDefaultPublicKeys() +PublicKeys getDefaultPublicKeys(const Settings & settings) { PublicKeys publicKeys; diff --git a/src/libstore/legacy-ssh-store.cc b/src/libstore/legacy-ssh-store.cc index 7c58c7226..fc159445a 100644 --- a/src/libstore/legacy-ssh-store.cc +++ b/src/libstore/legacy-ssh-store.cc @@ -18,9 +18,10 @@ namespace nix { -LegacySSHStoreConfig::LegacySSHStoreConfig(std::string_view scheme, std::string_view authority, const Params & params) - : StoreConfig(params) - , CommonSSHStoreConfig(scheme, ParsedURL::Authority::parse(authority), params) +LegacySSHStoreConfig::LegacySSHStoreConfig( + nix::Settings & settings, std::string_view scheme, std::string_view authority, const Params & params) + : StoreConfig(settings, params) + , CommonSSHStoreConfig(settings, scheme, ParsedURL::Authority::parse(authority), params) { } @@ -180,7 +181,7 @@ void LegacySSHStore::narFromPath(const StorePath & path, std::functionnarFromPath(*this, path, fun); } -static ServeProto::BuildOptions buildSettings() +static ServeProto::BuildOptions buildSettings(const Settings & settings) { return { .maxSilentTime = settings.getWorkerSettings().maxSilentTime, @@ -196,7 +197,7 @@ BuildResult LegacySSHStore::buildDerivation(const StorePath & drvPath, const Bas { auto conn(connections->get()); - conn->putBuildDerivationRequest(*this, drvPath, drv, buildSettings()); + conn->putBuildDerivationRequest(*this, drvPath, drv, buildSettings(config->settings)); return conn->getBuildDerivationResponse(*this); } @@ -240,7 +241,7 @@ void LegacySSHStore::buildPaths( } conn->to << ss; - ServeProto::write(*this, *conn, buildSettings()); + ServeProto::write(*this, *conn, buildSettings(config->settings)); conn->to.flush(); diff --git a/src/libstore/linux/include/nix/store/personality.hh b/src/libstore/linux/include/nix/store/personality.hh index dd9c4c2dc..6b491c812 100644 --- a/src/libstore/linux/include/nix/store/personality.hh +++ b/src/libstore/linux/include/nix/store/personality.hh @@ -3,6 +3,12 @@ #include +namespace nix { + +class Settings; + +} + namespace nix::linux { struct PersonalityArgs diff --git a/src/libstore/local-binary-cache-store.cc b/src/libstore/local-binary-cache-store.cc index 19c1f2794..39e77da69 100644 --- a/src/libstore/local-binary-cache-store.cc +++ b/src/libstore/local-binary-cache-store.cc @@ -9,9 +9,9 @@ namespace nix { LocalBinaryCacheStoreConfig::LocalBinaryCacheStoreConfig( - std::string_view scheme, PathView binaryCacheDir, const StoreReference::Params & params) - : Store::Config{params} - , BinaryCacheStoreConfig{params} + nix::Settings & settings, std::string_view scheme, PathView binaryCacheDir, const StoreReference::Params & params) + : Store::Config{settings, params} + , BinaryCacheStoreConfig{settings, params} , binaryCacheDir(binaryCacheDir) { } diff --git a/src/libstore/local-fs-store.cc b/src/libstore/local-fs-store.cc index ce35bab3e..943d4838a 100644 --- a/src/libstore/local-fs-store.cc +++ b/src/libstore/local-fs-store.cc @@ -8,18 +8,18 @@ namespace nix { -Path LocalFSStoreConfig::getDefaultStateDir() +Path LocalFSStoreConfig::getDefaultStateDir(const nix::Settings & settings) { return settings.nixStateDir.string(); } -Path LocalFSStoreConfig::getDefaultLogDir() +Path LocalFSStoreConfig::getDefaultLogDir(const nix::Settings & settings) { return settings.getLogFileSettings().nixLogDir.string(); } -LocalFSStoreConfig::LocalFSStoreConfig(PathView rootDir, const Params & params) - : StoreConfig(params) +LocalFSStoreConfig::LocalFSStoreConfig(nix::Settings & settings, PathView rootDir, const Params & params) + : StoreConfig(settings, params) /* Default `?root` from `rootDir` if non set * NOTE: We would like to just do rootDir.set(...), which would take care of * all normalization and error checking for us. Unfortunately we cannot do diff --git a/src/libstore/local-overlay-store.cc b/src/libstore/local-overlay-store.cc index 6459ac2e1..d12e05e1e 100644 --- a/src/libstore/local-overlay-store.cc +++ b/src/libstore/local-overlay-store.cc @@ -43,7 +43,7 @@ LocalOverlayStore::LocalOverlayStore(ref config) , LocalFSStore{*config} , LocalStore{static_cast>(config)} , config{config} - , lowerStore(openStore(config->lowerStoreUri.get()).dynamic_pointer_cast()) + , lowerStore(openStore(config->settings, config->lowerStoreUri.get()).dynamic_pointer_cast()) { if (config->checkMount.get()) { std::smatch match; diff --git a/src/libstore/local-store.cc b/src/libstore/local-store.cc index addb8a9f2..a49585d6e 100644 --- a/src/libstore/local-store.cc +++ b/src/libstore/local-store.cc @@ -56,9 +56,10 @@ namespace nix { -LocalStoreConfig::LocalStoreConfig(std::string_view scheme, std::string_view authority, const Params & params) - : StoreConfig(params) - , LocalFSStoreConfig(authority, params) +LocalStoreConfig::LocalStoreConfig( + nix::Settings & settings, std::string_view scheme, std::string_view authority, const Params & params) + : StoreConfig(settings, params) + , LocalFSStoreConfig(settings, authority, params) { } @@ -505,7 +506,8 @@ void LocalStore::openDB(State & state, bool create) auto openMode = config->readOnly ? SQLiteOpenMode::Immutable : create ? SQLiteOpenMode::Normal : SQLiteOpenMode::NoCreate; - state.db = SQLite(std::filesystem::path(dbDir) / "db.sqlite", {.mode = openMode, .useWAL = settings.useSQLiteWAL}); + state.db = + SQLite(std::filesystem::path(dbDir) / "db.sqlite", {.mode = openMode, .useWAL = config->settings.useSQLiteWAL}); #ifdef __CYGWIN__ /* The cygwin version of sqlite3 has a patch which calls @@ -529,7 +531,7 @@ void LocalStore::openDB(State & state, bool create) /* Set the SQLite journal mode. WAL mode is fastest, so it's the default. */ - std::string mode = settings.useSQLiteWAL ? "wal" : "truncate"; + std::string mode = config->settings.useSQLiteWAL ? "wal" : "truncate"; std::string prevMode; { SQLiteStmt stmt; @@ -974,7 +976,7 @@ const PublicKeys & LocalStore::getPublicKeys() { auto state(_state->lock()); if (!state->publicKeys) - state->publicKeys = std::make_unique(getDefaultPublicKeys()); + state->publicKeys = std::make_unique(getDefaultPublicKeys(config->settings)); return *state->publicKeys; } diff --git a/src/libstore/machines.cc b/src/libstore/machines.cc index f5f630ad0..8434c3594 100644 --- a/src/libstore/machines.cc +++ b/src/libstore/machines.cc @@ -91,9 +91,9 @@ StoreReference Machine::completeStoreReference() const return storeUri; } -ref Machine::openStore() const +ref Machine::openStore(Settings & settings) const { - return nix::openStore(completeStoreReference()); + return nix::openStore(settings, completeStoreReference()); } static std::vector expandBuilderLines(const std::string & builders) diff --git a/src/libstore/misc.cc b/src/libstore/misc.cc index 91879c055..f88572315 100644 --- a/src/libstore/misc.cc +++ b/src/libstore/misc.cc @@ -236,8 +236,8 @@ MissingPaths Store::queryMissing(const std::vector & targets) throw; } - if (!knownOutputPaths && settings.getWorkerSettings().useSubstitutes - && drvOptions.substitutesAllowed(settings.getWorkerSettings())) { + if (!knownOutputPaths && config.settings.getWorkerSettings().useSubstitutes + && drvOptions.substitutesAllowed(config.settings.getWorkerSettings())) { experimentalFeatureSettings.require(Xp::CaDerivations); // If there are unknown output paths, attempt to find if the @@ -250,7 +250,7 @@ MissingPaths Store::queryMissing(const std::vector & targets) continue; bool found = false; - for (auto & sub : getDefaultSubstituters()) { + for (auto & sub : getDefaultSubstituters(config.settings)) { auto realisation = sub->queryRealisation({hash, outputName}); if (!realisation) continue; @@ -267,8 +267,8 @@ MissingPaths Store::queryMissing(const std::vector & targets) } } - if (knownOutputPaths && settings.getWorkerSettings().useSubstitutes - && drvOptions.substitutesAllowed(settings.getWorkerSettings())) { + if (knownOutputPaths && config.settings.getWorkerSettings().useSubstitutes + && drvOptions.substitutesAllowed(config.settings.getWorkerSettings())) { auto drvState = make_ref>(DrvState(invalid.size())); for (auto & output : invalid) pool.enqueue(std::bind(checkOutput, drvPath, drv, output, drvState)); diff --git a/src/libstore/remote-store.cc b/src/libstore/remote-store.cc index f317e906e..660f02e73 100644 --- a/src/libstore/remote-store.cc +++ b/src/libstore/remote-store.cc @@ -114,6 +114,8 @@ void RemoteStore::initConnection(Connection & conn) void RemoteStore::setOptions(Connection & conn) { + auto & settings = config.settings; + conn.to << WorkerProto::Op::SetOptions << settings.keepFailed << settings.getWorkerSettings().keepGoing << settings.getWorkerSettings().tryFallback << verbosity << settings.getWorkerSettings().maxBuildJobs << settings.getWorkerSettings().maxSilentTime << true << (settings.verboseBuild ? lvlError : lvlVomit) diff --git a/src/libstore/s3-binary-cache-store.cc b/src/libstore/s3-binary-cache-store.cc index 2a3be0474..224bf3e7c 100644 --- a/src/libstore/s3-binary-cache-store.cc +++ b/src/libstore/s3-binary-cache-store.cc @@ -417,9 +417,9 @@ StringSet S3BinaryCacheStoreConfig::uriSchemes() } S3BinaryCacheStoreConfig::S3BinaryCacheStoreConfig( - std::string_view scheme, std::string_view _cacheUri, const Params & params) - : StoreConfig(params) - , HttpBinaryCacheStoreConfig(scheme, _cacheUri, params) + nix::Settings & settings, std::string_view scheme, std::string_view _cacheUri, const Params & params) + : StoreConfig(settings, params) + , HttpBinaryCacheStoreConfig(settings, scheme, _cacheUri, params) { assert(cacheUri.query.empty()); assert(cacheUri.scheme == "s3"); diff --git a/src/libstore/ssh-store.cc b/src/libstore/ssh-store.cc index dc70b4ba8..a711cf689 100644 --- a/src/libstore/ssh-store.cc +++ b/src/libstore/ssh-store.cc @@ -11,10 +11,11 @@ namespace nix { -SSHStoreConfig::SSHStoreConfig(std::string_view scheme, std::string_view authority, const Params & params) - : Store::Config{params} - , RemoteStore::Config{params} - , CommonSSHStoreConfig{scheme, authority, params} +SSHStoreConfig::SSHStoreConfig( + nix::Settings & settings, std::string_view scheme, std::string_view authority, const Params & params) + : Store::Config{settings, params} + , RemoteStore::Config{settings, params} + , CommonSSHStoreConfig{settings, scheme, authority, params} { } @@ -88,21 +89,22 @@ protected: }; }; -MountedSSHStoreConfig::MountedSSHStoreConfig(StringMap params) - : StoreConfig(params) - , RemoteStoreConfig(params) - , CommonSSHStoreConfig(params) - , SSHStoreConfig(params) - , LocalFSStoreConfig(params) +MountedSSHStoreConfig::MountedSSHStoreConfig(nix::Settings & settings, StringMap params) + : StoreConfig(settings, params) + , RemoteStoreConfig(settings, params) + , CommonSSHStoreConfig(settings, params) + , SSHStoreConfig(settings, params) + , LocalFSStoreConfig(settings, "", params) { } -MountedSSHStoreConfig::MountedSSHStoreConfig(std::string_view scheme, std::string_view host, StringMap params) - : StoreConfig(params) - , RemoteStoreConfig(params) - , CommonSSHStoreConfig(scheme, host, params) - , SSHStoreConfig(scheme, host, params) - , LocalFSStoreConfig(params) +MountedSSHStoreConfig::MountedSSHStoreConfig( + nix::Settings & settings, std::string_view scheme, std::string_view host, StringMap params) + : StoreConfig(settings, params) + , RemoteStoreConfig(settings, params) + , CommonSSHStoreConfig(settings, scheme, host, params) + , SSHStoreConfig(settings, scheme, host, params) + , LocalFSStoreConfig(settings, "", params) { } diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 149a0a553..7e78c37ce 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -32,7 +32,7 @@ using json = nlohmann::json; namespace nix { -Path StoreConfigBase::getDefaultNixStoreDir() +Path StoreConfigBase::getDefaultNixStoreDir(const nix::Settings & settings) { return #ifndef _WIN32 @@ -41,8 +41,8 @@ Path StoreConfigBase::getDefaultNixStoreDir() (getEnvNonEmpty("NIX_STORE_DIR").value_or(getEnvNonEmpty("NIX_STORE").value_or(NIX_STORE_DIR))); } -StoreConfig::StoreConfig(const Params & params) - : StoreConfigBase(params) +StoreConfig::StoreConfig(nix::Settings & settings, const Params & params) + : StoreConfigBase(settings) , StoreDirConfig{storeDir_} { } @@ -118,7 +118,7 @@ StorePath Store::addToStore( auto sink = sourceToSink([&](Source & source) { LengthSource lengthSource(source); storePath = addToStoreFromDump(lengthSource, name, fsm, method, hashAlgo, references, repair); - if (settings.warnLargePathThreshold && lengthSource.total >= settings.warnLargePathThreshold) + if (config.settings.warnLargePathThreshold && lengthSource.total >= config.settings.warnLargePathThreshold) warn("copied large path '%s' to the store (%s)", path, renderSize(lengthSource.total)); }); dumpPath(path, *sink, fsm, filter); @@ -184,7 +184,7 @@ void Store::addMultipleToStore(PathsSource && pathsToCopy, Activity & act, Repai addToStore(info, *source, repair, checkSigs); } catch (Error & e) { nrFailed++; - if (!settings.getWorkerSettings().keepGoing) + if (!config.settings.getWorkerSettings().keepGoing) throw e; printMsg(lvlError, "could not copy %s: %s", printStorePath(path), e.what()); showProgress(); @@ -315,7 +315,7 @@ void Store::narFromPath(const StorePath & path, Sink & sink) dumpPath(sourcePath, sink, FileSerialisationMethod::NixArchive); } -StringSet Store::Config::getDefaultSystemFeatures() +StringSet Store::Config::getDefaultSystemFeatures(const nix::Settings & settings) { auto res = settings.systemFeatures.get(); @@ -420,12 +420,12 @@ StorePathSet Store::queryDerivationOutputs(const StorePath & path) void Store::querySubstitutablePathInfos(const StorePathCAMap & paths, SubstitutablePathInfos & infos) { - if (!settings.getWorkerSettings().useSubstitutes) + if (!config.settings.getWorkerSettings().useSubstitutes) return; for (auto & path : paths) { std::optional lastStoresException = std::nullopt; - for (auto & sub : getDefaultSubstituters()) { + for (auto & sub : getDefaultSubstituters(config.settings)) { if (lastStoresException.has_value()) { logError(lastStoresException->info()); lastStoresException.reset(); @@ -476,7 +476,7 @@ void Store::querySubstitutablePathInfos(const StorePathCAMap & paths, Substituta } } if (lastStoresException.has_value()) { - if (!settings.getWorkerSettings().tryFallback) { + if (!config.settings.getWorkerSettings().tryFallback) { throw *lastStoresException; } else logError(lastStoresException->info()); @@ -486,7 +486,7 @@ void Store::querySubstitutablePathInfos(const StorePathCAMap & paths, Substituta StorePathSet Store::querySubstitutablePaths(const StorePathSet & paths) { - if (!settings.getWorkerSettings().useSubstitutes) + if (!config.settings.getWorkerSettings().useSubstitutes) return StorePathSet(); StorePathSet remaining; @@ -495,7 +495,7 @@ StorePathSet Store::querySubstitutablePaths(const StorePathSet & paths) StorePathSet res; - for (auto & sub : getDefaultSubstituters()) { + for (auto & sub : getDefaultSubstituters(config.settings)) { if (remaining.empty()) break; if (sub->storeDir != storeDir) @@ -521,7 +521,7 @@ StorePathSet Store::querySubstitutablePaths(const StorePathSet & paths) bool Store::isValidPath(const StorePath & storePath) { auto res = pathInfoCache->lock()->get(storePath); - if (res && res->isKnownNow(settings.getNarInfoDiskCacheSettings())) { + if (res && res->isKnownNow(config.settings.getNarInfoDiskCacheSettings())) { stats.narInfoReadAverted++; return res->didExist(); } @@ -587,7 +587,7 @@ std::optional> Store::queryPathInfoFromClie auto hashPart = std::string(storePath.hashPart()); auto res = pathInfoCache->lock()->get(storePath); - if (res && res->isKnownNow(settings.getNarInfoDiskCacheSettings())) { + if (res && res->isKnownNow(config.settings.getNarInfoDiskCacheSettings())) { stats.narInfoReadAverted++; if (res->didExist()) return std::make_optional(res->value); @@ -1240,7 +1240,7 @@ void Store::signPathInfo(ValidPathInfo & info) { // FIXME: keep secret keys in memory. - auto secretKeyFiles = settings.secretKeyFiles; + auto secretKeyFiles = config.settings.secretKeyFiles; for (auto & secretKeyFile : secretKeyFiles.get()) { SecretKey secretKey(readFile(secretKeyFile)); @@ -1253,7 +1253,7 @@ void Store::signRealisation(Realisation & realisation) { // FIXME: keep secret keys in memory. - auto secretKeyFiles = settings.secretKeyFiles; + auto secretKeyFiles = config.settings.secretKeyFiles; for (auto & secretKeyFile : secretKeyFiles.get()) { SecretKey secretKey(readFile(secretKeyFile)); diff --git a/src/libstore/store-dir-config.cc b/src/libstore/store-dir-config.cc index 8c756ff58..24bf82462 100644 --- a/src/libstore/store-dir-config.cc +++ b/src/libstore/store-dir-config.cc @@ -149,6 +149,7 @@ StoreDirConfig::makeFixedOutputPathFromCA(std::string_view name, const ContentAd } std::pair StoreDirConfig::computeStorePath( + const Settings & settings, std::string_view name, const SourcePath & path, ContentAddressMethod method, diff --git a/src/libstore/store-registration.cc b/src/libstore/store-registration.cc index 633556e48..3a0e683ab 100644 --- a/src/libstore/store-registration.cc +++ b/src/libstore/store-registration.cc @@ -6,24 +6,24 @@ namespace nix { -ref openStore() +ref openStore(Settings & settings) { - return openStore(StoreReference{settings.storeUri.get()}); + return openStore(settings, StoreReference{settings.storeUri.get()}); } -ref openStore(const std::string & uri, const Store::Config::Params & extraParams) +ref openStore(Settings & settings, const std::string & uri, const Store::Config::Params & extraParams) { - return openStore(StoreReference::parse(uri, extraParams)); + return openStore(settings, StoreReference::parse(uri, extraParams)); } -ref openStore(StoreReference && storeURI) +ref openStore(Settings & settings, StoreReference && storeURI) { - auto store = resolveStoreConfig(std::move(storeURI))->openStore(); + auto store = resolveStoreConfig(settings, std::move(storeURI))->openStore(); store->init(); return store; } -ref resolveStoreConfig(StoreReference && storeURI) +ref resolveStoreConfig(Settings & settings, StoreReference && storeURI) { auto & params = storeURI.params; @@ -32,9 +32,9 @@ ref resolveStoreConfig(StoreReference && storeURI) [&](const StoreReference::Auto &) -> ref { auto stateDir = getOr(params, "state", settings.nixStateDir.string()); if (access(stateDir.c_str(), R_OK | W_OK) == 0) - return make_ref(params); + return make_ref(settings, params); else if (pathExists(settings.nixDaemonSocketFile)) - return make_ref(params); + return make_ref(settings, params); #ifdef __linux__ else if ( !pathExists(stateDir) && params.empty() && !isRootUser() && !getEnv("NIX_STORE_DIR").has_value() @@ -47,21 +47,21 @@ ref resolveStoreConfig(StoreReference && storeURI) try { createDirs(chrootStore); } catch (SystemError & e) { - return make_ref(params); + return make_ref(settings, params); } warn("'%s' does not exist, so Nix will use '%s' as a chroot store", stateDir, chrootStore); } else debug("'%s' does not exist, so Nix will use '%s' as a chroot store", stateDir, chrootStore); - return make_ref("local", chrootStore, params); + return make_ref(settings, "local", chrootStore, params); } #endif else - return make_ref(params); + return make_ref(settings, params); }, [&](const StoreReference::Specified & g) { for (const auto & [storeName, implem] : Implementations::registered()) if (implem.uriSchemes.count(g.scheme)) - return implem.parseConfig(g.scheme, g.authority, params); + return implem.parseConfig(settings, g.scheme, g.authority, params); throw Error("don't know how to open Nix store with scheme '%s'", g.scheme); }, @@ -74,9 +74,9 @@ ref resolveStoreConfig(StoreReference && storeURI) return storeConfig; } -std::list> getDefaultSubstituters() +std::list> getDefaultSubstituters(Settings & settings) { - static auto stores([]() { + static auto stores([&]() { std::list> stores; std::set done; @@ -85,7 +85,7 @@ std::list> getDefaultSubstituters() if (!done.insert(ref).second) return; try { - stores.push_back(openStore(StoreReference{ref})); + stores.push_back(openStore(settings, StoreReference{ref})); } catch (Error & e) { logWarning(e.info()); } diff --git a/src/libstore/uds-remote-store.cc b/src/libstore/uds-remote-store.cc index 488a7cc3c..f7c2f8053 100644 --- a/src/libstore/uds-remote-store.cc +++ b/src/libstore/uds-remote-store.cc @@ -20,11 +20,14 @@ namespace nix { UDSRemoteStoreConfig::UDSRemoteStoreConfig( - std::string_view scheme, std::string_view authority, const StoreReference::Params & params) - : Store::Config{params} - , LocalFSStore::Config{params} - , RemoteStore::Config{params} - , path{authority.empty() ? settings.nixDaemonSocketFile.string() : authority} + nix::Settings & settings, + std::string_view scheme, + std::string_view authority, + const StoreReference::Params & params) + : Store::Config{settings, params} + , LocalFSStore::Config{settings, "", params} + , RemoteStore::Config{settings, params} + , path{authority.empty() ? settings.nixDaemonSocketFile.string() : std::string{authority}} { if (uriSchemes().count(scheme) == 0) { throw UsageError("Scheme must be 'unix'"); @@ -42,8 +45,8 @@ std::string UDSRemoteStoreConfig::doc() // empty string will later default to the same nixDaemonSocketFile. Why // don't we just wire it all through? I believe there are cases where it // will live reload so we want to continue to account for that. -UDSRemoteStoreConfig::UDSRemoteStoreConfig(const Params & params) - : UDSRemoteStoreConfig(*uriSchemes().begin(), "", params) +UDSRemoteStoreConfig::UDSRemoteStoreConfig(nix::Settings & settings, const Params & params) + : UDSRemoteStoreConfig(settings, *uriSchemes().begin(), "", params) { } diff --git a/src/libutil-test-support/include/nix/util/tests/json-characterization.hh b/src/libutil-test-support/include/nix/util/tests/json-characterization.hh index 744880f38..9b679a293 100644 --- a/src/libutil-test-support/include/nix/util/tests/json-characterization.hh +++ b/src/libutil-test-support/include/nix/util/tests/json-characterization.hh @@ -86,9 +86,11 @@ void checkpointJson(CharacterizationTest & test, PathView testStem, const T & go /** * Specialization for when we need to do "JSON -> `ref`" in one * direction, but "`const T &` -> JSON" in the other direction. + * + * Additional arguments are forwarded to `from_json` (e.g., settings). */ template -void checkpointJson(CharacterizationTest & test, PathView testStem, const ref & got) +void checkpointJson(CharacterizationTest & test, PathView testStem, const ref & got, auto... args) { using namespace nlohmann; @@ -103,7 +105,7 @@ void checkpointJson(CharacterizationTest & test, PathView testStem, const ref } else { json expectedJson = json::parse(readFile(file)); ASSERT_EQ(gotJson, expectedJson); - ref expected = adl_serializer>::from_json(expectedJson); + ref expected = adl_serializer>::from_json(args..., expectedJson); ASSERT_EQ(*got, *expected); } } @@ -137,9 +139,10 @@ struct JsonCharacterizationTest : virtual CharacterizationTest nix::writeJsonTest(*this, testStem, value); } - void checkpointJson(PathView testStem, const T & value) + template + void checkpointJson(PathView testStem, const T & value, Args &&... args) { - nix::checkpointJson(*this, testStem, value); + nix::checkpointJson(*this, testStem, value, std::forward(args)...); } }; diff --git a/src/nix/add-to-store.cc b/src/nix/add-to-store.cc index e87f49546..d72545f8f 100644 --- a/src/nix/add-to-store.cc +++ b/src/nix/add-to-store.cc @@ -40,8 +40,10 @@ struct CmdAddToStore : MixDryRun, StoreCommand auto sourcePath = PosixSourceAccessor::createAtRoot(makeParentCanonical(path)); - auto storePath = dryRun ? store->computeStorePath(*namePart, sourcePath, caMethod, hashAlgo, {}).first - : store->addToStoreSlow(*namePart, sourcePath, caMethod, hashAlgo, {}).path; + auto storePath = + dryRun + ? store->computeStorePath(store->config.settings, *namePart, sourcePath, caMethod, hashAlgo, {}).first + : store->addToStoreSlow(*namePart, sourcePath, caMethod, hashAlgo, {}).path; logger->cout("%s", store->printStorePath(storePath)); } diff --git a/src/nix/build-remote/build-remote.cc b/src/nix/build-remote/build-remote.cc index 81933543b..f59e99546 100644 --- a/src/nix/build-remote/build-remote.cc +++ b/src/nix/build-remote/build-remote.cc @@ -13,7 +13,6 @@ #include "nix/main/shared.hh" #include "nix/main/plugin.hh" #include "nix/store/pathlocks.hh" -#include "nix/store/globals.hh" #include "nix/util/serialise.hh" #include "nix/store/build-result.hh" #include "nix/store/store-open.hh" @@ -81,7 +80,7 @@ static int main_build_remote(int argc, char ** argv) initPlugins(); - auto store = openStore(); + auto store = openStore(settings); /* It would be more appropriate to use $XDG_RUNTIME_DIR, since that gets cleared on reboot, but it wouldn't work on macOS. */ @@ -234,7 +233,7 @@ static int main_build_remote(int argc, char ** argv) Activity act(*logger, lvlTalkative, actUnknown, fmt("connecting to '%s'", storeUri)); - sshStore = bestMachine->openStore(); + sshStore = bestMachine->openStore(settings); sshStore->connect(); } catch (std::exception & e) { auto msg = chomp(drainFD(5, {.block = false})); diff --git a/src/nix/derivation-add.cc b/src/nix/derivation-add.cc index 2dbc89d6f..39a4b115e 100644 --- a/src/nix/derivation-add.cc +++ b/src/nix/derivation-add.cc @@ -6,6 +6,7 @@ #include "nix/util/archive.hh" #include "nix/store/derivations.hh" #include "nix/store/globals.hh" +#include "nix/main/shared.hh" #include using namespace nix; diff --git a/src/nix/flake-prefetch-inputs.cc b/src/nix/flake-prefetch-inputs.cc index 4ea6342c3..a9d73dc0d 100644 --- a/src/nix/flake-prefetch-inputs.cc +++ b/src/nix/flake-prefetch-inputs.cc @@ -1,11 +1,12 @@ -#include "flake-command.hh" -#include "nix/fetchers/fetch-to-store.hh" -#include "nix/util/thread-pool.hh" -#include "nix/store/filetransfer.hh" -#include "nix/util/exit.hh" - #include +#include "nix/util/thread-pool.hh" +#include "nix/util/exit.hh" +#include "nix/store/filetransfer.hh" +#include "nix/fetchers/fetch-to-store.hh" + +#include "flake-command.hh" + using namespace nix; using namespace nix::flake; diff --git a/src/nix/flake.cc b/src/nix/flake.cc index 8906628ff..7c5f0da8b 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -1127,7 +1127,7 @@ struct CmdFlakeArchive : FlakeCommand, MixJSON, MixDryRun, MixNoCheckSigs } if (!dryRun && dstUri) { - ref dstStore = openStore(StoreReference{*dstUri}); + ref dstStore = openStore(settings, StoreReference{*dstUri}); copyPaths(*store, *dstStore, sources, NoRepair, checkSigs, substitute); } diff --git a/src/nix/formatter.cc b/src/nix/formatter.cc index 08f0b5f05..f39c1f46d 100644 --- a/src/nix/formatter.cc +++ b/src/nix/formatter.cc @@ -6,6 +6,7 @@ #include "nix/cmd/installable-derived-path.hh" #include "nix/util/environment-variables.hh" #include "nix/store/globals.hh" +#include "nix/main/shared.hh" #include "run.hh" diff --git a/src/nix/log.cc b/src/nix/log.cc index 8f251ddab..63ed5d319 100644 --- a/src/nix/log.cc +++ b/src/nix/log.cc @@ -41,7 +41,7 @@ struct CmdLog : InstallableCommand auto path = resolveDerivedPath(*store, *oneUp); RunPager pager; - auto log = fetchBuildLog(store, path, installable->what()); + auto log = fetchBuildLog(settings, store, path, installable->what()); logger->stop(); writeFull(getStandardOutput(), log); } diff --git a/src/nix/main.cc b/src/nix/main.cc index 01a1e8b33..1c0f1d875 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -249,7 +249,7 @@ static void showHelp(std::vector subcommand, NixArgs & toplevel) evalSettings.pureEval = true; EvalState state( {}, - openStore(StoreReference{.variant = StoreReference::Specified{.scheme = "dummy"}}), + openStore(settings, StoreReference{.variant = StoreReference::Specified{.scheme = "dummy"}}), fetchSettings, evalSettings); @@ -454,7 +454,7 @@ void mainWrapped(int argc, char ** argv) evalSettings.pureEval = false; EvalState state( {}, - openStore(StoreReference{.variant = StoreReference::Specified{.scheme = "dummy"}}), + openStore(settings, StoreReference{.variant = StoreReference::Specified{.scheme = "dummy"}}), fetchSettings, evalSettings); auto builtinsJson = nlohmann::json::object(); diff --git a/src/nix/make-content-addressed.cc b/src/nix/make-content-addressed.cc index cd0f3f59f..3f4a7e912 100644 --- a/src/nix/make-content-addressed.cc +++ b/src/nix/make-content-addressed.cc @@ -1,4 +1,5 @@ #include "nix/cmd/command.hh" +#include "nix/main/shared.hh" #include "nix/store/store-open.hh" #include "nix/store/make-content-addressed.hh" #include "nix/main/common-args.hh" @@ -30,7 +31,7 @@ struct CmdMakeContentAddressed : virtual CopyCommand, virtual StorePathsCommand, void run(ref srcStore, StorePaths && storePaths) override { - auto dstStore = !dstUri ? openStore() : openStore(StoreReference{*dstUri}); + auto dstStore = !dstUri ? openStore(settings) : openStore(settings, StoreReference{*dstUri}); auto remappings = makeContentAddressed(*srcStore, *dstStore, StorePathSet(storePaths.begin(), storePaths.end())); diff --git a/src/nix/nix-build/nix-build.cc b/src/nix/nix-build/nix-build.cc index 13c38f929..082e1f4a1 100644 --- a/src/nix/nix-build/nix-build.cc +++ b/src/nix/nix-build/nix-build.cc @@ -312,8 +312,8 @@ static void main_nix_build(int argc, char ** argv) if (packages && fromArgs) throw UsageError("'-p' and '-E' are mutually exclusive"); - auto store = openStore(); - auto evalStore = myArgs.evalStoreUrl ? openStore(StoreReference{*myArgs.evalStoreUrl}) : store; + auto store = openStore(settings); + auto evalStore = myArgs.evalStoreUrl ? openStore(settings, StoreReference{*myArgs.evalStoreUrl}) : store; auto state = std::make_unique(myArgs.lookupPath, evalStore, fetchSettings, evalSettings, store); state->repair = myArgs.repair; diff --git a/src/nix/nix-channel/nix-channel.cc b/src/nix/nix-channel/nix-channel.cc index 6c6a6356f..562fddd14 100644 --- a/src/nix/nix-channel/nix-channel.cc +++ b/src/nix/nix-channel/nix-channel.cc @@ -88,7 +88,7 @@ static void update(const StringSet & channelNames) { readChannels(); - auto store = openStore(); + auto store = openStore(settings); auto [fd, unpackChannelPath] = createTempFile(); writeFull( @@ -189,7 +189,7 @@ static int main_nix_channel(int argc, char ** argv) // Figure out the name of the `.nix-channels' file to use auto home = getHome(); channelsList = settings.useXDGBaseDirectories ? createNixStateDir() + "/channels" : home + "/.nix-channels"; - nixDefExpr = getNixDefExpr(); + nixDefExpr = getNixDefExpr(settings); // Figure out the name of the channels profile. profile = profilesDir(settings.getProfileDirsOptions()) + "/channels"; diff --git a/src/nix/nix-collect-garbage/nix-collect-garbage.cc b/src/nix/nix-collect-garbage/nix-collect-garbage.cc index ec6ede80c..305e211b5 100644 --- a/src/nix/nix-collect-garbage/nix-collect-garbage.cc +++ b/src/nix/nix-collect-garbage/nix-collect-garbage.cc @@ -102,7 +102,7 @@ static int main_nix_collect_garbage(int argc, char ** argv) removeOldGenerations(dir); } - auto store = openStore(); + auto store = openStore(settings); auto & gcStore = require(*store); options.action = dryRun ? GCOptions::gcReturnDead : GCOptions::gcDeleteDead; GCResults results; diff --git a/src/nix/nix-copy-closure/nix-copy-closure.cc b/src/nix/nix-copy-closure/nix-copy-closure.cc index 8141a7f62..f4434dbbf 100644 --- a/src/nix/nix-copy-closure/nix-copy-closure.cc +++ b/src/nix/nix-copy-closure/nix-copy-closure.cc @@ -49,10 +49,10 @@ static int main_nix_copy_closure(int argc, char ** argv) if (sshHost.empty()) throw UsageError("no host name specified"); - auto remoteConfig = make_ref("ssh", sshHost, LegacySSHStoreConfig::Params{}); + auto remoteConfig = make_ref(settings, "ssh", sshHost, LegacySSHStoreConfig::Params{}); remoteConfig->compress |= gzip; - auto to = toMode ? remoteConfig->openStore() : openStore(); - auto from = toMode ? openStore() : remoteConfig->openStore(); + auto to = toMode ? remoteConfig->openStore() : openStore(settings); + auto from = toMode ? openStore(settings) : remoteConfig->openStore(); RealisedPath::Set storePaths2; for (auto & path : storePaths) diff --git a/src/nix/nix-env/nix-env.cc b/src/nix/nix-env/nix-env.cc index 4a46e7ca6..29c88a730 100644 --- a/src/nix/nix-env/nix-env.cc +++ b/src/nix/nix-env/nix-env.cc @@ -1410,7 +1410,7 @@ static int main_nix_env(int argc, char ** argv) globals.instSource.type = srcUnknown; globals.instSource.systemFilter = "*"; - std::filesystem::path nixExprPath = getNixDefExpr(); + std::filesystem::path nixExprPath = getNixDefExpr(settings); if (!pathExists(nixExprPath)) { try { @@ -1509,7 +1509,7 @@ static int main_nix_env(int argc, char ** argv) if (!op) throw UsageError("no operation specified"); - auto store = openStore(); + auto store = openStore(settings); globals.state = std::shared_ptr(new EvalState(myArgs.lookupPath, store, fetchSettings, evalSettings)); diff --git a/src/nix/nix-instantiate/nix-instantiate.cc b/src/nix/nix-instantiate/nix-instantiate.cc index 3137e8bc6..232b7464c 100644 --- a/src/nix/nix-instantiate/nix-instantiate.cc +++ b/src/nix/nix-instantiate/nix-instantiate.cc @@ -166,8 +166,8 @@ static int main_nix_instantiate(int argc, char ** argv) if (evalOnly && !wantsReadWrite) settings.readOnlyMode = true; - auto store = openStore(); - auto evalStore = myArgs.evalStoreUrl ? openStore(StoreReference{*myArgs.evalStoreUrl}) : store; + auto store = openStore(settings); + auto evalStore = myArgs.evalStoreUrl ? openStore(settings, StoreReference{*myArgs.evalStoreUrl}) : store; auto state = std::make_unique(myArgs.lookupPath, evalStore, fetchSettings, evalSettings, store); state->repair = myArgs.repair; diff --git a/src/nix/nix-store/nix-store.cc b/src/nix/nix-store/nix-store.cc index 45fb8e581..54a4c85bf 100644 --- a/src/nix/nix-store/nix-store.cc +++ b/src/nix/nix-store/nix-store.cc @@ -1232,7 +1232,7 @@ static int main_nix_store(int argc, char ** argv) throw UsageError("no operation specified"); if (op != opDump && op != opRestore) /* !!! hack */ - store = openStore(); + store = openStore(settings); op(std::move(opFlags), std::move(opArgs)); diff --git a/src/nix/prefetch.cc b/src/nix/prefetch.cc index 35addff95..7ca229150 100644 --- a/src/nix/prefetch.cc +++ b/src/nix/prefetch.cc @@ -202,7 +202,7 @@ static int main_nix_prefetch_url(int argc, char ** argv) setLogFormat("bar"); - auto store = openStore(); + auto store = openStore(settings); auto state = std::make_unique(myArgs.lookupPath, store, fetchSettings, evalSettings); Bindings & autoArgs = *myArgs.getAutoArgs(*state); diff --git a/src/nix/repl.cc b/src/nix/repl.cc index 1fc30954b..859921fa5 100644 --- a/src/nix/repl.cc +++ b/src/nix/repl.cc @@ -2,6 +2,7 @@ #include "nix/expr/eval-settings.hh" #include "nix/util/config-global.hh" #include "nix/store/globals.hh" +#include "nix/main/shared.hh" #include "nix/store/store-open.hh" #include "nix/cmd/command.hh" #include "nix/cmd/installable-value.hh" diff --git a/src/nix/sigs.cc b/src/nix/sigs.cc index 3258aca99..9f06234ab 100644 --- a/src/nix/sigs.cc +++ b/src/nix/sigs.cc @@ -44,7 +44,7 @@ struct CmdCopySigs : StorePathsCommand // FIXME: factor out commonality with MixVerify. std::vector> substituters; for (auto & s : substituterUris) - substituters.push_back(openStore(StoreReference{s})); + substituters.push_back(openStore(settings, StoreReference{s})); ThreadPool pool{fileTransferSettings.httpConnections}; diff --git a/src/nix/unix/daemon.cc b/src/nix/unix/daemon.cc index 7fce2e8f6..b38c13455 100644 --- a/src/nix/unix/daemon.cc +++ b/src/nix/unix/daemon.cc @@ -457,7 +457,8 @@ static int main_nix_daemon(int argc, char ** argv) return true; }); - runDaemon(resolveStoreConfig(StoreReference{settings.storeUri.get()}), stdio, isTrustedOpt, processOps); + runDaemon( + resolveStoreConfig(settings, StoreReference{settings.storeUri.get()}), stdio, isTrustedOpt, processOps); return 0; } diff --git a/src/nix/upgrade-nix.cc b/src/nix/upgrade-nix.cc index 2dedb53c7..5bb3994dd 100644 --- a/src/nix/upgrade-nix.cc +++ b/src/nix/upgrade-nix.cc @@ -10,6 +10,7 @@ #include "nix/util/executable-path.hh" #include "nix/store/globals.hh" #include "nix/util/config-global.hh" +#include "nix/main/shared.hh" #include "self-exe.hh" using namespace nix; diff --git a/src/nix/verify.cc b/src/nix/verify.cc index 6fb10bf3e..636128fbb 100644 --- a/src/nix/verify.cc +++ b/src/nix/verify.cc @@ -65,9 +65,9 @@ struct CmdVerify : StorePathsCommand { std::vector> substituters; for (auto & s : substituterUris) - substituters.push_back(openStore(StoreReference{s})); + substituters.push_back(openStore(settings, StoreReference{s})); - auto publicKeys = getDefaultPublicKeys(); + auto publicKeys = getDefaultPublicKeys(settings); Activity act(*logger, actVerifyPaths); diff --git a/src/perl/lib/Nix/Store.xs b/src/perl/lib/Nix/Store.xs index 6c4bf2e70..2e3a1d40a 100644 --- a/src/perl/lib/Nix/Store.xs +++ b/src/perl/lib/Nix/Store.xs @@ -13,6 +13,9 @@ #include "nix/util/posix-source-accessor.hh" #include "nix/store/export-import.hh" +// TODO get rid of this, and stop relying on global store settings. +#include "nix/main/shared.hh" + #include #include @@ -62,13 +65,13 @@ StoreWrapper::new(char * s = nullptr) libStoreInitialized = true; } if (items == 1) { - _store = openStore(); + _store = openStore(settings); RETVAL = new StoreWrapper { .store = ref{_store} }; } else { RETVAL = new StoreWrapper { - .store = openStore(s) + .store = openStore(settings, s) }; } } catch (Error & e) { @@ -424,4 +427,4 @@ StoreWrapper::addTempRoot(char * storePath) SV * getStoreDir() PPCODE: - XPUSHs(sv_2mortal(newSVpv(resolveStoreConfig(StoreReference{settings.storeUri.get()})->storeDir.c_str(), 0))); + XPUSHs(sv_2mortal(newSVpv(resolveStoreConfig(settings, StoreReference{settings.storeUri.get()})->storeDir.c_str(), 0))); diff --git a/src/perl/meson.build b/src/perl/meson.build index 59f2a66b8..56d121e55 100644 --- a/src/perl/meson.build +++ b/src/perl/meson.build @@ -77,6 +77,10 @@ libsodium_dep = dependency('libsodium') nix_store_dep = dependency('nix-store') +# TEMP hack to get global settings, until we extend Perl API with +# explicit settings loading or similar. +nix_main_dep = dependency('nix-main') + # Finding Perl Headers is a pain. as they do not have # pkgconfig available, are not in a standard location, @@ -170,6 +174,7 @@ nix_perl_store_dep_list = [ curl_dep, libsodium_dep, nix_store_dep, + nix_main_dep, ] # # build diff --git a/src/perl/package.nix b/src/perl/package.nix index e25b2996c..3bd656886 100644 --- a/src/perl/package.nix +++ b/src/perl/package.nix @@ -6,6 +6,7 @@ perl, perlPackages, nix-store, + nix-main, version, curl, bzip2, @@ -45,6 +46,7 @@ perl.pkgs.toPerlModule ( buildInputs = [ nix-store + nix-main bzip2 libsodium perlPackages.DBI diff --git a/tests/functional/test-libstoreconsumer/main.cc b/tests/functional/test-libstoreconsumer/main.cc index 6cfe50047..625fa7d7f 100644 --- a/tests/functional/test-libstoreconsumer/main.cc +++ b/tests/functional/test-libstoreconsumer/main.cc @@ -17,7 +17,9 @@ int main(int argc, char ** argv) initLibStore(); - auto store = nix::openStore(); + Settings settings; + + auto store = nix::openStore(settings); // build the derivation