Using nix-shell shebang with Cabal & Stack

I’m reading this section from the Nix Manual. I’m trying to run some turtle scripts, but I’m having a hard time making them work. Also I have neither cabal nor stack installed globally, which would probably made this a lot easier.

Here’s some minimally broken examples I’ve come with:

#! /usr/bin/env nix-shell
#! nix-shell -p stack
-- stack --resolver lts-14.00 script --package turtle --no-nix-pure

{-# LANGUAGE OverloadedStrings #-}

import           Turtle

main :: IO()
main = sh $ do
    echo "Hello World"

this one does not break, but gives me a nix-shell instead of trying to run the script.

This second example is taken from the docs, it tries to run the script but fails:

#! /usr/bin/env nix-shell
#! nix-shell -i runghc -p haskellPackages.ghc haskellPackages.turtle

{-# LANGUAGE OverloadedStrings #-}

import           Turtle

main :: IO()
main = sh $ do
    echo "Hello World"

giving the following error:

    Could not find module ‘Turtle’
    Use -v to see a list of the files searched for.
  |
6 | import           Turtle
  | ^^^^^^^^^^^^^^^^^^^^^^^

Any hints on how I could made some or both of these scripts works with the nix-shell shebang?

#! /usr/bin/env nix-shell
#! nix-shell -i runghc -p "haskellPackages.ghcWithPackages (p: [p.turtle])"

{-# LANGUAGE OverloadedStrings #-}

import           Turtle

main :: IO()
main = sh $ do
    echo "Hello World"

1 Like

For completeness here’s how to do it with stack instead of a wrapped ghc.

#! /usr/bin/env nix-shell
-- stack --resolver lts-16.0 script --package turtle --no-nix-pure
#! nix-shell -p stack -i stack

{-# LANGUAGE OverloadedStrings #-}

import           Turtle

main :: IO()
main = sh $ do
  echo "Hello World"

3 Likes

you can use 2 shebang lines …? wow :brain: . Didn’t realize this was even a possibility.

2 Likes