1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-13 05:56:03 +01:00

Merge pull request #13402 from DavHau/build-cores

build-cores: detect cores automatically if set to 0
This commit is contained in:
Sergei Zimmerman 2025-07-09 23:06:55 +03:00 committed by GitHub
commit b19e9acc03
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 58 additions and 10 deletions

View file

@ -140,7 +140,7 @@ std::vector<Path> getUserConfigFiles()
return files;
}
unsigned int Settings::getDefaultCores()
unsigned int Settings::getDefaultCores() const
{
const unsigned int concurrency = std::max(1U, std::thread::hardware_concurrency());
const unsigned int maxCPU = getMaxCPU();

View file

@ -43,8 +43,6 @@ const uint32_t maxIdsPerBuild =
class Settings : public Config {
unsigned int getDefaultCores();
StringSet getDefaultSystemFeatures();
StringSet getDefaultExtraPlatforms();
@ -57,6 +55,8 @@ public:
Settings();
unsigned int getDefaultCores() const;
Path nixPrefix;
/**
@ -153,7 +153,7 @@ public:
Setting<unsigned int> buildCores{
this,
getDefaultCores(),
0,
"cores",
R"(
Sets the value of the `NIX_BUILD_CORES` environment variable in the [invocation of the `builder` executable](@docroot@/language/derivations.md#builder-execution) of a derivation.
@ -166,15 +166,13 @@ public:
-->
For instance, in Nixpkgs, if the attribute `enableParallelBuilding` for the `mkDerivation` build helper is set to `true`, it passes the `-j${NIX_BUILD_CORES}` flag to GNU Make.
The value `0` means that the `builder` should use all available CPU cores in the system.
If set to `0`, nix will detect the number of CPU cores and pass this number via NIX_BUILD_CORES.
> **Note**
>
> The number of parallel local Nix build jobs is independently controlled with the [`max-jobs`](#conf-max-jobs) setting.
)",
{"build-cores"},
// Don't document the machine-specific default value
false};
{"build-cores"}};
/**
* Read-only mode. Don't copy stuff to the store, don't change

View file

@ -1083,7 +1083,7 @@ void DerivationBuilderImpl::initEnv()
env["NIX_STORE"] = store.storeDir;
/* The maximum number of cores to utilize for parallel building. */
env["NIX_BUILD_CORES"] = fmt("%d", settings.buildCores);
env["NIX_BUILD_CORES"] = fmt("%d", settings.buildCores ? settings.buildCores : settings.getDefaultCores());
/* In non-structured mode, set all bindings either directory in the
environment or via a file, as specified by