Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make store setting and store flags use StoreReference #10761

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/libcmd/command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,32 @@ CopyCommand::CopyCommand()
.longName = "from",
.description = "URL of the source Nix store.",
.labels = {"store-uri"},
.handler = {&srcUri},
.handler = {[&](std::string storeUri) {
srcUri = StoreReference::parse(storeUri);
}},
});

addFlag({
.longName = "to",
.description = "URL of the destination Nix store.",
.labels = {"store-uri"},
.handler = {&dstUri},
.handler = {[&](std::string storeUri) {
dstUri = StoreReference::parse(storeUri);
}},
});
}

ref<Store> CopyCommand::createStore()
{
return srcUri.empty() ? StoreCommand::createStore() : openStore(srcUri);
return !srcUri ? StoreCommand::createStore() : openStore(*srcUri);
}

ref<Store> CopyCommand::getDstStore()
{
if (srcUri.empty() && dstUri.empty())
if (!srcUri && !dstUri)
throw UsageError("you must pass '--from' and/or '--to'");

return dstUri.empty() ? openStore() : openStore(dstUri);
return !dstUri ? openStore() : openStore(*dstUri);
}

EvalCommand::EvalCommand()
Expand Down
2 changes: 1 addition & 1 deletion src/libcmd/command.hh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private:
*/
struct CopyCommand : virtual StoreCommand
{
std::string srcUri, dstUri;
std::optional<StoreReference> srcUri, dstUri;

CopyCommand();

Expand Down
4 changes: 3 additions & 1 deletion src/libcmd/common-eval-args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ MixEvalArgs::MixEvalArgs()
)",
.category = category,
.labels = {"store-url"},
.handler = {&evalStoreUrl},
.handler = {[&](std::string s) {
evalStoreUrl = StoreReference::parse(s);
}},
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/libcmd/common-eval-args.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "canon-path.hh"
#include "common-args.hh"
#include "search-path.hh"
#include "store-reference.hh"

#include <filesystem>

Expand All @@ -25,7 +26,7 @@ struct MixEvalArgs : virtual Args, virtual MixRepair

LookupPath lookupPath;

std::optional<std::string> evalStoreUrl;
std::optional<StoreReference> evalStoreUrl;

private:
struct AutoArgExpr { std::string expr; };
Expand Down
2 changes: 2 additions & 0 deletions src/libexpr/flake/flake.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <nlohmann/json.hpp>

#include "terminal.hh"
#include "flake.hh"
#include "eval.hh"
Expand Down
15 changes: 9 additions & 6 deletions src/libexpr/primops/fetchClosure.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,23 +182,26 @@ static void prim_fetchClosure(EvalState & state, const PosIdx pos, Value * * arg
.pos = state.positions[pos]
});

auto parsedURL = parseURL(*fromStoreUrl);
auto parsedURL = StoreReference::parse(*fromStoreUrl);

if (parsedURL.scheme != "http" &&
parsedURL.scheme != "https" &&
!(getEnv("_NIX_IN_TEST").has_value() && parsedURL.scheme == "file"))
auto * storeVariant = std::get_if<StoreReference::Specified>(&parsedURL.variant);

if (!storeVariant ||
(storeVariant->scheme != "http" &&
storeVariant->scheme != "https" &&
!(getEnv("_NIX_IN_TEST").has_value() && storeVariant->scheme == "file")))
throw Error({
.msg = HintFmt("'fetchClosure' only supports http:// and https:// stores"),
.pos = state.positions[pos]
});

if (!parsedURL.query.empty())
if (!parsedURL.params.empty())
throw Error({
.msg = HintFmt("'fetchClosure' does not support URL query parameters (in '%s')", *fromStoreUrl),
.pos = state.positions[pos]
});

auto fromStore = openStore(parsedURL.to_string());
auto fromStore = openStore(parsedURL);

if (toPath)
runFetchClosureWithRewrite(state, pos, *fromStore, *fromPath, *toPath, v);
Expand Down
2 changes: 2 additions & 0 deletions src/libexpr/primops/fetchTree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <iomanip>
#include <regex>

#include <nlohmann/json.hpp>

namespace nix {

void emitTreeAttrs(
Expand Down
1 change: 1 addition & 0 deletions src/libfetchers/unix/git.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "git-utils.hh"
#include "logging.hh"
#include "finally.hh"
#include "json-utils.hh"

#include "fetch-settings.hh"

Expand Down
13 changes: 7 additions & 6 deletions src/libstore-c/nix_api_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ Store * nix_store_open(nix_c_context * context, const char * uri, const char ***
if (uri_str.empty())
return new Store{nix::openStore()};

if (!params)
return new Store{nix::openStore(uri_str)};
auto base_uri = nix::StoreReference::parse(uri_str);

nix::Store::Params params_map;
for (size_t i = 0; params[i] != nullptr; i++) {
params_map[params[i][0]] = params[i][1];
if (params) {
for (size_t i = 0; params[i] != nullptr; i++) {
base_uri.params[params[i][0]] = params[i][1];
}
}
return new Store{nix::openStore(uri_str, params_map)};

return new Store{nix::openStore(std::move(base_uri))};
}
NIXC_CATCH_ERRS_NULL
}
Expand Down
16 changes: 8 additions & 8 deletions src/libstore/daemon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,22 +215,22 @@ struct ClientSettings
auto & name(i.first);
auto & value(i.second);

auto setSubstituters = [&](Setting<Strings> & res) {
auto setSubstituters = [&](Setting<std::vector<StoreReference>> & res) {
if (name != res.name && res.aliases.count(name) == 0)
return false;
StringSet trusted = settings.trustedSubstituters;
std::set<StoreReference> trusted = settings.trustedSubstituters;
for (auto & s : settings.substituters.get())
trusted.insert(s);
Strings subs;
std::vector<StoreReference> subs;
auto ss = tokenizeString<Strings>(value);
for (auto & s : ss)
if (trusted.count(s))
subs.push_back(s);
else if (!hasSuffix(s, "/") && trusted.count(s + "/"))
subs.push_back(s + "/");
for (auto & s : ss) {
auto ref = StoreReference::parse(s);
if (trusted.count(ref))
subs.push_back(ref);
else
warn("ignoring untrusted substituter '%s', you are not a trusted user.\n"
"Run `man nix.conf` for more information on the `substituters` configuration option.", s);
}
res = subs;
return true;
};
Expand Down
6 changes: 4 additions & 2 deletions src/libstore/derivations.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <boost/container/small_vector.hpp>
#include <nlohmann/json.hpp>

#include "derivations.hh"
#include "downstream-placeholder.hh"
#include "store-api.hh"
Expand All @@ -7,8 +10,7 @@
#include "split.hh"
#include "common-protocol.hh"
#include "common-protocol-impl.hh"
#include <boost/container/small_vector.hpp>
#include <nlohmann/json.hpp>
#include "json-utils.hh"

namespace nix {

Expand Down
64 changes: 64 additions & 0 deletions src/libstore/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@

namespace nix {

DECLARE_CONFIG_SERIALISER(StoreReference)
DECLARE_CONFIG_SERIALISER(std::optional<StoreReference>)
DECLARE_CONFIG_SERIALISER(std::set<StoreReference>)
DECLARE_CONFIG_SERIALISER(std::vector<StoreReference>)

/* The default location of the daemon socket, relative to nixStateDir.
The socket is in a directory to allow you to control access to the
Expand Down Expand Up @@ -319,6 +323,66 @@ template<> void BaseSetting<SandboxMode>::convertToArg(Args & args, const std::s
});
}

template<> StoreReference BaseSetting<StoreReference>::parse(const std::string & str) const
{
return StoreReference::parse(str);
}

template<> std::string BaseSetting<StoreReference>::to_string() const
{
return value.render();
}

template<> std::optional<StoreReference> BaseSetting<std::optional<StoreReference>>::parse(const std::string & str) const
{
if (str == "")
return std::nullopt;
else
return StoreReference::parse(str);
}

template<> std::string BaseSetting<std::optional<StoreReference>>::to_string() const
{
return value ? value->render() : "";
}

template<> std::set<StoreReference> BaseSetting<std::set<StoreReference>>::parse(const std::string & str) const
{
std::set<StoreReference> res;
for (const auto & s : tokenizeString<std::vector<std::string>>(str))
res.insert(StoreReference::parse(s));
return res;
}

template<> std::string BaseSetting<std::set<StoreReference>>::to_string() const
{
std::set<std::string> strings;
for (const auto & ref : value)
strings.insert(ref.render());
return concatStringsSep(" ", strings);
}

template<> std::vector<StoreReference> BaseSetting<std::vector<StoreReference>>::parse(const std::string & str) const
{
std::vector<StoreReference> res;
for (const auto & s : tokenizeString<std::vector<std::string>>(str))
res.push_back(StoreReference::parse(s));
return res;
}

template<> std::string BaseSetting<std::vector<StoreReference>>::to_string() const
{
std::vector<std::string> strings;
for (const auto & ref : value)
strings.push_back(ref.render());
return concatStringsSep(" ", strings);
}

template class BaseSetting<StoreReference>;
template class BaseSetting<std::optional<StoreReference>>;
template class BaseSetting<std::set<StoreReference>>;
template class BaseSetting<std::vector<StoreReference>>;

unsigned int MaxBuildJobsSetting::parse(const std::string & str) const
{
if (str == "auto") return std::max(1U, std::thread::hardware_concurrency());
Expand Down
16 changes: 12 additions & 4 deletions src/libstore/globals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "config.hh"
#include "environment-variables.hh"
#include "experimental-features.hh"
#include "store-reference.hh"
#include "users.hh"

#include <map>
Expand Down Expand Up @@ -116,7 +117,7 @@ public:
*/
Path nixDaemonSocketFile;

Setting<std::string> storeUri{this, getEnv("NIX_REMOTE").value_or("auto"), "store",
Setting<StoreReference> storeUri{this, StoreReference::parse(getEnv("NIX_REMOTE").value_or("auto")), "store",
R"(
The [URL of the Nix store](@docroot@/store/types/index.md#store-url-format)
to use for most operations.
Expand Down Expand Up @@ -904,9 +905,16 @@ public:
// Don't document the machine-specific default value
false};

Setting<Strings> substituters{
Setting<std::vector<StoreReference>> substituters{
this,
Strings{"https://cache.nixos.org/"},
{
{
.variant = StoreReference::Specified {
.scheme = "https",
.authority = "cache.nixos.org",
},
},
},
"substituters",
R"(
A list of [URLs of Nix stores](@docroot@/store/types/index.md#store-url-format) to be used as substituters, separated by whitespace.
Expand All @@ -925,7 +933,7 @@ public:
)",
{"binary-caches"}};

Setting<StringSet> trustedSubstituters{
Setting<std::set<StoreReference>> trustedSubstituters{
this, {}, "trusted-substituters",
R"(
A list of [Nix store URLs](@docroot@/store/types/index.md#store-url-format), separated by whitespace.
Expand Down
4 changes: 2 additions & 2 deletions src/libstore/legacy-ssh-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ ref<LegacySSHStore::Connection> LegacySSHStore::openConnection()
Strings command = remoteProgram.get();
command.push_back("--serve");
command.push_back("--write");
if (remoteStore.get() != "") {
if (std::optional rs = remoteStore.get()) {
command.push_back("--store");
command.push_back(remoteStore.get());
command.push_back(rs->render());
}
conn->sshConn = master.startCommand(std::move(command));
conn->to = FdSink(conn->sshConn->in.get());
Expand Down
1 change: 1 addition & 0 deletions src/libstore/nar-info.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "globals.hh"
#include "nar-info.hh"
#include "store-api.hh"
#include "json-utils.hh"

namespace nix {

Expand Down
2 changes: 1 addition & 1 deletion src/libstore/ssh-store-config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct CommonSSHStoreConfig : virtual StoreConfig
const Setting<bool> compress{this, false, "compress",
"Whether to enable SSH compression."};

const Setting<std::string> remoteStore{this, "", "remote-store",
const Setting<std::optional<StoreReference>> remoteStore{this, std::nullopt, "remote-store",
R"(
[Store URL](@docroot@/store/types/index.md#store-url-format)
to be used on the remote machine. The default is `auto`
Expand Down
4 changes: 2 additions & 2 deletions src/libstore/ssh-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ ref<RemoteStore::Connection> SSHStore::openConnection()
auto conn = make_ref<Connection>();
Strings command = remoteProgram.get();
command.push_back("--stdio");
if (remoteStore.get() != "") {
if (std::optional rs = remoteStore.get()) {
command.push_back("--store");
command.push_back(remoteStore.get());
command.push_back(rs->render());
}
command.insert(command.end(),
extraRemoteProgramArgs.begin(), extraRemoteProgramArgs.end());
Expand Down
4 changes: 2 additions & 2 deletions src/libstore/store-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1330,9 +1330,9 @@ std::list<ref<Store>> getDefaultSubstituters()
static auto stores([]() {
std::list<ref<Store>> stores;

StringSet done;
std::set<StoreReference> done;

auto addStore = [&](const std::string & uri) {
auto addStore = [&](const StoreReference & uri) {
if (!done.insert(uri).second) return;
try {
stores.push_back(openStore(uri));
Expand Down
11 changes: 4 additions & 7 deletions src/libstore/store-api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -863,13 +863,10 @@ OutputPathMap resolveDerivedPath(Store &, const DerivedPath::Built &, Store * ev
*/
ref<Store> openStore(StoreReference && storeURI);


/**
* Opens the store at `uri`, where `uri` is in the format expected by `StoreReference::parse`

*/
ref<Store> openStore(const std::string & uri = settings.storeUri.get(),
const Store::Params & extraParams = Store::Params());
static inline ref<Store> openStore(const StoreReference & storeURI = settings.storeUri.get())
{
return openStore(StoreReference { storeURI });
}


/**
Expand Down
Loading
Loading