NixOS / nix

Nix, the purely functional package manager
https://nixos.org/
GNU Lesser General Public License v2.1
12.66k stars 1.51k forks source link

nix eval `--arg`/`--argstr` not working #2678

Open msteen opened 5 years ago

msteen commented 5 years ago

According to nix eval --help, it has support for --arg and --argstr:

Usage: nix eval <FLAGS>... <INSTALLABLE>

Summary: evaluate a Nix expression.

Flags:
      --arg <NAME> <EXPR>       argument to be passed to Nix functions
      --argstr <NAME> <STRING>  string-valued argument to be passed to Nix functions
  -f, --file <FILE>             evaluate FILE rather than the default
  -I, --include <PATH>          add a path to the list of locations used to look up <...> file names
      --json                    produce JSON output
      --raw                     print strings unquoted

I assume it works like it does for nix-instantiate and others, but when I try to use it, they seem to be ignored:

$ nix eval '({ foo }: foo)' --arg foo true
<LAMBDA>

I also tried supplying a file to see if it was only not working for expressions, but alas:

$ cat ./foo.nix
{ foo }: foo
$ nix eval --file ./foo.nix '' --arg foo true
<LAMBDA>

While the same expression with nix-instantiate does work as expected:

$ nix-instantiate --eval --expr '({ foo }: foo)' --arg foo true
true

Am I misunderstanding something, or is this a bug?

For now I work around it by injecting the string literal directly into my expression by first escaping it:

# Based on `escapeNixString`:
# https://github.com/NixOS/nixpkgs/blob/d4224f05074b6b8b44fd9bd68e12d4f55341b872/lib/strings.nix#L316
nix_str() {
  str=$(jq --null-input --arg str "$*" '$str')
  printf '%s' "${str//\$/\\\$}"
}
$ nix-info
system: "x86_64-linux", multi-user?: yes, version: nix-env (Nix) 2.1.3, nixpkgs: /wheel/fork/nixpkgs
erikarvstedt commented 4 years ago

The issue still persists. Another workaround is:

mystr="content" nix eval 'import ./dostuff.nix (getenv "mystr")'
joefiorini commented 4 years ago

I ran into this same issue today on the latest nix preview. Seems it's still broken. The workaround is still valid, however the command is a little different:

mystr="content" nix eval --impure --expr 'import ./dostuff.nix (getenv "mystr")'
stale[bot] commented 3 years ago

I marked this as stale due to inactivity. → More info

oxalica commented 3 years ago

Still important. Also run into this issue.

stale[bot] commented 2 years ago

I marked this as stale due to inactivity. → More info

oxalica commented 2 years ago

The issue remains in Nix 2.4

lilyball commented 2 years ago

I would really love to have a solution here. I especially want --argstr support so I can pass arbitrary strings into expressions without having to worry about escaping them properly.

adrian-gierakowski commented 2 years ago

the issue is still present in 2.9.1

GuillaumeDesforges commented 2 years ago

Also ran into this issue.

adrian-gierakowski commented 2 years ago

@edolstra is this intentional or is this a bug? If the former we should update nix eval‘s documentation (I’d be happy to submit a PR).

adrian-gierakowski commented 2 years ago

Bump

aakropotkin commented 1 year ago

If I were going to fix this to essentially auto call files which are functions and pass args exactly like nix build, would that be something that could merge? ( assuming it was implemented in a sane manner )

@edolstra

Ericson2314 commented 9 months ago

Discussed in Today's Nix team meeting:

  • --arg and --apply can be unified with something discussed previously
    • nix build --apply '{ hello, foo }: ...' hello=nixpkgs#hello foo=nixpkgs#firefox
    • nix build --apply '{ f, hello}: f hello' hello=nixpkgs#hello f=myFlake#myFun
    • perhaps nix build --expr 'f hello' --let f=nixpkgs#bundle hello=nixpkgs#hello
  • overrideInputs and impure
  • how much of the language do we want to reimplement as CLI flags? (and how)
    • @infinisil's pure eval proposal may affect the design
  • nix eval --expr ... --arg ... is already quite similar to --apply proposals above

This depends on too much unsettled stuff, let's postpone it until we have more information to inform the design.

As a stop-gap, can make --arg/--argstr without attribute selection an error rather than silently ignored.

The gist of it is that we don't yet know what we want, and we don't think we'll be able to figure out very soon / other stuff on the CLI stabilization hot-path comes first (we can leave these flags experimental even if the base nix eval is stable).

The stop gap however at least will make it less surprising --- rather than the flags silently being ignored, it will be an error that they are used.

nixos-discourse commented 9 months ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2024-01-08-nix-team-meeting-minutes-114/38156/1

ad-si commented 8 months ago

Another workaround using --apply:

nix eval --expr '({ foo }: foo)' --apply 'f: f { foo = true; }'