Featured image of post Why can you import nixpkgs { }?

Why can you import nixpkgs { }?

Have you ever wondered about import nixpkgs { } or do you internalized the snippet as a beginner and never gave it a second look?

When reading nix.dev’s import documentation, I felt it is missing an important hint that is described in the Nix Reference Manual’s import documentation.

Both say that import expects a path. And furher, if path is a file, path is imported. And if path is a directory, path/default.nix is imported.

So, you might wonder: Is nixpkgs a path to a file or a path to a directory?

Usually, nixpkgs is declared in a flake’s inputs and passed to its outputs as parameter, e.g., …

{
  inputs = {
    nixpkgs.url = "https://channels.nixos.org/nixpkgs-unstable/nixexprs.tar.zst";
  };

  outputs =
    { self, nixpkgs }:
    let
      pkgs = import nixpkgs { ... };
    in
    {
      ...
    };
}

But, clearly nixpkgs is not a path, right? Let’s check in the nix repl.

Note: :lf <ref> loads the Nix flake and adds it to scope. :t <expr> describes the result of an evaluation.

$ nix repl
nix-repl> :lf .
Added 12 variables.
_type, dirtyRev, dirtyShortRev, inputs, lastModified, lastModifiedDate, narHash, outPath, outputs, packages, sourceInfo, submodules

nix-repl> :t ./.
a path

nix-repl> :t inputs.nixpkgs
a set

As we can see, nixpkgs is a set! But what happens when we import a set?

nix-repl> import { }
error:
       … while calling the 'import' builtin
         at «string»:1:1:
            1| import { }
             | ^

       … while realising the context of a path

       error: cannot coerce a set to a string: { }

The Nix reference manual has a hint:

The path argument must meet the same criteria as an interpolated expression.

What this says is that a set can be coerced to a path or a string when it has either of the __toString or outPath attributes.

nix-repl> a = { __toString = self: "AAA"; } 

nix-repl> "${a}"
"AAA"
nix-repl> b = { outPath = "BBB";} 

nix-repl> :p b
{ outPath = "BBB"; }

nix-repl> "${b}"
"BBB"

So, to follow our import nixpkgs { }, we need to examine its __toString and outPath attributes.

Let’s see …

nix-repl> inputs.nixpkgs.__toString
error: attribute '__toString' missing
       at «string»:1:1:
            1| inputs.nixpkgs.__toString
             | ^

Let’s see again …

nix-repl> inputs.nixpkgs.outPath   
"/nix/store/4b8fr08m7yklfr8vmfsjsclvv2aa0764-source"

Bingo! outPath is set to a directory, and according to the documentation this means the default.nix in this directory will be imported.

Let’s see where this default.nix is located …

$ ls /nix/store/4b8fr08m7yklfr8vmfsjsclvv2aa0764-source
ci  CONTRIBUTING.md  COPYING  default.nix  doc  flake.nix  lib  maintainers  modules  nixos  pkgs  README.md  shell.nix

As a last step, let’s verify that default.nix evaluates to a function since our snippet is calling it with { }.

nix-repl> :t import inputs.nixpkgs
a function

We can keep following the rabbit hole and see how this whole machinery works with …

nix-repl> :edit inputs.nixpkgs.outPath + "/default.nix"
nix-repl> :edit import inputs.nixpkgs
Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy