-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Document string context #8595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fricklerhandwerk
merged 5 commits into
NixOS:master
from
obsidiansystems:addDrvOutputDependencies
May 8, 2024
Merged
Document string context #8595
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5f05df6
Document string context
Ericson2314 8200024
review pass
fricklerhandwerk 65dbc95
review pass, reorder sections
fricklerhandwerk 7a5dc32
Merge remote-tracking branch 'upstream/master' into addDrvOutputDepen…
Ericson2314 d2b8bfc
Fix field order
Ericson2314 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,134 @@ | ||
| # String context | ||
|
|
||
| > **Note** | ||
| > | ||
| > This is an advanced topic. | ||
| > The Nix language is designed to be used without the programmer consciously dealing with string contexts or even knowing what they are. | ||
|
|
||
| A string in the Nix language is not just a sequence of characters like strings in other languages. | ||
| It is actually a pair of a sequence of characters and a *string context*. | ||
| The string context is an (unordered) set of *string context elements*. | ||
|
|
||
Ericson2314 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| The purpose of string contexts is to collect non-string values attached to strings via | ||
| [string concatenation](./operators.md#string-concatenation), | ||
| [string interpolation](./string-interpolation.md), | ||
| and similar operations. | ||
fricklerhandwerk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| The idea is that a user can combine together values to create a build instructions for derivations without manually keeping track of where they come from. | ||
| Then the Nix language implicitly does that bookkeeping to efficiently obtain the closure of derivation inputs. | ||
|
|
||
| > **Note** | ||
| > | ||
| > String contexts are *not* explicitly manipulated in idiomatic Nix language code. | ||
|
|
||
| String context elements come in different forms: | ||
|
|
||
| - [deriving path]{#string-context-element-derived-path} | ||
|
|
||
| A string context element of this type is a [deriving path](@docroot@/glossary.md#gloss-deriving-path). | ||
| They can be either of type [constant](#string-context-constant) or [output](#string-context-output), which correspond to the types of deriving paths. | ||
|
|
||
| - [Constant string context elements]{#string-context-constant} | ||
|
|
||
| > **Example** | ||
| > | ||
| > [`builtins.storePath`] creates a string with a single constant string context element: | ||
| > | ||
| > ```nix | ||
| > builtins.getContext (builtins.storePath "/nix/store/wkhdf9jinag5750mqlax6z2zbwhqb76n-hello-2.10") | ||
| > ``` | ||
| > evaluates to | ||
| > ```nix | ||
| > { | ||
| > "/nix/store/wkhdf9jinag5750mqlax6z2zbwhqb76n-hello-2.10" = { | ||
| > path = true; | ||
| > }; | ||
| > } | ||
| > ``` | ||
|
|
||
| [deriving path]: @docroot@/glossary.md#gloss-deriving-path | ||
| [store path]: @docroot@/glossary.md#gloss-store-path | ||
| [`builtins.storePath`]: ./builtins.md#builtins-storePath | ||
|
|
||
| - [Output string context elements]{#string-context-output} | ||
|
|
||
| > **Example** | ||
| > | ||
| > The behavior of string contexts are best demonstrated with a built-in function that is still experimental: [`builtins.outputOf`]. | ||
| > This example will *not* work with stable Nix! | ||
| > | ||
| > ```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-element-derivation-deep} | ||
|
|
||
| *derivation deep* is an advanced feature intended to be used with the | ||
| [`exportReferencesGraph` derivation attribute](./advanced-attributes.html#adv-attr-exportReferencesGraph). | ||
| 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 the outputs of those. | ||
|
|
||
| > **Example** | ||
| > | ||
| > The best way to illustrate *derivation deep* string contexts is with [`builtins.addDrvOutputDependencies`]. | ||
| > Take a regular constant string context element pointing to a derivation, and transform it into a "Derivation deep" string context element. | ||
| > | ||
| > ```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.addDrvOutputDependencies`]: ./builtins.md#builtins-addDrvOutputDependencies | ||
| [`builtins.unsafeDiscardOutputDependency`]: ./builtins.md#builtins-unsafeDiscardOutputDependency | ||
|
|
||
| ## Inspecting string contexts | ||
|
|
||
| Most basically, [`builtins.hasContext`] will tell whether a string has a non-empty context. | ||
|
|
||
| When more granular information is needed, [`builtins.getContext`] can be used. | ||
| It creates an [attribute set] representing the string context, which can be inspected as usual. | ||
|
|
||
| [`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#builtins-unsafeDiscardStringContext) will make a copy of a string, but with an empty string context. | ||
| The returned string can be used in more ways, e.g. by operators that require the string context to be empty. | ||
| The requirement to explicitly discard the string context in such use cases helps ensure that string context elements are not lost by mistake. | ||
| The "unsafe" marker is only there to remind that Nix normally guarantees that dependencies are tracked, whereas the returned string has lost them. | ||
|
|
||
| ## Constructing string contexts | ||
|
|
||
| [`builtins.appendContext`] will create a 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. | ||
| A string with arbitrary contexts can be made like this: | ||
|
|
||
| 1. Create a string with the desired string context elements. | ||
Ericson2314 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| (The contents of the string do not matter.) | ||
| 2. Dump its context with [`builtins.getContext`]. | ||
| 3. Combine it with a base string and repeated [`builtins.appendContext`] calls. | ||
|
|
||
| [`builtins.appendContext`]: ./builtins.md#builtins-appendContext | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.