forked from LnL7/nix
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
builtins.addDrvOutputDependencies
The new primop is fairly simple. What is far more weird is the test plan. The test plan is taken by Robert's comment in NixOS#7910 (comment) describing how we could migrate *Nixpkgs* without a breaking change in Nix. The Nix testsuite has its own `mkDerivation`, and we so we do the same thing with it: - `drvPath` is now overridden to not have the funky `DrvDeep` string context anymore. - Tests that previously needed to `builtins.unsafeDiscardOutputDependency` a `drvPath` no don't. - Tests that previous did *not* need to `builtins.unsafeDiscardOutputDependency` a `drvPath` now *do*. Also, there is a regular language test testing the `derivation` primop in isolation (again, no breaking change to it!) which has been extended. Co-authored-by: Robert Hensing <roberth@users.noreply.github.com> Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
- Loading branch information
1 parent
d12c614
commit 7b63fc1
Showing
20 changed files
with
371 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# String context | ||
|
||
Strings in the Nix language are not just a sequences of characters like strings in other languages. | ||
They are actually pairs of sequences of characters and what is known as a *string context*. | ||
The string context is an (unordered) set of *string context elements*. | ||
|
||
String contexts are not intended to be intentionally manipulated. | ||
Rather, they are supposed to collect non-string values combined with strings via | ||
[string concatenation](./operators.md#string-concatenation), | ||
[string interpolation](./string-interpolation.md), | ||
and similar operations. | ||
The idea is that a user can combine together values to create a build recipe without manually keeping track of where the "ingredients" come from, and then the Nix language does that bookkeeping implicitly to come up with the right derivation inputs. | ||
In this idiom, string with context (i.e. with a non-empty string context) are concatenated together without their contents being inspected. | ||
|
||
String context elements come in 3 forms: | ||
|
||
- [*constant*]{#string-context-element-constant} | ||
- [*output*]{#string-context-element-output} | ||
- [*derivation deep*]{#string-context-element-derivation-deep} | ||
|
||
*Constant* and *output* string contexts elements are just | ||
[derived paths](@docroot@/glossary.md#gloss-derived-path); | ||
those are just the names of the two kinds of derived path. | ||
See the documentation on derived paths for further details. | ||
|
||
*derivation deep* is an advanced feature intended to be used with the | ||
[`exportReferencesGraph`](./advanced-attributes.html#adv-attr-exportReferencesGraph) | ||
advanced derivation feature. | ||
A *derivation deep* string context element is a derivation path, and refers to both its outputs and the entire build closure of that derivation: | ||
all its outputs, all the other derivations the given derivation depends on, and all their outputs too. | ||
|
||
## Inspecting string contexts | ||
|
||
Most basically, [`builtins.hasContext`] will whether a string as a non-empty context. | ||
|
||
When more granualr information than merely whether the string context is empty is needed, [`builtins.getContext`] can be used. | ||
It creates an [attribute set] representing the string context; the attribute set can be inspected normally. | ||
|
||
[`builtins.hasContext`]: ./builtins.md#builtins-hasContext | ||
[`builtins.getContext`]: ./builtins.md#builtins-getContext | ||
[attribute set]: ./values.md#attribute-set | ||
|
||
## Clearing string contexts | ||
|
||
[`buitins.unsafeDiscardStringContext`](./builtins.md#) will make a copy of another string, but with an empty string context. | ||
The new string can be inspected in more ways, e.g. by operators that require that the string context is empty. | ||
Explicitly discarding the string context and then expecting it helps ensure that string context elements are not lost by mistake. | ||
|
||
## Constructing string contexts | ||
|
||
[`builtins.appendContext`] will create copy of a string but with additional string context elements. | ||
The context is specified explicitly by an [attribute set] in the format that [`builtins.hasContext`] produces. | ||
We there can create strings arbitrary contexts in 3 steps: | ||
|
||
1. Create strings with the string context elements we want | ||
2. Dump their contexts with [`builtins.getContext`] | ||
3. Combine them together with a base string and repeated [`builtins.appendContext`] calls. | ||
|
||
The remainder of this section will focus on step 1: making strings with individual string context elements on which to apply `builtins.getContext`. | ||
|
||
[`builtins.appendContext`]: ./builtins.md#builtins-appendContext | ||
|
||
## Constant string context elements | ||
|
||
A constant string context element is just a constant [derived path]; | ||
a constant derived path is just a [store path]. | ||
We therefore want to use [`builtins.storePath`] to create a string with a single constant string context element: | ||
|
||
> **Example** | ||
> | ||
> ```nix | ||
> builtins.getContext (builtins.storePath "/nix/store/wkhdf9jinag5750mqlax6z2zbwhqb76n-hello-2.10") | ||
> ``` | ||
> evaluates to | ||
> ```nix | ||
> { | ||
> "/nix/store/wkhdf9jinag5750mqlax6z2zbwhqb76n-hello-2.10" = { | ||
> path = true; | ||
> }; | ||
> } | ||
> ``` | ||
[derived path]: @docroot@/glossary.md#gloss-derived-path | ||
[store path]: @docroot@/glossary.md#gloss-store-path | ||
[`builtins.storePath`]: ./builtins.md#builtins-storePath | ||
## Output string context elements | ||
The best way to illustrate this with a builtin function that is still experimental: [`builtins.ouputOf`]. | ||
This example will *not* work the stable Nix! | ||
> **Example** | ||
> | ||
> ```nix | ||
> builtins.getContext | ||
> (builtins.outputOf | ||
> (builtins.storePath "/nix/store/fvchh9cvcr7kdla6n860hshchsba305w-hello-2.12.drv") | ||
> "out") | ||
> ``` | ||
> evaluates to | ||
> ```nix | ||
> { | ||
> "/nix/store/fvchh9cvcr7kdla6n860hshchsba305w-hello-2.12.drv" = { | ||
> outputs = [ "out" ]; | ||
> }; | ||
> } | ||
> ``` | ||
[`builtins.outputOf`]: ./builtins.md#builtins-outputOf | ||
## "Derivation deep" string context elements | ||
These best way to illustrate this is with [`builtins.addDrvOutputDependencies`]. | ||
We take a regular constant string context element pointing to a derivation, and transform it unto a "Derivation deep" string context element. | ||
> **Example** | ||
> | ||
> ```nix | ||
> builtins.getContext | ||
> (builtins.addDrvOutputDependencies | ||
> (builtins.storePath "/nix/store/fvchh9cvcr7kdla6n860hshchsba305w-hello-2.12.drv")) | ||
> ``` | ||
> evaluates to | ||
> ```nix | ||
> { | ||
> "/nix/store/fvchh9cvcr7kdla6n860hshchsba305w-hello-2.12.drv" = { | ||
> allOutputs = true; | ||
> }; | ||
> } | ||
> ``` | ||
[`builtins.unsafeDiscardOutputDependency`] does this the opposite of [`builtins.addDrvOutputDependencies`], but is not prefered because it "weakens" rather than "strengens" the string context. | ||
What is meant by that is that since the "derivation deep" string context element always refers to the underlying derivation (among many more things), | ||
replacing a constant string context element with a "derivation deep" one is a safe operation that just enlargens the string context without forgetting anything. | ||
[`builtins.addDrvOutputDependencies`]: ./builtins.md#builtins-addDrvOutputDependencies | ||
[`builtins.unsafeDiscardOutputDependency`]: ./builtins.md#builtins-unsafeDiscardOutputDependency |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.