nix-lib documentation


default.nix 

fuseAttrs 

Type: [attrs] -> attrs

Fuses a list of attribute sets into a single attribute set.

fuseAttrs usage example 

x = [ { a = 1; } { b = 2; } ]
fuseAttrs x
=> { a = 1; b = 2; }

mapFuse 

Type: ('a -> attrs) -> ['a] -> attrs

Applies a function to attrsList before fusing the resulting list
of attribute sets.

f : 'a -> attrs

attrsList : ['a]

mapFuse usage example 

x = [ "to" "ta" "ti" ]
f = s: { ${s} = s + s; }
mapFuse f x
=> { to = "toto"; ta = "tata"; ti = "titi"; }

singleAttr 

Type: str -> 'a -> attrs

Equivalent of lib.singleton but for an attribute set.

name : Function argument

value : Function argument

singleAttr usage example 

singleAttr "a" 1
=> { a = 1; }

mapSingleFuse 

Create an attribute set from a list of values, mapping those
values through the function f.

f : Function argument

mapSingleFuse usage example 

mapSingleFuse (x: "val-${x}") [ "a" "b" ]
=> { a = "val-a"; b = "val-b" }

mkRel 

Type: path -> str -> path

Creates a relative path as a string

path : Function argument

file : Function argument

mkRel usage example 

mkRel /home/test/ "file.txt"
=> "/home/test/file.txt"

recursiveFuse 

Type: [attrs] -> attrs

Same as fuseAttrs but using lib.recursiveUpdate to merge attribute
sets together.


mkConfig 

Creates a confugiration by merging enabled modules,
services and extraConfig.

structured function argument

: enabledModules : List of modules to enable with enableModules
enabledServices : List of services to import
extraConfig : Extra configuration, defaults to { }
root : Path relative to which the enabled services will be imported

mkConfig usage example 

mkConfig {
enabledModules = [ "ht-defaults" ];
enabledServices = [ "toto" ];
extraConfig = { services.nginx.enable = true; };
root = ./.;
}
=>
{
imports = [ ./toto ];
ht-defaults.enable = true;
services.nginx.enable = true;
}

nixpkgs.nix 

recursiveUpdateUntil 

Does the same as the update operator '//' except that attributes are
merged until the given predicate is verified. The predicate should
accept 3 arguments which are the path to reach the attribute, a part of
the first attribute set and a part of the second attribute set. When
the predicate is satisfied, the value of the first attribute set is
replaced by the value of the second attribute set.

Inputs 

pred

: Predicate, taking the path to the current attribute as a list of strings for attribute names, and the two values at that path from the original arguments.

lhs

: Left attribute set of the merge.

rhs

: Right attribute set of the merge.

Type 

recursiveUpdateUntil :: ( [ String ] -> AttrSet -> AttrSet -> Bool ) -> AttrSet -> AttrSet -> AttrSet

Examples 

:::

lib.attrsets.recursiveUpdateUntil usage example 

recursiveUpdateUntil (path: l: r: path == ["foo"]) {
# first attribute set
foo.bar = 1;
foo.baz = 2;
bar = 3;
} {
#second attribute set
foo.bar = 1;
foo.quz = 2;
baz = 4;
}

=> {
foo.bar = 1; # 'foo.*' from the second set
foo.quz = 2; #
bar = 3; # 'bar' from the first set
baz = 4; # 'baz' from the second set
}

:::

pred : Function argument

lhs : Function argument

rhs : Function argument


recursiveUpdate 

A recursive variant of the update operator ‘//’. The recursion
stops when one of the attribute values is not an attribute set,
in which case the right hand side value takes precedence over the
left hand side value.

Inputs 

lhs

: Left attribute set of the merge.

rhs

: Right attribute set of the merge.

Type 

recursiveUpdate :: AttrSet -> AttrSet -> AttrSet

Examples 

:::

lib.attrsets.recursiveUpdate usage example 

recursiveUpdate {
boot.loader.grub.enable = true;
boot.loader.grub.device = "/dev/hda";
} {
boot.loader.grub.device = "";
}

returns: {
boot.loader.grub.enable = true;
boot.loader.grub.device = "";
}

:::

lhs : Function argument

rhs : Function argument


hasPrefix 

Determine whether a string has given prefix.

Inputs 

pref
: Prefix to check for

str
: Input string

Type 

hasPrefix :: string -> string -> bool

Examples 

:::

lib.strings.hasPrefix usage example 

hasPrefix "foo" "foobar"
=> true
hasPrefix "foo" "barfoo"
=> false

:::

pref : Function argument

str : Function argument


escape 

Escape occurrence of the elements of list in string by
prefixing it with a backslash.

Inputs 

list
: 1. Function argument

string
: 2. Function argument

Type 

escape :: [string] -> string -> string

Examples 

:::

lib.strings.escape usage example 

escape ["(" ")"] "(foo)"
=> "\\(foo\\)"

:::

list : Function argument


stringToCharacters 

Convert a string s to a list of characters (i.e. singleton strings).
This allows you to, e.g., map a function over each character. However,
note that this will likely be horribly inefficient; Nix is not a
general purpose programming language. Complex string manipulations
should, if appropriate, be done in a derivation.
Also note that Nix treats strings as a list of bytes and thus doesn't
handle unicode.

Inputs 

s
: 1. Function argument

Type 

stringToCharacters :: string -> [string]

Examples 

:::

lib.strings.stringToCharacters usage example 

stringToCharacters ""
=> [ ]
stringToCharacters "abc"
=> [ "a" "b" "c" ]
stringToCharacters "🦄"
=> [ "<EFBFBD>" "<EFBFBD>" "<EFBFBD>" "<EFBFBD>" ]

:::

s : Function argument


escapeRegex 

Turn a string s into an exact regular expression

Inputs 

s
: 1. Function argument

Type 

escapeRegex :: string -> string

Examples 

:::

lib.strings.escapeRegex usage example 

escapeRegex "[^a-z]*"
=> "\\[\\^a-z]\\*"

:::


addContextFrom 

Appends string context from string like object src to target.

:::{.warning}
This is an implementation
detail of Nix and should be used carefully.
:::

Strings in Nix carry an invisible context which is a list of strings
representing store paths. If the string is later used in a derivation
attribute, the derivation will properly populate the inputDrvs and
inputSrcs.

Inputs 

src
: The string to take the context from. If the argument is not a string,
it will be implicitly converted to a string.

target
: The string to append the context to. If the argument is not a string,
it will be implicitly converted to a string.

Type 

addContextFrom :: string -> string -> string

Examples 

:::

lib.strings.addContextFrom usage example 

pkgs = import <nixpkgs> { };
addContextFrom pkgs.coreutils "bar"
=> "bar"

The context can be displayed using the toString function:

nix-repl> builtins.getContext (lib.strings.addContextFrom pkgs.coreutils "bar")
{
"/nix/store/m1s1d2dk2dqqlw3j90jl3cjy2cykbdxz-coreutils-9.5.drv" = { ... };
}

:::

src : Function argument

target : Function argument


splitString 

Cut a string with a separator and produces a list of strings which
were separated by this separator.

Inputs 

sep
: 1. Function argument

s
: 2. Function argument

Type 

splitString :: string -> string -> [string]

Examples 

:::

lib.strings.splitString usage example 

splitString "." "foo.bar.baz"
=> [ "foo" "bar" "baz" ]
splitString "/" "/usr/local/bin"
=> [ "" "usr" "local" "bin" ]

:::

sep : Function argument

s : Function argument


unique 

Remove duplicate elements from the list. O(n^2) complexity.

Inputs 

list

: Input list

Type 

unique :: [a] -> [a]

Examples 

:::

lib.lists.unique usage example 

unique [ 3 2 3 4 ]
=> [ 3 2 4 ]

:::


flip 

Flip the order of the arguments of a binary function.

Inputs 

f

: 1. Function argument

a

: 2. Function argument

b

: 3. Function argument

Type 

flip :: (a -> b -> c) -> (b -> a -> c)

Examples 

:::

lib.trivial.flip usage example 

flip concat [1] [2]
=> [ 2 1 ]

:::

f : Function argument

a : Function argument

b : Function argument