Skip to content

nix-instantiate does not apply --arg #254

Closed

Description

@cstrahan

nix-instantiate does not apply arguments when used with --eval:

$ nix-instantiate --arg 'pkgs' '(import <nixpkgs> {}).pkgs' --eval  test.nix
<LAMBDA>

$ nix-instantiate --arg 'pkgs' '(import <nixpkgs> {}).pkgs' --eval --xml test.nix
<?xml version='1.0' encoding='utf-8'?>
<expr>
  <function column="1" line="1" path="/Users/charlesstrahan/src/cstrahan.github.com/test.nix">
    <attrspat>
      <attr name="pkgs" />
    </attrspat>
  </function>
</expr>

Where test.nix is:

{ pkgs ? (import <nixpkgs> {}).pkgs }:

pkgs.writeTextFile {
  name = "test-file";
  text = ''
    Look! I'm a simple text file.
  '';
}

Activity

  1. shlevy commented on May 12, 2014

    @shlevy
    Member

    This is expected. The file you're evaluating is a function, why should it apply the argument if you're just evaluating? Note that if the body of a function were an attribute set and you request a specific attribute, the argument will be applied, e.g. try nix-instantiate --eval -E '{ foo }: { inherit foo; }' --arg foo true -A foo

  2. cstrahan commented on May 12, 2014

    @cstrahan
    ContributorAuthor

    Ultimately, I just wanted to print the resulting derivation - in ATerm and/or XML. (For context: I'm writing a Nix tutorial in the same vein as "Learn Python The Hard Way", wherein I introduce concepts by way of simple CLI examples, increasing in complexity as I go).

    Perhaps my mental model is off, resulting in faulty expectations; given my current understanding, I can't see why I should need to specify an attr in order for the output to reflect the application. Is this a matter of laziness (i.e. function application is non-strict whereas attr resolution is strict, and thus my original examples results in an unevaluated thunk, whereas yours fully evaluates to true)? If that's the case, I would have expected the result to (at least) be in WHNF. Another thing I just realized: --strict makes no difference, which suggests that this theory isn't accurate.

    If it's not a matter of laziness, I'm not sure what else I might be misunderstanding.

    Does any of the above betray any ignorance/confusion on my part that you might be able to help me better understand? Can you explain why it shouldn't apply the argument? Are there any components of the Nix toolchain that depend on nix-instantiate exhibiting this behavior?

    For my purposes, I can accomplish what I want through indirection - having another "wrapper" file perform the application:

    import ./test.nix {
      inherit (import <nixpkgs> {}) pkgs;
    }
    

    While that works, it's unintuitive (at least to me) and doesn't present quite as well in my tutorial as passing a simple command line argument.

  3. edolstra commented on May 13, 2014

    @edolstra
    Member

    Yeah, it's a bit inconsistent. Note that it does use the arguments in conjunction with -A:

    $ nix-instantiate --eval -E '{ x }: { y = x; }' --arg x 100 -A y
    100
    
  4. cstrahan commented on May 13, 2014

    @cstrahan
    ContributorAuthor

    Awesome - thanks!

  5. added a commit that references this issue on Nov 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions