Stack interpreter option - Add an external dependency - haskell

I'm making a script with Turtle and I need a dependency from GitHub. I saw that you can add such a dependency when you make a project by putting this in your stack.yaml:
packages:
- location:
git: https://github.com/githubuser/reponame.git
commit: somecommitID
But is it possible to add it via the command line?
This is the command line used to run the script:
stack --resolver lts-3.2 --install-ghc runghc --package turtle
Edit:
These are the first lines of my script:
#!/usr/bin/env stack
-- stack --resolver lts-3.1 --install-ghc runghc --package turtle
import Turtle
...

We do not support all of stack.yaml options on the commandline. I recommend putting a stack.yaml somewhere. If you don't want to just put it in the same folder as the script, use "--stack-yaml"

Related

Haskell stack script with a dependency from Github?

Is there a way to run a stack script (https://docs.haskellstack.org/en/stable/GUIDE/#script-interpreter) with a dependency from Github? I would like to run a script with the dependency somePackageFromGithub, so e.g.:
#!/usr/bin/env stack
-- stack --resolver lts-18.0 script --package somePackageFromGithub
module Main where
main :: IO ()
main = putStrLn "Hello World!"
Usually I would put this in the stack.yaml file as an extra dependency:
extra-deps:
- git: https://github.com/user/somePackageFromGithub.git
commit: "some hash"
But the stack script subcommand does not allow to specify a stack.yaml file (v.2.7.1): Ignoring override stack.yaml file for script command. Is there a way around this limitation of the stack cli interface?

How to compile a haskell/stack script into an executable?

This question is similar to Compile stack script instead of running it but I don't think it has been answered completely.
I have a script which I want to run in two ways:
Using ./Script.hs when I am developing it locally, and need a fast develop-run-test cycle
Compiling it to a standalone binary (static, if possible), so that I can ship it to my production servers without any dependency on haskell/stack.
I've tried putting the following on top of my script, but that results in the following error Did not find executable at specified path: <the path of my script>:
#!/usr/bin/env stack
{- stack
--resolver=lts-15.6
script
--compile
--copy-bins
--package shake
--package bytestring
--package text
--package hashable
--package binary
--package deepseq
--package string-conv
--package http-client
--package http-types
--package safe
--ghc-options=-threaded
--ghc-options=-with-rtsopts=-N
-}

Is it possible to specify a `stack.yaml` file for a haskell script?

Let's say I have the following script inside my haskell project directory:
#!/usr/bin/env stack
-- stack --resolver lts-12.5 script
-- some imports here
main :: IO ()
main = do
-- some code here
I'd like to use the stack.yaml file that exists inside the project directory, in order to get the required packages, as I want to get them from a specific git commit instead of lts-12.5.
I've tried adding --stack-yaml stack.yaml after --resolver lts-12.5 but I'm getting this warning when I run the script: Ignoring override stack.yaml file for script command: stack.yaml.
Is it possible to use my stack.yaml file for the script? or is it possible to specify the git commit from which I want to get the package (like using location with commit and git inside stack.yaml)?
You can achieve this using custom snapshot files. For example, in snapshot.yaml:
resolver: ghc-8.4.3
name: has-acme-missiles
packages:
- acme-missiles-0.3
In Main.hs:
#!/usr/bin/env stack
-- stack --resolver snapshot.yaml script
import Acme.Missiles
main :: IO ()
main = launchMissiles
Results in:
$ ./Main.hs
Nuclear launch detected.

How do specify the resolver (and GHC) Travis should use to test my Haskell package?

I'm trying to test my Haskell package against several Stackage resolvers on Travis, but my --resolver environment variable is being ignored.
For example, if I specify
env:
- ARGS="--resolver lts-4.0"
in my .travis.yml, I still still seem to be using a different resolver (the one in my stack.yaml?) and GHC, as shown by lines like
Installing library in
/home/travis/build/orome/crypto-enigma-hs/.stack-work/install/x86_64-linux/lts-9.1/8.0.2/lib/x86_64-linux-ghc-8.0.2/crypto-enigma-0.0.2.9-6Cs7XSzJkwSDxsEMnLKb0X
in the corresponding build log, which indicates a different resolver (9.1), and corresponding GHC (8.0.2) being used.
How should my build (stack.yaml, .travis.yml, etc.) be configured to ensure that the resolvers (and corresponding GHC) I specify are used to preform my Travis builds and tests?
With env you just define environment variables. You still have to use them. stack on its own does not respect the ARGS variable, so use it in your script, e.g.
install:
# Build dependencies
- stack $ARGS --no-terminal --install-ghc test --only-dependencies
script:
# Build the package, its tests, and its docs and run the tests
- stack $ARGS --no-terminal --install-ghc test --haddock --no-haddock-deps
You should probably use a better name, for example RESOLVER:
env:
- RESOLVER=lts-4.0
- RESOLVER=lts-6.0
- RESOLVER=lts-8.0
install:
# Build dependencies
- stack --resolver $RESOLVER --no-terminal --install-ghc test --only-dependencies
script:
# Build the package, its tests, and its docs and run the tests
- stack --resolver $RESOLVER --no-terminal --install-ghc test --haddock --no-haddock-deps
Also keep in mind that it's usually a better idea to use multiple stack.yml to hold the configuration for that specific LTS variant.
For more information, see stack's Travis documentation and Travis' environment variables documentation.

Force Haskell Stack build to use the Resolver Specified after Shebang

I'm trying to use haskell for scripting in the pattern specified here on the "Script interpreter" section:
https://haskell-lang.org/tutorial/stack-script
When I have comments like this in my script after the shebang:
#!/usr/bin/env stack
{- stack
--resolver nightly-2016-11-26
--install-ghc
runghc
--package http-conduit
-}
{-# LANGUAGE OverloadedStrings #-}
I know I can safely say run:
$ stack myfile.hs
And it correctly picks up the resolver version I specified I need.
However, if I also try to build my script, to have the best of both worlds in scripting for development while I figure things out, and compilation once I'm more sure...
username#myhost:~/$ stack exec -- ghc myfile.hs && ./myfile
Run from outside a project, using implicit global project config
Using resolver: lts-7.10 from implicit global project's config file: /home/username/.stack/global-project/stack.yaml
That will error out because it's using resolver 'lts-7.10' from my global config instead of 'nightly-2016-11-26' even though my comments after the shebang specify a specific, non-global resolver to use.
While I know I could run ghc above and explicitly call --resolver on the command line, I would prefer if this were picked up from what's within the file itself. Is this possible?

Resources