what goes into a minimal stack.yaml file? - haskell

I have projects compiling with cabal build (but using hpack for the convenience). For some reason, I would like to compile them with stack build. What goes into a minimal stack.yaml file?
The stack documentation gives details what goes into such a file, but does not show a minimal (or any, as much as I can see) example. I fear to use stack init on an existing project for unintended consequences.
What is a small example from which to go?

A minimal stack.yaml:
resolver: lts-19.16 # for ghc 9.0.2 newest lts
packages:
- .
A not-so minimal skeleton from which to expand:
resolver: lts-19.16 # for ghc 9.0.2 newest lts
packages:
- .
extra-deps: # include here other local packages
ghc-options:
"$everything": -haddock # to produce documentation with haddock

Related

how to use a my own Haskell library in a stack project?

I have a Haskell library I wrote that I compile with stack.
I now need to use it in another Haskell stack project.
In order to do that, I have added its current version to the extra_deps of my new project's stack.yaml and listed it among the build_depends of one its executables.
When I run stack build, however, it obviously tries to download the library from the internet. How do I specify that the library is local and where to find it?
Thanks in advance!
You can use github coordinates in the extra-deps section of your stack.yaml.
extra-deps:
- github: snoyberg/http-client
commit: a5f4f30f01366738f913968163d856366d7e0342
Then add the dependency in your package.yaml:
dependencies:
- http-client >= 0.1.0.0

Getting error while using gtk2hs-buildtool library in Haskell project using Stack

I am trying to add dependency of gtk2hs-buildtool to my Haskell project but following error while building stack.
In the dependencies for TicTacToe-0.1.0.0:
gtk2hs-buildtools needed, but the stack configuration has no specified version (latest matching
version is 0.13.5.4)
needed since TicTacToe is a build target.
TicTacToe.cabal file:
library
exposed-modules:
TicTacToeEngine
other-modules:
Paths_TicTacToe
hs-source-dirs:
src
build-depends:
base >=4.7 && <5,
gtk2hs-buildtools
default-language: Haskell2010
If you are using stack tool you shouldn't touch the <packagename>.cabal file. stack is in charge to generate it from package.yaml. Despite of the fact that is a common practise to modify the <packagename>.cabal, development workflow will be easier if you don't.
The files you are interested in (and the ones that stack tool uses) are the stack.yaml and package.yaml.
In stack.yaml you should see an entry called resolver: lts-XX.XX. That means that your dependencies version are managed such that they match those in the given lts (a.k.a. snapshot). You can go to https://www.stackage.org/, click on your lts version and search for the gtk2hs-buildtools package to get the right version for your project (example: lts-12.26 uses gtk2hs-buildtools-0.13.4.0). In your package.yaml, in the dependencies section write the entry entry - gtk2hs-buildtools
From lts-13.11 and above gtk2hs-buildtools is not available in stackage, so you need to add it as an extra-dep. in the stack.yaml, in the section extra-dep add the following entry gtk2hs-buildtools-0.13.4.0 (or the version number you'd like to use). Then add in the package.yaml and entry gtk2hs-buildtools. It is necessary to add the entry in both files. Refer to stack docs o understand why.
Just to ensure you can build your project, your files should something look like the following:
if using resolver above or equal to 13.11
stack.yaml
resolver: lts-13.11
extra-deps:
- gtk2hs-buildtools-0.13.4.0
package.yaml
dependencies:
- base
- gtk2hs-buildtools
if using resolver below 13.11
stack.yaml
resolver: lts-12.26
extra-deps:
package.yaml
dependencies:
- base
- gtk2hs-buildtools

Stack won't resolve a 'hidden' dependency

I am working on my first major Haskell application, and want to add mockery to create disposable test WAI threads. Importing mockery and running stack test resulted in the compiler error:
Failed to load interface for ‘Test.Mockery.Directory’
It is a member of the hidden package ‘mockery-0.3.5’.
Perhaps you need to add ‘mockery’ to the build-depends in your .cabal file.
Use -v to see a list of the files searched for.
So, I added mockery to my cabal file under test dependencies. However, when I run stack build or stack test mockery is automatically removed from the cabal file.
I have also tried listing mockery-0.3.5 under extra-deps in the stack.yaml file. This unsurprisingly didn't work, since mockery is part of my lts, and extra deps is for packages outside of lts.
How can I get stack to recognize that mockery should be included as a dependency to to project?
Here is my stack.yaml:
flags: {}
ghc-options:
! '*': -Wall
packages:
- .
extra-deps: [
]
resolver: lts-9.5
I'm using stack version 1.5.1
I imagine this is a stupid build issue and look forward to confronting my obvious oversight.
In stack.yaml you declare the Stackage LTS version, a curated list of hackage dependencies that you want to depend on. You can also depend on local packages and packages in git that are not in Hackage. You may also change the versions of the packages in LTS as long as they respect the constraints of the other dependencies.
package.yaml is the build file. Any packages you want to import directly in your Haskell code must be declared in here as dependencies, even if they are explicitly declared in the stack.yaml.
Finally, when you see It is a member of the hidden package, that means that one of your dependencies is using that package, but it is not declared as a dependency in your build file.

Difference between new-template.cabal and stack.yaml

I want to use reactive-banana in my new Haskell project. I never used cabal-install or stack before. I created a directory and initialized project files in it using stack new. I see now 2 files in the directory: new-template.cabal and stack.yaml.
How do I set dependencies and make sure they are downloaded and compiled?
At first I tried to add - reactive-banana-0.8.0.2 in stack.yaml under extra-deps:, but both stack build and stack solver didn't download it. Then I augmented a part called library in new-template.cabal to this:
library
hs-source-dirs: src
exposed-modules: Lib
build-depends: base >= 4.7 && < 5
, reactive-banana >= 0.8
default-language: Haskell2010
Every time I tried to run stack build, it crashed with an error and suggestion to add some package to stack.yaml under extra-deps:, and this happened three times until finally all packages installed, and I could import them in stack ghci REPL.
So my question is, what is the idiomatic way to use stack? Which of these 2 files should I use to specify dependencies and other project metadata? What is the sample workflow of an average Haskell developer with stack?
When using stack I generally don't put any versions bounds in my .cabal file. I let the resolver and extra-deps setting in the stack.yaml file determine which versions of packages to select.
Here is a stack.yaml file which brings in reactive-banana-0.8.1.2:
flags: {}
packages:
- '.'
extra-deps:
- reactive-banana-0.8.1.2
- psqueues-0.2.0.2
resolver: lts-2.17
In my .cabal file I just have:
build-depends: base >= 4.7 && < 5, reactive-banana
The reactive-banana version is pinned by the stack.yaml file.
If you want to use GHC 7.10 change the resolver to something like nightly-2015-06-17.
I determine the extra-deps iteratively, by just running stack build and adding whatever dependencies are missing to the stack.yaml file until all dependencies are satisfied.
You will only need to do this with packages which are not in Stackage - like reactive-banana. A great many of commonly used packages are in Stackage and their versions will be determined by the resolver setting.
In the default configuration, stack works with two package databases: a centralised per-user one and a project-specific one. The centralised database only pulls packages from Stackage, a subset of Hackage with known-to-be-compatible packages, while you can put whatever you want on the project-specific database. All packages you use must be in the cabal file, but those not on Stackage (that is, the ones that will go to the project-specific database) must also be listed in the extra-deps section of stack.yaml. reactive-banana is not on Stackage, so you need to add it to stack.yaml, like this:
# etc.
extra-deps:
- reactive-banana-0.8.1.2
# etc.
stack solver can fill in the extra dependencies in stack.yaml for you.

How to install a package using stack?

Using cabal, I could install hakyll with the command:
cabal install hakyll
How can I do the same thing using stack?
stack install hakyll
stack offers a curated set of packages that won't blow your machine up. If you want to check what packages are available, or exactly what version is supported, or on what version of GHC you can get it, check out https://www.stackage.org/.
For example, you can get hakyll 4.6.9.0 right now for both GHC 7.8.4 and GHC 7.10.1. Pretty neat. - source
EDIT: I forgot to mention, Yuan Wang's method works for getting the version of hakyll that is not curated into stackage. It's up to you what version you need.
add hakyll in stack.yaml generated by stack init or stack new
yaml file should look like:
flags: {}
packages:
- '.'
extra-deps:
- hakyll-4.7.1.0
resolver: lts-2.15
after that, run stack solver installs it
https://github.com/commercialhaskell/stack/wiki/stack.yaml
This documentation worked for me
On package.yaml add the library under dependencies, for example:
dependencies:
- base >= 4.7 && < 5
- hakyll # added here

Resources