globals.cc 8.0 KB

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