globals.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. build 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. multiplexedBuildOutput = false;
  30. reservedSize = 8 * 1024 * 1024;
  31. fsyncMetadata = true;
  32. useSQLiteWAL = true;
  33. syncBeforeRegistering = false;
  34. useSubstitutes = true;
  35. useChroot = 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. }
  54. void Settings::processEnvironment()
  55. {
  56. nixStore = canonPath(getEnv("NIX_STORE_DIR", getEnv("NIX_STORE", NIX_STORE_DIR)));
  57. nixLogDir = canonPath(getEnv("GUIX_LOG_DIRECTORY", NIX_LOG_DIR));
  58. nixStateDir = canonPath(getEnv("GUIX_STATE_DIRECTORY", NIX_STATE_DIR));
  59. nixDBPath = getEnv("GUIX_DATABASE_DIRECTORY", nixStateDir + "/db");
  60. nixConfDir = canonPath(getEnv("GUIX_CONFIGURATION_DIRECTORY", GUIX_CONFIGURATION_DIRECTORY));
  61. nixBinDir = canonPath(getEnv("NIX_BIN_DIR", NIX_BIN_DIR));
  62. nixDaemonSocketFile = canonPath(nixStateDir + DEFAULT_SOCKET_PATH);
  63. guixProgram = canonPath(getEnv("GUIX", nixBinDir + "/guix"));
  64. }
  65. void Settings::set(const string & name, const string & value)
  66. {
  67. settings[name] = value;
  68. overrides[name] = value;
  69. }
  70. string Settings::get(const string & name, const string & def)
  71. {
  72. auto i = settings.find(name);
  73. if (i == settings.end()) return def;
  74. return i->second;
  75. }
  76. Strings Settings::get(const string & name, const Strings & def)
  77. {
  78. auto i = settings.find(name);
  79. if (i == settings.end()) return def;
  80. return tokenizeString<Strings>(i->second);
  81. }
  82. bool Settings::get(const string & name, bool def)
  83. {
  84. bool res = def;
  85. _get(res, name);
  86. return res;
  87. }
  88. int Settings::get(const string & name, int def)
  89. {
  90. int res = def;
  91. _get(res, name);
  92. return res;
  93. }
  94. void Settings::update()
  95. {
  96. _get(tryFallback, "build-fallback");
  97. _get(maxBuildJobs, "build-max-jobs");
  98. _get(buildCores, "build-cores");
  99. _get(thisSystem, "system");
  100. _get(multiplexedBuildOutput, "multiplexed-build-output");
  101. _get(maxSilentTime, "build-max-silent-time");
  102. _get(buildTimeout, "build-timeout");
  103. _get(reservedSize, "gc-reserved-space");
  104. _get(fsyncMetadata, "fsync-metadata");
  105. _get(useSQLiteWAL, "use-sqlite-wal");
  106. _get(syncBeforeRegistering, "sync-before-registering");
  107. _get(useSubstitutes, "build-use-substitutes");
  108. _get(buildUsersGroup, "build-users-group");
  109. _get(useChroot, "build-use-chroot");
  110. _get(impersonateLinux26, "build-impersonate-linux-26");
  111. _get(keepLog, "build-keep-log");
  112. // _get(logCompression, "build-log-compression");
  113. _get(maxLogSize, "build-max-log-size");
  114. _get(cacheFailure, "build-cache-failure");
  115. _get(pollInterval, "build-poll-interval");
  116. _get(checkRootReachability, "gc-check-reachability");
  117. _get(gcKeepOutputs, "gc-keep-outputs");
  118. _get(gcKeepDerivations, "gc-keep-derivations");
  119. _get(autoOptimiseStore, "auto-optimise-store");
  120. _get(envKeepDerivations, "env-keep-derivations");
  121. }
  122. void Settings::_get(string & res, const string & name)
  123. {
  124. SettingsMap::iterator i = settings.find(name);
  125. if (i == settings.end()) return;
  126. res = i->second;
  127. }
  128. void Settings::_get(bool & res, const string & name)
  129. {
  130. SettingsMap::iterator i = settings.find(name);
  131. if (i == settings.end()) return;
  132. if (i->second == "true") res = true;
  133. else if (i->second == "false") res = false;
  134. else throw Error(format("configuration option `%1%' should be either `true' or `false', not `%2%'")
  135. % name % i->second);
  136. }
  137. void Settings::_get(StringSet & res, const string & name)
  138. {
  139. SettingsMap::iterator i = settings.find(name);
  140. if (i == settings.end()) return;
  141. res.clear();
  142. Strings ss = tokenizeString<Strings>(i->second);
  143. res.insert(ss.begin(), ss.end());
  144. }
  145. void Settings::_get(Strings & res, const string & name)
  146. {
  147. SettingsMap::iterator i = settings.find(name);
  148. if (i == settings.end()) return;
  149. res = tokenizeString<Strings>(i->second);
  150. }
  151. template<class N> void Settings::_get(N & res, const string & name)
  152. {
  153. SettingsMap::iterator i = settings.find(name);
  154. if (i == settings.end()) return;
  155. if (!string2Int(i->second, res))
  156. throw Error(format("configuration setting `%1%' should have an integer value") % name);
  157. }
  158. string Settings::pack()
  159. {
  160. string s;
  161. foreach (SettingsMap::iterator, i, settings) {
  162. if (i->first.find('\n') != string::npos ||
  163. i->first.find('=') != string::npos ||
  164. i->second.find('\n') != string::npos)
  165. throw Error("invalid option name/value");
  166. s += i->first; s += '='; s += i->second; s += '\n';
  167. }
  168. return s;
  169. }
  170. Settings::SettingsMap Settings::getOverrides()
  171. {
  172. return overrides;
  173. }
  174. const string nixVersion = PACKAGE_VERSION;
  175. }