diff --git a/nix.conf.example b/nix.conf.example index e17cf3c25f..6175d86149 100644 --- a/nix.conf.example +++ b/nix.conf.example @@ -59,6 +59,18 @@ #build-max-jobs = 1 +### Option `build-cores' +# +# This option defines the number of CPU cores to utilize in parallel +# within a build job, i.e. by passing an appropriate `-jN' flag to +# GNU make. The default is 1, meaning that parallel building within +# jobs is disabled. Passing the special values `0', `auto', or +# `guess' causes Nix to try and auto-detect the number of available +# cores on the local host. This setting can be overridden using the +# `--cores' command line switch. +#build-cores = 1 + + ### Option `build-max-silent-time' # # This option defines the maximum number of seconds that a builder can diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index 3fbec4b524..19aa1e71cc 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -135,6 +135,12 @@ static void initAndRun(int argc, char * * argv) /* Get some settings from the configuration file. */ thisSystem = querySetting("system", SYSTEM); maxBuildJobs = queryIntSetting("build-max-jobs", 1); + string tmp = querySetting("build-cores", "/UNDEFINED"); + std::transform(tmp.begin(), tmp.end(), tmp.begin(), tolower); + if (tmp == "auto" || tmp == "guess") + buildCores = 0; + else + buildCores = queryIntSetting("build-cores", 1); maxSilentTime = queryIntSetting("build-max-silent-time", 0); /* Catch SIGINT. */ @@ -226,6 +232,14 @@ static void initAndRun(int argc, char * * argv) tryFallback = true; else if (arg == "--max-jobs" || arg == "-j") maxBuildJobs = getIntArg(arg, i, args.end()); + else if (arg == "--cores") { + string tmp = *(++i); + std::transform(tmp.begin(), tmp.end(), tmp.begin(), tolower); + if (tmp == "auto" || tmp == "guess") + buildCores = 0u; + else + buildCores = getIntArg(arg, --i, args.end()); + } else if (arg == "--readonly-mode") readOnlyMode = true; else if (arg == "--max-silent-time") diff --git a/src/libstore/build.cc b/src/libstore/build.cc index f901c1f7d9..a53c23bf44 100644 --- a/src/libstore/build.cc +++ b/src/libstore/build.cc @@ -1411,6 +1411,9 @@ void DerivationGoal::startBuilder() in the store or in the build directory). */ env["NIX_STORE"] = nixStore; + /* The maximum number of cores to utilize for parallel building. */ + env["NIX_BUILD_CORES"] = (format("%d") % buildCores).str(); + /* Add all bindings specified in the derivation. */ foreach (StringPairs::iterator, i, drv.env) env[i->first] = i->second; diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index cc0e44e8e3..75d2f69c2b 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -22,6 +22,7 @@ bool keepGoing = false; bool tryFallback = false; Verbosity buildVerbosity = lvlInfo; unsigned int maxBuildJobs = 1; +unsigned int buildCores = 1; bool readOnlyMode = false; string thisSystem = "unset"; time_t maxSilentTime = 0; diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh index d3388e309c..a74a741d67 100644 --- a/src/libstore/globals.hh +++ b/src/libstore/globals.hh @@ -55,6 +55,11 @@ extern Verbosity buildVerbosity; /* Maximum number of parallel build jobs. 0 means unlimited. */ extern unsigned int maxBuildJobs; +/* Number of CPU cores to utilize in parallel within a build, i.e. by passing + this number to Make via '-j'. 0 means that the number of actual CPU cores on + the local host ought to be auto-detected. */ +extern unsigned int buildCores; + /* Read-only mode. Don't copy stuff to the store, don't change the database. */ extern bool readOnlyMode;