Skip to content

Commit 38bb7f5

Browse files
committed
Document and test -- separator behavior with installables
Clarifies that the first positional argument is always treated as the installable, even after --. Adds tests to prevent accidental change. Addresses #13994
1 parent aa0265f commit 38bb7f5

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/nix/run.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ R""(
3333
# nix run nixpkgs#vim -- --help
3434
```
3535

36+
* Run the default app from the current directory with arguments:
37+
38+
```console
39+
# nix run . -- arg1 arg2
40+
```
41+
42+
Note: The first positional argument is always treated as the *installable*,
43+
even after `--`. To pass arguments to the default installable, specify it
44+
explicitly: `nix run . -- arg1 arg2` or `nix run -- . arg1 arg2`.
45+
3646
# Description
3747

3848
`nix run` builds and runs [*installable*](./nix.md#installables), which must evaluate to an

tests/functional/flakes/run.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,39 @@ echo "_=..." >> "$TEST_ROOT"/actual-env
5555
sort "$TEST_ROOT"/actual-env | uniq > "$TEST_ROOT"/actual-env.sorted
5656
diff "$TEST_ROOT"/expected-env.sorted "$TEST_ROOT"/actual-env.sorted
5757

58+
# Test for issue #13994: verify behavior of -- separator with installable
59+
# Create a flake with an app that prints its arguments
5860
clearStore
61+
rm -rf "$TEST_HOME"/.cache "$TEST_HOME"/.config "$TEST_HOME"/.local
62+
cd "$TEST_HOME"
63+
64+
cat <<'EOF' > print-args.sh
65+
#!/bin/sh
66+
printf "ARGS:"
67+
for arg in "$@"; do
68+
printf " %s" "$arg"
69+
done
70+
printf "\n"
71+
EOF
72+
chmod +x print-args.sh
73+
74+
cat <<EOF > flake.nix
75+
{
76+
outputs = {self}: {
77+
apps.$system.default = {
78+
type = "app";
79+
program = "\${self}/print-args.sh";
80+
};
81+
};
82+
}
83+
EOF
84+
85+
# Test correct usage: installable before --
86+
nix run --no-write-lock-file . -- myarg1 myarg2 2>&1 | grepQuiet "ARGS: myarg1 myarg2"
87+
88+
# Test that first positional argument is still treated as installable after -- (issue #13994)
89+
nix run --no-write-lock-file -- . myarg1 myarg2 2>&1 | grepQuiet "ARGS: myarg1 myarg2"
90+
91+
# And verify that a non-installable first argument causes an error
92+
expectStderr 1 nix run --no-write-lock-file -- myarg1 myarg2 | grepQuiet "error.*myarg1"
5993

0 commit comments

Comments
 (0)