2003-06-16 13:33:38 +00:00
|
|
|
#include "globals.hh"
|
2006-09-04 21:06:23 +00:00
|
|
|
#include "util.hh"
|
2014-07-16 14:02:05 +00:00
|
|
|
#include "archive.hh"
|
2017-04-13 18:53:23 +00:00
|
|
|
#include "args.hh"
|
2003-06-16 13:33:38 +00:00
|
|
|
|
2005-09-22 15:43:22 +00:00
|
|
|
#include <algorithm>
|
2017-02-27 15:01:54 +00:00
|
|
|
#include <map>
|
|
|
|
#include <thread>
|
2018-02-08 16:26:18 +00:00
|
|
|
#include <dlfcn.h>
|
2005-02-01 22:07:48 +00:00
|
|
|
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
2013-03-08 00:24:59 +00:00
|
|
|
/* The default location of the daemon socket, relative to nixStateDir.
|
|
|
|
The socket is in a directory to allow you to control access to the
|
|
|
|
Nix daemon by setting the mode/ownership of the directory
|
|
|
|
appropriately. (This wouldn't work on the socket itself since it
|
|
|
|
must be deleted and recreated on startup.) */
|
|
|
|
#define DEFAULT_SOCKET_PATH "/daemon-socket/socket"
|
|
|
|
|
Explicitly model all settings and fail on unrecognized ones
Previously, the Settings class allowed other code to query for string
properties, which led to a proliferation of code all over the place making
up new options without any sort of central registry of valid options. This
commit pulls all those options back into the central Settings class and
removes the public get() methods, to discourage future abuses like that.
Furthermore, because we know the full set of options ahead of time, we
now fail loudly if someone enters an unrecognized option, thus preventing
subtle typos. With some template fun, we could probably also dump the full
set of options (with documentation, defaults, etc.) to the command line,
but I'm not doing that yet here.
2017-02-22 03:50:18 +00:00
|
|
|
/* chroot-like behavior from Apple's sandbox */
|
|
|
|
#if __APPLE__
|
|
|
|
#define DEFAULT_ALLOWED_IMPURE_PREFIXES "/System/Library /usr/lib /dev /bin/sh"
|
|
|
|
#else
|
|
|
|
#define DEFAULT_ALLOWED_IMPURE_PREFIXES ""
|
|
|
|
#endif
|
2013-03-08 00:24:59 +00:00
|
|
|
|
2012-07-30 23:55:41 +00:00
|
|
|
Settings settings;
|
2003-07-31 13:47:13 +00:00
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
static GlobalConfig::Register r1(&settings);
|
|
|
|
|
2012-07-30 23:55:41 +00:00
|
|
|
Settings::Settings()
|
2018-03-27 16:41:31 +00:00
|
|
|
: nixPrefix(NIX_PREFIX)
|
2019-11-22 15:06:44 +00:00
|
|
|
, nixStore(canonPath(getEnv("NIX_STORE_DIR").value_or(getEnv("NIX_STORE").value_or(NIX_STORE_DIR))))
|
|
|
|
, nixDataDir(canonPath(getEnv("NIX_DATA_DIR").value_or(NIX_DATA_DIR)))
|
|
|
|
, nixLogDir(canonPath(getEnv("NIX_LOG_DIR").value_or(NIX_LOG_DIR)))
|
|
|
|
, nixStateDir(canonPath(getEnv("NIX_STATE_DIR").value_or(NIX_STATE_DIR)))
|
|
|
|
, nixConfDir(canonPath(getEnv("NIX_CONF_DIR").value_or(NIX_CONF_DIR)))
|
|
|
|
, nixLibexecDir(canonPath(getEnv("NIX_LIBEXEC_DIR").value_or(NIX_LIBEXEC_DIR)))
|
|
|
|
, nixBinDir(canonPath(getEnv("NIX_BIN_DIR").value_or(NIX_BIN_DIR)))
|
2018-02-14 22:05:55 +00:00
|
|
|
, nixManDir(canonPath(NIX_MAN_DIR))
|
2017-04-13 18:53:23 +00:00
|
|
|
, nixDaemonSocketFile(canonPath(nixStateDir + DEFAULT_SOCKET_PATH))
|
2012-07-30 23:55:41 +00:00
|
|
|
{
|
2014-05-02 10:46:03 +00:00
|
|
|
buildUsersGroup = getuid() == 0 ? "nixbld" : "";
|
2019-11-22 15:06:44 +00:00
|
|
|
lockCPU = getEnv("NIX_AFFINITY_HACK") == "1";
|
2017-06-12 14:44:43 +00:00
|
|
|
|
2019-11-22 15:06:44 +00:00
|
|
|
caFile = getEnv("NIX_SSL_CERT_FILE").value_or(getEnv("SSL_CERT_FILE").value_or(""));
|
2017-06-12 14:44:43 +00:00
|
|
|
if (caFile == "") {
|
|
|
|
for (auto & fn : {"/etc/ssl/certs/ca-certificates.crt", "/nix/var/nix/profiles/default/etc/ssl/certs/ca-bundle.crt"})
|
|
|
|
if (pathExists(fn)) {
|
|
|
|
caFile = fn;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
Explicitly model all settings and fail on unrecognized ones
Previously, the Settings class allowed other code to query for string
properties, which led to a proliferation of code all over the place making
up new options without any sort of central registry of valid options. This
commit pulls all those options back into the central Settings class and
removes the public get() methods, to discourage future abuses like that.
Furthermore, because we know the full set of options ahead of time, we
now fail loudly if someone enters an unrecognized option, thus preventing
subtle typos. With some template fun, we could probably also dump the full
set of options (with documentation, defaults, etc.) to the command line,
but I'm not doing that yet here.
2017-02-22 03:50:18 +00:00
|
|
|
|
2017-05-02 12:36:59 +00:00
|
|
|
/* Backwards compatibility. */
|
|
|
|
auto s = getEnv("NIX_REMOTE_SYSTEMS");
|
2019-11-22 15:06:44 +00:00
|
|
|
if (s) {
|
2017-10-24 08:52:34 +00:00
|
|
|
Strings ss;
|
2019-11-22 15:06:44 +00:00
|
|
|
for (auto & p : tokenizeString<Strings>(*s, ":"))
|
2017-10-24 08:52:34 +00:00
|
|
|
ss.push_back("@" + p);
|
|
|
|
builders = concatStringsSep(" ", ss);
|
|
|
|
}
|
2017-05-02 12:36:59 +00:00
|
|
|
|
2017-05-15 15:30:33 +00:00
|
|
|
#if defined(__linux__) && defined(SANDBOX_SHELL)
|
|
|
|
sandboxPaths = tokenizeString<StringSet>("/bin/sh=" SANDBOX_SHELL);
|
Explicitly model all settings and fail on unrecognized ones
Previously, the Settings class allowed other code to query for string
properties, which led to a proliferation of code all over the place making
up new options without any sort of central registry of valid options. This
commit pulls all those options back into the central Settings class and
removes the public get() methods, to discourage future abuses like that.
Furthermore, because we know the full set of options ahead of time, we
now fail loudly if someone enters an unrecognized option, thus preventing
subtle typos. With some template fun, we could probably also dump the full
set of options (with documentation, defaults, etc.) to the command line,
but I'm not doing that yet here.
2017-02-22 03:50:18 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
allowedImpureHostPrefixes = tokenizeString<StringSet>(DEFAULT_ALLOWED_IMPURE_PREFIXES);
|
2005-09-22 15:43:22 +00:00
|
|
|
}
|
2005-02-01 22:07:48 +00:00
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
void loadConfFile()
|
2005-02-01 22:07:48 +00:00
|
|
|
{
|
2018-03-27 16:41:31 +00:00
|
|
|
globalConfig.applyConfigFile(settings.nixConfDir + "/nix.conf");
|
2017-04-20 12:58:16 +00:00
|
|
|
|
|
|
|
/* We only want to send overrides to the daemon, i.e. stuff from
|
|
|
|
~/.nix/nix.conf or the command line. */
|
2018-03-27 16:41:31 +00:00
|
|
|
globalConfig.resetOverriden();
|
2017-04-20 12:58:16 +00:00
|
|
|
|
2018-10-25 11:00:21 +00:00
|
|
|
auto dirs = getConfigDirs();
|
|
|
|
// Iterate over them in reverse so that the ones appearing first in the path take priority
|
|
|
|
for (auto dir = dirs.rbegin(); dir != dirs.rend(); dir++) {
|
|
|
|
globalConfig.applyConfigFile(*dir + "/nix/nix.conf");
|
|
|
|
}
|
2005-02-01 22:07:48 +00:00
|
|
|
}
|
2005-02-14 13:07:09 +00:00
|
|
|
|
2017-04-13 18:53:23 +00:00
|
|
|
unsigned int Settings::getDefaultCores()
|
2006-12-08 15:44:00 +00:00
|
|
|
{
|
2017-04-13 18:53:23 +00:00
|
|
|
return std::max(1U, std::thread::hardware_concurrency());
|
2009-02-27 11:04:41 +00:00
|
|
|
}
|
|
|
|
|
2018-09-28 13:57:27 +00:00
|
|
|
StringSet Settings::getDefaultSystemFeatures()
|
|
|
|
{
|
|
|
|
/* For backwards compatibility, accept some "features" that are
|
|
|
|
used in Nixpkgs to route builds to certain machines but don't
|
|
|
|
actually require anything special on the machines. */
|
2019-11-04 12:48:58 +00:00
|
|
|
StringSet features{"nixos-test", "benchmark", "big-parallel", "recursive-nix"};
|
2018-09-28 13:57:27 +00:00
|
|
|
|
|
|
|
#if __linux__
|
|
|
|
if (access("/dev/kvm", R_OK | W_OK) == 0)
|
|
|
|
features.insert("kvm");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return features;
|
|
|
|
}
|
|
|
|
|
2019-11-26 18:48:34 +00:00
|
|
|
bool Settings::isExperimentalFeatureEnabled(const std::string & name)
|
2019-10-16 15:45:09 +00:00
|
|
|
{
|
|
|
|
auto & f = experimentalFeatures.get();
|
2019-11-26 18:48:34 +00:00
|
|
|
return std::find(f.begin(), f.end(), name) != f.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Settings::requireExperimentalFeature(const std::string & name)
|
|
|
|
{
|
|
|
|
if (!isExperimentalFeatureEnabled(name))
|
2019-10-16 15:45:09 +00:00
|
|
|
throw Error("experimental Nix feature '%s' is disabled", name);
|
|
|
|
}
|
|
|
|
|
2017-04-13 18:53:23 +00:00
|
|
|
const string nixVersion = PACKAGE_VERSION;
|
2009-02-27 11:04:41 +00:00
|
|
|
|
2017-04-20 14:52:53 +00:00
|
|
|
template<> void BaseSetting<SandboxMode>::set(const std::string & str)
|
2008-11-20 12:25:11 +00:00
|
|
|
{
|
2017-04-13 18:53:23 +00:00
|
|
|
if (str == "true") value = smEnabled;
|
|
|
|
else if (str == "relaxed") value = smRelaxed;
|
|
|
|
else if (str == "false") value = smDisabled;
|
|
|
|
else throw UsageError("option '%s' has invalid value '%s'", name, str);
|
Explicitly model all settings and fail on unrecognized ones
Previously, the Settings class allowed other code to query for string
properties, which led to a proliferation of code all over the place making
up new options without any sort of central registry of valid options. This
commit pulls all those options back into the central Settings class and
removes the public get() methods, to discourage future abuses like that.
Furthermore, because we know the full set of options ahead of time, we
now fail loudly if someone enters an unrecognized option, thus preventing
subtle typos. With some template fun, we could probably also dump the full
set of options (with documentation, defaults, etc.) to the command line,
but I'm not doing that yet here.
2017-02-22 03:50:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 14:52:53 +00:00
|
|
|
template<> std::string BaseSetting<SandboxMode>::to_string()
|
Explicitly model all settings and fail on unrecognized ones
Previously, the Settings class allowed other code to query for string
properties, which led to a proliferation of code all over the place making
up new options without any sort of central registry of valid options. This
commit pulls all those options back into the central Settings class and
removes the public get() methods, to discourage future abuses like that.
Furthermore, because we know the full set of options ahead of time, we
now fail loudly if someone enters an unrecognized option, thus preventing
subtle typos. With some template fun, we could probably also dump the full
set of options (with documentation, defaults, etc.) to the command line,
but I'm not doing that yet here.
2017-02-22 03:50:18 +00:00
|
|
|
{
|
2017-04-13 18:53:23 +00:00
|
|
|
if (value == smEnabled) return "true";
|
|
|
|
else if (value == smRelaxed) return "relaxed";
|
|
|
|
else if (value == smDisabled) return "false";
|
|
|
|
else abort();
|
2008-11-20 12:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 15:34:47 +00:00
|
|
|
template<> void BaseSetting<SandboxMode>::toJSON(JSONPlaceholder & out)
|
|
|
|
{
|
|
|
|
AbstractSetting::toJSON(out);
|
|
|
|
}
|
|
|
|
|
2017-06-07 16:41:20 +00:00
|
|
|
template<> void BaseSetting<SandboxMode>::convertToArg(Args & args, const std::string & category)
|
2017-06-07 14:17:17 +00:00
|
|
|
{
|
2017-06-07 16:41:20 +00:00
|
|
|
args.mkFlag()
|
|
|
|
.longName(name)
|
|
|
|
.description("Enable sandboxing.")
|
2017-11-21 17:50:56 +00:00
|
|
|
.handler([=](std::vector<std::string> ss) { override(smEnabled); })
|
2017-06-07 16:41:20 +00:00
|
|
|
.category(category);
|
|
|
|
args.mkFlag()
|
|
|
|
.longName("no-" + name)
|
|
|
|
.description("Disable sandboxing.")
|
2017-11-21 17:50:56 +00:00
|
|
|
.handler([=](std::vector<std::string> ss) { override(smDisabled); })
|
2017-06-07 16:41:20 +00:00
|
|
|
.category(category);
|
|
|
|
args.mkFlag()
|
|
|
|
.longName("relaxed-" + name)
|
|
|
|
.description("Enable sandboxing, but allow builds to disable it.")
|
2017-11-21 17:50:56 +00:00
|
|
|
.handler([=](std::vector<std::string> ss) { override(smRelaxed); })
|
2017-06-07 16:41:20 +00:00
|
|
|
.category(category);
|
2017-06-07 14:17:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 14:52:53 +00:00
|
|
|
void MaxBuildJobsSetting::set(const std::string & str)
|
2014-02-08 05:05:46 +00:00
|
|
|
{
|
2017-04-13 18:53:23 +00:00
|
|
|
if (str == "auto") value = std::max(1U, std::thread::hardware_concurrency());
|
|
|
|
else if (!string2Int(str, value))
|
2017-07-30 11:27:57 +00:00
|
|
|
throw UsageError("configuration setting '%s' should be 'auto' or an integer", name);
|
2014-02-08 05:05:46 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 16:26:18 +00:00
|
|
|
|
|
|
|
void initPlugins()
|
|
|
|
{
|
|
|
|
for (const auto & pluginFile : settings.pluginFiles.get()) {
|
2018-02-13 12:51:52 +00:00
|
|
|
Paths pluginFiles;
|
|
|
|
try {
|
|
|
|
auto ents = readDirectory(pluginFile);
|
|
|
|
for (const auto & ent : ents)
|
|
|
|
pluginFiles.emplace_back(pluginFile + "/" + ent.name);
|
|
|
|
} catch (SysError & e) {
|
|
|
|
if (e.errNo != ENOTDIR)
|
|
|
|
throw;
|
|
|
|
pluginFiles.emplace_back(pluginFile);
|
|
|
|
}
|
|
|
|
for (const auto & file : pluginFiles) {
|
|
|
|
/* handle is purposefully leaked as there may be state in the
|
|
|
|
DSO needed by the action of the plugin. */
|
|
|
|
void *handle =
|
|
|
|
dlopen(file.c_str(), RTLD_LAZY | RTLD_LOCAL);
|
|
|
|
if (!handle)
|
2018-04-12 01:02:50 +00:00
|
|
|
throw Error("could not dynamically open plugin file '%s': %s", file, dlerror());
|
2018-02-13 12:51:52 +00:00
|
|
|
}
|
2018-02-08 16:26:18 +00:00
|
|
|
}
|
2018-02-13 19:43:32 +00:00
|
|
|
|
2018-03-27 16:41:31 +00:00
|
|
|
/* Since plugins can add settings, try to re-apply previously
|
|
|
|
unknown settings. */
|
|
|
|
globalConfig.reapplyUnknownSettings();
|
|
|
|
globalConfig.warnUnknownSettings();
|
2018-02-08 16:26:18 +00:00
|
|
|
}
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|