How to use nix-shell with groovy

Hello, I’m trying to run nix-shell with groovy as an interpreter.

#! /usr/bin/env nix-shell
#! nix-shell -i groovy -p groovy

println "hello"

running this gets an error

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/home/john/projects/netbeans/update-nix-dependencies.groovy: 2: unexpected char: '#' @ line 2, column 1.
   #! nix-shell -i groovy -p groovy
   ^

1 error

Is there any way to fix this? I’m not sure if it is it a nix problem or a groovy issue.

This looks like a groovy issue. I’m not particularly familiar with groovy, but my guess is that # is not a comment character, and that groovy as a special case allows the first line to begin with #! but disallows this otherwise.

In this scenario, my recommendation is to instead split it into two files like so:

> cat update-nix-dependencies
#! /usr/bin/env nix-shell
#! nix-shell -i bash -p bash groovy
exec groovy update-nix-dependencies.groovy "$@"
> cat update-nix-dependencies.groovy
println "hello"

Thanks for the help! I created GROOVY-9310 to report the issue.

I started working on the antlr4 grammar to ignore all shebang entries in a groovy script. I was wondering if anyone has an example of a script with more than two directives?

#! /usr/bin/env nix-shell
#! nix-shell -i groovy -p groovy
#! ????

I want to make the lexer so it ignores any number of shebang directives at the top of the file but I also need examples to test with. I thought I saw an example using python somewhere that had 3 directives but I can’t find it. Any thoughts?

I started a PR for this.