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

Classes

struct  Derivation
struct  FullInputs
struct  Options
struct  Output
struct  OutputChecks
struct  Type

Concepts

concept  RenderableDerivation

Typedefs

template<typename Out = Output>
using Outputs = std::map<std::string, Out>
typedef std::map< std::string, std::pair< Output, std::optional< StorePath > > > OutputsAndOptPaths
typedef std::map< StorePath, StringSet > Inputs
using Basic = Derivation<StorePathSet>
 Derivation that depends only on other store objects.
using Full = Derivation<std::set<SingleDerivedPath>>
 Derivation that depends on the outputs of other derivations in addition.
using ReadDerivation = fun<Full(const StorePath & drvPath)>
using FullDeferred = Derivation<std::set<SingleDerivedPath>, Output::Deferred>
 Full, but statically known to have no output paths yet.
using FullInputAddressed = Derivation<std::set<SingleDerivedPath>, Output::InputAddressed>
 Full, but statically known to be input-addressed.

Enumerations

enum struct  ATermVersion { Traditional , DynamicDerivations }

Functions

std::optional< Options< StorePath > > tryResolve (const Options< SingleDerivedPath > &drvOptions, fun< std::optional< StorePath >(ref< const SingleDerivedPath > drvPath, const std::string &outputName)> queryResolutionChain)
Full parse (const StoreDirConfig &store, std::string &&s, std::string_view name, bool supportWindowsStoreDir, const ExperimentalFeatureSettings &xpSettings)
template<typename Inputs, typename Out>
requires RenderableDerivation<Inputs, Out>
std::string unparse (const Derivation< Inputs, Out > &drv, const StoreDirConfig &store, bool supportWindowsStoreDir)
template std::string unparse (const masked::Drv< Output::Deferred > &drv, const StoreDirConfig &store, bool)
template std::string unparse (const masked::Drv< Output::InputAddressed > &drv, const StoreDirConfig &store, bool)
template<typename Out>
std::string unparse (const Derivation< std::set< SingleDerivedPath >, Out > &drv, const StoreDirConfig &store, bool supportWindowsStoreDir)
template std::string unparse (const Full &drv, const StoreDirConfig &store, bool)
template std::string unparse (const FullDeferred &drv, const StoreDirConfig &store, bool)
template std::string unparse (const FullInputAddressed &drv, const StoreDirConfig &store, bool)
Source & read (Source &in, const StoreDirConfig &store, Basic &drv, std::string_view name)
void write (Sink &out, const StoreDirConfig &store, const Basic &drv)
bool hasDynamicDrvDep (const FullInputs &inputs)
Full unresolve (const Basic &drv)
bool shouldResolve (const Full &drv)
std::optional< Basic > tryResolve (const Full &drv, Store &store, Store *evalStore)
std::optional< Basic > tryResolve (const Full &drv, Store &store, fun< std::optional< StorePath >(ref< const SingleDerivedPath > drvPath, const std::string &outputName)> queryResolutionChain)
template<typename Inputs>
Type type (const Derivation< Inputs, Output > &drv)
template Type type (const Basic &drv)
template Type type (const Full &drv)
template<typename Inputs>
OutputsAndOptPaths outputsAndOptPaths (const Derivation< Inputs, Output > &drv, const StoreDirConfig &store)
template OutputsAndOptPaths outputsAndOptPaths (const Basic &drv, const StoreDirConfig &store)
template OutputsAndOptPaths outputsAndOptPaths (const Full &drv, const StoreDirConfig &store)
bool hasDynamicDrvDep (const std::set< SingleDerivedPath > &inputs)
template<typename Inputs>
void checkInvariants (const Derivation< Inputs, Output > &drv, const StoreDirConfig &store, auto &&readDerivation, const StorePath &drvPath)
template void checkInvariants (const Basic &drv, const StoreDirConfig &store, std::monostate &&readDerivation, const StorePath &drvPath)
template void checkInvariants (const Full &drv, const StoreDirConfig &store, ReadDerivation &&readDerivation, const StorePath &drvPath)
void checkInvariants (const Basic &drv, const StoreDirConfig &store)
void checkInvariants (const Full &drv, const StoreDirConfig &store, ReadDerivation &readDerivation)
void checkInvariants (const Full &drv, Store &store)
ReadDerivation readInvalid (Store &store)
void fillInOutputPaths (Basic &drv, const StoreDirConfig &store)
void fillInOutputPaths (Full &drv, const StoreDirConfig &store, ReadDerivation &readDerivation)
void fillInOutputPaths (Full &drv, Store &store)
std::optional< FullInputAddressed > fillInOutputPaths (FullDeferred drv, const StoreDirConfig &store, ReadDerivation &readDerivation)
Full parseJsonAndValidate (Store &store, const nlohmann::json &json)
template<typename Inputs>
void checkInvariants (const Derivation< Inputs, Output > &drv, Store &store, const StorePath &drvPath)

Variables

constexpr bool defaultSupportWindowsStoreDir

Detailed Description

This represents all the special options on a Derivation.

Currently, these options are parsed from the environment variables with the aid of StructuredAttrs.

The first goal of this data type is to make sure that no other code uses StructuredAttrs to ad-hoc parse some additional options. That ensures this data type is up to date and fully correct.

The second goal of this data type is to allow an alternative to hackily parsing the options from the environment variables. The ATerm format cannot change, but in alternatives to it (like the JSON format), we have the option of instead storing the options separately. That would be nice to separate concerns, and not make any environment variable names magical.

Typedef Documentation

◆ Basic

using nix::derivation::Basic = Derivation<StorePathSet>

Derivation that depends only on other store objects.

This type is what's used in Store::buildDerivation or for resolved derivations

See also
derivation::tryResolve.

◆ Full

Derivation that depends on the outputs of other derivations in addition.

This type is what's constructed by the evaluator and written to the store in ATerm format.

◆ FullDeferred

Full, but statically known to have no output paths yet.

The outputs are Output::Deferred rather than the Output variant, so "we have not computed the output paths" is carried in the type.

See also
fillInOutputPaths, which turns this into a FullInputAddressed.

◆ FullInputAddressed

Full, but statically known to be input-addressed.

The outputs are Output::InputAddressed rather than the Output variant, so "the output paths are computed" is carried in the type.

◆ Inputs

For inputs that are sub-derivations, we specify exactly which output IDs we are interested in.

◆ OutputsAndOptPaths

typedef std::map<std::string, std::pair<Output, std::optional<StorePath> > > nix::derivation::OutputsAndOptPaths

These are analogues to the previous Outputs data type, but they also contains, for each output, the (optional) store path in which it would be written. To calculate values of these types, see the corresponding functions in Basic.

◆ ReadDerivation

How to get a derivation's value from its store path.

Several computations over derivations — filling in output paths, checking invariants, computing masked hash — need to recurse into input derivations, but that is all they need a store for; everything else they touch is StoreDirConfig, for printing paths. Taking just this much lets them be used (and tested) without a real store, and makes it evident from the signature that nothing else is queried.

Enumeration Type Documentation

◆ ATermVersion

enum struct nix::derivation::ATermVersion
strong

All ATerm Derivation format versions currently known.

Unknown versions are rejected at the parsing stage.

Enumerator
Traditional 

Older unversioned form

DynamicDerivations 

Newer versioned form; only this version so far.

Function Documentation

◆ checkInvariants() [1/4]

void nix::derivation::checkInvariants ( const Basic & drv,
const StoreDirConfig & store )

Check that the derivation is valid and does not present any illegal states.

This is mainly a matter of checking the outputs, where our C++ representation supports all sorts of combinations we do not yet allow.

This overload does not validate the derivation name or add path context to errors. Use this when you don't have a StorePath or when you want to handle error context yourself.

Parameters
storeThe store to use for validation

◆ checkInvariants() [2/4]

template<typename Inputs>
void nix::derivation::checkInvariants ( const Derivation< Inputs, Output > & drv,
const StoreDirConfig & store,
auto && readDerivation,
const StorePath & drvPath )

Like the above, but instead of reading input derivations from a store, uses a given ReadDerivation to get them.

◆ checkInvariants() [3/4]

template<typename Inputs>
void nix::derivation::checkInvariants ( const Derivation< Inputs, Output > & drv,
Store & store,
const StorePath & drvPath )

This overload does everything the base checkInvariants does, but also validates that the derivation name matches the path, and improves any error messages that occur using the derivation path.

Parameters
storeThe store to use for validation
drvPathThe path to this derivation

◆ checkInvariants() [4/4]

void nix::derivation::checkInvariants ( const Full & drv,
const StoreDirConfig & store,
ReadDerivation & readDerivation )

Like the above, but instead of reading input derivations from a store, uses a given ReadDerivation to get them.

◆ fillInOutputPaths() [1/3]

void nix::derivation::fillInOutputPaths ( Basic & drv,
const StoreDirConfig & store )

Fill in output paths as needed.

For input-addressed derivations (ready or deferred), it computes the derivation masked hash and based on the result:

  • If Regular: converts Deferred outputs to InputAddressed, and ensures all InputAddressed outputs (whether preexisting or newly computed) have the right computed paths. Likewise defines (if absent or the empty string) or checks (if preexisting and non-empty) environment variables for each output with their path.
  • If Deferred: converts InputAddressed to Deferred.

Also for fixed-output content-addressed derivations, likewise updates output paths in env vars.

Parameters
storeThe store to use for path computation

◆ fillInOutputPaths() [2/3]

void nix::derivation::fillInOutputPaths ( Full & drv,
const StoreDirConfig & store,
ReadDerivation & readDerivation )

Like the above, but instead of reading input derivations from a store, uses a given ReadDerivation to get them.

◆ fillInOutputPaths() [3/3]

std::optional< FullInputAddressed > nix::derivation::fillInOutputPaths ( FullDeferred drv,
const StoreDirConfig & store,
ReadDerivation & readDerivation )

Functional, statically-typed variant of the above, for a derivation all of whose outputs are known to be Deferred.

Rather than mutating in place — which is only possible because Output is a variant able to hold either alternative — this consumes its argument and returns a derivation whose outputs are statically InputAddressed, so the "outputs are filled in now" fact is carried in the type. Everything but the (small) outputs is moved through, so this is no more expensive than the mutating version.

Returns std::nullopt if there is no input address to fill in yet, i.e. when the derivation (transitively) depends on a floating content-addressing derivation. That is the case Deferred exists for, and it is precisely the case this function cannot represent in its return type.

◆ hasDynamicDrvDep() [1/2]

bool nix::derivation::hasDynamicDrvDep ( const FullInputs & inputs)

Does the derivation have a dependency on the output of a dynamic derivation?

In other words, does it depend on the output of a derivation that is itself an output of a derivation? This corresponds to a dependency that is an inductive derived path with more than one layer of DerivedPath::Built.

◆ hasDynamicDrvDep() [2/2]

bool nix::derivation::hasDynamicDrvDep ( const std::set< SingleDerivedPath > & inputs)

Does the derivation have a dependency on the output of a dynamic derivation?

In other words, does it depend on the output of a derivation that is itself an output of a derivation? This corresponds to a dependency that is an inductive derived path with more than one layer of DerivedPath::Built.

◆ outputsAndOptPaths()

template<typename Inputs>
OutputsAndOptPaths nix::derivation::outputsAndOptPaths ( const Derivation< Inputs, Output > & drv,
const StoreDirConfig & store )

Calculates the maps that contains all the Outputs, but augmented with knowledge of the Store paths they would be written into.

◆ parse()

Full nix::derivation::parse ( const StoreDirConfig & store,
std::string && s,
std::string_view name,
bool supportWindowsStoreDir = defaultSupportWindowsStoreDir,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings )

Read a derivation from a file.

◆ parseJsonAndValidate()

Full nix::derivation::parseJsonAndValidate ( Store & store,
const nlohmann::json & json )

Parse a derivation from JSON, and also perform various conveniences such as:

  1. Filling in output paths in as needed/required.
  2. Checking invariants in general.

In the future it might also do things like:

  • assist with the migration from older JSON formats.
  • (a somewhat example of the above) initialize DerivationOptions from their traditional encoding inside the env and structuredAttrs.
Parameters
storeThe store to use for path computation and validation
jsonThe JSON representation of the derivation
Returns
A validated derivation with output paths filled in
Exceptions
Errorif parsing fails, output paths can't be computed, or validation fails

◆ readInvalid()

ReadDerivation nix::derivation::readInvalid ( Store & store)

Store::readInvalidDerivation as a ReadDerivation, for callers that do have a whole store to hand.

◆ shouldResolve()

bool nix::derivation::shouldResolve ( const Full & drv)

Determine whether this derivation should be resolved before building.

Resolution is needed when:

  • Input-addressed derivations are deferred (depend on CA derivations)
  • Content-addressed derivations have input drvs and are either:
    • Floating (non-fixed), which must always be resolved
    • Fixed, which can optionally be resolved when ca-derivations is enabled
  • Impure derivations always need resolution
  • Any input derivations have outputs from dynamic derivations

◆ tryResolve() [1/3]

std::optional< Basic > nix::derivation::tryResolve ( const Full & drv,
Store & store,
fun< std::optional< StorePath >(ref< const SingleDerivedPath > drvPath, const std::string &outputName)> queryResolutionChain )

Like the above, but instead of querying the Nix database for realisations, uses a given mapping from input derivation paths + output names to actual output store paths.

◆ tryResolve() [2/3]

std::optional< Basic > nix::derivation::tryResolve ( const Full & drv,
Store & store,
Store * evalStore = nullptr )

Return the underlying basic derivation but with these changes:

  1. Input drvs are emptied, but the outputs of them that were used are added directly to input sources.
  2. Input placeholders are replaced with realized input store paths.

◆ tryResolve() [3/3]

std::optional< Options< StorePath > > nix::derivation::tryResolve ( const Options< SingleDerivedPath > & drvOptions,
fun< std::optional< StorePath >(ref< const SingleDerivedPath > drvPath, const std::string &outputName)> queryResolutionChain )

This is the counterpart of Derivation::tryResolve. In particular, it takes the same sort of callback, which is used to resolve non-constant deriving paths.

We need this function when resolving a derivation, and we will use this as part of that if/when Derivation includes DerivationOptions

◆ type()

template<typename Inputs>
Type nix::derivation::type ( const Derivation< Inputs, Output > & drv)

Return true iff this is a fixed-output derivation.

◆ unparse() [1/2]

template<typename Inputs, typename Out>
requires RenderableDerivation<Inputs, Out>
std::string nix::derivation::unparse ( const Derivation< Inputs, Out > & drv,
const StoreDirConfig & store,
bool supportWindowsStoreDir )

This one, unlike the public one, is polymorphic on the output parameter to support the masked hash intermediate form.

Print a derivation in one of the intermediate forms.

The masked::HashInputs cases are not round-trippable: parse cannot read them back, as their input derivations are named by hash rather than by store path.

◆ unparse() [2/2]

template<typename Out>
std::string nix::derivation::unparse ( const Derivation< std::set< SingleDerivedPath >, Out > & drv,
const StoreDirConfig & store,
bool supportWindowsStoreDir = defaultSupportWindowsStoreDir )

Print a derivation.

◆ unresolve()

Full nix::derivation::unresolve ( const Basic & drv)

Convert a Basic derivation to a Full derivation. The resulting derivation has empty input drvs since a Basic derivation is already resolved.

Variable Documentation

◆ defaultSupportWindowsStoreDir

bool nix::derivation::defaultSupportWindowsStoreDir
inlineconstexpr
Initial value:
=
false

Whether the store directory may contain characters that the ATerm format escapes — in practice the \\endiskip of a Windows store directory such as C:\ProgramData\nix\store.

We could always enable it, as escape sequences should only be allowed to be used when needed, but out of an abundance of caution we make it a flag instead.