Search code examples
haskelldependency-managementcabalcabal-install

How can I pin a version of a Haskell dependency to a version of an underlying native dependency with Cabal?


In my particuar case, I have a dependency in my Cabal file on the Haskell package bindings-libzip. In particular, I could accept several different versions of libzip, e.g. bindings-libzip-0.11 or bindings-libzip-0.10. These in turn have a dependency on the respective native C libzip libraries versions 0.11 and 0.10.

Therefore I have bindings-libzip >= 0.10 < 0.12 in my .cabal file.

The Haskell package bindings-libzip-x specifies with PkgConfig-Depends that libzip version x must be present on a client machine. Let's say a downstream user has version 0.10 of libzip installed. However, when pulling down my package, this user pulls down the latest dependencies possible and transitively pulls down version 0.11 of bindings-libzip. This causes the build process to error out with a message about an incorrect version of libzip installed.

Is there any way I can specify in my .cabal file to use bindings-libzip-0.11 if and only if pkg-config detects version 0.11 of libzip and to use bindings-libzip-0.10 if and only if pkg-config detects version 0.10 of libzip?


Solution

  • I'm submitting another answer because this uses another idea...

    Using a custom Setup.hs with defaultMainWithHooksArgs allows you to inspect and modify the args to the cabal configure command.

    This is a Setup.hs which does no modification:

    import Distribution.Simple
    import Distribution.Simple.Configure
    import System.Environment
    
    main = do
      args <- getArgs
      defaultMainWithHooksArgs simpleUserHooks args
    

    If your .cabal file has a flag defined, e.g.:

    Flag Foo
      Default:  False
    

    then in the args you will see "--flags=-foo". So the idea is:

    1. Define two flags in the .cabal file - use10 and use11 to select which version of bindings-libzip to use.
    2. In your custom Setup.hs determine which version to use.
    3. Find the "--flags=..." arg and modify it appropriately before passing it along to defaultMainWithHooksArgs.