Why does nix-instantiate not evaluate a function?

According to nix-instantiate documentation:

When evaluating Nix expressions, the expression evaluator will automatically try to call functions that it encounters. It can automatically call functions for which every argument has a default value (e.g., { argName ? defaultValue }: ...).

But when I try to evaluate my test expression from a file:

{ input ? "whatever" }:

{
  inherit input;
  aString = "something-${input}";
}

I get back a <LAMBDA>:

 > nix-instantiate --eval --strict function.nix    
<LAMBDA>

But when I provide any argument it evaluates just fine:

 > nix-instantiate --eval --strict --arg x 1 function.nix
{ aString = "something-whatever"; input = "whatever"; }

Is this intended behavior? It’s very confusing.

1 Like

This behavior makes zero sense to me:

 > nix-instantiate --eval -E '{ x ? "xxx" }: { y = x; }'     
<LAMBDA>
 > nix-instantiate --eval -E '{ x ? "xxx" }: { y = x; }' -A y
"xxx"

Why does it evaluate it with -A provided?

> nix-instantiate --eval -E '{ x ? "xxx" }: { y = x; }'     
<LAMBDA>

I understand that the behavior is a bit confusing, but it kind of makes sense too. When you evaluate a function, you get a <lambda>, which is exactly the same.

Passing an argument, you explicitly state that you expect a function, and that function to be called with your arg. The same can be mimicked in the expression itself:

$ nix-instantiate --eval -E '({ x ? "xxx" }: { y = x; }) {}'
{ y = "xxx"; }

What confuses me is why the --strict does not work on that lambda. Possibly something to be patched.
As far as I understand, the following should work, but does not. Feels like a bug in either the code or the doc:

$ nix-instantiate --eval --strict -E '{ x ? "xxx" }: { y = x; }'

I agree that the --strict flag here is the issue. If I was not using it’d expect to get back <LAMBDA>, but because I am specifying --strict I’d expect it to evaluate the function:

Although the manual does not say it should evaluate functions:

When used with --eval, recursively evaluate list elements and attributes. Normally, such sub-expressions are left unevaluated (since the Nix expression language is lazy).

But it would be a more intuitive behavior. Thanks for explaining.

1 Like