Using `yesod devel` without building dependencies

Hi,

I tried using yesod devel to spin up a development server. However stack (yesod devel calls stack) will attempt to build all (or at least a lot of the) dependencies, even if I am in a nix-shell that provides those. Running nix-build works as expected and uses the binary cache.

How can I tell stack to use the dependencies provides by Nix?

The stack call issued by yesod devel looks like

stack build --fast --file-watch homepage:lib --exec "/nix/store/dp2hxgh06wbbi75laxxbk2jswjfff5nz-yesod-bin-1.6.0.3/bin/yesod devel-signal" --flag homepage:dev --flag homepage:library-only
1 Like

Unfortunately stack doesn’t have any ability to use dependencies from nix.

Here’s a long-winded post where I talk a little about the different ways to do Haskell development with nix:

When using stack with nix I use a resolver like ghc-8.6.5. This causes stack to use the package db that nix provides for you. I think there are several ways to get stack to use a particular resolver, and I am not sure what the easiest is for you (i.e. a user configured default resolver, the resolver set in a stack yaml, or the --resolver flag passed directly to stack).

1 Like

When using stack with nix I use a resolver like ghc-8.6.5 . This causes stack to use the package db that nix provides for you.

Huh, I didn’t know the ghc-8.6.5 resolver worked like this. This is pretty neat!

Here’s an example of using stack to build a project that requires aeson and lens, but to pull those libraries from nixpkgs:

stack.yaml:

resolver: ghc-8.6.5
packages:
- .
nix:
    shell-file: stack-shell.nix

stack-shell.nix:

with (import <nixpkgs> {});

let
  ghc = haskell.packages.ghc865.ghcWithPackages (p: [p.aeson p.lens]);
in

haskell.lib.buildStackProject {
  inherit ghc;
  name = "myEnv";
  buildInputs = [ zlib ];
}

Of course, at this point you’re not gaining much over cabal-install (other than a slightly nicer build tool…?).

Just using the resolver ghc-8.6.5 does not work for me. After throwing many extra-deps into stack.yaml it starts compiling again. Maybe I need to use buildStackProject. I will certainly try this.

That’s the downside of using the ghc-8.6.5 approach. You have to manually get dependencies from nix through the stack-shell.nix file.

It takes away a lot of the benefit of using stack, in my opinion.


In general, if you’re using stack and one of the LTS/nightly resolvers, there is no way to get Haskell dependencies from nix. The blog post linked above explains this.

I hope to give the stack-shell.nix-approach a go today. To be honest, I do not want to use stack in the first place. However yesod devel uses stack internally and I do want to use the development server provided by yesod devel.

You can use ghcid in a similar fashion to the yesod devel development server if you really want to avoid stack:

You would use cabal2nix to generate a default.nix that enumerates your dependencies. Then you would have a shell.nix that imports that derivation. In a shell built from that shell.nix, stack with a ghc-8.6.5 resolver would find all the required dependencies in the package db nix provides for you.

I did this for a while when using intero for editor tooling which also wants to continually reload everything into a running ghci process.