Nix
"Nix" is an ambiguous term that can refer to the Nix programming language, the package manager, the collection of packages Nixpkgs, and the operating system NixOS (at the very least).
These parts are documented in the Nix Reference Manual, the Nixpkgs Manual, the NixOS Manual, a short-form tutorial series called the Nix Pills, and the official documentation nix.dev.
nix (language)
The first layer in the stack is the Nix DSL (or the Nix expression language). The official documentation provides a quick tutorial and the official manual provides a comprehensive reference.
The editor tooling I use is
- lsp: nil (alternative: rnix-lsp)
- formatter: nixpkgs-fmt (alternative: alejandra)
- linter: statix
Despite the relative simplicity of the language ("JSON with functions"), there can still be unusual behavior. Within a few days of playing around with the expression language, I found what I thought was a interpreter bug. I posted it on the Discourse (and filed an issue), which caught the attention of a long-time Nix contributor, which led to increased attention on a few old outstanding issues and finally led to a fix which landed in 2.17. So even very simple languages can have nasty parser bugs!
nix (package manager)
Important paths
-
/nix/store
: Nix store (built derivations, where everything lives) -
/nix/var/nix/gcroots
: Roots of the garbage collector -
/nix/var/nix/profiles
: System profiles -
/nix/var/nix/profiles/system
: Current system -
/etc/profiles/per-user/<user>
: User packages -
~/.local/state/nix/profiles
: User profiles (if using xdg) -
~/.local/state/nix/profiles/home-manager
: Current Home Manager generation ~/.nix-profile
(if not using xdg)
nix (cli)
Here are some quick recipes for common tasks.
-
enter a (bash) shell with a given package
nix-shell -I nixpkgs=$(nixpkgs) -p python3
or, to use the current shell,
nix shell $(nixpkgs)#python3
where the
$(nixpkgs)
syntax is explained in "removing channels and flake registries". -
query list of dependencies of (current) system
nix-store --query --requisites /nix/var/nix/profiles/system
-
in tree format
nix-store --query --tree /nix/var/nix/profiles/system
-
list of things referring to a store path
nix-store --query --referrers <store-path>
-
optimize nix store (dedup)
nix-store --optimise
or
nix store optimise
-
nix-env --delete-generations old # all non-current generations nix-env --delete-generations 14d # generations older than 14 days nix-store --gc
or use
nix-collect-garbage -d
which essentially wraps the abovenix-collect-garbage --delete-old nix-collect-garbage --delete-older-than 14d
There is a difference between running with
sudo
(system) and nosudo
(user); try--dry-run
. -
verify nix store paths are valid (hashes match and it is trusted)
nix store verify --all
-
why does one package depend on another?
nix why-depends $(nixpkgs)#zotero $(nixpkgs)#nss
/nix/store/ihp6sm6xn1q19pblxb968q3cm8x9aimq-zotero-6.0.27 └───/nix/store/mbyn9dp2pf3vfsp82g0a289ldck3xibw-nss-3.90
-
why does my (current) system depend on a package?
nix why-depends /nix/var/nix/profiles/system $(nixpkgs)#nss
/nix/store/7hjlhfzzf4ricswgm1wzvpaac34pwvbm-nixos-system-sora-23.11.20231009.f99e5f0 └───/nix/store/bvsjja2xsx2z68h52wxwcriw9vjjzazb-etc └───/nix/store/6xk1k4kl42qqkds2vrprm0mbp1k2mn0l-user-environment └───/nix/store/baxyh4bqi0amw2pi6gv5c28b6lr75jzb-home-manager-path └───/nix/store/ihp6sm6xn1q19pblxb968q3cm8x9aimq-zotero-6.0.27 └───/nix/store/mbyn9dp2pf3vfsp82g0a289ldck3xibw-nss-3.90
-
pass
--precise
to see more information on each edge/nix/store/7hjlhfzzf4ricswgm1wzvpaac34pwvbm-nixos-system-sora-23.11.20231009.f99e5f0 → /nix/store/bvsjja2xsx2z68h52wxwcriw9vjjzazb-etc → /nix/store/6xk1k4kl42qqkds2vrprm0mbp1k2mn0l-user-environment → /nix/store/baxyh4bqi0amw2pi6gv5c28b6lr75jzb-home-manager-path → /nix/store/ihp6sm6xn1q19pblxb968q3cm8x9aimq-zotero-6.0.27 → /nix/store/mbyn9dp2pf3vfsp82g0a289ldck3xibw-nss-3.90 └───activate: …fsx38qi-setup-etc.pl /nix/store/bvsjja2xsx2z68h52wxwcriw9vjjzazb-etc/etc...if (( _localstatus > … └───etc/profiles/per-user/ikue -> /nix/store/6xk1k4kl42qqkds2vrprm0mbp1k2mn0l-user-environment └───bin/accessdb -> /nix/store/baxyh4bqi0amw2pi6gv5c28b6lr75jzb-home-manager-path/bin/accessdb └───bin/.zotero-wrapped -> /nix/store/ihp6sm6xn1q19pblxb968q3cm8x9aimq-zotero-6.0.27/bin/.zotero-wrapped └───usr/lib/zotero-bin-6.0.27/gmp-clearkey/0.1/libclearkey.so: …sm01mc-nspr-4.35/lib:/nix/store/mbyn9dp2pf3vfsp82g0a289ldck3xibw-nss-3.90/lib:/nix/store/73whsps…
-
and
--all
for all paths, not just the shortest one (or both flags)/nix/store/7hjlhfzzf4ricswgm1wzvpaac34pwvbm-nixos-system-sora-23.11.20231009.f99e5f0 └───/nix/store/bvsjja2xsx2z68h52wxwcriw9vjjzazb-etc ├───/nix/store/6xk1k4kl42qqkds2vrprm0mbp1k2mn0l-user-environment │ └───/nix/store/baxyh4bqi0amw2pi6gv5c28b6lr75jzb-home-manager-path │ ├───/nix/store/ihp6sm6xn1q19pblxb968q3cm8x9aimq-zotero-6.0.27 │ │ └───/nix/store/mbyn9dp2pf3vfsp82g0a289ldck3xibw-nss-3.90 │ └───/nix/store/9qmg4vg8hrs6pbbd4cxjrq4jb8fcyxk7-chromium-117.0.5938.149 │ └───/nix/store/ypas0qsb3ikz6k84bk8q89qjlyr9snk5-chromium-unwrapped-117.0.5938.149 │ └───/nix/store/mbyn9dp2pf3vfsp82g0a289ldck3xibw-nss-3.90 └───/nix/store/hrm25v2z602j1qywsia9x638wv1l41f5-system-units └───/nix/store/3dxhmg5jabmihc14j8m0b1rlaq6p3inq-unit-home-manager-ikue.service └───/nix/store/j6xq61kffmfzqcnhgd32ia13z8yl3hk0-home-manager-generation ├───/nix/store/baxyh4bqi0amw2pi6gv5c28b6lr75jzb-home-manager-path └───/nix/store/fncqlph162dpxh4x4879m2y6zy33fkyf-home-manager-files └───/nix/store/3haw6qx1gmyka60xnrs8mi7d4c81pv6l-hm_fontconfigconf.d10hmfonts.conf └───/nix/store/baxyh4bqi0amw2pi6gv5c28b6lr75jzb-home-manager-path