Haskell: Can't get `stack` to work

I have the following flake.nix:

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    devenv.url = "github:cachix/devenv";
  };

  nixConfig = {
    extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
    extra-substituters = "https://devenv.cachix.org";
  };

  outputs = inputs@{ nixpkgs, devenv, ... }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs {
        inherit system;

        overlays = [
          devenv.overlays.default
        ];
      };
    in
    {
      devShells.${system}.default = devenv.lib.mkShell
        {
          inherit inputs pkgs;

          modules = [
            ({ pkgs, ... }: {
              languages.haskell.enable = true;
            })
          ];
        };
    };
}

if I run nix develop --impure and then

stack init
stack build

then I’m getting this error message:

error:
       … while calling the 'derivationStrict' builtin

         at /builtin/derivation.nix:9:12: (source not available)

       … while evaluating derivation 'myEnv'
         whose name attribute is located at /nix/store/4qviwiyv0ylfn50l2wvmc0zykr8g1fps-nixos-23.05/nixos/pkgs/stdenv/generic/make-derivation.nix:303:7

       … while evaluating attribute 'LD_LIBRARY_PATH' of derivation 'myEnv'

         at «string»:1:411:

            1| with (import <nixpkgs> {}); let inputs = [haskell.compiler.ghc963 git gcc gmp]; libPath = lib.makeLibraryPath inputs; stackExtraArgs = lib.concatMap (pkg: [ ''--extra-lib-dirs=${lib.getLib pkg}/lib''   ''--extra-include-dirs=${lib.getDev pkg}/include'' ]) inputs; in runCommand ''myEnv'' { buildInputs = lib.optional stdenv.isLinux glibcLocales ++ inputs; STACK_PLATFORM_VARIANT=''nix''; STACK_IN_NIX_SHELL=1; LD_LIBRARY_PATH = libPath;STACK_IN_NIX_EXTRA_ARGS = stackExtraArgs; LANG="en_US.UTF-8";} ""
             |                                                                                                                                                                                                                                                                                                                                                                                                                           ^

       error: attribute 'ghc963' missing

       at «string»:1:43:

            1| with (import <nixpkgs> {}); let inputs = [haskell.compiler.ghc963 git gcc gmp]; libPath = lib.makeLibraryPath inputs; stackExtraArgs = lib.concatMap (pkg: [ ''--extra-lib-dirs=${lib.getLib pkg}/lib''   ''--extra-include-dirs=${lib.getDev pkg}/include'' ]) inputs; in runCommand ''myEnv'' { buildInputs = lib.optional stdenv.isLinux glibcLocales ++ inputs; STACK_PLATFORM_VARIANT=''nix''; STACK_IN_NIX_SHELL=1; LD_LIBRARY_PATH = libPath;STACK_IN_NIX_EXTRA_ARGS = stackExtraArgs; LANG="en_US.UTF-8";} ""
             |                                           ^
       Did you mean one of ghc943, ghc96, ghc961, ghc962 or ghc90?

I tried to set languages.haskell.package to one of the suggested compilers like ghc96 but I’m still getting the error message. What am I doing wrong?

If you run stack --verbose build you should be able to see how stack is trying to run nix-shell to pull in GHC for you.

If the Nix channel that the underlying nix-shell doesn’t have the version of GHC you need, you’ll see this error.

There are a couple ways to fix this, but the underlying idea is to either switch your stack resolver to a version that uses a GHC contained in the Nixpkgs that nix-shell is trying to use. Or, switch your Nixpkgs version to a version that contains the GHC version that stack wants.

That’s my output from stack -v build:

2.13.1 x86_64 hpack-0.36.0
2024-01-12 08:40:00.510198: [debug] Checking for project config at: /home/tornax/Programming/haskell-age-guesser/stack.yaml
2024-01-12 08:40:00.510435: [debug] Loading project config file stack.yaml
2024-01-12 08:40:00.513106: [debug] Use of Casa server enabled: (CasaRepoPrefix "https://casa.stackage.org", 1280).
2024-01-12 08:40:00.515374: [debug] (SQL) SELECT COUNT(*) FROM "last_performed" WHERE ("action"=?) AND ("timestamp">=?); [PersistInt64 1,PersistUTCTime 2024-01-11 07:40:00.515333034 UTC]
2024-01-12 08:40:00.515700: [debug] Using package location completions from a lock file
2024-01-12 08:40:00.517151: [debug] Loaded snapshot from Pantry database.
2024-01-12 08:40:00.633662: [debug] Running Hpack on /home/tornax/Programming/haskell-age-guesser/package.yaml
2024-01-12 08:40:00.635413: [debug] Hpack output unchanged in /home/tornax/Programming/haskell-age-guesser/haskell-age-guesser.cabal
2024-01-12 08:40:00.635543: [debug] Prefetching git repos: []
2024-01-12 08:40:00.635562: [debug] []
2024-01-12 08:40:00.636247: [debug] PATH is: Just "/nix/store/x26m6ir72l7042xfm04wipwb4s9wcdjc-devenv-profile/bin:/run/wrappers/bin:/home/tornax/.nix-profile/bin:/nix/profile/bin:/home/tornax/.local/state/nix/profile/bin:/etc/profiles/per-user/tornax/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin"
2024-01-12 08:40:00.636276: [debug] Using a nix-shell environment with nix packages: haskell.compiler.ghc963, git, gcc, gmp
2024-01-12 08:40:00.636354: [debug] Run process: /run/current-system/sw/bin/nix-shell --pure -E "with (import <nixpkgs> {}); let inputs = [haskell.compiler.ghc963 git gcc gmp]; libPath = lib.makeLibraryPath inputs; stackExtraArgs = lib.concatMap (pkg: [ ''--extra-lib-dirs=${lib.getLib pkg}/lib''   ''--extra-include-dirs=${lib.getDev pkg}/include'' ]) inputs; in runCommand ''myEnv'' { buildInputs = lib.optional stdenv.isLinux glibcLocales ++ inputs; STACK_PLATFORM_VARIANT=''nix''; STACK_IN_NIX_SHELL=1; LD_LIBRARY_PATH = libPath;STACK_IN_NIX_EXTRA_ARGS = stackExtraArgs; LANG=\"en_US.UTF-8\";} \"\"" --run "'/nix/store/c697a38517nnsszmm4p6avyvmgryjvxk-stack-2.13.1/bin/stack' $STACK_IN_NIX_EXTRA_ARGS '--internal-re-exec-version=2.13.1' '-v' 'build'"
error:
       … while calling the 'derivationStrict' builtin

         at /builtin/derivation.nix:9:12: (source not available)

       … while evaluating derivation 'myEnv'
         whose name attribute is located at /nix/store/4qviwiyv0ylfn50l2wvmc0zykr8g1fps-nixos-23.05/nixos/pkgs/stdenv/generic/make-derivation.nix:303:7

       … while evaluating attribute 'LD_LIBRARY_PATH' of derivation 'myEnv'

         at «string»:1:411:

            1| with (import <nixpkgs> {}); let inputs = [haskell.compiler.ghc963 git gcc gmp]; libPath = lib.makeLibraryPath inputs; stackExtraArgs = lib.concatMap (pkg: [ ''--extra-lib-dirs=${lib.getLib pkg}/lib''   ''--extra-include-dirs=${lib.getDev pkg}/include'' ]) inputs; in runCommand ''myEnv'' { buildInputs = lib.optional stdenv.isLinux glibcLocales ++ inputs; STACK_PLATFORM_VARIANT=''nix''; STACK_IN_NIX_SHELL=1; LD_LIBRARY_PATH = libPath;STACK_IN_NIX_EXTRA_ARGS = stackExtraArgs; LANG="en_US.UTF-8";} ""
             |                                                                                                                                                                                                                                                                                                                                                                                                                           ^

       error: attribute 'ghc963' missing

       at «string»:1:43:

            1| with (import <nixpkgs> {}); let inputs = [haskell.compiler.ghc963 git gcc gmp]; libPath = lib.makeLibraryPath inputs; stackExtraArgs = lib.concatMap (pkg: [ ''--extra-lib-dirs=${lib.getLib pkg}/lib''   ''--extra-include-dirs=${lib.getDev pkg}/include'' ]) inputs; in runCommand ''myEnv'' { buildInputs = lib.optional stdenv.isLinux glibcLocales ++ inputs; STACK_PLATFORM_VARIANT=''nix''; STACK_IN_NIX_SHELL=1; LD_LIBRARY_PATH = libPath;STACK_IN_NIX_EXTRA_ARGS = stackExtraArgs; LANG="en_US.UTF-8";} ""
             |                                           ^
       Did you mean one of ghc943, ghc96, ghc961, ghc962 or ghc90?

I adjusted the dev-shell:

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    devenv.url = "github:cachix/devenv";
  };

  nixConfig = {
    extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
    extra-substituters = "https://devenv.cachix.org";
  };

  outputs = inputs@{ nixpkgs, devenv, ... }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs {
        inherit system;

        overlays = [
          devenv.overlays.default
        ];
      };
    in
    {
      devShells.${system}.default = devenv.lib.mkShell
        {
          inherit inputs pkgs;

          modules = [
            ({ pkgs, ... }: {
              languages.haskell = {
                enable = true;
                package = pkgs.haskell.compiler.ghc963;
              };
            })
          ];
        };
    };
}

However, I’m still getting the error message?!

Here are the two relevant lines from the output of stack -v build:

You can see that the nix-shell command line is:

/run/current-system/sw/bin/nix-shell --pure -E "with (import <nixpkgs> {}); let inputs = [haskell.compiler.ghc963 git gcc gmp]; libPath = lib.makeLibraryPath inputs; stackExtraArgs = lib.concatMap (pkg: [ ''--extra-lib-dirs=${lib.getLib pkg}/lib'' ''--extra-include-dirs=${lib.getDev pkg}/include'' ]) inputs; in runCommand ''myEnv'' { buildInputs = lib.optional stdenv.isLinux glibcLocales ++ inputs; STACK_PLATFORM_VARIANT=''nix''; STACK_IN_NIX_SHELL=1; LD_LIBRARY_PATH = libPath;STACK_IN_NIX_EXTRA_ARGS = stackExtraArgs; LANG=\"en_US.UTF-8\";} \"\"" --run "'/nix/store/c697a38517nnsszmm4p6avyvmgryjvxk-stack-2.13.1/bin/stack' $STACK_IN_NIX_EXTRA_ARGS '--internal-re-exec-version=2.13.1' '-v' 'build'"

You can see that nix-shell is effectively just importing <nixpkgs>. You need to make sure that you’re either running it in an environment where your NIX_PATH is set to something that contains a version of Nixpkgs that contains the version of GHC you’re trying to use, or make the inner call to nix-shell not rely on <nixpkgs>.

I’m not sure what the recommended way to do this is with devenv, but here is the Stack documentation on the topic:

You should be able to find a lot of people describing the same topic if you google for something like “stack nixpkgs ghc attribute missing Did you mean one of”.

You can see that nix-shell is effectively just importing <nixpkgs>. You need to make sure that you’re either running it in an environment where your NIX_PATH is set to something that contains a version of Nixpkgs that contains the version of GHC you’re trying to use, or make the inner call to nix-shell not rely on <nixpkgs>.

ooh. Well, both are things I don’t know how to fix that xD

I’m not sure what the recommended way to do this is with devenv, but here is the Stack documentation on the topic:

holy shit, I just copied their flake.nix and adjusted the version of ghc and it works! Thanks man!

1 Like