lib
This page is an overview of the Nix built-in functions, and functions available in Nixpkgs’ lib
.
builtins
)Built-ins are core language functions built directly into the Nix language evaluator. These functions are available through the global builtins
constant.
Resources:
lib.asserts
)Assertions are generally used to check and/or enforce certain requirements in your configuration files. They’re often used to handle conflicts between features or dependencies.
Resources:
lib.attrset
)Attribute sets are collections of name-value pairs (called attributes) enclosed in curly braces ( { }
)
Resources:
lib.customization
)Customization functions let you override derivations and packages.
Sources:
lib.debug
)Debug functions are useful for debugging Nix expressions.
trace
-like functions take two values, print the first to stderr and return the second.traceVal
-like functions take one argument which both printed and returned.traceSeq
-like functions fully evaluate their traced value before printing (not just to “weak head normal form” like trace does by default).-Fn
take an additional function as their first argument, which is applied to the traced value before it is printed.Resources:
lib.fileset
)A safer and more convenient interface for working with local files (compared to nix’s built-in functionality).
Resources:
lib.generators
)Generators can create various file formats from Nix data structures. They all use a similar interface: generator { config-attrs } data
. config-attrs
are used to overwrite the generator’s default attributes.
Resources:
lib.lists
)List manipulation functions let you create and manipulate lists.
Resources:
lib.meta
)Meta functions override metadata attributes about derivations, packages, attribute sets, and other Nix constructs.
Resources:
lib.modules
)Modules functions let you define, evaluate, and merge Nix modules. They also include functions to import module definitions from other formats, like JSON and TOML.
Two of the most commonly used functions in this set are:
mkIf
, which lets you adds conditional statements to your modules, andmkMerge
, which lets you combine multiple module definitions into one.Resources:
lib.options
)Options are attributes commonly used to enable and configure Nix modules. Options are declared using the mkOption
function.
Sources:
lib.sources
)Source filtering functions change how sources are copied to the Nix store. For example, if you want to add a Git repository to your Nix store, but don’t want to include the .git
folder, you can use lib.sources.cleanSource ./folder
.
Sources:
lib.strings
)String manipulation functions are used to create, manipulate, and parse strings (i.e. text). Common uses include:
lib.toJSON
)lib.elem
, lib.elemAt
, lib.substring
, etc.)lib.isInt
, lib.isPath
, etc.)Sources:
lib.trivial
)Miscellaneous functions are functions that don’t fit under any other category. This is a grab bag of different functions, including:
lib.trivial.min
, lib.trivial.max
, etc.)lib.trivial.or
, lib.trivial.and
, etc.)lib.trivial.bitAnd
, lib.trivial.bitOr
, etc.)lib.trivial.version
, lib.trivial.release
)
lib.trivial.oldestSupportedRelease
, lib.trivial.isInOldestRelease
)lib.trivial.warn
, lib.trivial.throwIf
, etc.)Resources:
lib.versions
)Versions functions are for parsing version numbers from a string. This works well in tandem with lib.trivial.version
, which retrieves the current Nixpkgs version.
Resources: