1

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?

5
  • Does this answer your question? Asking Nix for metadata about the given package -- note that the OP there is explicitly asking where the source comes from, so the answers to that question are a superset of what you're asking for here. Apr 5, 2022 at 14:55
  • @CharlesDuffy I don't think this is a duplicate, because the linked question is not as specific. It wouldn't show up for a search query like this question. Apr 5, 2022 at 17:41
  • @RobertHensing, forming links that wouldn't show up in the search is the entire point of and benefit to having the close-as-duplicate mechanism -- so we can direct folks whose search terms find an answer like this to the existing, pre-answered instance. That's part of why, unlike other close methods, closing a question as a duplicate doesn't auto-delete it and lets it still be upvoted while closed -- because good duplicates (which is to say, ones that use different terminology) add value. Apr 5, 2022 at 17:45
  • Fair enough then Apr 6, 2022 at 11:22
  • Wait, no, this would make it impossible to answer this question, which is different. It should be marked as similar. Apr 6, 2022 at 11:23

1 Answer 1

5

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.