Skip to content

Commit 1a0f607

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 1a0f607

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,41 @@ 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+
output=$(nix run --no-write-lock-file . -- myarg1 myarg2 2>&1)
87+
[[ "$output" == "ARGS: myarg1 myarg2" ]] || fail "Expected 'ARGS: myarg1 myarg2', got '$output'"
88+
89+
# Test that first positional argument is still treated as installable after -- (issue #13994)
90+
output=$(nix run --no-write-lock-file -- . myarg1 myarg2 2>&1)
91+
[[ "$output" == "ARGS: myarg1 myarg2" ]] || fail "Expected 'ARGS: myarg1 myarg2', got '$output'"
92+
93+
# And verify that a non-installable first argument causes an error
94+
expectStderr 1 nix run --no-write-lock-file -- myarg1 myarg2 | grepQuiet "error.*myarg1"
5995

0 commit comments

Comments
 (0)