From e899eb6564a27d8e1a8ae94f3c601afff9cdd7b6 Mon Sep 17 00:00:00 2001 From: Antoine Viallon Date: Fri, 24 Mar 2023 23:05:57 +0100 Subject: [PATCH] libstore+nix-build: add load-limit setting and use its value for NIX_LOAD_LIMIT env var Aimed at solving issue https://github.com/NixOS/nix/issues/7091 Signed-off-by: Antoine Viallon --- src/libstore/build/local-derivation-goal.cc | 3 +++ src/libstore/daemon.cc | 2 ++ src/libstore/globals.hh | 16 ++++++++++++++++ src/nix-build/nix-build.cc | 1 + 4 files changed, 22 insertions(+) diff --git a/src/libstore/build/local-derivation-goal.cc b/src/libstore/build/local-derivation-goal.cc index 3484d204441..847cb173f17 100644 --- a/src/libstore/build/local-derivation-goal.cc +++ b/src/libstore/build/local-derivation-goal.cc @@ -1112,6 +1112,9 @@ void LocalDerivationGoal::initEnv() /* The maximum number of cores to utilize for parallel building. */ env["NIX_BUILD_CORES"] = fmt("%d", settings.buildCores); + /* The maximum average load to target for buildsystems supporting it. */ + env["NIX_LOAD_LIMIT"] = fmt("%d", settings.loadLimit); + initTmpDir(); /* Compatibility hack with Nix <= 0.7: if this is a fixed-output diff --git a/src/libstore/daemon.cc b/src/libstore/daemon.cc index 7f8b0f90533..9196c13fddd 100644 --- a/src/libstore/daemon.cc +++ b/src/libstore/daemon.cc @@ -189,6 +189,7 @@ struct ClientSettings time_t maxSilentTime; bool verboseBuild; unsigned int buildCores; + unsigned int loadLimit; bool useSubstitutes; StringMap overrides; @@ -202,6 +203,7 @@ struct ClientSettings settings.maxSilentTime = maxSilentTime; settings.verboseBuild = verboseBuild; settings.buildCores = buildCores; + settings.loadLimit = loadLimit; settings.useSubstitutes = useSubstitutes; for (auto & i : overrides) { diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh index 93086eaf8bd..07f97fae911 100644 --- a/src/libstore/globals.hh +++ b/src/libstore/globals.hh @@ -150,6 +150,22 @@ public: )", {"build-cores"}, false}; + Setting loadLimit{ + this, + getDefaultCores(), + "load-limit", + R"( + Sets the value of the `NIX_LOAD_LIMIT` environment variable in the + invocation of builders. Builders can use this variable at their + discretion to schedule jobs based on the average load of the system. + For instance, in Nixpkgs, if the derivation attribute + `enableParallelBuilding` is set to `true`, the builder passes the + `-lN` flag to GNU Make. It can be overriden using the `--max-build-load` + command line switch and defaults to `1`. The value `0`means that + the builder should ignore the average system load. + )", + {}, false}; + /* Read-only mode. Don't copy stuff to the store, don't change the database. */ bool readOnlyMode = false; diff --git a/src/nix-build/nix-build.cc b/src/nix-build/nix-build.cc index a4b3b1f968c..7cecc1d3a35 100644 --- a/src/nix-build/nix-build.cc +++ b/src/nix-build/nix-build.cc @@ -465,6 +465,7 @@ static void main_nix_build(int argc, char * * argv) env["NIX_BUILD_TOP"] = env["TMPDIR"] = env["TEMPDIR"] = env["TMP"] = env["TEMP"] = *tmp; env["NIX_STORE"] = store->storeDir; env["NIX_BUILD_CORES"] = std::to_string(settings.buildCores); + env["NIX_LOAD_LIMIT"] = std::to_string(settings.loadLimit); auto passAsFile = tokenizeString(getOr(drv.env, "passAsFile", ""));