Exploring Nixpkgs lib
Following Nix Hour 71
Setup: Sparse-clone Nixpkgs
Nixpkgs is multi-GB, but you can pull in only what you need:
mkdir ~/src && cd ~/src
git clone --depth=1 --filter=blob:none --sparse https://github.com/NixOS/nixpkgs
cd nixpkgs
git sparse-checkout set lib pkgs
Loading into nix repl
In the video, infinisil runs nix repl default.nix from inside lib/. This
worked in older Nix but fails now:
error: path ".../lib/default.nix" is not a flake (because it's not a directory)
Modern Nix only accepts directories. The equivalent is to import from the repo
root, which wires together lib, pkgs, and everything else in one shot:
cd ~/src/nixpkgs
nix repl .
nix-repl> pkgs = import ./. {}
nix-repl> lib.trivial.version
"26.11.20260902.dirty"
nix-repl> pkgs.hello
«derivation /nix/store/vlp8xby1jjmif5hdsck5vfdq4fljpvli-hello-2.12.3.drv»
Getting rid of the lib. and pkgs. prefixes
In the video, he can type fixedPoints directly instead of lib.fixedPoints.
That’s because the old nix repl <file> behavior spread every key of the
resulting attrset as a top-level repl variable. That’s gone. The manual
equivalent is inherit:
# Pull sub-libraries to the top level
nix-repl> inherit (lib) attrsets strings lists fixedPoints;
nix-repl> fixedPoints.fix
«lambda fix @ /home/jr/src2/nixpkgs/lib/fixed-points.nix:93:5»
# Pull individual packages to the top level
nix-repl> inherit (pkgs) hello git;
nix-repl> hello
«derivation /nix/store/vlp8xby1jjmif5hdsck5vfdq4fljpvli-hello-2.12.3.drv»
inherit (lib) attrsets is just sugar for attrsets = lib.attrsets. It binds
the value as a top-level repl variable.
Alternatively, :l <nixpkgs> loads everything at once but takes longer:
nix-repl> :l <nixpkgs>
Added 17000 variables.
What lib is
lib is Nixpkg’s standard library. A collection of pure Nix utility functions.
It doesn’t depend on packages, so it’s self-contained. (Its tests pull in
packages, but the functions themselves don’t.)
nixpkgs/lib is fairly narrowly focused: it’s purly utility functions for
writing Nix expressions. Things like manipulating attrsets, lists, strings,
generating NixOS module types, and working with file paths. Nix as a language is
pretty minimal by design, so lib is where all the ergonomic helpers live that
you’d otherwise have to rewrite constantly. Importantly, lib doesn’t return
any derivations, its just operates purely at the evaluation level.
The dependency chain in Nixpkgs flows one way:
lib <- pkgs <- nixos
NixOS depends on pkgs, which depends on lib. Each layer is fairly distinct.
It’s organized into sub-libraries, each corresponding to a single .nix file
with a matching name. For example, fixed-points.nix maps to lib.fixedPoints:
ls ~/src/nixpkgs/lib/
fixed-points.nix lists.nix strings.nix attrsets.nix ...
nix-repl> lib.fixedPoints
{
composeExtensions = «lambda composeExtensions @ /home/jr/src/nixpkgs/lib/fixed-points.nix:341:5»;
composeManyExtensions = «lambda foldr @ /home/jr/src/nixpkgs/lib/lists.nix:138:14»;
converge = «lambda converge @ /home/jr/src/nixpkgs/lib/fixed-points.nix:153:5»;
extends = «lambda extends @ /home/jr/src/nixpkgs/lib/fixed-points.nix:326:5»;
fix = «lambda fix @ /home/jr/src/nixpkgs/lib/fixed-points.nix:93:5»;
fix' = «lambda fix' @ /home/jr/src/nixpkgs/lib/fixed-points.nix:119:5»;
makeExtensible = «lambda makeExtensibleWithCustomName @ /home/jr/src/nixpkgs/lib/fixed-points.nix:451:19»;
makeExtensibleWithCustomName = «lambda makeExtensibleWithCustomName @ /home/jr/src/nixpkgs/lib/fixed-points.nix:451:5»;
toExtension = «lambda toExtension @ /home/jr/src/nixpkgs/lib/fixed-points.nix:513:5»;
}
nix-repl> lib.fixedPoints.<TAB>
lib.fixedPoints.composeExtensions lib.fixedPoints.fix'
lib.fixedPoints.composeManyExtensions lib.fixedPoints.makeExtensible
lib.fixedPoints.converge lib.fixedPoints.makeExtensibleWithCustomName
lib.fixedPoints.extends lib.fixedPoints.toExtension
lib.fixedPoints.fix
How lib/default.nix wires it together
default.nix does two things:
- Maps sub-library names to their files:
fixedPoints = callLibs ./fixed-points.nix;
- Re-exposes selected symbols at the top-level
libnamespace:
inherit (self.fixedPoints) fix;
This means many functions are reachable two ways. Via the sub-library or
directly off lib:
nix-repl> lib.fixedPoints.fix
«lambda fix @ /home/jr/src/nixpkgs/lib/fixed-points.nix:93:5»
nix-repl> lib.fix
«lambda fix @ /home/jr/src/nixpkgs/lib/fixed-points.nix:93:5»
Why function equality comparisons return false
Even though both paths point to the same source location, Nix compares lambdas by identity, not by value:
nix-repl> lib.fixedPoints.fix == lib.fix
false
This is a known Nix quirk, functions are never structurally equal, even to themselves through a different binding.
What isn’t at the top level, and why
Not every sub-library function gets re-exposed on lib directly. The main
reason: name collisions. Some function names exist in multiple sub-libraries
with different semantics.
hasPrefix is the clearest example:
nix-repl> lib.strings.hasPrefix "foo" "foobar"
true
nix-repl> lib.lists.hasPrefix [ "foo" ] [ "foobar" ]
false
nix-repl> lib.lists.hasPrefix [ "foo" ] [ "foo" "bar" ]
true
nix-repl> lib.path.hasPrefix /foo /foo/bar
true
Both are legitimately named hasPrefix, but they operate on different types.
Exposing one at the top level would shadow the other.
Functions like attrsets.unionOfDisjoint are also sub-library-only:
nix-repl> inherit (lib) attrsets;
nix-repl> attrsets.unionOfDisjoint { x = 10; } { y = 20; }
{ x = 10; y = 20; }
# Duplicate keys cause an error, not a silent merge:
nix-repl> attrsets.unionOfDisjoint { x = 10; } { y = 20; x = 10; }
{ x = «error: unionOfDisjoint: collision on x»; y = 20; }
# Not on lib directly:
nix-repl> lib.unionOfDisjoint
error: undefined variable 'unionOfDisjoint'
If a function name could plausibly mean different things depending on the type it operates on, it stays namespaced under its sub-library.
Sub-Libraries
Open lib/default.nix, these are the sub-libraries:
lib = makeExtensible' (
self:
let
callLibs = file: import file { lib = self; };
in
{
# often used, or depending on very little
trivial = callLibs ./trivial.nix;
fixedPoints = callLibs ./fixed-points.nix;
# datatypes
attrsets = callLibs ./attrsets.nix;
lists = callLibs ./lists.nix;
strings = callLibs ./strings.nix;
stringsWithDeps = callLibs ./strings-with-deps.nix;
# packaging
customisation = callLibs ./customisation.nix;
derivations = callLibs ./derivations.nix;
maintainers = import ../maintainers/maintainer-list.nix;
teams = callLibs ../maintainers/computed-team-list.nix;
meta = callLibs ./meta.nix;
versions = callLibs ./versions.nix;
# module system
modules = callLibs ./modules.nix;
options = callLibs ./options.nix;
types = callLibs ./types.nix;
# constants
licenses = callLibs ./licenses;
sourceTypes = callLibs ./source-types.nix;
systems = callLibs ./systems;
# serialization
cli = callLibs ./cli.nix;
gvariant = callLibs ./gvariant.nix;
generators = callLibs ./generators.nix;
# misc
asserts = callLibs ./asserts.nix;
debug = callLibs ./debug.nix;
misc = callLibs ./deprecated/misc.nix;
# domain-specific
fetchers = callLibs ./fetchers.nix;
# Eval-time filesystem handling
path = callLibs ./path;
filesystem = callLibs ./filesystem.nix;
fileset = callLibs ./fileset;
sources = callLibs ./sources.nix;
# back-compat aliases
platforms = self.systems.doubles;
# linux kernel configuration
kernel = callLibs ./kernel.nix;
# network
network = callLibs ./network;
# flakes
flakes = callLibs ./flakes.nix;
If we look at trivial.nix, we see that it provides “trivial” functions for
reuse. For example the warn function:
nix-repl> lib.warn "Warning" true
evaluation warning: Warning
true
fixed-points.nix contains nix’s lazy language if you want to do some
self-recursive stuff fixed-points has some utilities for that.
Support functions for packaging in the customization.nix. Such as
callPackageWith, which passes autoArgs to the entire package set.
fetchers.nix, all fetchers rely on packages because you need things like
curl and openssl to verify certificates and stuff like that.
derivations.nix doesn’t return derivations but has useful functions for
derivations.
nix-repl> lib.attrsets.toDerivation /nix/store/zia61mzfpiwp28drxsljp507d5cgw45x-derive_builder-0.20.2.drv
«derivation»
nix-repl> d = attrsets.toDerivation /nix/store/zia61mzfpiwp28drxsljp507d5cgw45x-derive_builder-0.20.2.drv
nix-repl> d.outPath
"/nix/store/zia61mzfpiwp28drxsljp507d5cgw45x-derive_builder-0.20.2.drv"
nix-repl> d.name
"derive_builder-0.20.2.drv"
Then we can try to build that derivation:
nix build -E 'with import ./lib; attrsets.toDerivation /nix/store/zia61mzfpiwp28drxsljp507d5cgw45x-
derive_builder-0.20.2.drv
Working with pkgs from the repl
cd src/nixpkgs/
nix repl .
nix-repl> pkgs = import /home/jr/src/nixpkgs {}
nix-repl> lib.meta.getExe pkgs.hello
"/nix/store/wzr035k31pmpn2caabq8qwv1npg571z9-hello-2.12.3/bin/hello"
nix-repl> :q
nix build /nix/store/wzr035k31pmpn2caabq8qwv1npg571z9-hello-2.12.3/bin/hello
You can also use:
nix repl
nix-repl> :l <nixpkgs>
nix-repl> pkgs.hello
«derivation /nix/store/gx2drhxsvkh7xr490rg7dpqyn53iw3z0-hello-2.12.3.drv»
The Module System is a part of lib
The module system barely interacts with lib but is a part of it.
default.nix:
# module system
modules = callLibs ./modules.nix;
options = callLibs ./options.nix;
types = callLibs ./types.nix;
Generators
You can find serialization formats here:
nix-repl> lib.generators.toYAML {} { foo.bar = [ 10 "baz" ]; }
"{\"foo\":{\"bar\":[10,\"baz\"]}}"
nix-repl> builtins.trace (lib.generators.toYAML {} { foo.bar = [ 10 "baz" ]; }) null
trace: {"foo":{"bar":[10,"baz"]}}
null
You can also use pkgs.formats, better for more complex:
nix-repl> pkgs.format.yaml {}
nix-repl> format = pkgs.formats.yaml {}
nix-repl> format.generate "foo.yaml" { foo = [ "bar" 1 ]; }
«derivation /nix/store/757br628kf4sb7wpi8kdyqfqpqdg7fkr-foo.yaml.drv»
nix-repl> :b format.generate "foo.yaml" { foo = [ "bar" 1 ]; }
This derivation produced the following outputs:
out -> /nix/store/2iz63y8hdqn5rn9amb2pigj5ima54215-foo.yaml
[1 built, 20 copied (151.4 MiB), 61.4 MiB DL]
nix-repl> :q
cat /nix/store/2iz63y8hdqn5rn9amb2pigj5ima54215-foo.yaml
File: /nix/store/2iz63y8hdqn5rn9amb2pigj5ima54215-foo.yaml
%YAML 1.1
---
foo:
- bar
- 1