haskellghccabalcabal-installghc-pkg

How do I create a GHC package environment file that's equivalent to not having one?


When using GHC without a package environment file, you'll be able to use several libraries that it ships with, such as containers and text. When you do have a package environment, though, you can only use the packages it mentions. Is there an easy way to create an environment file that contains all of the packages that are exposed without one? It looks like I could hack together a solution based on parsing the output of ghc-pkg list -v, but I'm hoping there's a more elegant way. If this is version-specific, then I'm looking for a solution that works with GHC 9.8.2 and Cabal 3.10.3.0.


Solution

  • If you look at some of the ghc-pkg subcommands and options, you'll see that parsing isn't too hard. The following shell script ought to work. Since it runs ghc-pkg field on every package to check whether it's exposed or hidden, it's too slow to be used on every ghc invocation, but it should be fine for generating a static environment file for each new GHC version that comes along:

    #!/bin/bash
    
    set -e
    
    echo clear-package-db
    echo global-package-db
    
    for p in $(ghc-pkg list --simple-output --show-unit-ids)
    do
            if [ "$(ghc-pkg --simple-output --unit-id field $p exposed)" = True ]
            then
               echo package-id $p
            fi
    done