Why can't I access Flake attributes in Nix repl?

I’m trying to examine contents of one of the inputs, but if I just use Nix repl I see no inputs:

 > nix repl .
Nix 2.28.3
Type :? for help.
Loading installable 'git+file:///home/jakubgs/work/infra-lido#'...
Added 9 variables.
nix-repl> inputs
error: undefined variable 'inputs'
       at «string»:1:1:
            1| inputs
             | ^

But I can get them using builtins.getFlake:

nix-repl> flake = builtins.getFlake ./.
nix-repl> flake.inputs.<tab>
flake.inputs.nixpkgs            flake.inputs.nixpkgs-unstable

Am I missing something or is that the only way to get the other Flake attributes that are not in outputs?

The output of builtins.getFlake (and the flake inputs provided to a flake’s outputs function) has some extra attrs merged into it for convenience. nix repl is only loading the true outputs (which you can see under (builtins.getFlake <whatever>).outputs).

nix build and nix eval also only operate on the true outputs. I personally have my flake export its own inputs as an output, just for convenience.

1 Like

try nix repl -f .

https://nix.dev/manual/nix/2.22/command-ref/new-cli/nix3-repl#options-that-change-the-interpretation-of-installables

1 Like

-f would make it load the default.nix from the current directory, completely ignoring the flake. That’s certainly not what the OP is asking for.

2 Likes

If you use :lf .# the other attributes are also available in e.g. inputs.

1 Like

Use :lf . if you’re in the same directory as your flake.nix file

$ nix repl
Nix 2.28.3
Type :? for help.
nix-repl> :lf .                                                                                         
Added 10 variables.

Or specify the flake explicitly:

nix-repl> :lf github:bluemonk/neovim-devenv
Added 12 variables.

nix-repl> outputs.packages.x86_64-linux
{
  default = «derivation /nix/store/8icsssld8af6fl76d4ii6vilhlbs5qbk-neovim-0.11.2.drv»;
}

You can access all the attributes:

nix-repl> :lf github:nixos/nixpkgs?ref=nixos-unstable                                                   
Added 17 variables.

nix-repl> outputs.lib.lists.length (outputs.lib.attrNames outputs.legacyPackages.x86_64-linux)          
25148
1 Like

Yeah, you’re right, seems like :lf . is the simplest solution to this.

But using -f also works in a way:

 > nix repl -f flake.nix 
Nix 2.28.3
Type :? for help.
Loading installable ''...
Added 3 variables.
nix-repl> inputs 
{
  cachix = { ... };
  disko = { ... };
...

How do you do that? If add inherit inputs; into packages I just get:

       error: attribute 'i686-linux' missing
       at /nix/store/kp5vgc1iskr2alqjndlllb6lgk2ay8sx-source/flake.nix:69:28:
           68|       checks = forAllSystems (system: {
           69|         pre-commit-check = git-hooks.lib.${system}.run {
             |                            ^
           70|           src = ./.;

Which I have no idea what that is about.

inputs isn’t a package, so putting it under packages definitely isn’t right, especially since packages is system-spaced, and inputs isn’t a system string (which is what that error is about, I believe).

I just put it directly in the outputs attrset. It’s not part of the standard flake schema, but nothing forces you to fit everything into the schema. The nix CLI just won’t be able to do much with an output that doesn’t follow the schema. It still works with nix eval and such, though, so it’s fine for debugging purposes.

1 Like

If you put it in lib it won’t throw warnings either.

1 Like