Ghc: can't find a package database at

I’m developing a package called som that depends on the package grid.
There is a version of grid on Hackage, but I need to use a newer version.
I’ve built this newer version of grid, and the correct version shows up:

$ nix-env -f "<nixpkgs>" -qaP -A haskellPackages.grid
haskellPackages.grid  grid-7.8.14

Here’s the shell.nix in my som directory.

$ cat shell.nix
(import ./. {}).env

When I try to start a nx shell in the som directory, I get an error:

$ nix-shell
these derivations will be built:
  /nix/store/s10dijmq41lny75kgxzpq37xvijba484-grid-7.8.14.drv
  /nix/store/cily89yd9gr2dljlvshld12sgpb9c4si-ghc-8.6.4-with-packages.drv
building '/nix/store/s10dijmq41lny75kgxzpq37xvijba484-grid-7.8.14.drv'...
setupCompilerEnvironmentPhase
Build with /nix/store/gvshp9yvc6gql09r3cyryj2zgsnfk6br-ghc-8.6.4.
unpacking sources
unpacking source archive /nix/store/rpr2cz3yxwc7ccd410x775mig57xyqdq-grid
source root is grid
patching sources
compileBuildDriverPhase
setupCompileFlags: -package-db=/build/setup-package.conf.d -j1 -threaded
Loaded package environment from /build/grid/.ghc.environment.x86_64-linux-8.6.4
ghc: can't find a package database at /home/amy/.cabal/store/ghc-8.6.4/package.db
builder for '/nix/store/s10dijmq41lny75kgxzpq37xvijba484-grid-7.8.14.drv' failed with exit code 1
cannot build derivation '/nix/store/cily89yd9gr2dljlvshld12sgpb9c4si-ghc-8.6.4-with-packages.drv': 1 dependencies couldn't be built
error: build of '/nix/store/cily89yd9gr2dljlvshld12sgpb9c4si-ghc-8.6.4-with-packages.drv' failed

I notice the message ghc: can't find a package database at /home/amy/.cabal/store/ghc-8.6.4/package.db. That directory does exist, but apparently it doesn’t have any packages.

$ ghc-pkg list --package-db=/home/amy/.cabal/store/ghc-8.6.4/package.db
/home/amy/.cabal/store/ghc-8.6.4/package.db
(no packages)

What am I doing wrong?

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.6.4

Here’s my cabal file

$ cat som.cabal
cabal-version:       >=1.10
name:                som
version:             10.1.11
synopsis:            Self-Organising Maps
-- description:
homepage:            https://github.com/mhwombat/som
bug-reports:         https://github.com/mhwombat/som/issues
license:             BSD3
license-file:        LICENSE
author:              Amy de Buitléir
maintainer:          amy@nualeargais.ie
copyright:           2012-2019 Amy de Buitléir
category:            Math
build-type:          Simple
extra-source-files:  ChangeLog.md, README.md

library
  exposed-modules:
      Data.Datamining.Clustering.Classifier
      Data.Datamining.Clustering.DSOM
      Data.Datamining.Clustering.DSOMInternal
      Data.Datamining.Clustering.SGM
      Data.Datamining.Clustering.SGMInternal
      Data.Datamining.Clustering.SGM4
      Data.Datamining.Clustering.SGM4Internal
      Data.Datamining.Clustering.SGM6
      Data.Datamining.Clustering.SGM6Internal
      Data.Datamining.Clustering.SOM
      Data.Datamining.Clustering.SOMInternal
      Data.Datamining.Pattern
  other-modules:       Paths_som
  hs-source-dirs:      src
  ghc-options: -Wall
  build-depends:
      base >=4.7 && <5
    , containers ==0.5.* || ==0.6.*
    , deepseq ==1.4.*
    , grid >=7.8.14 && <7.9
  default-language:    Haskell2010

test-suite som-test
  type: exitcode-stdio-1.0
  main-is: Spec.hs
  other-modules:
      Data.Datamining.Clustering.DSOMQC
      Data.Datamining.Clustering.SGMQC
      Data.Datamining.Clustering.SGM4QC
      Data.Datamining.Clustering.SGM6QC
      Data.Datamining.Clustering.SOMQC
      Data.Datamining.PatternQC
      Paths_som
  hs-source-dirs:
      test
  ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall
  build-depends:
      QuickCheck
    , base >=4.6 && <5
    , containers
    , deepseq
    , grid
    , random
    , som
    , test-framework
    , test-framework-quickcheck2
  default-language: Haskell2010

Delete /build/grid/.ghc.environment.x86_64-linux-8.6.4 and it will work I think.

I don’t understand how you have setup you shell.nix or how grid is “installed” though. It doesn’t look like a standard way.

1 Like

That solved the problem, thank you.

I installed grid by typing cabal new-install in the grid directory, but I probably shouldn’t have done that. It was something I tried in desperation when things weren’t working. ;^)

FYI, the creation of these environment files can be disabled by passing --write-ghc-environment-files never to cabal. There was a long discussion, including of the possibility of making it off by default, here.

2 Likes

Had a similar issue recently and you can solve this by not passing gitignored files (of which these ghc-environments are part of) to the nix-build/nix-shell:

{ pkgs ? import ./pinned-nixpkgs.nix {} }:
let
  gitignore = pkgs.nix-gitignore.gitignoreSource [] ./.;
in
pkgs.haskellPackages.callCabal2nix "PROJECT_NAME" gitignore {}

Here’s an example using nix-gitignore in a default.nix.