Shellcheck: Add support for #!/usr/bin/env nix-shell

Created on 9 May 2018  路  12Comments  路  Source: koalaman/shellcheck

For new checks and feature suggestions

Here's a snippet or screenshot that shows the problem:

#!/usr/bin/env nix-shell
true

Here's what shellcheck currently says:

^-- SC1008: This shebang was unrecognized. Note that ShellCheck only handles sh/bash/dash/ksh.

Here's what I wanted or expected to see:

Empty output, because it's a standard way of using nix-shell.

Most helpful comment

I would really love to see shellcheck gain native support for this, so I don't end up with errors in VS Code every time I edit a shell script that uses nix-shell. In my own scripts I can add shell directives, but I'm often opening scripts I don't control.

All 12 comments

I don't know much about nix-shell, from [0], it seems like it downloads nix deps and opens a bash shell, so will it be okay to treat the file as bash?

[0] https://nixos.org/nix/manual/#idm140737318501472

What the file is supposed to contain depends on the -i argument.

This is an example of correct usage:

#!/usr/bin/env nix-shell
#!nix-shell -i bash -p qemu jq ec2_api_tools awscli

The -i can also be sh or zsh, etc.

-i <interpreter> so it can be bash/ruby/python etc. shellcheck could disable itself if != sh/bash.

Can we get some feedback from whoever maintains this that this is a feature that would be accepted?

I am very hesitant to add first-class support for new shells, because it's easy to add but hard to maintain and remove.

I'd prefer a simple third party wrapper script nix-shellcheck that invokes shellcheck -s bash on such files, similar to how hadolint and bats testing works.

@koalaman Thanks for your reply.

I don't know what you mean by first-class support for new shells in this particular case.

The only thing that needs to happen is a conditional to check for the first line being equal to #!/usr/bin/env nix-shell

and in that case, it should look for the -i option. If the -i option is equal to sh it should check the file ignoring the first two lines. If the -i option is equal to zsh (or bash), it should do something similar, etc.

I don't understand your concerns.

@coretemp Would the shell directive be useful in this case?

The shell directive does solve this issue for me! Thanks @qubidt. I just wanted to share an example of how to use it here for the next person stumbling upon this issue:

$ cat test-shell.sh
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p hello
#shellcheck shell=bash

hello
$ ./test-shell.sh
Hello, world!
$ shellcheck test-shell.sh
$ echo $?
0

I would really love to see shellcheck gain native support for this, so I don't end up with errors in VS Code every time I edit a shell script that uses nix-shell. In my own scripts I can add shell directives, but I'm often opening scripts I don't control.

Agree with @lilyball, if a PR implementing support for this (identifying the -i parameter in the second line) be accepted?

@expipiplus1 Sure, it makes sense for autoidentifying purpose

Great, it looks like it would require a slight change to the parser to not ignore additional "shebang"s at the top of the file, at the moment the pretend #!nix-shell -i foo line is ignored as a comment.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

szepeviktor picture szepeviktor  路  4Comments

helau picture helau  路  4Comments

bje- picture bje-  路  3Comments

maxisme picture maxisme  路  3Comments

quchen picture quchen  路  3Comments