2

Is it possible to break up a long #! nix-shell -p line into multiple lines?

Example:

#!/usr/bin/env nix-shell
#! nix-shell -p "haskell.packages.ghc822.ghcWithPackages (p: with p; [lens text bytestring hspec brick async])"
#! nix-shell -i "runghc"

{-# language OverloadedStrings #-}

import            Data.Text
import            Control.Lens
import            Data.Monoid

main = print $ ("foo" :: Text, True) & _1 %~ (<> " bar")

I've tried like this, but unsurprisingly that GHC doesn't see the lens and text packages.

#!/usr/bin/env nix-shell
#! nix-shell -p "haskell.packages.ghc822.text"
#! nix-shell -p "haskell.packages.ghc822.lens"
#! nix-shell -p "haskell.packages.ghc822.ghc"
#! nix-shell -i "runghc"

{-# language OverloadedStrings #-}

import            Data.Text
import            Control.Lens
import            Data.Monoid

main = print $ ("foo" :: Texts, True) & _1 %~ (<> " bar")

I vaguely remember that someone on GitHub wrote a custom #! interpreter for nix that allows for special handling of Haskell (and other) package dependencies, allowing them to break up into multiple lines, but its name eludes me and I can't find it. Perhaps some of you remember?

Any other ideas I could try?

Wizek
  • 4,854
  • 2
  • 25
  • 52

1 Answers1

3

nix-shell -p reuses Nixpkgs' stdenv to do the combining of packages into appropriate environment variables. That works fine for shell commands, some libraries etc, but not for Haskell packages.

Haskell packages require a specially wrapped GHC, which is too specific for stdenv. You can do it manually using the ghcWithPackages function:

#!/usr/bin/env nix-shell
#!nix-shell -p 'haskellPackages.ghcWithPackages(pkgs: with pkgs; [lens acme-missiles])'
#!nix-shell -i runhaskell

import Acme.Missiles
import Control.Lens

main =
  if Just (Just True) ^.. traverse . traverse == [True]
  then putStrLn "Crisis averted"
  else launchMissiles

So to answer your first question, it can not be broken up in the way you like. All Haskell dependencies need to be in a single -p/--packages option and the nix-shell shebang parser does not currently support "shell words" across lines. (I suppose it could be added in a non-breaking way if you feel like it...)

Robert Hensing
  • 6,708
  • 18
  • 23