1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-07 01:51:47 +02:00

Read per-user settings from ~/.config/nix/nix.conf

This commit is contained in:
Eelco Dolstra 2017-04-20 14:58:16 +02:00
parent 562585e901
commit f05d5f89ff
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
8 changed files with 70 additions and 24 deletions

View file

@ -9,6 +9,7 @@ void Config::set(const std::string & name, const std::string & value)
if (i == _settings.end())
throw UsageError("unknown setting '%s'", name);
i->second.setting->set(value);
i->second.setting->overriden = true;
}
void Config::addSetting(AbstractSetting * setting)
@ -22,6 +23,7 @@ void Config::addSetting(AbstractSetting * setting)
auto i = initials.find(setting->name);
if (i != initials.end()) {
setting->set(i->second);
setting->overriden = true;
initials.erase(i);
set = true;
}
@ -34,6 +36,7 @@ void Config::addSetting(AbstractSetting * setting)
alias, setting->name);
else {
setting->set(i->second);
setting->overriden = true;
initials.erase(i);
set = true;
}
@ -47,11 +50,11 @@ void Config::warnUnknownSettings()
warn("unknown setting '%s'", i.first);
}
StringMap Config::getSettings()
StringMap Config::getSettings(bool overridenOnly)
{
StringMap res;
for (auto & opt : _settings)
if (!opt.second.isAlias)
if (!opt.second.isAlias && (!overridenOnly || opt.second.setting->overriden))
res.emplace(opt.first, opt.second.setting->to_string());
return res;
}
@ -94,6 +97,12 @@ void Config::applyConfigFile(const Path & path, bool fatal)
} catch (SysError &) { }
}
void Config::resetOverriden()
{
for (auto & s : _settings)
s.second.setting->overriden = false;
}
AbstractSetting::AbstractSetting(
const std::string & name,
const std::string & description,

View file

@ -51,9 +51,11 @@ public:
void warnUnknownSettings();
StringMap getSettings();
StringMap getSettings(bool overridenOnly = false);
void applyConfigFile(const Path & path, bool fatal = false);
void resetOverriden();
};
class AbstractSetting
@ -68,6 +70,8 @@ public:
int created = 123;
bool overriden = false;
protected:
AbstractSetting(
@ -78,7 +82,7 @@ protected:
virtual ~AbstractSetting()
{
// Check against a gcc miscompilation causing our constructor
// not to run.
// not to run (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80431).
assert(created == 123);
}
@ -88,6 +92,8 @@ protected:
bool parseBool(const std::string & str);
std::string printBool(bool b);
bool isOverriden() { return overriden; }
};
struct DefaultSettingTag { };

View file

@ -429,6 +429,18 @@ Path getCacheDir()
}
Path getConfigDir()
{
Path configDir = getEnv("XDG_CONFIG_HOME");
if (configDir.empty()) {
Path homeDir = getEnv("HOME");
if (homeDir.empty()) throw Error("$XDG_CONFIG_HOME and $HOME are not set");
configDir = homeDir + "/.config";
}
return configDir;
}
Paths createDirs(const Path & path)
{
Paths created;

View file

@ -110,9 +110,12 @@ void deletePath(const Path & path, unsigned long long & bytesFreed);
Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix",
bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755);
/* Return the path to $XDG_CACHE_HOME/.cache. */
/* Return $XDG_CACHE_HOME or $HOME/.cache. */
Path getCacheDir();
/* Return $XDG_CONFIG_HOME or $HOME/.config. */
Path getConfigDir();
/* Create a directory and all its parents, if necessary. Returns the
list of created directories, in order of creation. */
Paths createDirs(const Path & path);