gnu: notmuch: Update to 0.21.
[jackhill/guix/guix.git] / nix / libstore / globals.cc
CommitLineData
36457566
LC
1#include "config.h"
2
3#include "globals.hh"
4#include "util.hh"
2bb04905 5#include "archive.hh"
36457566
LC
6
7#include <map>
8#include <algorithm>
9
10
11namespace nix {
12
13
14/* The default location of the daemon socket, relative to nixStateDir.
15 The socket is in a directory to allow you to control access to the
16 Nix daemon by setting the mode/ownership of the directory
17 appropriately. (This wouldn't work on the socket itself since it
18 must be deleted and recreated on startup.) */
19#define DEFAULT_SOCKET_PATH "/daemon-socket/socket"
20
21
22Settings settings;
23
24
25Settings::Settings()
26{
27 keepFailed = false;
28 keepGoing = false;
29 tryFallback = false;
30 buildVerbosity = lvlError;
31 maxBuildJobs = 1;
32 buildCores = 1;
33 readOnlyMode = false;
34 thisSystem = SYSTEM;
35 maxSilentTime = 0;
36 buildTimeout = 0;
37 useBuildHook = true;
38 printBuildTrace = false;
322eeb87 39 reservedSize = 8 * 1024 * 1024;
36457566
LC
40 fsyncMetadata = true;
41 useSQLiteWAL = true;
42 syncBeforeRegistering = false;
43 useSubstitutes = true;
44 useChroot = false;
45 useSshSubstituter = false;
46 impersonateLinux26 = false;
47 keepLog = true;
48 compressLog = true;
49 maxLogSize = 0;
50 cacheFailure = false;
51 pollInterval = 5;
52 checkRootReachability = false;
53 gcKeepOutputs = false;
54 gcKeepDerivations = true;
55 autoOptimiseStore = false;
56 envKeepDerivations = false;
57 lockCPU = getEnv("NIX_AFFINITY_HACK", "1") == "1";
58 showTrace = false;
2bb04905 59 enableImportNative = false;
36457566
LC
60}
61
62
63void Settings::processEnvironment()
64{
65 nixStore = canonPath(getEnv("NIX_STORE_DIR", getEnv("NIX_STORE", NIX_STORE_DIR)));
66 nixDataDir = canonPath(getEnv("NIX_DATA_DIR", NIX_DATA_DIR));
67 nixLogDir = canonPath(getEnv("NIX_LOG_DIR", NIX_LOG_DIR));
68 nixStateDir = canonPath(getEnv("NIX_STATE_DIR", NIX_STATE_DIR));
69 nixDBPath = getEnv("NIX_DB_DIR", nixStateDir + "/db");
70 nixConfDir = canonPath(getEnv("NIX_CONF_DIR", NIX_CONF_DIR));
71 nixLibexecDir = canonPath(getEnv("NIX_LIBEXEC_DIR", NIX_LIBEXEC_DIR));
72 nixBinDir = canonPath(getEnv("NIX_BIN_DIR", NIX_BIN_DIR));
73 nixDaemonSocketFile = canonPath(nixStateDir + DEFAULT_SOCKET_PATH);
74}
75
76
77void Settings::loadConfFile()
78{
79 Path settingsFile = (format("%1%/%2%") % nixConfDir % "nix.conf").str();
80 if (!pathExists(settingsFile)) return;
81 string contents = readFile(settingsFile);
82
83 unsigned int pos = 0;
84
85 while (pos < contents.size()) {
86 string line;
87 while (pos < contents.size() && contents[pos] != '\n')
88 line += contents[pos++];
89 pos++;
90
91 string::size_type hash = line.find('#');
92 if (hash != string::npos)
93 line = string(line, 0, hash);
94
95 vector<string> tokens = tokenizeString<vector<string> >(line);
96 if (tokens.empty()) continue;
97
98 if (tokens.size() < 2 || tokens[1] != "=")
99 throw Error(format("illegal configuration line `%1%' in `%2%'") % line % settingsFile);
100
101 string name = tokens[0];
102
103 vector<string>::iterator i = tokens.begin();
104 advance(i, 2);
105 settings[name] = concatStringsSep(" ", Strings(i, tokens.end())); // FIXME: slow
106 };
107}
108
109
110void Settings::set(const string & name, const string & value)
111{
112 settings[name] = value;
113 overrides[name] = value;
114}
115
116
2bb04905
LC
117string Settings::get(const string & name, const string & def)
118{
119 auto i = settings.find(name);
120 if (i == settings.end()) return def;
121 return i->second;
122}
123
124
125Strings Settings::get(const string & name, const Strings & def)
126{
127 auto i = settings.find(name);
128 if (i == settings.end()) return def;
129 return tokenizeString<Strings>(i->second);
130}
131
132
133bool Settings::get(const string & name, bool def)
134{
135 bool res = def;
136 _get(res, name);
137 return res;
138}
139
b23b4d39
ED
140int Settings::get(const string & name, int def)
141{
142 int res = def;
143 _get(res, name);
144 return res;
145}
146
2bb04905 147
36457566
LC
148void Settings::update()
149{
2bb04905
LC
150 _get(tryFallback, "build-fallback");
151 _get(maxBuildJobs, "build-max-jobs");
152 _get(buildCores, "build-cores");
153 _get(thisSystem, "system");
154 _get(maxSilentTime, "build-max-silent-time");
155 _get(buildTimeout, "build-timeout");
156 _get(reservedSize, "gc-reserved-space");
157 _get(fsyncMetadata, "fsync-metadata");
158 _get(useSQLiteWAL, "use-sqlite-wal");
159 _get(syncBeforeRegistering, "sync-before-registering");
160 _get(useSubstitutes, "build-use-substitutes");
161 _get(buildUsersGroup, "build-users-group");
162 _get(useChroot, "build-use-chroot");
163 _get(impersonateLinux26, "build-impersonate-linux-26");
164 _get(keepLog, "build-keep-log");
165 _get(compressLog, "build-compress-log");
166 _get(maxLogSize, "build-max-log-size");
167 _get(cacheFailure, "build-cache-failure");
168 _get(pollInterval, "build-poll-interval");
169 _get(checkRootReachability, "gc-check-reachability");
170 _get(gcKeepOutputs, "gc-keep-outputs");
171 _get(gcKeepDerivations, "gc-keep-derivations");
172 _get(autoOptimiseStore, "auto-optimise-store");
173 _get(envKeepDerivations, "env-keep-derivations");
174 _get(sshSubstituterHosts, "ssh-substituter-hosts");
175 _get(useSshSubstituter, "use-ssh-substituter");
176 _get(logServers, "log-servers");
177 _get(enableImportNative, "allow-unsafe-native-code-during-evaluation");
178 _get(useCaseHack, "use-case-hack");
36457566
LC
179
180 string subs = getEnv("NIX_SUBSTITUTERS", "default");
181 if (subs == "default") {
182 substituters.clear();
183#if 0
184 if (getEnv("NIX_OTHER_STORES") != "")
185 substituters.push_back(nixLibexecDir + "/nix/substituters/copy-from-other-stores.pl");
186#endif
187 substituters.push_back(nixLibexecDir + "/nix/substituters/download-using-manifests.pl");
188 substituters.push_back(nixLibexecDir + "/nix/substituters/download-from-binary-cache.pl");
189 if (useSshSubstituter)
190 substituters.push_back(nixLibexecDir + "/nix/substituters/download-via-ssh");
191 } else
192 substituters = tokenizeString<Strings>(subs, ":");
193}
194
195
2bb04905 196void Settings::_get(string & res, const string & name)
36457566
LC
197{
198 SettingsMap::iterator i = settings.find(name);
199 if (i == settings.end()) return;
200 res = i->second;
201}
202
203
2bb04905 204void Settings::_get(bool & res, const string & name)
36457566
LC
205{
206 SettingsMap::iterator i = settings.find(name);
207 if (i == settings.end()) return;
208 if (i->second == "true") res = true;
209 else if (i->second == "false") res = false;
210 else throw Error(format("configuration option `%1%' should be either `true' or `false', not `%2%'")
211 % name % i->second);
212}
213
214
2bb04905 215void Settings::_get(StringSet & res, const string & name)
36457566
LC
216{
217 SettingsMap::iterator i = settings.find(name);
218 if (i == settings.end()) return;
219 res.clear();
220 Strings ss = tokenizeString<Strings>(i->second);
221 res.insert(ss.begin(), ss.end());
222}
223
2bb04905 224void Settings::_get(Strings & res, const string & name)
36457566
LC
225{
226 SettingsMap::iterator i = settings.find(name);
227 if (i == settings.end()) return;
228 res = tokenizeString<Strings>(i->second);
229}
230
231
2bb04905 232template<class N> void Settings::_get(N & res, const string & name)
36457566
LC
233{
234 SettingsMap::iterator i = settings.find(name);
235 if (i == settings.end()) return;
236 if (!string2Int(i->second, res))
237 throw Error(format("configuration setting `%1%' should have an integer value") % name);
238}
239
240
241string Settings::pack()
242{
243 string s;
244 foreach (SettingsMap::iterator, i, settings) {
245 if (i->first.find('\n') != string::npos ||
246 i->first.find('=') != string::npos ||
247 i->second.find('\n') != string::npos)
248 throw Error("illegal option name/value");
249 s += i->first; s += '='; s += i->second; s += '\n';
250 }
251 return s;
252}
253
254
255void Settings::unpack(const string & pack) {
256 Strings lines = tokenizeString<Strings>(pack, "\n");
257 foreach (Strings::iterator, i, lines) {
258 string::size_type eq = i->find('=');
259 if (eq == string::npos)
260 throw Error("illegal option name/value");
261 set(i->substr(0, eq), i->substr(eq + 1));
262 }
263}
264
265
266Settings::SettingsMap Settings::getOverrides()
267{
268 return overrides;
269}
270
271
272const string nixVersion = PACKAGE_VERSION;
273
274
275}