Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
nix::UnixDerivationBuilderImpl Class Reference

#include <unix-derivation-builder-impl.hh>

Inheritance diagram for nix::UnixDerivationBuilderImpl:
nix::DerivationBuilderImpl nix::DerivationBuilder nix::DerivationBuilderParams nix::RestrictionContext nix::ChrootDerivationBuilder nix::DarwinDerivationBuilder nix::FreeBSDDerivationBuilder nix::LinuxDerivationBuilder nix::ChrootFreeBSDDerivationBuilder nix::ChrootLinuxDerivationBuilder nix::ChrootFreeBSDDerivationBuilder nix::ChrootLinuxDerivationBuilder

Classes

struct  DaemonWorkerState
struct  RunChildArgs

Public Member Functions

void cleanupOnDestruction () noexcept
std::optional< Descriptor > startBuild () override
BuilderExit unprepareBuild () override
void cleanupBuild (bool force) override
bool killChild () override
 DerivationBuilderImpl (std::shared_ptr< BuildingStore > store, std::shared_ptr< DerivationBuilderCallbacks > miscMethods, DerivationBuilderParams params)
Public Member Functions inherited from nix::DerivationBuilderImpl
 DerivationBuilderImpl (std::shared_ptr< BuildingStore > store, std::shared_ptr< DerivationBuilderCallbacks > miscMethods, DerivationBuilderParams params)
SingleDrvOutputs registerOutputs (LocalStore &localStore) override
SingleDrvOutputs checkSubmittedOutputs (LocalStore &localStore) override
Public Member Functions inherited from nix::DerivationBuilder
MuxablePipePollState::CommChannel logChannel ()
Descriptor logDescriptor ()
Public Member Functions inherited from nix::RestrictionContext
bool isAllowed (const DerivedPath &id)
void addDependency (const StorePath &path)

Protected Types

typedef StringMap Environment
Protected Types inherited from nix::DerivationBuilderImpl
typedef std::map< StorePath, StorePath > RedirectedOutputs

Protected Member Functions

const StorePathSet & originalPaths () override
bool isAllowed (const StorePath &path) override
bool isAllowed (const DrvOutput &id) override
bool isAllowed (const DerivedPath &req)
bool shouldModifySandbox () override
void submitOutput (const SingleDerivedPath &path, const OutputName &output) override
virtual bool needsHashRewrite ()
virtual std::unique_ptr< UserLock > getBuildUser ()
PathsInChroot getPathsInSandbox ()
virtual void setBuildTmpDir ()
virtual std::filesystem::path tmpDirInSandbox ()
virtual void prepareUser ()
virtual void prepareSandbox ()
virtual Strings getPreBuildHookArgs ()
void openSlave ()
virtual void startChild ()
void processSandboxSetupMessages ()
void addDependencyImpl (const StorePath &path) override
void chownToBuilder (const std::filesystem::path &path)
void chownToBuilder (int fd, const std::filesystem::path &path)
void writeBuilderFile (const OsFilename &name, std::string_view contents)
void runChild (RunChildArgs args)
virtual void enterChroot ()
virtual void setUser ()
virtual void execBuilder (const Strings &args, const Strings &envStrs)
virtual void killSandbox (bool getStats)
Protected Member Functions inherited from nix::DerivationBuilderImpl
virtual std::filesystem::path realPathInHost (const StorePath &p)

Protected Attributes

std::filesystem::path topTmpDir
AutoCloseFD tmpDirFd
Environment env
bool usingSubmitted
AutoCloseFD daemonSocket
std::thread daemonThread
std::list< DaemonWorkerState > daemonWorkerThreads
Protected Attributes inherited from nix::DerivationBuilderImpl
Pid pid
std::shared_ptr< BuildingStore > store
std::shared_ptr< DerivationBuilderCallbacks > miscMethods
std::filesystem::path tmpDir
const derivation::Type derivationType
const LocalSettings & localSettings = store->getLocalSettings()
std::unique_ptr< UserLock > buildUser
StringMap inputRewrites
StringMap outputRewrites
RedirectedOutputs redirectedOutputs
OutputPathMap scratchOutputs

Static Protected Attributes

static const std::filesystem::path homeDir = "/homeless-shelter"

Friends

struct RestrictedStore

Additional Inherited Members

Public Attributes inherited from nix::DerivationBuilderImpl
Sync< OutputPathMap > submittedOutputs
Public Attributes inherited from nix::DerivationBuilder
AutoCloseFD builderOut
Public Attributes inherited from nix::RestrictionContext
Sync< State > state_
Public Attributes inherited from nix::DerivationBuilderParams
const StorePath & drvPath
BuildResult & buildResult
const BasicDerivation & drv
const DerivationOptions< StorePath > & drvOptions
const StorePathSet & inputPaths
const std::map< std::string, InitialOutput > initialOutputs
const BuildMode & buildMode
PathsInChroot defaultPathsInChroot
StringSet systemFeatures
DesugaredEnv desugaredEnv

Detailed Description

This class represents the state for building locally on Unix.

Todo
Ideally, it would not be a class, but a single function. However, besides the main entry point, there are a few more methods which are externally called, and need to be gotten rid of. There are also some virtual methods (either directly here or inherited from DerivationBuilderCallbacks, a stop-gap) that represent outgoing rather than incoming call edges that either should be removed, or become (higher order) function parameters.

Member Function Documentation

◆ addDependencyImpl()

void nix::UnixDerivationBuilderImpl::addDependencyImpl ( const StorePath & path)
overrideprotectedvirtual

This is the underlying implementation to be defined. The caller will ensure that this is only called on newly added dependencies, and that idempotent calls are a no-op.

Implements nix::RestrictionContext.

◆ chownToBuilder() [1/2]

void nix::UnixDerivationBuilderImpl::chownToBuilder ( const std::filesystem::path & path)
protected

Make a file owned by the builder.

SAFETY: this function is prone to TOCTOU as it receives a path and not a descriptor. It's only safe to call in a child of a directory only visible to the owner.

◆ chownToBuilder() [2/2]

void nix::UnixDerivationBuilderImpl::chownToBuilder ( int fd,
const std::filesystem::path & path )
protected

Make a file owned by the builder addressed by its file descriptor.

Parameters
pathOnly used for error messages.

◆ cleanupBuild()

void nix::UnixDerivationBuilderImpl::cleanupBuild ( bool force)
overridevirtual

Delete the temporary directory, if we have one.

Parameters
forceWe know the build succeeded, so don't attempt to preserve anything for debugging.

Implements nix::DerivationBuilder.

◆ cleanupOnDestruction()

void nix::UnixDerivationBuilderImpl::cleanupOnDestruction ( )
inlinenoexcept

Cleanup code to run when destroying any UnixDerivationBuilderImpl implementation.

◆ enterChroot()

virtual void nix::UnixDerivationBuilderImpl::enterChroot ( )
inlineprotectedvirtual

Move the current process into the chroot, if any. Called early by runChild().

Reimplemented in nix::ChrootFreeBSDDerivationBuilder, nix::ChrootLinuxDerivationBuilder, and nix::LinuxDerivationBuilder.

◆ execBuilder()

void nix::UnixDerivationBuilderImpl::execBuilder ( const Strings & args,
const Strings & envStrs )
protectedvirtual

Execute the derivation builder process. Called by runChild() as its final step. Should not return unless there is an error.

Reimplemented in nix::DarwinDerivationBuilder.

◆ getBuildUser()

virtual std::unique_ptr< UserLock > nix::UnixDerivationBuilderImpl::getBuildUser ( )
inlineprotectedvirtual

Acquire a build user lock. Return nullptr if no lock is available.

Reimplemented in nix::ChrootLinuxDerivationBuilder.

◆ getPathsInSandbox()

PathsInChroot nix::UnixDerivationBuilderImpl::getPathsInSandbox ( )
protected

Return the paths that should be made available in the sandbox. This includes:

  • The paths specified by the sandbox-paths setting, and their closure in the Nix store.
  • The contents of the __impureHostDeps derivation attribute, if the sandbox is in relaxed mode.
  • The paths returned by the pre-build-hook.
  • The paths in the input closure of the derivation.

◆ isAllowed() [1/2]

bool nix::UnixDerivationBuilderImpl::isAllowed ( const DrvOutput & id)
inlineoverrideprotectedvirtual

◆ isAllowed() [2/2]

bool nix::UnixDerivationBuilderImpl::isAllowed ( const StorePath & path)
inlineoverrideprotectedvirtual

Recursive Nix calls are only allowed to build or realize paths in the original input closure or added via a recursive Nix call (so e.g. you can't do nix-store -r /nix/store/<bla> where /nix/store/<bla> is some arbitrary path in a binary cache).

Implements nix::RestrictionContext.

◆ killChild()

bool nix::UnixDerivationBuilderImpl::killChild ( )
overridevirtual

Forcibly kill the child process, if any.

Returns
whether the child was still alive and needed to be killed.

Implements nix::DerivationBuilder.

◆ killSandbox()

void nix::UnixDerivationBuilderImpl::killSandbox ( bool getStats)
protectedvirtual

Kill any processes running under the build user UID or in the cgroup of the build.

Reimplemented in nix::ChrootLinuxDerivationBuilder, and nix::DarwinDerivationBuilder.

◆ needsHashRewrite()

virtual bool nix::UnixDerivationBuilderImpl::needsHashRewrite ( )
inlineprotectedvirtual

Whether we need to perform hash rewriting if there are valid output paths.

Reimplemented in nix::ChrootDerivationBuilder.

◆ openSlave()

void nix::UnixDerivationBuilderImpl::openSlave ( )
protected

Open the slave side of the pseudoterminal and use it as stderr.

◆ originalPaths()

const StorePathSet & nix::UnixDerivationBuilderImpl::originalPaths ( )
inlineoverrideprotectedvirtual

Paths that are already allowed to begin with

Implements nix::RestrictionContext.

◆ prepareSandbox()

void nix::UnixDerivationBuilderImpl::prepareSandbox ( )
protectedvirtual

Called by prepareBuild() to do any setup in the parent to prepare for a sandboxed build.

Reimplemented in nix::ChrootDerivationBuilder, nix::ChrootFreeBSDDerivationBuilder, nix::ChrootLinuxDerivationBuilder, and nix::DarwinDerivationBuilder.

◆ prepareUser()

virtual void nix::UnixDerivationBuilderImpl::prepareUser ( )
inlineprotectedvirtual

Ensure that there are no processes running that conflict with buildUser.

Reimplemented in nix::ChrootLinuxDerivationBuilder.

◆ processSandboxSetupMessages()

void nix::UnixDerivationBuilderImpl::processSandboxSetupMessages ( )
protected

Process messages send by the sandbox initialization.

◆ runChild()

void nix::UnixDerivationBuilderImpl::runChild ( RunChildArgs args)
protected

Run the builder's process.

◆ setUser()

void nix::UnixDerivationBuilderImpl::setUser ( )
protectedvirtual

Change the current process's uid/gid to the build user, if any. Called by runChild().

Reimplemented in nix::ChrootLinuxDerivationBuilder, and nix::DarwinDerivationBuilder.

◆ shouldModifySandbox()

bool nix::UnixDerivationBuilderImpl::shouldModifySandbox ( )
inlineoverrideprotectedvirtual

Whether mounting dependencies inside the sandbox should happen, or if it should be entirely skipped.

Implements nix::RestrictionContext.

◆ startBuild()

std::optional< Descriptor > nix::UnixDerivationBuilderImpl::startBuild ( )
overridevirtual

Set up build environment / sandbox, acquiring resources (e.g. locks as needed). After this is run, the builder should be started.

Returns
logging pipe if successful, std::nullopt if we could not acquire a build user. In that case, the caller must wait and then try again.
Note
"success" just means that we were able to set up the environment and start the build. The builder could have immediately exited with failure, and that would still be considered a successful start.

Implements nix::DerivationBuilder.

◆ startChild()

void nix::UnixDerivationBuilderImpl::startChild ( )
protectedvirtual

Called by prepareBuild() to start the child process for the build. Must set pid. The child must call runChild().

Reimplemented in nix::ChrootFreeBSDDerivationBuilder, and nix::ChrootLinuxDerivationBuilder.

◆ submitOutput()

void nix::UnixDerivationBuilderImpl::submitOutput ( const SingleDerivedPath & path,
const OutputName & output )
overrideprotectedvirtual

Register a store path to an output name For builder-rpc-v0

Implements nix::RestrictionContext.

◆ tmpDirInSandbox()

virtual std::filesystem::path nix::UnixDerivationBuilderImpl::tmpDirInSandbox ( )
inlineprotectedvirtual

Return the path of the temporary directory in the sandbox.

Reimplemented in nix::ChrootDerivationBuilder.

◆ unprepareBuild()

BuilderExit nix::UnixDerivationBuilderImpl::unprepareBuild ( )
overridevirtual

Tear down build environment after the builder exits (either on its own or if it is killed).

Returns
The builder's exit status and whether the disk seemed full at exit time.

Implements nix::DerivationBuilder.

◆ writeBuilderFile()

void nix::UnixDerivationBuilderImpl::writeBuilderFile ( const OsFilename & name,
std::string_view contents )
protected

Create a file in tmpDir owned by the builder.

Parameters
nameMust not contain more than one path segment and none of them must be .., .
contentsContents to be written to the file.

Otherwise this function throws an Error.

Member Data Documentation

◆ daemonSocket

AutoCloseFD nix::UnixDerivationBuilderImpl::daemonSocket
protected

The recursive Nix daemon socket.

◆ daemonThread

std::thread nix::UnixDerivationBuilderImpl::daemonThread
protected

The daemon main thread.

◆ daemonWorkerThreads

std::list<DaemonWorkerState> nix::UnixDerivationBuilderImpl::daemonWorkerThreads
protected

The daemon worker threads.

◆ tmpDirFd

AutoCloseFD nix::UnixDerivationBuilderImpl::tmpDirFd
protected

The file descriptor of the temporary directory.

◆ topTmpDir

std::filesystem::path nix::UnixDerivationBuilderImpl::topTmpDir
protected

The top-level temporary directory. tmpDir is either equal to or a child of this directory.

◆ usingSubmitted

bool nix::UnixDerivationBuilderImpl::usingSubmitted
protected

Whether or not derivation is using outputs submitted via recursive-nix


The documentation for this class was generated from the following files: