globals.cc 5.7 KB

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