Merge remote-tracking branch 'origin/master' into wip-texlive
[jackhill/guix/guix.git] / nix / libstore / globals.cc
1 #include "config.h"
2
3 #include "globals.hh"
4 #include "util.hh"
5 #include "archive.hh"
6
7 #include <map>
8 #include <algorithm>
9
10
11 namespace 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 build 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
22 Settings settings;
23
24
25 Settings::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;
39 multiplexedBuildOutput = false;
40 reservedSize = 8 * 1024 * 1024;
41 fsyncMetadata = true;
42 useSQLiteWAL = true;
43 syncBeforeRegistering = false;
44 useSubstitutes = true;
45 useChroot = false;
46 impersonateLinux26 = false;
47 keepLog = true;
48 #if HAVE_BZLIB_H
49 logCompression = COMPRESSION_BZIP2;
50 #else
51 logCompression = COMPRESSION_GZIP;
52 #endif
53 maxLogSize = 0;
54 cacheFailure = false;
55 pollInterval = 5;
56 checkRootReachability = false;
57 gcKeepOutputs = false;
58 gcKeepDerivations = true;
59 autoOptimiseStore = false;
60 envKeepDerivations = false;
61 lockCPU = getEnv("NIX_AFFINITY_HACK", "1") == "1";
62 showTrace = false;
63 }
64
65
66 void Settings::processEnvironment()
67 {
68 nixStore = canonPath(getEnv("NIX_STORE_DIR", getEnv("NIX_STORE", NIX_STORE_DIR)));
69 nixLogDir = canonPath(getEnv("GUIX_LOG_DIRECTORY", NIX_LOG_DIR));
70 nixStateDir = canonPath(getEnv("GUIX_STATE_DIRECTORY", NIX_STATE_DIR));
71 nixDBPath = getEnv("GUIX_DATABASE_DIRECTORY", nixStateDir + "/db");
72 nixConfDir = canonPath(getEnv("GUIX_CONFIGURATION_DIRECTORY", GUIX_CONFIGURATION_DIRECTORY));
73 nixBinDir = canonPath(getEnv("NIX_BIN_DIR", NIX_BIN_DIR));
74 nixDaemonSocketFile = canonPath(nixStateDir + DEFAULT_SOCKET_PATH);
75 guixProgram = canonPath(getEnv("GUIX", nixBinDir + "/guix"));
76 }
77
78
79 void Settings::set(const string & name, const string & value)
80 {
81 settings[name] = value;
82 overrides[name] = value;
83 }
84
85
86 string Settings::get(const string & name, const string & def)
87 {
88 auto i = settings.find(name);
89 if (i == settings.end()) return def;
90 return i->second;
91 }
92
93
94 Strings Settings::get(const string & name, const Strings & def)
95 {
96 auto i = settings.find(name);
97 if (i == settings.end()) return def;
98 return tokenizeString<Strings>(i->second);
99 }
100
101
102 bool Settings::get(const string & name, bool def)
103 {
104 bool res = def;
105 _get(res, name);
106 return res;
107 }
108
109 int Settings::get(const string & name, int def)
110 {
111 int res = def;
112 _get(res, name);
113 return res;
114 }
115
116
117 void Settings::update()
118 {
119 _get(tryFallback, "build-fallback");
120 _get(maxBuildJobs, "build-max-jobs");
121 _get(buildCores, "build-cores");
122 _get(thisSystem, "system");
123 _get(multiplexedBuildOutput, "multiplexed-build-output");
124 _get(maxSilentTime, "build-max-silent-time");
125 _get(buildTimeout, "build-timeout");
126 _get(reservedSize, "gc-reserved-space");
127 _get(fsyncMetadata, "fsync-metadata");
128 _get(useSQLiteWAL, "use-sqlite-wal");
129 _get(syncBeforeRegistering, "sync-before-registering");
130 _get(useSubstitutes, "build-use-substitutes");
131 _get(buildUsersGroup, "build-users-group");
132 _get(useChroot, "build-use-chroot");
133 _get(impersonateLinux26, "build-impersonate-linux-26");
134 _get(keepLog, "build-keep-log");
135 // _get(logCompression, "build-log-compression");
136 _get(maxLogSize, "build-max-log-size");
137 _get(cacheFailure, "build-cache-failure");
138 _get(pollInterval, "build-poll-interval");
139 _get(checkRootReachability, "gc-check-reachability");
140 _get(gcKeepOutputs, "gc-keep-outputs");
141 _get(gcKeepDerivations, "gc-keep-derivations");
142 _get(autoOptimiseStore, "auto-optimise-store");
143 _get(envKeepDerivations, "env-keep-derivations");
144 }
145
146
147 void Settings::_get(string & res, const string & name)
148 {
149 SettingsMap::iterator i = settings.find(name);
150 if (i == settings.end()) return;
151 res = i->second;
152 }
153
154
155 void Settings::_get(bool & res, const string & name)
156 {
157 SettingsMap::iterator i = settings.find(name);
158 if (i == settings.end()) return;
159 if (i->second == "true") res = true;
160 else if (i->second == "false") res = false;
161 else throw Error(format("configuration option `%1%' should be either `true' or `false', not `%2%'")
162 % name % i->second);
163 }
164
165
166 void Settings::_get(StringSet & res, const string & name)
167 {
168 SettingsMap::iterator i = settings.find(name);
169 if (i == settings.end()) return;
170 res.clear();
171 Strings ss = tokenizeString<Strings>(i->second);
172 res.insert(ss.begin(), ss.end());
173 }
174
175 void Settings::_get(Strings & res, const string & name)
176 {
177 SettingsMap::iterator i = settings.find(name);
178 if (i == settings.end()) return;
179 res = tokenizeString<Strings>(i->second);
180 }
181
182
183 template<class N> void Settings::_get(N & res, const string & name)
184 {
185 SettingsMap::iterator i = settings.find(name);
186 if (i == settings.end()) return;
187 if (!string2Int(i->second, res))
188 throw Error(format("configuration setting `%1%' should have an integer value") % name);
189 }
190
191
192 string Settings::pack()
193 {
194 string s;
195 foreach (SettingsMap::iterator, i, settings) {
196 if (i->first.find('\n') != string::npos ||
197 i->first.find('=') != string::npos ||
198 i->second.find('\n') != string::npos)
199 throw Error("invalid option name/value");
200 s += i->first; s += '='; s += i->second; s += '\n';
201 }
202 return s;
203 }
204
205
206 Settings::SettingsMap Settings::getOverrides()
207 {
208 return overrides;
209 }
210
211
212 const string nixVersion = PACKAGE_VERSION;
213
214
215 }