Using cblrepo in Arch Linux

, , ,

UPDATE 2014-01-24: See this post.

The cblrepo command line utility helps to maintain a set of Haskell packages, and is used as the de facto Hackage package manager in Arch Linux.

At least in Arch Linux, the idea is to first install the base set of available Haskell packages from the haskell-core repo. If you now want to install a package from and want to track it with Arch Linux’s own package manager pacman, you have to somehow generate a PKGBUILD from the Hackage package and feed it into makepkg, and then call sudo pacman -U <generated_package> on it. You can do this with cblrepo, but it takes a number of steps that could get very tedious, very fast:

  1. Sync all the packages on Hackage with cblrepo, via cblrepo sync.
  2. Initialize the cblrepo.db file, which is generated automatically each time you execute a cblrepo add ... command.
  3. Add all of the standard packages that comes with installing GHC (ghc in Arch Linux) with cblrepo add --ghc-pkg ....
  4. Add all the packages installed by yourself from the [haskell-core] repository with cblrepo add --distro-pkg ...
  5. Add the <package_name>.cabal URL from the Hackage package’s page, which is in the format<package_name>.1
  6. Run cblrepo pkgbuild <package_name>.
  7. Go into the generated haskell-<package_name_lowercased> directory which contains the PKGBUILD we are after, and run makepkg -s.
  8. Finally run sudo pacman -U <package_arch_linux_format> on it to track it with pacman.

The problem is that steps 3 and 4 take forever to do by hand because you have to add every single package individually, writing out the version numbers (and, it is something of a mystery unless you know where to look for the crucial information).2 Plus, you have to redo all steps 1 through 7 each time you upgrade ghc. So, naturally, I wrote a script (called to automate things a bit.

#!/usr/bin/env zsh
usage="Usage: ./ <HACKAGE_PACKAGES_FILE> <MODE>"

# Exit immediately if any errors are found
setopt errexit
# Avoid "no matches found" error if a file does not exist; see
setopt local_options no_nomatch

if [[ -z $1 ]]; then
    echo $usage
    exit 1
if [[ ! -f $1 ]]; then
    echo "\`$1' does not exist or is not a regular file"
    exit 1
if [[ -z $2 ]]; then
    echo $usage
    exit 1



case $mode in
    ### Remove any old cblrepo.db file. ###
    rm -f cblrepo.db

    # Add packages provided by GHC

    # Pacman provides information about which modules are exposed by installing the
    # 'ghc' package. We put each package into an array.
    provided=($(pacman -Qi ghc | grep Provides | cut -d ":" -f2))

    for p in $provided; do
        # Change the syntax to be compatible with cblrepo. The `cut` command here
        # removes the 'haskell-' prefix for each package, and `sed` here replaces
        # each '=' sign with a ',', as per cblrepo's requirements.
        package=$(echo $p | cut -c9- | sed 's/=/,/')
        command="cblrepo add --ghc-pkg $package"
        # Tell user what we are going to do.
        echo $command
        # Actually execute the command.
        eval $command

    # Add packages installed by the user from [haskell-core] or some other Arch Linux repository
    installed=($(pacman -Qq | grep "^haskell-" | sed 's/^haskell-//'))
    # Filter out those packages that were installed from Hackage using this very
    # same script (in Arch Linux, the hackage packages, once installed, are in
    # the format `haskell-<lowercased_package_name>'). This way, we avoid
    # duplicate definitions and the packages added with --distro-pkg will really
    # be those packages available from the distribution's official haskell
    # repository.

    for p in $installed_filtered; do
        version=$(pacman -Q haskell-$p | cut -d " " -f2 | sed 's/-/,/')
        command="cblrepo add --distro-pkg $p,$version"
        echo $command
        eval $command

    if [[ $mode == initdb-sync ]]; then
        # Sync cblrepo with Hackage
        echo -n "Syncing cblrepo with Hackage..."
        cblrepo sync
        echo "done"

    # Add packages from Hackage
    for hp in $hackage_packages_file; do
        # Grab latest version of package
        cabal_file=$(curl -s $hackage_url/package/$hp | grep -ioE "Cabal source package[)<>/lia href=\"]+\/package\/.+\.cabal" | grep -ioE "\/package.+")
        command="cblrepo add --cbl-url $hackage_url$cabal_file"
        echo $command
        eval $command

    # Link the generated cblrepo.db file into ~/.cblrepo
    ln -sf $PWD/cblrepo.db ~/.cblrepo/cblrepo.db
    ### Generate PKGBUILD files for Hackage packages ###

    # Remove any old packages.
    echo "Deleting old PKGBUILD directories..."
    rm -rfv haskell-*

    for hp in ${hackage_packages_file}; do
        command="cblrepo pkgbuild --patchdir patch $hp"
        echo "($i/${#hackage_packages_file}) $command"
        eval $command
        (( i+=1 ))
    ### Create Arch Linux packages for the Hackage packages ###
    for pdir in haskell-*; do
        cd $pdir
        echo $(basename $PWD)
        makepkg -sf
        sudo pacman -U $(basename $PWD)-*.pkg.tar.xz
        cd ..
        echo "  Finished making/installing package for $pdir"
    echo "Unrecognized <MODE>; valid ones are: initdb initdb-sync pkgbuild makepkg"

A simple HACKAGE_PACKAGES_FILE (perhaps named hackage_pkgs) might look something like this:


Here is sample output from the command ./ hackage_pkgs initdb:

cblrepo add --ghc-pkg array,
cblrepo add --ghc-pkg base,
cblrepo add --ghc-pkg binary,
cblrepo add --ghc-pkg bin-package-db,
cblrepo add --ghc-pkg bytestring,
cblrepo add --ghc-pkg containers,
cblrepo add --ghc-pkg deepseq,
cblrepo add --ghc-pkg directory,
cblrepo add --ghc-pkg filepath,
cblrepo add --ghc-pkg ghc-prim,
cblrepo add --ghc-pkg haskell2010,
cblrepo add --ghc-pkg haskell98,
cblrepo add --ghc-pkg hoopl,
cblrepo add --ghc-pkg hpc,
cblrepo add --ghc-pkg integer-gmp,
cblrepo add --ghc-pkg old-locale,
cblrepo add --ghc-pkg old-time,
cblrepo add --ghc-pkg pretty,
cblrepo add --ghc-pkg process,
cblrepo add --ghc-pkg template-haskell,
cblrepo add --ghc-pkg time,
cblrepo add --ghc-pkg unix,
cblrepo add --ghc-pkg cabal,1.16.0
cblrepo add --distro-pkg aeson,,5
cblrepo add --distro-pkg ansi-terminal,0.6.1,1
cblrepo add --distro-pkg ansi-wl-pprint,,1
cblrepo add --distro-pkg attoparsec,,4
cblrepo add --distro-pkg base-unicode-symbols,,27
cblrepo add --distro-pkg base64-bytestring,,2
cblrepo add --distro-pkg binary,,1
cblrepo add --distro-pkg blaze-builder,,1
cblrepo add --distro-pkg blaze-html,,2
cblrepo add --distro-pkg blaze-markup,,2
cblrepo add --distro-pkg byteable,0.1.1,1
cblrepo add --distro-pkg cairo,,1
cblrepo add --distro-pkg case-insensitive,,1
cblrepo add --distro-pkg cereal,,1
cblrepo add --distro-pkg cmdargs,0.10.7,1
cblrepo add --distro-pkg colour,2.3.3,3
cblrepo add --distro-pkg conduit,,3
cblrepo add --distro-pkg cpphs,1.17.1,2
cblrepo add --distro-pkg cryptohash,0.11.1,1
cblrepo add --distro-pkg data-default,0.5.3,3
cblrepo add --distro-pkg data-default-class,0.0.1,2
cblrepo add --distro-pkg data-default-instances-base,0.0.1,2
cblrepo add --distro-pkg data-default-instances-containers,0.0.1,2
cblrepo add --distro-pkg data-default-instances-dlist,0.0.1,3
cblrepo add --distro-pkg data-default-instances-old-locale,0.0.1,2
cblrepo add --distro-pkg digest,,3
cblrepo add --distro-pkg dlist,,1
cblrepo add --distro-pkg entropy,,1
cblrepo add --distro-pkg extensible-exceptions,,27
cblrepo add --distro-pkg fgl,,27
cblrepo add --distro-pkg glib,,1
cblrepo add --distro-pkg gluraw,,2
cblrepo add --distro-pkg glut,,1
cblrepo add --distro-pkg graphviz,2999.16.0.0,8
cblrepo add --distro-pkg gtk,,1
cblrepo add --distro-pkg hashable,,1
cblrepo add --distro-pkg haskell-src-exts,1.14.0,1
cblrepo add --distro-pkg highlighting-kate,,3
cblrepo add --distro-pkg hinotify,0.3.6,1
cblrepo add --distro-pkg hlint,1.8.55,1
cblrepo add --distro-pkg hostname,1.0,27
cblrepo add --distro-pkg hs-bibutils,5.0,3
cblrepo add --distro-pkg hscolour,1.20.3,27
cblrepo add --distro-pkg hslua,0.3.10,1
cblrepo add --distro-pkg http,4000.2.10,2
cblrepo add --distro-pkg http-types,0.8.3,2
cblrepo add --distro-pkg hunit,,2
cblrepo add --distro-pkg json,0.7,9
cblrepo add --distro-pkg lifted-base,,1
cblrepo add --distro-pkg mmorph,1.0.1,1
cblrepo add --distro-pkg monad-control,,1
cblrepo add --distro-pkg monadcatchio-mtl,,1
cblrepo add --distro-pkg monadcatchio-transformers,,1
cblrepo add --distro-pkg monads-tf,,27
cblrepo add --distro-pkg mtl,2.1.2,27
cblrepo add --distro-pkg mwc-random,,1
cblrepo add --distro-pkg nats,0.1.2,1
cblrepo add --distro-pkg network,,1
cblrepo add --distro-pkg opengl,,1
cblrepo add --distro-pkg openglraw,,2
cblrepo add --distro-pkg pandoc,,4
cblrepo add --distro-pkg pandoc-types,1.12.3,5
cblrepo add --distro-pkg pango,,1
cblrepo add --distro-pkg parsec,3.1.4,1
cblrepo add --distro-pkg pcre-light,0.4,27
cblrepo add --distro-pkg polyparse,1.9,1
cblrepo add --distro-pkg pqueue,1.2.1,1
cblrepo add --distro-pkg primitive,,1
cblrepo add --distro-pkg quickcheck,2.6,2
cblrepo add --distro-pkg random,,27
cblrepo add --distro-pkg regex-base,0.93.2,27
cblrepo add --distro-pkg regex-compat,0.95.1,27
cblrepo add --distro-pkg regex-pcre,0.94.4,1
cblrepo add --distro-pkg regex-posix,0.95.2,27
cblrepo add --distro-pkg regex-tdfa,1.1.8,30
cblrepo add --distro-pkg resourcet,0.4.10,2
cblrepo add --distro-pkg safe,0.3.3,3
cblrepo add --distro-pkg sdl,0.6.5,1
cblrepo add --distro-pkg sdl-image,0.6.1,28
cblrepo add --distro-pkg sdl-ttf,0.6.2,3
cblrepo add --distro-pkg semigroups,0.12.1,1
cblrepo add --distro-pkg sha,1.6.3,1
cblrepo add --distro-pkg split,0.2.2,2
cblrepo add --distro-pkg stm,2.4.2,3
cblrepo add --distro-pkg syb,0.4.1,2
cblrepo add --distro-pkg system-fileio,0.3.11,6
cblrepo add --distro-pkg system-filepath,0.4.8,1
cblrepo add --distro-pkg tagged,0.7,1
cblrepo add --distro-pkg tagsoup,0.13,1
cblrepo add --distro-pkg temporary,,3
cblrepo add --distro-pkg test-framework,,3
cblrepo add --distro-pkg test-framework-quickcheck2,,3
cblrepo add --distro-pkg texmath,,3
cblrepo add --distro-pkg text,,1
cblrepo add --distro-pkg transformers,,27
cblrepo add --distro-pkg transformers-base,0.4.1,27
cblrepo add --distro-pkg uniplate,1.6.12,1
cblrepo add --distro-pkg unix-compat,,2
cblrepo add --distro-pkg unordered-containers,,1
cblrepo add --distro-pkg utf8-string,0.3.7,27
cblrepo add --distro-pkg utility-ht,0.0.9,1
cblrepo add --distro-pkg vector,,1
cblrepo add --distro-pkg void,0.6.1,6
cblrepo add --distro-pkg wl-pprint-text,,1
cblrepo add --distro-pkg x11,,5
cblrepo add --distro-pkg x11-xft,0.3.1,31
cblrepo add --distro-pkg xml,1.3.13,4
cblrepo add --distro-pkg xmonad,0.11,7
cblrepo add --distro-pkg xmonad-contrib,0.11.2,2
cblrepo add --distro-pkg yaml,,4
cblrepo add --distro-pkg zip-archive,0.1.4,1
cblrepo add --distro-pkg zlib,,2
cblrepo add --cbl-url
cblrepo add --cbl-url
cblrepo add --cbl-url
cblrepo add --cbl-url

This is infinitely faster than adding each package by hand. Feel free to copy/paste/modify/redistribute the code in to suit your needs.

  1. I use a Hackage-tailored search engine for Firefox to make package searching on Hackage easier.↩︎

  2. The command for finding out which packages are provided by GHC is pacman -Qi ghc, and you have to look for the Provides field. As for the packages from [haskell-core], you have to do some manual grepping for packages that start with haskell- from the command pacman -Q.↩︎