How to pass multiple arguments to "nix-shell --run" without quoting arguments with single quotes?

On aws-vault, multiple arguments can be passed easily without quoting arguments with single quotes, but nix-shell does not support that.

aws-vault exec satellite-dev -- aws sts get-caller-identity --output text

-- here prevents aws-vault from getting confused when aws-vault parse arguments.
Without --, aws-vault would think that --output option is passed to itself, not aws command.

aws-vault: error: unknown long flag '--output', try --help

Since nix-shell does not seem to support this, making passing multiple argument a complicated thing.

Neither

nix-shell -p awscli2 --run aws sts get-caller-identity --output text

or

nix-shell -p awscli2 --run -- aws sts get-caller-identity --output text

works. Fails with error: unrecognised flag '--output'

If arguments don’t contain ones starting with hyphens, nix-shell tries to parse arguments as nix expressions.

nix-shell -p awscli2 --run -- aws sts get-caller-identity
error: undefined variable 'sts' at (string):1:110
(use '--show-trace' to show detailed location information)

So, I ended up quoting all arguments with single quotes, but quoting arguments every time is not very convenient. If arguments contains spaces it will get complicated further.

nix-shell -p awscli2 --run -- 'aws sts get-caller-identity --output text'

It also makes writing wrapper script that runs command on nix-shell complicated. The shell script below will not work with multiple arguments, because with $@, all arguments remain split (and nix-shell --run does not support multiple arguments).

#!/usr/bin/env bash -eux
nix-shell -p awscli2 --run "$@"

$* combines all arguments to single string, but also re-splits arguments, causing problems with spaces. Are there any workarounds? Or will nix-shell supports passing multiple arguments in the future?

nix run works this way, I believe.

2 Likes

You can not do what you want with nix-shell, though its more modern variant nix shell from nix 2.4+ or nix shell and nix run from 2.3 or older, allow for the style you ask for.

Be aware that using nix shell in 2.4 requires you to enable the experimental features nix-command and flakes.

2 Likes