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>
|
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
|
|
|
|
2012-07-30 23:55:41 +00:00
|
|
|
Settings::Settings()
|
2017-04-13 18:53:23 +00:00
|
|
|
: Config({})
|
|
|
|
, nixPrefix(NIX_PREFIX)
|
|
|
|
, nixStore(canonPath(getEnv("NIX_STORE_DIR", getEnv("NIX_STORE", NIX_STORE_DIR))))
|
|
|
|
, nixDataDir(canonPath(getEnv("NIX_DATA_DIR", NIX_DATA_DIR)))
|
|
|
|
, nixLogDir(canonPath(getEnv("NIX_LOG_DIR", NIX_LOG_DIR)))
|
|
|
|
, nixStateDir(canonPath(getEnv("NIX_STATE_DIR", NIX_STATE_DIR)))
|
|
|
|
, nixConfDir(canonPath(getEnv("NIX_CONF_DIR", NIX_CONF_DIR)))
|
|
|
|
, nixLibexecDir(canonPath(getEnv("NIX_LIBEXEC_DIR", NIX_LIBEXEC_DIR)))
|
|
|
|
, nixBinDir(canonPath(getEnv("NIX_BIN_DIR", NIX_BIN_DIR)))
|
|
|
|
, 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" : "";
|
2013-09-06 14:36:56 +00:00
|
|
|
lockCPU = getEnv("NIX_AFFINITY_HACK", "1") == "1";
|
2017-06-12 14:44:43 +00:00
|
|
|
|
|
|
|
caFile = getEnv("NIX_SSL_CERT_FILE", getEnv("SSL_CERT_FILE", ""));
|
|
|
|
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");
|
2017-10-24 08:52:34 +00:00
|
|
|
if (s != "") {
|
|
|
|
Strings ss;
|
|
|
|
for (auto & p : tokenizeString<Strings>(s, ":"))
|
|
|
|
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
|
|
|
|
2012-07-30 23:55:41 +00:00
|
|
|
void Settings::loadConfFile()
|
2005-02-01 22:07:48 +00:00
|
|
|
{
|
2017-04-13 18:53:23 +00:00
|
|
|
applyConfigFile(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. */
|
|
|
|
resetOverriden();
|
|
|
|
|
|
|
|
applyConfigFile(getConfigDir() + "/nix/nix.conf");
|
2005-02-01 22:07:48 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 23:55:41 +00:00
|
|
|
void Settings::set(const string & name, const string & value)
|
2005-02-01 22:07:48 +00:00
|
|
|
{
|
2017-04-13 18:53:23 +00:00
|
|
|
Config::set(name, value);
|
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
|
|
|
}
|
|
|
|
|
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.")
|
|
|
|
.handler([=](Strings ss) { value = smEnabled; })
|
|
|
|
.category(category);
|
|
|
|
args.mkFlag()
|
|
|
|
.longName("no-" + name)
|
|
|
|
.description("Disable sandboxing.")
|
|
|
|
.handler([=](Strings ss) { value = smDisabled; })
|
|
|
|
.category(category);
|
|
|
|
args.mkFlag()
|
|
|
|
.longName("relaxed-" + name)
|
|
|
|
.description("Enable sandboxing, but allow builds to disable it.")
|
|
|
|
.handler([=](Strings ss) { value = smRelaxed; })
|
|
|
|
.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
|
|
|
}
|
|
|
|
|
2006-09-04 21:06:23 +00:00
|
|
|
}
|