Haskell-stack build fails

Hi all,

I’m having some difficulties to build the “hello world” project of exercism.io, the first haskell exercise.

This project uses stack and the log of the build displays many lines with
Cabal-simple_mPHDZzAJ_2.2.0.1_ghc-8.4.3: The program 'ar' is required but it could not be found.

I wonder if it’s because of my customized stack.yaml

system-ghc: true
enable: false
resolver: lts-12.4

Since it’s my first post, I really would like to thank all the people working on NixOS. I do enjoy using it.

1 Like

Hi @jojo,

I’m not sure exactly what problem you’re facing, but it would be very helpful for me (and others who might come across this question), if you could give a detailed explanation of the exact code you’re trying to build, the commands you’re trying to run, the full output, etc. Also, if you could link to a single repo (on Github) where you can reproduce the problem, that would be very helpful.

Here’s a couple links that explain how to create a minimal reproducible example:

There are lots of people successfully using stack on NixOS, so I’m hopeful that we will be able to find a solution to your problem!

1 Like

I’m mobile now but may be able to give enough direction:

I have been working on Exercism’s Haskell Problems recently and ran into exactly the same problem. IIRC I wound up needing to set a ‘compiler’ key in the stack.yaml to reflect the then-current version of GHC versus what appears in Stack’s resolver.

You can look that up with some variation of ‘nix-env -QAPA pkgs.ghc.compiler’ but I’ve almost certainly got the attribute path wrong in the final argument there.

There is a different stack.yaml key that is visible in the stack docs that allows the compiler to be newer than what was specified in the resolver by patch-level only, but I didn’t see an improvement the last time I tried it.


Edit: misread the OP and didn’t see ‘enable: false’, so most of my post is useless to that person. Sorry for the noise.

Thanks for the quick answers and sorry to be late.

@shanesveller : I found that enable: false was necessary to avoid error: attribute 'ghc843' missing, at (string):1:43 but I don’t need to stick to it.

As you suggested, I removed it and I added compiler : ghc-8.6.5 , since my last system ghc is the 8.6.5 version. My stack.yaml is now

compiler: ghc-8.6.5
resolver: lts-12.4

Building the project with stack seemed to work a bit better :slight_smile:

Now I have another bug :

--  While building package stm- using:
      /home/jlucas/.stack/setup-exe-cache/x86_64-linux-nix/Cabal-simple_mPHDZzAJ_2.4.0.1_ghc-8.6.5 --builddir=.stack-work/dist/x86_64-linux-nix/Cabal- build --ghc-options " -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1
Progress 14/18

@cdepillabout Ok, I will try to make a reproducible example if I cannot overcome the new issue. Thanks for the links.

Since I’m still stuck, here is a detailed explanation of what I did.
(I’m on NixOS 19.03, I use ghc-8.6.5 and stack

  1. Install exercism package (nix-env -i exercism)
  2. Then exercism download --exercise=hello-world --track=haskell will download and install the data in /home/jlucas/exercism/haskell/hello-world.
  3. After cd /home/jlucas/exercism/haskell/hello-world, in the file ./stack.yaml, I added compiler: ghc-8.6.5 (or whatever is your system version I guess)
  4. Next stack build works for me
  5. But stack test returns the next error log (I only pasted relevant lines)
WARNING: Ignoring stm's bounds on base (>=4.3 && <4.12); using base-
stm        > Building library for stm-
stm        > /run/user/1000/stack-a8a4ca0abf4d9831/stm- error: Not in scope: ‘always’
stm        > /run/user/1000/stack-a8a4ca0abf4d9831/stm- error: Not in scope: ‘alwaysSucceeds’
--  While building package stm- using:
      /home/jlucas/.stack/setup-exe-cache/x86_64-linux-nix/Cabal-simple_mPHDZzAJ_2.4.0.1_ghc-8.6.5 --builddir=.stack-work/dist/x86_64-linux-nix/Cabal- build --ghc-options " -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1
Progress 14/18

The error seems to be caused by the test dependency, stm, that cannot be built, isn’t it ?

Just to check, I verified that I could create an empty stack project. from scratch, following the guide. Here is what I did

  1. At first, stack new helloworld, returned the error

    Cannot determine project root directory for Docker sandbox.
  2. Thus, in ~/.stack/config.yaml I disabled nix with

        enable: false

    and stack new helloworld worked

  3. Next, I did,

    cd helloworld
    stack build

    The last command returned the error:

    I don't know how to install GHC on your system configuration, please install manually
  4. Thus, in ~/.stack/config.yaml I re-enabled nix :

      enable: true

    With that, stack build did work… :thinking: :laughing:

  5. stack test also worked.
    Thus, at the end, I was able to build a full project from scratch. What was surprising is that I had to first disable nix to create the project, then to enable nix to build and test the project.

Still stuck on this.

stack test


WARNING: Ignoring stm's bounds on base (>=4.3 && <4.12); using base-
Reason: trusting snapshot over cabal file dependency information.
stm               > /run/user/1000/stack-cde9a1ad5aa8f630/stm- error: Not in scope: ‘always’
stm               >    |                            
stm               > 34 |         always,            
stm               >    |         ^^^^^^             
stm               >                                 
stm               > /run/user/1000/stack-cde9a1ad5aa8f630/stm- error: Not in scope: ‘alwaysSucceeds’
stm               >    |                            
stm               > 35 |         alwaysSucceeds,    
stm               >    |  
While building package stm- using:
      /home/jlucas/.stack/setup-exe-cache/x86_64-linux-nix/Cabal-simple_mPHDZzAJ_2.4.0.1_ghc-8.6.5 --builddir=.stack-work/dist/x86_64-linux-nix/Cabal- build --ghc-options " -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1

Thus I bet I need to get stm- somehow. Stack seems not able to build this version automatically. What should I do ?

What is weird is that the deprecation of always and alwaysSucceeds happened on v2.5.0.0 not v2.4.5.0. Citing the source code on top of Control/Monad/STM.hs from the stm package version 2.5.0

-- Note that invariant checking (namely the @always@ and @alwaysSucceeds@
-- functions) has been removed. See ticket [#14324](https://ghc.haskell.org/trac/ghc/ticket/14324) and
-- the [removal proposal](https://github.com/ghc-proposals/ghc-proposals/blob/d5f1dbb456d13d298902e8d27ede65ab47598695/proposals/0011-deprecate-stm-invariants.rst).
-- Existing users are encouraged to encapsulate their STM operations in safe
-- abstractions which can perform the invariant checking without help from the
-- runtime system.

I don’t use stack, so I am not able to help further.

Following a suggestion to fully replace stack with a nix-shell I wrote a default.nix file

    pkgs = import <nixpkgs> { };
    pkgs.haskellPackages.developPackage {
    root = ./.;
    modifier = drv: pkgs.haskell.lib.overrideCabal drv (attrs: {
      buildTools = with pkgs.haskellPackages; 
        (attrs.buildTools or []) ++ [cabal-install ghcid] ;

And executed
nix-shell --run ghcid

I got the same error log as before. I even tried to add stm_2_5_0_0 to haskellPackage in default.nix… Still the same error log.

Since the error does not seem related to stack or Nix, is the error caused by the cabal configuration of the project ?

@jojo Could you push your code (with default.nix) into some git repo? I can take a look.

1 Like

Thanks. Here are the steps to reproduce

  • Install exercism package ( nix-env -i exercism )
  • Then exercism download --exercise=hello-world --track=haskell will download and install the data in /home/<user>/exercism/haskell/hello-world.
  • cd /home/<user>/exercism/haskell/hello-world
  • Write default.nix file in current directory
  • Run nix-shell --run ghcid

Actually you will need to run once something like stack test to build a hello-world.cabal file before last step. Sorry to forget to mention that.


Oh yea, I see what is happening. There is no .cabal file, because they use hpack. You need to generate the cabal file, so commands like ghcid and cabal work correctly. Do this:

  1. Add hpack along side ghcid in your default.nix (there should be three packages in total: [cabal-install ghcid hpack]).
  2. Run nix-shell --run "hpack && ghcid"

You can also use ghcid to automatically run the tests:

nix-shell --run "hpack && ghcid -c 'cabal new-repl test' -T 'Tests.main'"

But this will require you to add a module name to the Tests module; basically add module Tests where as the first line of test/Tests.hs.

With these two changes, exercism’s exercises can be made “nix-friendly”.

I did that from scratch. After nix-shell --run "hpack && ghcid" I got stuck with All good (2 modules, at 11:41:19) on top of a fully cleaned terminal; for unknown reason.

Then I escaped from this with Ctr+C and tried

nix-shell --run "hpack && ghcid -c 'cabal new-repl test' -T 'Tests.main'"

I got a full black terminal once more with

<interactive>:36:1-10: error:
    Not in scope: ‘Tests.main’
    No module named ‘Tests’ is imported.


at the bottom. And, again, I’m stuck in this state.

In both cases I can write characters with the keyboard but pressing enter does nothing except going to a new line. Ctr+C is the only solution to escape this.

I cannot understand that it works on your system. The only reason I see would be that we have something that differs in our environment… But using nix-shell should prevent such a case.

I got stuck with All good (2 modules, at 11:41:19)

That means you succeeded (you didn’t get stuck). ghcid will say “All good” when the compilation succeeds (and it tried to compile 2 modules), and there are no errors.

No module named ‘Tests’ is imported.

Did you add module Tests where as the first line to the file test/Tests.hs, as indicated above?

It works ! Thank you so much. Sorry for the wrong error log, you were right, the line was missing. My bad.

Ok, I was really surprised that it did not return to the prompt after a success, but I guess that it must be a kind conventional behavior to see a rapid prompt.

Ok now I know more about ghcid

I’m glad that your problem got resolved!

Ok now I know more about ghci d

Yes, trial and error is how we learn things. :slight_smile:

Nix and ghcid come in handy for writing runnable “Haskell scripts” too; see Nix recipes for Haskellers – Sridhar Ratnakumar

1 Like