glistix/nix/attrset

Types and functions related to Nix attribute sets.

Types

Represents any attribute set in Nix, containing values of a single type.

pub type AttrSet(value)

Functions

pub fn from_array(attrs: Array(#(String, a))) -> AttrSet(a)

Creates an attribute set from an array of #(name, value) pairs.

If there are two attributes with the same name, the first occurrence takes precedence.

pub fn from_dict(dict: Dict(String, a)) -> AttrSet(a)

Creates an attribute set from a dictionary with string keys.

pub fn from_list(attrs: List(#(String, a))) -> AttrSet(a)

Creates an attribute set from a list of #(name, value) pairs.

If there are two attributes with the same name, the first occurrence takes precedence.

pub fn get(set: AttrSet(a), attr: String) -> Result(a, Nil)

Gets the value associated with that attribute, if it exists.

pub fn intersect(
  first: AttrSet(a),
  with second: AttrSet(b),
) -> AttrSet(a)

Generates an attribute set with all attributes in the first set which have the same name as some attribute in the second set.

pub fn map_values(
  in set: AttrSet(a),
  with fun: fn(String, a) -> b,
) -> AttrSet(b)

Updates values in the given attribute set using the output of the given function, which is called with each attribute’s name and value and returns the new value.

pub fn merge(
  first: AttrSet(a),
  with second: AttrSet(a),
) -> AttrSet(a)

Merges two attribute sets, such that attributes in the second set override those in the first with the same name.

pub fn names(in set: AttrSet(a)) -> Array(String)

Obtains the list of attribute names in the given attribute set. Returns an Array as it uses builtins.attrNames. You can use array.to_list to convert to a List.

Examples

from_list([#("a", 5), #("b", 6)]) |> names
// -> array.from_list(["a", "b"])
pub fn new() -> AttrSet(a)

Creates a new, empty attribute set.

pub fn set(
  set: AttrSet(a),
  at attr: String,
  to value: a,
) -> AttrSet(a)

Updates the value of an attribute in the attribute set, returning the new, updated attribute set.

The original attribute set is NOT changed and is immutable.

pub fn size(set: AttrSet(a)) -> Int

Gets the amount of attributes in the attribute set.

pub fn to_array(set: AttrSet(a)) -> Array(#(String, a))

Obtains an array of #(name, value) pairs from an attribute set.

pub fn to_dict(set: AttrSet(a)) -> Dict(String, a)

Converts an attribute set to a dictionary with string keys.

pub fn to_list(set: AttrSet(a)) -> List(#(String, a))

Obtains a list of #(name, value) pairs from an attribute set.

pub fn values(in set: AttrSet(a)) -> Array(a)

Obtains the list of values in the given attribute set. Returns an Array as it uses builtins.attrValues. You can use array.to_list to convert to a List.

Examples

from_list([#("a", 5), #("b", 6)]) |> values
// -> array.from_list([5, 6])
Search Document