Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/libstore/dummy-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ ref<DummyStoreConfig> adl_serializer<ref<DummyStore::Config>>::from_json(const j
{
auto & obj = getObject(json);
auto cfg = make_ref<DummyStore::Config>(DummyStore::Config::Params{});
const_cast<PathSetting &>(cfg->storeDir_).set(getString(valueAt(obj, "store")));
cfg->storeDir_.set(getString(valueAt(obj, "store")));
cfg->readOnly = true;
return cfg;
}
Expand Down
30 changes: 28 additions & 2 deletions src/libstore/include/nix/store/store-api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,32 @@ struct MissingPaths
uint64_t narSize{0};
};

/**
* A setting for the Nix store directory. Automatically canonicalises the
* path and rejects the empty string. Stored as `std::string` because
* store directory are valid file paths on *some* OS, but not neccessarily the OS of this build of Nix.
*
* (For example, consider `SSHStore` from Linux to Windows, or vice versa, the foreign path will not be a valid
* `std::filesystem::path`.)
*/
class StoreDirSetting : public BaseSetting<std::string>
{
public:
StoreDirSetting(
Config * options,
const std::string & def,
const std::string & name,
const std::string & description,
const StringSet & aliases = {});

std::string parse(const std::string & str) const override;

void operator=(const std::string & v)
{
this->assign(v);
}
};

/**
* Need to make this a separate class so I can get the right
* initialization order in the constructor for `StoreConfig`.
Expand All @@ -86,11 +112,11 @@ private:
* Compute the default Nix store directory from environment variables
* (`NIX_STORE_DIR`, `NIX_STORE`) or the compile-time default.
*/
static Path getDefaultNixStoreDir();
static std::string getDefaultNixStoreDir();

public:

const PathSetting storeDir_{
StoreDirSetting storeDir_{
this,
getDefaultNixStoreDir(),
"store",
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/include/nix/store/store-dir-config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ MakeError(BadStorePathName, BadStorePath);
*/
struct StoreDirConfig
{
const Path & storeDir;
const std::string & storeDir;

// pure methods

Expand Down
26 changes: 24 additions & 2 deletions src/libstore/store-api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,35 @@ using json = nlohmann::json;

namespace nix {

Path StoreConfigBase::getDefaultNixStoreDir()
std::string StoreConfigBase::getDefaultNixStoreDir()
{
return
#ifndef _WIN32
canonPath
#endif
(getEnvNonEmpty("NIX_STORE_DIR").value_or(getEnvNonEmpty("NIX_STORE").value_or(NIX_STORE_DIR)));
(getEnvNonEmpty("NIX_STORE_DIR").value_or(getEnvNonEmpty("NIX_STORE").value_or(NIX_STORE_DIR)))
#ifndef _WIN32
.string()
#endif
;
}

StoreDirSetting::StoreDirSetting(
Config * options,
const std::string & def,
const std::string & name,
const std::string & description,
const StringSet & aliases)
: BaseSetting<std::string>(def, true, name, description, aliases)
{
options->addSetting(this);
}

std::string StoreDirSetting::parse(const std::string & str) const
{
if (str.empty())
throw UsageError("setting '%s' is a path and paths cannot be empty", name);
return canonPath(str).string();
}

StoreConfig::StoreConfig(const Params & params)
Expand Down
40 changes: 0 additions & 40 deletions src/libutil/configuration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -514,46 +514,6 @@ template class BaseSetting<std::filesystem::path>;
template class BaseSetting<std::optional<std::filesystem::path>>;
template class BaseSetting<std::optional<std::string>>;

PathSetting::PathSetting(
Config * options,
const Path & def,
const std::string & name,
const std::string & description,
const StringSet & aliases)
: BaseSetting<Path>(def, true, name, description, aliases)
{
options->addSetting(this);
}

Path PathSetting::parse(const std::string & str) const
{
return parsePath(*this, str).string();
}

OptionalPathSetting::OptionalPathSetting(
Config * options,
const std::optional<Path> & def,
const std::string & name,
const std::string & description,
const StringSet & aliases)
: BaseSetting<std::optional<Path>>(def, true, name, description, aliases)
{
options->addSetting(this);
}

std::optional<Path> OptionalPathSetting::parse(const std::string & str) const
{
if (str == "")
return std::nullopt;
else
return parsePath(*this, str).string();
}

void OptionalPathSetting::operator=(const std::optional<Path> & v)
{
this->assign(v);
}

bool ExperimentalFeatureSettings::isEnabled(const ExperimentalFeature & feature) const
{
auto & f = experimentalFeatures.get();
Expand Down
52 changes: 0 additions & 52 deletions src/libutil/include/nix/util/configuration.hh
Original file line number Diff line number Diff line change
Expand Up @@ -379,58 +379,6 @@ public:
}
};

/**
* A special setting for Paths. These are automatically canonicalised
* (e.g. "/foo//bar/" becomes "/foo/bar").
*
* It is mandatory to specify a path; i.e. the empty string is not
* permitted.
*/
class PathSetting : public BaseSetting<Path>
{
public:

PathSetting(
Config * options,
const Path & def,
const std::string & name,
const std::string & description,
const StringSet & aliases = {});

Path parse(const std::string & str) const override;

Path operator+(const char * p) const
{
return value + p;
}

void operator=(const Path & v)
{
this->assign(v);
}
};

/**
* Like `PathSetting`, but the absence of a path is also allowed.
*
* `std::optional` is used instead of the empty string for clarity.
*/
class OptionalPathSetting : public BaseSetting<std::optional<Path>>
{
public:

OptionalPathSetting(
Config * options,
const std::optional<Path> & def,
const std::string & name,
const std::string & description,
const StringSet & aliases = {});

std::optional<Path> parse(const std::string & str) const override;

void operator=(const std::optional<Path> & v);
};

struct ExperimentalFeatureSettings : Config
{

Expand Down
Loading