Question about --attr in nix-instantiate

I’m trying to figure out what derivation will be built when I’m running nix-instantiate --attr system '<nixpkgs/nixos>' and it seems I don’t understand how --attr work.

When I’m running nix-instantiate --eval '<nixpkgs/nixos>' I see that to level expression of nixpkgs/default.nix at ec1314b6d28559550b1a8bedf646bcb630e72e3d · NixOS/nixpkgs · GitHub is function:

19808a4ad1d1:~# nix-instantiate --eval '<nixpkgs/nixos>'
<LAMBDA>

But according to the nix-instantiate's help --attr option “… Select an attribute from the top-level Nix expression being evaluated. …”. How can we select attribute of the function?

when nix-instantiate evaluates its expression and finds that it is a function it’ll automatically call that function with the argument set provided on the command line (or the empty set, if no arguments are given). the attribute selected by --attr is pulled from the result of that evaluation:

~ % nix-instantiate --eval --expr '{ a = 1; }' -A a                    
1
~ % nix-instantiate --eval --expr '{ a ? 1 }: { inherit a; }'               
<LAMBDA>
~ % nix-instantiate --eval --expr '{ a ? 1 }: { inherit a; }' -A a
1
~ % nix-instantiate --eval --expr '{ a ? 1 }: { inherit a; }' -A a --argstr a test
"test"
1 Like