Compare commits

..
Author SHA1 Message Date
John Ericson 6f867cce2c Move cgroup functionality inside nix::linux namespace
This is what we do for other OS-specific functionality.
2026-02-17 14:58:19 -05:00
John Ericson eb826a5a03 Factor out chroot logic from ChrootDerivationBuilder into its own file
This shrinks the big derivation builder compilation unit, and hopefully
puts on on the road to being able to do all derivation builder cleanup
with RAII.
2026-02-17 14:58:19 -05:00
36 changed files with 319 additions and 302 deletions

No files matched your search

+1 -1
View File
@@ -11,7 +11,7 @@
namespace nix::eval_cache {
CachedEvalError::CachedEvalError(ref<AttrCursor> cursor, Symbol attr)
: CloneableError(cursor->root->state, "cached failure of attribute '%s'", cursor->getAttrPathStr(attr))
: EvalError(cursor->root->state, "cached failure of attribute '%s'", cursor->getAttrPathStr(attr))
, cursor(cursor)
, attr(attr)
{
+1 -1
View File
@@ -14,7 +14,7 @@ namespace nix::eval_cache {
struct AttrDb;
class AttrCursor;
struct CachedEvalError : CloneableError<CachedEvalError, EvalError>
struct CachedEvalError : EvalError
{
const ref<AttrCursor> cursor;
const Symbol attr;
+7 -7
View File
@@ -18,7 +18,7 @@ class EvalErrorBuilder;
*
* Most subclasses should inherit from `EvalError` instead of this class.
*/
class EvalBaseError : public CloneableError<EvalBaseError, Error>
class EvalBaseError : public Error
{
template<class T>
friend class EvalErrorBuilder;
@@ -26,14 +26,14 @@ public:
EvalState & state;
EvalBaseError(EvalState & state, ErrorInfo && errorInfo)
: CloneableError(errorInfo)
: Error(errorInfo)
, state(state)
{
}
template<typename... Args>
explicit EvalBaseError(EvalState & state, const std::string & formatString, const Args &... formatArgs)
: CloneableError(formatString, formatArgs...)
: Error(formatString, formatArgs...)
, state(state)
{
}
@@ -60,23 +60,23 @@ MakeError(InfiniteRecursionError, EvalError);
* Inherits from EvalBaseError (not EvalError) because resource exhaustion
* should not be cached.
*/
struct StackOverflowError : public CloneableError<StackOverflowError, EvalBaseError>
struct StackOverflowError : public EvalBaseError
{
StackOverflowError(EvalState & state)
: CloneableError(state, "stack overflow; max-call-depth exceeded")
: EvalBaseError(state, "stack overflow; max-call-depth exceeded")
{
}
};
MakeError(IFDError, EvalBaseError);
struct InvalidPathError : public CloneableError<InvalidPathError, EvalError>
struct InvalidPathError : public EvalError
{
public:
Path path;
InvalidPathError(EvalState & state, const Path & path)
: CloneableError(state, "path '%s' is not valid", path)
: EvalError(state, "path '%s' is not valid", path)
{
}
};
@@ -9,14 +9,14 @@
namespace nix {
class BadNixStringContextElem final : public CloneableError<BadNixStringContextElem, Error>
class BadNixStringContextElem : public Error
{
public:
std::string_view raw;
template<typename... Args>
BadNixStringContextElem(std::string_view raw_, const Args &... args)
: CloneableError("")
: Error("")
{
raw = raw_;
auto hf = HintFmt(args...);
+5 -6
View File
@@ -1312,12 +1312,11 @@ static void prim_warn(EvalState & state, const PosIdx pos, Value ** args, Value
state.forceString(*args[0], pos, "while evaluating the first argument; the message passed to builtins.warn");
{
ErrorInfo info{
.level = lvlWarn,
.msg = HintFmt(std::string(msgStr)),
.pos = state.positions[pos],
.isFromExpr = true,
};
BaseError msg(std::string{msgStr});
msg.atPos(state.positions[pos]);
auto info = msg.info();
info.level = lvlWarn;
info.isFromExpr = true;
logWarning(info);
}
+2 -2
View File
@@ -74,11 +74,11 @@ namespace nix {
struct GitSourceAccessor;
struct GitError final : public CloneableError<GitError, Error>
struct GitError : public Error
{
template<typename... Ts>
GitError(const git_error & error, Ts &&... args)
: CloneableError("")
: Error("")
{
auto hf = HintFmt(std::forward<Ts>(args)...);
err.msg = HintFmt("%1%: %2% (libgit2 error code = %3%)", Uncolored(hf.str()), error.message, error.klass);
+1 -1
View File
@@ -28,7 +28,7 @@
namespace nix {
AwsAuthError::AwsAuthError(int errorCode)
: CloneableError("AWS authentication error: '%s' (%d)", aws_error_str(errorCode), errorCode)
: Error("AWS authentication error: '%s' (%d)", aws_error_str(errorCode), errorCode)
, errorCode(errorCode)
{
}
+65 -120
View File
@@ -362,17 +362,25 @@ Goal::Co DerivationBuildingGoal::tryToBuild(StorePathSet inputPaths)
return LocalBuildCapability{*localStoreP, ext};
}();
auto acquireResources = [&](bool & done, PathLocks & outputLocks) -> Goal::Co {
/**
* Activity that denotes waiting for a lock.
*/
std::unique_ptr<Activity> actLock;
/**
* Locks on (fixed) output paths.
*/
PathLocks outputLocks;
while (true) {
trace("trying to build");
/**
* Output paths to acquire locks on, if known a priori.
*
* The locks are automatically released when the caller's `PathLocks` goes
* out of scope, including on exception unwinding. If we can't acquire the lock, then
* continue; hopefully some other goal can start a build, and if not, the
* main loop will sleep a few seconds and then retry this goal.
*/
/* Obtain locks on all output paths, if the paths are known a priori.
The locks are automatically released when we exit this function or Nix
crashes. If we can't acquire the lock, then continue; hopefully some
other goal can start a build, and if not, the main loop will sleep a few
seconds and then retry this goal. */
std::set<std::filesystem::path> lockFiles;
/* FIXME: Should lock something like the drv itself so we don't build same
CA drv concurrently */
@@ -416,8 +424,7 @@ Goal::Co DerivationBuildingGoal::tryToBuild(StorePathSet inputPaths)
debug("skipping build of derivation '%s', someone beat us to it", worker.store.printStorePath(drvPath));
outputLocks.setDeletion(true);
outputLocks.unlock();
done = true;
co_return Return{};
co_return doneSuccess(BuildResult::Success::AlreadyValid, std::move(validOutputs));
}
/* If any of the outputs already exist but are not valid, delete
@@ -432,133 +439,71 @@ Goal::Co DerivationBuildingGoal::tryToBuild(StorePathSet inputPaths)
}
}
co_return Return{};
};
// Check and repair modes are only supported for local builds.
if (buildMode != bmNormal) {
// can try hook: false
// can try local: true
// preference: doesn't matter because it's only one option
break;
} else {
// can try hook: true
// can try local: true
// preference: unknown
auto tryHookLoop = [&](bool & valid) -> Goal::Co {
{
PathLocks outputLocks;
co_await acquireResources(valid, outputLocks);
if (valid)
co_return doneSuccess(BuildResult::Success::AlreadyValid, checkPathValidity(initialOutputs).second);
if (drvOptions.preferLocalBuild) {
// preference: local
if (std::holds_alternative<LocalBuildCapability>(localBuildResult)) {
// Can build locally as preferred, so do it.
break;
}
// We cannot build locally, but we can maybe do the hook.
}
// If we don't prefer a local build, OR we do but the local build was rejected, then we try the hook if
// possible.
switch (tryBuildHook(drvOptions)) {
case rpAccept:
/* Yes, it has started doing so. Wait until we get
EOF from the hook. */
valid = true;
actLock.reset();
co_return buildWithHook(
std::move(inputPaths), std::move(initialOutputs), std::move(drvOptions), std::move(outputLocks));
case rpDecline:
// We should do it ourselves.
co_return Return{};
break;
case rpPostpone:
/* Not now; wait until at least one child finishes or
the wake-up timeout expires. */
break;
}
}
PathLocks outputLocks;
{
// First attempt was postponed. Retry in a loop with an activity
// that lives until accept or decline.
Activity act(
*logger,
lvlWarn,
actBuildWaiting,
fmt("waiting for a machine to build '%s'", Magenta(worker.store.printStorePath(drvPath))));
while (true) {
if (!actLock)
actLock = std::make_unique<Activity>(
*logger,
lvlWarn,
actBuildWaiting,
fmt("waiting for a machine to build '%s'", Magenta(worker.store.printStorePath(drvPath))));
outputLocks.unlock();
co_await waitForAWhile();
co_await acquireResources(valid, outputLocks);
if (valid)
break;
switch (tryBuildHook(drvOptions)) {
case rpAccept:
/* Yes, it has started doing so. Wait until we get
EOF from the hook. */
break;
case rpPostpone:
/* Not now; wait until at least one child finishes or
the wake-up timeout expires. */
outputLocks.unlock();
continue;
case rpDecline:
// We should do it ourselves.
co_return Return{};
}
continue;
case rpDecline:
// We should do it ourselves.
break;
}
}
if (valid) {
co_return doneSuccess(BuildResult::Success::AlreadyValid, checkPathValidity(initialOutputs).second);
} else {
co_return buildWithHook(
std::move(inputPaths), std::move(initialOutputs), std::move(drvOptions), std::move(outputLocks));
}
};
auto tryBuildLocally = [&](bool & valid) -> Goal::Co {
if (auto * cap = std::get_if<LocalBuildCapability>(&localBuildResult)) {
PathLocks outputLocks;
co_await acquireResources(valid, outputLocks);
if (valid)
co_return doneSuccess(BuildResult::Success::AlreadyValid, checkPathValidity(initialOutputs).second);
valid = true;
co_return buildLocally(
*cap, std::move(inputPaths), std::move(initialOutputs), std::move(drvOptions), std::move(outputLocks));
}
co_return Return{};
};
if (buildMode != bmNormal) {
// Check and repair modes operate on the state of this store specifically,
// so they must always build locally.
bool valid = false;
co_await tryBuildLocally(valid);
if (valid)
co_return Return{};
} else if (drvOptions.preferLocalBuild) {
// Local is preferred, so try it first. If it's not available, fall back to the hook.
{
bool valid = false;
co_await tryBuildLocally(valid);
if (valid)
co_return Return{};
}
{
bool valid = false;
co_await tryHookLoop(valid);
if (valid)
co_return Return{};
}
} else {
// Default preference is a remote build: they tend to be faster and preserve local
// resources for other tasks. Fall back to local if no remote is available.
{
bool valid = false;
co_await tryHookLoop(valid);
if (valid)
co_return Return{};
}
{
bool valid = false;
co_await tryBuildLocally(valid);
if (valid)
co_return Return{};
}
break;
}
std::string storePath = worker.store.printStorePath(drvPath);
auto * rejection = std::get_if<LocalBuildRejection>(&localBuildResult);
assert(rejection);
co_return doneFailure(reject(*rejection, storePath));
// can do hook: false (either we can't try, or we tried and were declined)
// can try local: true
// preference: doesn't matter
actLock.reset();
if (auto * cap = std::get_if<LocalBuildCapability>(&localBuildResult)) {
co_return buildLocally(
*cap, std::move(inputPaths), std::move(initialOutputs), std::move(drvOptions), std::move(outputLocks));
} else {
outputLocks.unlock();
std::string storePath = worker.store.printStorePath(drvPath);
co_return doneFailure(reject(std::get<LocalBuildRejection>(localBuildResult), storePath));
}
}
Goal::Co DerivationBuildingGoal::buildWithHook(
+2 -2
View File
@@ -1,11 +1,11 @@
#include "nix/store/build/goal.hh"
#include "nix/store/build/worker.hh"
#include "nix/store/worker-settings.hh"
#include "nix/store/globals.hh"
namespace nix {
TimedOut::TimedOut(time_t maxDuration)
: CloneableError(BuildResult::Failure::TimedOut, "timed out after %1% seconds", maxDuration)
: BuildError(BuildResult::Failure::TimedOut, "timed out after %1% seconds", maxDuration)
, maxDuration(maxDuration)
{
}
+3 -3
View File
@@ -60,12 +60,12 @@ namespace {
using curlSList = std::unique_ptr<::curl_slist, decltype([](::curl_slist * list) { ::curl_slist_free_all(list); })>;
using curlMulti = std::unique_ptr<::CURLM, decltype([](::CURLM * multi) { ::curl_multi_cleanup(multi); })>;
struct curlMultiError final : CloneableError<curlMultiError, Error>
struct curlMultiError : Error
{
::CURLMcode code;
curlMultiError(::CURLMcode code)
: CloneableError{"unexpected curl multi error: %s", ::curl_multi_strerror(code)}
: Error{"unexpected curl multi error: %s", ::curl_multi_strerror(code)}
{
assert(code != CURLM_OK);
}
@@ -1212,7 +1212,7 @@ void FileTransfer::download(
template<typename... Args>
FileTransferError::FileTransferError(
FileTransfer::Error error, std::optional<std::string> response, const Args &... args)
: CloneableError(args...)
: Error(args...)
, error(error)
, response(response)
{
+2 -3
View File
@@ -34,13 +34,12 @@ struct AwsCredentials
}
};
class AwsAuthError final : public CloneableError<AwsAuthError, Error>
class AwsAuthError : public Error
{
std::optional<int> errorCode;
public:
using CloneableError::CloneableError;
using Error::Error;
AwsAuthError(int errorCode);
std::optional<int> getErrorCode() const
@@ -58,7 +58,7 @@ enum struct BuildResultFailureStatus : uint8_t {
* This is both an exception type (inherits from Error) and serves as
* the failure variant in BuildResult::inner.
*/
struct BuildError : public CloneableError<BuildError, Error>
struct BuildError : public Error
{
using Status = BuildResultFailureStatus;
using enum Status;
@@ -80,7 +80,7 @@ public:
*/
template<typename... Args>
BuildError(Status status, const Args &... args)
: CloneableError(args...)
: Error(args...)
, status{status}
{
}
@@ -97,7 +97,7 @@ public:
* Also used for deserialization.
*/
BuildError(Args args)
: CloneableError(std::move(args.msg))
: Error(std::move(args.msg))
, status{args.status}
, isNonDeterministic{args.isNonDeterministic}
@@ -108,7 +108,7 @@ public:
* Default constructor for deserialization.
*/
BuildError()
: CloneableError("")
: Error("")
{
}
@@ -20,14 +20,14 @@ namespace nix {
* Denotes a build failure that stemmed from the builder exiting with a
* failing exist status.
*/
struct BuilderFailureError final : CloneableError<BuilderFailureError, BuildError>
struct BuilderFailureError : BuildError
{
int builderStatus;
std::string extraMsgAfter;
BuilderFailureError(BuildResult::Failure::Status status, int builderStatus, std::string extraMsgAfter)
: CloneableError{
: BuildError{
status,
/* No message for now, because the caller will make for
us, with extra context */
+1 -1
View File
@@ -10,7 +10,7 @@
namespace nix {
struct TimedOut final : CloneableError<TimedOut, BuildError>
struct TimedOut : BuildError
{
time_t maxDuration;
@@ -22,7 +22,7 @@ struct Package
}
};
class BuildEnvFileConflictError final : public CloneableError<BuildEnvFileConflictError, Error>
class BuildEnvFileConflictError : public Error
{
public:
const Path fileA;
@@ -30,7 +30,7 @@ public:
int priority;
BuildEnvFileConflictError(const Path fileA, const Path fileB, int priority)
: CloneableError(
: Error(
"Unable to build profile. There is a conflict for the following files:\n"
"\n"
" %1%\n"
@@ -403,7 +403,7 @@ ref<FileTransfer> getFileTransfer();
*/
ref<FileTransfer> makeFileTransfer(const FileTransferSettings & settings = fileTransferSettings);
class FileTransferError final : public CloneableError<FileTransferError, Error>
class FileTransferError : public Error
{
public:
FileTransfer::Error error;
@@ -146,7 +146,7 @@ struct RealisedPath
auto operator<=>(const RealisedPath &) const = default;
};
class MissingRealisation final : public CloneableError<MissingRealisation, Error>
class MissingRealisation : public Error
{
public:
MissingRealisation(DrvOutput & outputId)
@@ -155,7 +155,7 @@ public:
}
MissingRealisation(std::string_view drv, OutputName outputName)
: CloneableError(
: Error(
"cannot operate on output '%s' of the "
"unbuilt derivation '%s'",
outputName,
+1 -1
View File
@@ -166,7 +166,7 @@ struct SQLiteTxn
~SQLiteTxn();
};
struct SQLiteError : CloneableError<SQLiteError, Error>
struct SQLiteError : Error
{
std::string path;
std::string errMsg;
+1 -1
View File
@@ -17,7 +17,7 @@ namespace nix {
SQLiteError::SQLiteError(
const char * path, const char * errMsg, int errNo, int extendedErrNo, int offset, HintFmt && hf)
: CloneableError("")
: Error("")
, path(path)
, errMsg(errMsg)
, errNo(errNo)
+2 -2
View File
@@ -18,11 +18,11 @@ static std::string parsePublicHostKey(std::string_view host, std::string_view ss
}
}
class InvalidSSHAuthority final : public CloneableError<InvalidSSHAuthority, Error>
class InvalidSSHAuthority : public Error
{
public:
InvalidSSHAuthority(const ParsedURL::Authority & authority, std::string_view reason)
: CloneableError("invalid SSH authority: '%s': %s", authority.to_string(), reason)
: Error("invalid SSH authority: '%s': %s", authority.to_string(), reason)
{
}
};
@@ -1,5 +1,7 @@
#ifdef __linux__
# include "chroot.hh"
namespace nix {
struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
@@ -11,14 +13,14 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
}
/**
* The root of the chroot environment.
* The chroot root directory.
*/
std::filesystem::path chrootRootDir;
/**
* RAII object to delete the chroot directory.
* RAII cleanup for the chroot directory.
*/
std::shared_ptr<AutoDelete> autoDelChroot;
std::optional<AutoDelete> autoDelChroot;
PathsInChroot pathsInChroot;
@@ -54,78 +56,7 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
void prepareSandbox() override
{
/* Create a temporary directory in which we set up the chroot
environment using bind-mounts. We put it in the Nix store
so that the build outputs can be moved efficiently from the
chroot to their final location. */
std::filesystem::path chrootParentDir = store.toRealPath(drvPath) + ".chroot";
deletePath(chrootParentDir);
/* Clean up the chroot directory automatically. */
autoDelChroot = std::make_shared<AutoDelete>(chrootParentDir);
printMsg(lvlChatty, "setting up chroot environment in %1%", PathFmt(chrootParentDir));
if (mkdir(chrootParentDir.c_str(), 0700) == -1)
throw SysError("cannot create %s", PathFmt(chrootRootDir));
chrootRootDir = chrootParentDir / "root";
if (mkdir(chrootRootDir.c_str(), buildUser && buildUser->getUIDCount() != 1 ? 0755 : 0750) == -1)
throw SysError("cannot create %1%", PathFmt(chrootRootDir));
if (buildUser
&& chown(
chrootRootDir.c_str(), buildUser->getUIDCount() != 1 ? buildUser->getUID() : 0, buildUser->getGID())
== -1)
throw SysError("cannot change ownership of %1%", PathFmt(chrootRootDir));
/* Create a writable /tmp in the chroot. Many builders need
this. (Of course they should really respect $TMPDIR
instead.) */
std::filesystem::path chrootTmpDir = chrootRootDir / "tmp";
createDirs(chrootTmpDir);
chmod(chrootTmpDir, 01777);
/* Create a /etc/passwd with entries for the build user and the
nobody account. The latter is kind of a hack to support
Samba-in-QEMU. */
createDirs(chrootRootDir / "etc");
if (drvOptions.useUidRange(drv))
chownToBuilder(chrootRootDir / "etc");
if (drvOptions.useUidRange(drv) && (!buildUser || buildUser->getUIDCount() < 65536))
throw Error(
"feature 'uid-range' requires the setting '%s' to be enabled",
store.config->getLocalSettings().autoAllocateUids.name);
/* Declare the build user's group so that programs get a consistent
view of the system (e.g., "id -gn"). */
writeFile(
chrootRootDir / "etc" / "group",
fmt("root:x:0:\n"
"nixbld:!:%1%:\n"
"nogroup:x:65534:\n",
sandboxGid()));
/* Create /etc/hosts with localhost entry. */
if (derivationType.isSandboxed())
writeFile(chrootRootDir / "etc" / "hosts", "127.0.0.1 localhost\n::1 localhost\n");
/* Make the closure of the inputs available in the chroot,
rather than the whole Nix store. This prevents any access
to undeclared dependencies. Directories are bind-mounted,
while other inputs are hard-linked (since only directories
can be bind-mounted). !!! As an extra security
precaution, make the fake Nix store only writable by the
build user. */
std::filesystem::path chrootStoreDir = chrootRootDir / std::filesystem::path(store.storeDir).relative_path();
createDirs(chrootStoreDir);
chmod(chrootStoreDir, 01775);
if (buildUser && chown(chrootStoreDir.c_str(), 0, buildUser->getGID()) == -1)
throw SysError("cannot change ownership of %1%", PathFmt(chrootStoreDir));
// Start with the default sandbox paths
pathsInChroot = getPathsInSandbox();
for (auto & i : inputPaths) {
@@ -147,6 +78,22 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
if (i.second.second)
pathsInChroot.erase(store.printStorePath(*i.second.second));
}
// Set up chroot parameters
BuildChrootParams params{
.chrootParentDir = store.toRealPath(drvPath) + ".chroot",
.useUidRange = drvOptions.useUidRange(drv),
.isSandboxed = derivationType.isSandboxed(),
.buildUser = buildUser.get(),
.storeDir = store.storeDir,
.chownToBuilder = [this](const std::filesystem::path & path) { this->chownToBuilder(path); },
.getSandboxGid = [this]() { return this->sandboxGid(); },
};
// Create the chroot
auto [rootDir, cleanup] = setupBuildChroot(params);
chrootRootDir = std::move(rootDir);
autoDelChroot.emplace(std::move(cleanup));
}
Strings getPreBuildHookArgs() override
@@ -166,21 +113,23 @@ struct ChrootDerivationBuilder : virtual DerivationBuilderImpl
{
DerivationBuilderImpl::cleanupBuild(force);
/* Move paths out of the chroot for easier debugging of
build failures. */
if (!force && buildMode == bmNormal)
for (auto & [_, status] : initialOutputs) {
if (!status.known)
continue;
if (buildMode != bmCheck && status.known->isValid())
continue;
std::filesystem::path p = store.toRealPath(status.known->path);
std::filesystem::path chrootPath = chrootRootDir / p.relative_path();
if (pathExists(chrootPath))
std::filesystem::rename(chrootPath, p);
}
if (autoDelChroot) {
/* Move paths out of the chroot for easier debugging of
build failures. */
if (!force && buildMode == bmNormal)
for (auto & [_, status] : initialOutputs) {
if (!status.known)
continue;
if (buildMode != bmCheck && status.known->isValid())
continue;
std::filesystem::path p = store.toRealPath(status.known->path);
std::filesystem::path chrootPath = chrootRootDir / p.relative_path();
if (pathExists(chrootPath))
std::filesystem::rename(chrootPath, p);
}
autoDelChroot.reset(); /* this runs the destructor */
autoDelChroot.reset();
}
}
std::pair<std::filesystem::path, std::filesystem::path> addDependencyPrep(const StorePath & path)
+87
View File
@@ -0,0 +1,87 @@
#include "chroot.hh"
#include "nix/util/file-system.hh"
#include "nix/store/globals.hh"
#include "nix/util/logging.hh"
#include "nix/util/error.hh"
#include <sys/stat.h>
namespace nix {
std::pair<std::filesystem::path, AutoDelete> setupBuildChroot(const BuildChrootParams & params)
{
/* Create a temporary directory in which we set up the chroot
environment using bind-mounts. We put it in the Nix store
so that the build outputs can be moved efficiently from the
chroot to their final location. */
auto chrootParentDir = params.chrootParentDir;
deletePath(chrootParentDir);
printMsg(lvlChatty, "setting up chroot environment in %1%", PathFmt(chrootParentDir));
if (mkdir(chrootParentDir.c_str(), 0700) == -1)
throw SysError("cannot create %s", PathFmt(chrootParentDir));
std::filesystem::path chrootRootDir = chrootParentDir / "root";
if (mkdir(chrootRootDir.c_str(), params.buildUser && params.buildUser->getUIDCount() != 1 ? 0755 : 0750) == -1)
throw SysError("cannot create %1%", PathFmt(chrootRootDir));
if (params.buildUser
&& chown(
chrootRootDir.c_str(),
params.buildUser->getUIDCount() != 1 ? params.buildUser->getUID() : 0,
params.buildUser->getGID())
== -1)
throw SysError("cannot change ownership of %1%", PathFmt(chrootRootDir));
/* Create a writable /tmp in the chroot. Many builders need
this. (Of course they should really respect $TMPDIR
instead.) */
std::filesystem::path chrootTmpDir = chrootRootDir / "tmp";
createDirs(chrootTmpDir);
chmod(chrootTmpDir, 01777);
/* Create a /etc/passwd with entries for the build user and the
nobody account. The latter is kind of a hack to support
Samba-in-QEMU. */
createDirs(chrootRootDir / "etc");
if (params.useUidRange)
params.chownToBuilder(chrootRootDir / "etc");
if (params.useUidRange && (!params.buildUser || params.buildUser->getUIDCount() < 65536))
throw Error(
"feature 'uid-range' requires the setting '%s' to be enabled",
settings.getLocalSettings().autoAllocateUids.name);
/* Declare the build user's group so that programs get a consistent
view of the system (e.g., "id -gn"). */
writeFile(
chrootRootDir / "etc" / "group",
fmt("root:x:0:\n"
"nixbld:!:%1%:\n"
"nogroup:x:65534:\n",
params.getSandboxGid()));
/* Create /etc/hosts with localhost entry. */
if (params.isSandboxed)
writeFile(chrootRootDir / "etc" / "hosts", "127.0.0.1 localhost\n::1 localhost\n");
/* Make the closure of the inputs available in the chroot,
rather than the whole Nix store. This prevents any access
to undeclared dependencies. Directories are bind-mounted,
while other inputs are hard-linked (since only directories
can be bind-mounted). !!! As an extra security
precaution, make the fake Nix store only writable by the
build user. */
std::filesystem::path chrootStoreDir = chrootRootDir / std::filesystem::path(params.storeDir).relative_path();
createDirs(chrootStoreDir);
chmod(chrootStoreDir, 01775);
if (params.buildUser && chown(chrootStoreDir.c_str(), 0, params.buildUser->getGID()) == -1)
throw SysError("cannot change ownership of %1%", PathFmt(chrootStoreDir));
return {std::move(chrootRootDir), AutoDelete(chrootParentDir)};
}
} // namespace nix
+55
View File
@@ -0,0 +1,55 @@
#pragma once
///@file
#include <filesystem>
#include <map>
#include <memory>
#include <functional>
#include "nix/store/build/derivation-builder.hh"
#include "nix/util/file-system.hh"
#include "nix/store/user-lock.hh"
#include "nix/store/store-api.hh"
namespace nix {
/**
* Parameters for setting up a chroot environment.
*/
struct BuildChrootParams
{
/** The directory where the chroot will be created */
std::filesystem::path chrootParentDir;
/** Whether the derivation uses UID range feature */
bool useUidRange;
/** Whether the derivation type is sandboxed */
bool isSandboxed;
/** Build user (may be null if not using a build user) */
UserLock * buildUser;
/** The store directory (e.g., "/nix/store") */
std::string storeDir;
/** Callback to change ownership of a path to the build user */
std::function<void(const std::filesystem::path &)> chownToBuilder;
/** Function to get the sandbox GID */
std::function<gid_t()> getSandboxGid;
};
/**
* Set up a chroot build environment.
*
* Creates the chroot directory structure and sets up necessary directories
* (/tmp, /etc, store directory). Returns the chroot root path and an AutoDelete
* that will clean up the chroot directory when destroyed.
*
* @param params Parameters for chroot setup
* @return Pair of (chroot root path, cleanup object)
*/
std::pair<std::filesystem::path, AutoDelete> setupBuildChroot(const BuildChrootParams & params);
} // namespace nix
@@ -55,10 +55,10 @@
namespace nix {
struct NotDeterministic final : CloneableError<NotDeterministic, BuildError>
struct NotDeterministic : BuildError
{
NotDeterministic(auto &&... args)
: CloneableError(BuildResult::Failure::NotDeterministic, args...)
: BuildError(BuildResult::Failure::NotDeterministic, args...)
{
isNonDeterministic = true;
}
@@ -26,6 +26,8 @@
namespace nix {
using namespace nix::linux;
static void setupSeccomp(const LocalSettings & localSettings)
{
if (!localSettings.filterSyscalls)
+1
View File
@@ -1,5 +1,6 @@
sources += files(
'build/child.cc',
'build/chroot.cc',
'build/derivation-builder.cc',
'build/hook-instance.cc',
'pathlocks.cc',
+2 -2
View File
@@ -31,11 +31,11 @@ unsigned int getMaxCPU()
{
#ifdef __linux__
try {
auto cgroupFS = getCgroupFS();
auto cgroupFS = linux::getCgroupFS();
if (!cgroupFS)
return 0;
auto cpuFile = *cgroupFS / getCurrentCgroup().rel() / "cpu.max";
auto cpuFile = *cgroupFS / linux::getCurrentCgroup().rel() / "cpu.max";
auto cpuMax = readFile(cpuFile);
auto cpuMaxParts = tokenizeString<std::vector<std::string>>(cpuMax, " \n");
+1 -1
View File
@@ -378,7 +378,7 @@ std::set<ExperimentalFeature> parseFeatures(const StringSet & rawFeatures)
}
MissingExperimentalFeature::MissingExperimentalFeature(ExperimentalFeature feature, std::string reason)
: CloneableError(
: Error(
"experimental Nix feature '%1%' is disabled%2%; add '--extra-experimental-features %1%' to enable it",
showExperimentalFeature(feature),
Uncolored(optionalBracket(" (", reason, ")")))
+12 -30
View File
@@ -226,31 +226,13 @@ public:
{
return err;
};
[[noreturn]] virtual void throwClone() const = 0;
};
template<typename Derived, typename Base>
class CloneableError : public Base
{
public:
using Base::Base;
/**
* Rethrow a copy of this exception. Useful when the exception can get
* modified when appending traces.
*/
[[noreturn]] void throwClone() const override
{
throw Derived(static_cast<const Derived &>(*this));
}
};
#define MakeError(newClass, superClass) \
class newClass : public CloneableError<newClass, superClass> \
{ \
public: \
using CloneableError<newClass, superClass>::CloneableError; \
#define MakeError(newClass, superClass) \
class newClass : public superClass \
{ \
public: \
using superClass::superClass; \
}
MakeError(Error, BaseError);
@@ -262,21 +244,21 @@ MakeError(UnimplementedError, Error);
* std::error_code. Use when you want to catch and check an error condition like
* no_such_file_or_directory (ENOENT) without ifdefs.
*/
class SystemError : public CloneableError<SystemError, Error>
class SystemError : public Error
{
std::error_code errorCode;
public:
template<typename... Args>
SystemError(std::errc posixErrNo, Args &&... args)
: CloneableError(std::forward<Args>(args)...)
: Error(std::forward<Args>(args)...)
, errorCode(std::make_error_code(posixErrNo))
{
}
template<typename... Args>
SystemError(std::error_code errorCode, Args &&... args)
: CloneableError(std::forward<Args>(args)...)
: Error(std::forward<Args>(args)...)
, errorCode(errorCode)
{
}
@@ -308,7 +290,7 @@ public:
* support is too WIP to justify the code churn, but if it is finished
* then a better identifier becomes moe worth it.
*/
class SysError final : public CloneableError<SysError, SystemError>
class SysError : public SystemError
{
public:
int errNo;
@@ -319,7 +301,7 @@ public:
*/
template<typename... Args>
SysError(int errNo, const Args &... args)
: CloneableError(static_cast<std::errc>(errNo), "")
: SystemError(static_cast<std::errc>(errNo), "")
, errNo(errNo)
{
auto hf = HintFmt(args...);
@@ -387,7 +369,7 @@ namespace windows {
* Unless you need to catch a specific error number, don't catch this in
* portable code. Catch `SystemError` instead.
*/
class WinError : public CloneableError<WinError, SystemError>
class WinError : public SystemError
{
public:
DWORD lastError;
@@ -399,7 +381,7 @@ public:
*/
template<typename... Args>
WinError(DWORD lastError, const Args &... args)
: CloneableError(std::error_code(lastError, std::system_category()), "")
: SystemError(std::error_code(lastError, std::system_category()), "")
, lastError(lastError)
{
auto hf = HintFmt(args...);
@@ -80,7 +80,7 @@ std::set<ExperimentalFeature> parseFeatures(const StringSet &);
* An experimental feature was required for some (experimental)
* operation, but was not enabled.
*/
class MissingExperimentalFeature final : public CloneableError<MissingExperimentalFeature, Error>
class MissingExperimentalFeature : public Error
{
public:
/**
+2 -2
View File
@@ -133,14 +133,14 @@ std::pair<int, std::string> runProgram(RunOptions && options);
void runProgram2(const RunOptions & options);
class ExecError final : public CloneableError<ExecError, Error>
class ExecError : public Error
{
public:
int status;
template<typename... Args>
ExecError(int status, const Args &... args)
: CloneableError(args...)
: Error(args...)
, status(status)
{
}
@@ -231,19 +231,19 @@ ref<SourceAccessor> makeEmptySourceAccessor();
*/
MakeError(RestrictedPathError, Error);
struct SymlinkNotAllowed final : public CloneableError<SymlinkNotAllowed, Error>
struct SymlinkNotAllowed : public Error
{
CanonPath path;
SymlinkNotAllowed(CanonPath path)
: CloneableError("relative path '%s' points to a symlink, which is not allowed", path.rel())
: Error("relative path '%s' points to a symlink, which is not allowed", path.rel())
, path(std::move(path))
{
}
template<typename... Args>
SymlinkNotAllowed(CanonPath path, const std::string & fs, Args &&... args)
: CloneableError(fs, std::forward<Args>(args)...)
: Error(fs, std::forward<Args>(args)...)
, path(std::move(path))
{
}
+2 -2
View File
@@ -13,7 +13,7 @@
#include <dirent.h>
#include <mntent.h>
namespace nix {
namespace nix::linux {
std::optional<std::filesystem::path> getCgroupFS()
{
@@ -174,4 +174,4 @@ CanonPath getRootCgroup()
return rootCgroup;
}
} // namespace nix
} // namespace nix::linux
+2 -2
View File
@@ -8,7 +8,7 @@
#include "nix/util/types.hh"
#include "nix/util/canon-path.hh"
namespace nix {
namespace nix::linux {
std::optional<std::filesystem::path> getCgroupFS();
@@ -41,4 +41,4 @@ CanonPath getCurrentCgroup();
*/
CanonPath getRootCgroup();
} // namespace nix
} // namespace nix::linux
-2
View File
@@ -427,9 +427,7 @@ void mainWrapped(int argc, char ** argv)
evalSettings.pureEval = true;
#ifndef _WIN32
setLogFormat("bar");
#endif
settings.verboseBuild = false;
// If on a terminal, progress will be displayed via progress bars etc. (thus verbosity=notice)
+2 -2
View File
@@ -251,8 +251,8 @@ static void daemonLoop(ref<const StoreConfig> storeConfig, std::optional<Trusted
experimentalFeatureSettings.require(Xp::Cgroups);
// This also sets the root cgroup to the current one.
auto rootCgroup = getRootCgroup();
auto cgroupFS = getCgroupFS();
auto rootCgroup = linux::getRootCgroup();
auto cgroupFS = linux::getCgroupFS();
if (!cgroupFS)
throw Error("cannot determine the cgroups file system");
auto rootCgroupPath = *cgroupFS / rootCgroup.rel();