There’s a really convenient way to write quick scripts in haskell on Nix systems, even including haskell packages:
#!/usr/bin/env nix-shell
#!nix-shell --pure -i runghc -p "haskellPackages.ghcWithPackages (pkgs: [ ...all of the packages I want... ])"
main = do
# do stuff
putStrLn "Hello world from a Haskell script!"
This is nice because it runs on any Nix system, whether or not they have a haskell installation. But when I try the same for OCaml I get an error:
Is there any way to get the ocaml interpreter to ignore the shebang lines? It already seems like one of them is being ignore but the second one is causing problems.
I don’t have access to a Nix system, but from what I can tell the problem is caused by a combination of these two factors:
Unlike many other programming languages, OCaml doesn’t use # for comments. The interpreter will ignore the first shebang line but not the second like you noted.
The use of a second shebang line is a workaround for a limitation of the env command as per nix-shell documentation:
The lines starting with #! nix-shell specify nix-shell options (see above). Note that you cannot write #! /usr/bin/env nix-shell -i ... because many operating systems only allow one argument in #! lines.
However, you might find that your env command supports the -S flag (env documentation for GNU coreutils). In that case you could use just one shebang line like this:
Alternatively you could write a wrapper script that filters out the shebang lines with awk or tail and then invokes the OCaml interpreter. Actually, an even simpler way could be a “heredoc”: