Finding things in nixpkgs and NixOS source
Jade Lovelace <jade at jade dot fyi>
Get the slides at
https://jade.fyi/nixcon2023/slides
# whoami * Author of nix-doc (not to be confused with nixdoc) * Most involved in Nix for Haskell and Rust packaging * I use NixOS as a dev workstation and server
# the sources are for everyone * We do need to improve our documentation * Nix is uniquely participatory: a *lot* of us write modules and packages, which means reading sources * Navigating the source is a bit of witchcraft, and there's a lot of it * With good tooling, the sources of the docs sometimes are faster than the docs
# motivation (2.94 million lines of it)
# roadmap * nix * static analysis * nixd * ctags * dynamic analysis * using the repl * using nix-doc * nixos * ctags * repl usage
# Static analysis * Any method that takes 15 seconds or less and doesn't involve running any code * I am showing the fancy ways of doing it * ripgrep is static analysis too; it's a good fallback
# nixd Newish language server, using Nix as a library. In nixpkgs as `nixd`. Good for: * need go-to-definition * interactive help writing nix code Limitations: * does not eval everything * needs some setup per-project * completions are imperfect but they exist * not sure how to get goto definition to go past args for some things
# nixd setup Install from nixpkgs and configure in your editor. Then: `.nixd.json`:
# demo (nixd) FIXME
# ctags * Very old and simple source code index format * Usually gets through abstraction by being too naive to get broken * Typically generated by simple parse-tree traversal * Wide editor support ([VSCode extension](https://marketplace.visualstudio.com/items?itemName=jaydenlin.ctags-support), built into vim/emacs) Sample: ``` boot.initrd.luks.reusePassphrases nixos/modules/system/boot/luksroot.nix 533 ```
# ctags on nix * `nix-doc tags .` in nixpkgs, then in vim you can `:tj fixedPoints`, `C-]`, etc. * [WIP ctags for NixOS options](https://github.com/NixOS/nixpkgs/pull/249243)
# demo (ctags on nix) FIXME
# summary (static analysis)
# dynamic analysis * Static analysis often doesn't cut it, power tools time! * `nix repl` is your friend
# using the repl (nix) * `nix repl -f .`, `nix repl -f '<nixpkgs>` * `:lf flake-ref` (more on this later) * `:e derivation-or-function` to open in editor
# getting docs in the repl
# getting docs in the repl (ii)
Oops. Related: * [RFC 0145](https://github.com/NixOS/rfcs/pull/145) * [nix#3904](https://github.com/NixOS/nix/issues/3904)
# nix-doc?
# using nix-doc with the repl
# `builtins.unsafeGetAttrPos` Probably the biggest dynamic analysis hammer.
# summary (dynamic analysis) * `:e function/package` * \+ works pretty well * \+ built-in * \- issues with wrappers (e.g. `fetchFromGitHub`: `lib.makeOverridable`) * \- doesn't help for attr sets * `builtins.doc` (nix-doc) * \+ concise output on functions * \- issues with wrappers * `builtins.unsafeGetAttrPos` * \+ can get you close to the source if other things fail due to abstraction * \- unergonomic args and return value for interactive use
# NixOS ctags ([PR](https://github.com/NixOS/nixpkgs/pull/249243)) With the PR checked out:
Then add `opts-tags` to ctags search path (`:set tags+=opts-tags` in vim).
# NixOS ctags (demo) FIXME
# NixOS in the repl Getting the configuration into the repl: * Non-flakes: `nix repl -I nixos-config=/path/to/configuration.nix -f <nixpkgs/nixos>` * Flakes: `nix repl`, `:lf .`, `nixosConfigurations.xx.{....}`
# NixOS in the repl (demo) FIXME