Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

context

Source: /nix/store/mls72plk3raskl1r5afh3cl9ik3rn969-source/nix/context.nix

Module Description

Context manipulation utilities for attribute-based state

Combinators

has

Extracts a named attribute from a context.

A utility function for working with attrset-based contexts. In Nix, any attrset naturally “has” its attributes, so this is simply attribute access.

Type Signature

AttrSet -> String -> Value

Parameters

  • ctx: An attrset context
  • name: Name of the attribute to extract

Example

has { x = 42; y = 1; } "x"  # => 42

See Also

  • put - Updates an attribute in context
  • lift - Uses has/put implicitly for effect lifting

lift

Lifts an effect to work with larger contexts.

An effect requiring type A can run in any context that has an attribute containing A. This is the standard way to compose effects that work on different parts of a shared context.

Type Signature

String -> Fx<A, V> -> Fx<Ctx, V>

where Ctx is an attrset containing attribute name: A

Parameters

  • name: The attribute name in the larger context
  • e: Effect requiring the inner type

Example

runFx (
  provide { num = 10; other = "x"; } (
    lift "num"
      (pending (n: immediate n (n * 3)))
  )
)  # => 30

How It Works

Uses contraMap to:

  1. Extract the named attribute for the inner effect
  2. Update the named attribute with any state changes

See Also

  • contraMap - Lower-level context transformation
  • lens.zoomOut - Lens-based version

put

Updates a named attribute in a context.

Returns a new attrset with the specified attribute updated. If the attribute doesn’t exist, it is added.

Type Signature

AttrSet -> String -> Value -> AttrSet

Parameters

  • ctx: The original context attrset
  • name: Name of the attribute to update
  • value: New value for the attribute

Example

put { x = 1; } "x" 42    # => { x = 42; }
put { x = 1; } "y" 2     # => { x = 1; y = 2; }

See Also

  • has - Reads an attribute from context
  • state.set - Sets the entire state, not just an attribute

Generated from /nix/store/mls72plk3raskl1r5afh3cl9ik3rn969-source/nix/context.nix