Search code examples
nix

How can I search in Nixpkgs for a package expression?


From the manual:

Components are installed from a set of Nix expressions that tell Nix how to build those packages, including, if necessary, their dependencies. There is a collection of Nix expressions called the Nixpkgs package collection that contains packages ranging from basic development stuff such as GCC and Glibc, to end-user applications like Mozilla Firefox.

Lets assume I want to search for the nix expression of the package Go for example. Where should I look in the repository to find the right file?


Solution

  • My tool of choice is nix repl. Often its tab completion is sufficient to find attribute names. Sometimes you might want to use https://search.nixos.org/packages.

    For a convenient workflow, you could set EDITOR and use the :edit command in nix repl. Usually I open nixpkgs in VSCode and then run nix repl . in a VSCode terminal so I can Ctrl+click file locations as well.

    I never use the directory structure, because search is so much more convenient.

    [~/nixpkgs]$ export EDITOR=...  # nano doesn't seem to work for this
    [~/nixpkgs]$ nix repl .
    
    nix-repl> :edit go
    

    or

    nix-repl> go.meta.position
    "~/nixpkgs/pkgs/development/compilers/go/1.17.nix:278"
    

    This generally gives you the location of a mkDerivation call, or a call to a similar function.

    To get the location where an attribute is defined, use

    nix-repl> builtins.unsafeGetAttrPos "go" pkgs
    { column = 3; file = "~/nixpkgs/pkgs/top-level/all-packages.nix"; line = 12753; }
    

    And then there's the recursive directory search option (like grep -R, IDE-integrated search, etc). This generally works really well as package names tend to be specific. Too bad go isn't. We generally don't do crazy code formatting in Nix, a leading space and an equals sign do a pretty good job at finding definitions, even for go, if you ignore the ones in lib/.

    [~/nixpkgs]$ git grep -n ' go ='
    lib/attrsets.nix:125:    go = prefixLength: hasValue: value: updates:
    lib/debug.nix:234:      go = x: generators.toPretty
    lib/deprecated.nix:92:    let go = xs: acc:
    lib/filesystem.nix:29:    let go = path:
    lib/generators.nix:237:    go = indent: v: with builtins;
    lib/trivial.nix:500:      go = i:
    nixos/modules/security/apparmor/includes.nix:9:    let go = { path ? null, mode ? "r", trail ? "" }:
    pkgs/stdenv/booter.nix:63:      go = pred: n:
    pkgs/top-level/all-packages.nix:12753:  go = go_1_17;
    pkgs/top-level/all-packages.nix:21043:    go = buildPackages.go_1_16;
    pkgs/top-level/all-packages.nix:21046:    go = buildPackages.go_1_17;
    pkgs/top-level/all-packages.nix:21049:    go = buildPackages.go_1_18;
    pkgs/top-level/all-packages.nix:21055:    go = buildPackages.go_1_16;
    pkgs/top-level/all-packages.nix:21058:    go = buildPackages.go_1_17;
    pkgs/top-level/all-packages.nix:21061:    go = buildPackages.go_1_18;
    pkgs/top-level/all-packages.nix:26493:    go = go_1_16;