configuration-tools: Tools for specifying and parsing configurations

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Tools for specifying and parsing configurations

This package provides a collection of utils on top of the packages optparse-applicative, aeson, and yaml for configuring libraries and applications in a convenient and composable way.

The main features are

  1. configuration management through integration of command line option parsing and configuration files and

  2. a Setup.hs file that generates a PkgInfo module for each component of a package that provides information about the package and the build.

Documentation on how to use this package can be found in the README and in the API documentation of the modules Configuration.Utils and Configuration.Utils.Setup.

[Skip to Readme]


Versions 0.2.1, 0.2.2, 0.2.3, 0.2.4,, 0.2.5, 0.2.6, 0.2.7, 0.2.8, 0.2.9, 0.2.10, 0.2.11, 0.2.12, 0.2.13, 0.2.14, 0.2.15, 0.3.0, 0.3.1, 0.4.0, 0.4.0, 0.4.1, 0.4.2, 0.5.0, 0.6.0, 0.6.1
Change log
Dependencies aeson (>=, ansi-wl-pprint (>=0.6), attoparsec (>=, base (>=4.8 && <5.0), base-unicode-symbols (>=, base64-bytestring (>=1.0), bytestring (>=, Cabal (>=1.24), case-insensitive (>=1.2), connection (>=0.2), data-default (>=0.5), deepseq (>=1.3), directory (>=, dlist (>=0.7.1), enclosed-exceptions (>=1.0), filepath (>=, http-client (>=0.4), http-client-tls (>=0.2), http-types (>=0.8), monad-control (>=1.0), mtl (>=2.2), network-uri (>=, optparse-applicative (>=, process (>=, profunctors (>=4.0.4), semigroups (>=0.18), text (>=1.0), tls (>=1.2), transformers (>=0.4), unordered-containers (>=, x509 (>=1.5), x509-system (>=1.5), x509-validation (>=1.5.1), yaml (>= [details]
License MIT
Copyright (c) 2015-2018 Lars Kuhtz <>, (c) 2014-2015 AlephCloud, Inc.
Author Lars Kuhtz <>
Maintainer Lars Kuhtz <>
Category Configuration, Console
Home page
Bug tracker
Source repo head: git clone -b master
this: git clone 0.4.0)
Uploaded by larsk at 2018-08-21T22:55:47Z



Manual Flags


enable loading of configuration files from HTTP URLs

Automatic Flags

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info


Maintainer's Corner

For package maintainers and hackage trustees

Readme for configuration-tools-0.4.0

[back to package description]

Build Status Hackage


This package provides a collection of utilities on top of the packages optparse-applicative, aeson, and yaml for configuring libraries and applications in a composable way.

The main features are

  1. compositional configuration management with integration of command line option parsing and configuration files, validation of configurations, and loading of configurations from remote locations,
  2. a Setup.hs file that generates a PkgInfo module for each component of a package that provide information about the package and the build, and
  3. a set of types for configuration of HTTP services and clients along with aeson instances and command line option parsers.

The ultimate goal for this package is a general framework for compositional configuration management for software components. Instead of designing such a framework from scratch the approach of this package is to first explore design and implementation patterns based on practical examples and by gluing together existing technology.

Therefor at the current state this package mostly provides operators and coding patterns for writing stylish boilerplate code.

Once we feel that the developed patterns cover a sufficient portion of real world requirements we plan to rewrite this package such that the boilerplate is hidden behind a clean and simple DSL.


Assuming that you have a recent version version of GHC and Cabal installed in your system this package can be install from Hackage via

cabal install configuration-tools

If you don't need support for remote configuration files this package can be build with a much smaller set of dependencies via

cabal install -f-remote-configs configuration-tools

The package can be tested via

git clone
cd hs-configurationt-tools
cabal configure --enable-tests
cabal build
cabal test

If you have issues building this package, first ensure that the installed version of the Cabal library matches the version that is used by your cabal binary. You may compare the results of cabal info Cabal and cabal --version.

Configuration Management

The goal of this package is to make management of configurations easy by providing an idiomatic style of defining and deploying configurations.

For each data type that is used as a configuration type the following must be provided:

  1. a default value,

  2. a FromJSON instance that yields a function that takes a value and updates that value with the parsed values,

  3. a ToJSON instance, and

  4. an options parser that yields a function that takes a value and updates that value with the values provided as command line options.

Optionally, a function for validating the configuration value may be provided.

The package provides operators and functions that make the implementation of these requisites easy for the common case that the configuration is encoded mainly through nested records.

In addition to the user defined command line options the following options are recognized by the application:

As long as the package wasn't build with -f-remote-configs the following two options are available. They affect how configuration files are loaded from remote URLs.

The operators provided in this package assume that lenses are provided for field of the configuration record types.

An complete usage example can be found in the file example/Example.hs of the cabal package.

Usage Example

Remark: there are unicode equivalents for some operators available in Configuration.Utils that lead to better aligned and more readable code.

We start with language extensions and imports.

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}

module Main
( main
) where

import Configuration.Utils
import Data.Monoid

Next we define the types that are used for the configuration of our application. In this contrived example these types define a simplified version of HTTP URLs.

data Auth = Auth
    { _user :: !String
    , _pwd :: !String

We have to define lenses for the configuration types. Here we do it explicitly. Alternatively one could have used TemplateHaskell along with makeLenses from the module Control.Lens from the lens package.

user :: Functor f => (String -> f String) -> Auth -> f Auth
user f s = (\u → s { _user = u }) <$> f (_user s)

pwd :: Functor f => (String -> f String) -> Auth -> f Auth
pwd f s = (\p -> s { _pwd = p }) <$> f (_pwd s)

(Note, that the module Configuration.Utils defines its own type synonyms for lenses. If you import Control.Lens you should hide Lens and Lens' from either module.)

We must provide a default value. If there is no reasonable default the respective value could, for instance, be wrapped into Maybe. Here we use the monoid identity value of the type.

defaultAuth :: Auth
defaultAuth = Auth
    { _user = ""
    , _pwd = ""

Now we define an aeson FromJSON instance that yields a function that updates a given Auth value with the values from the parsed JSON value. The <*< operator is functional composition lifted for applicative functors and % is a version of $ with a different precedence that helps to reduce the use of parenthesis in applicative style code.

instance FromJSON (Auth -> Auth) where
    parseJSON = withObject "Auth" $ \o -> id
        <$< user ..: "user" % o
        <*< pwd ..: "pwd" % o

The ToJSON instance is needed to print the configuration (as YAML document) when the user provides the --print-config command line option.

instance ToJSON Auth where
    toJSON a = object
        [ "user" .= _user a
        , "pwd" .= _pwd a

Finally we define a command line option parser using the machinery from the optparse-applicative package. Similar to the FromJSON instance the parser does not yield a value directly but instead yields a function that updates a given Auth value with the value from the command line.

pAuth :: MParser Auth
pAuth = id
    <$< user .:: strOption
        % long "user"
        <> help "user name"
    <*< pwd .:: strOption
        % long "pwd"
        <> help "password for user"

You may consult the documentation of the optparse-applicative package for further information on how to define command line options.

The following definitions for the HttpURL are similar to definitions for the Auth type above. In addition it is demonstrated how to deal with nested configuration types. Mainly the usage of ..: is replaced by %.: and .:: is replaced by %::.

data HttpURL = HttpURL
    { _auth :: !Auth
    , _domain :: !String
    , _path :: !String

auth :: Functor f => (Auth -> f Auth) -> HttpURL -> f HttpURL
auth f s = (\u → s { _auth = u }) <$> f (_auth s)

domain :: Functor f => (String -> f String) -> HttpURL -> f HttpURL
domain f s = (\u → s { _domain = u }) <$> f (_domain s)

path :: Functor f => (String -> f String) -> HttpURL -> f HttpURL
path f s = (\u → s { _path = u }) <$> f (_path s)

defaultHttpURL :: HttpURL
defaultHttpURL = HttpURL
    { _auth = defaultAuth
    , _domain = ""
    , _path = ""

instance FromJSON (HttpURL -> HttpURL) where
    parseJSON = withObject "HttpURL" $ \o -> id
        <$< auth %.: "auth" % o
        <*< domain ..: "domain" % o
        <*< path ..: "path" % o

instance ToJSON HttpURL where
    toJSON a = object
        [ "auth" .= _auth a
        , "domain" .= _domain a
        , "path" .= _path a

pHttpURL :: MParser HttpURL
pHttpURL = id
    <$< auth %:: pAuth
    <*< domain .:: strOption
        % long "domain"
        <> short 'd'
        <> help "HTTP domain"
    <*< path .:: strOption
        % long "path"
        <> help "HTTP URL path"

Now that everything is set up the configuration can be used to create a ProgramInfo value. The ProgramInfo value is than use with the runWithConfiguratin function to wrap a main function that takes an HttpURL argument with configuration file and command line parsing.

mainInfo :: ProgramInfo HttpURL
mainInfo = programInfo "HTTP URL" pHttpURL defaultHttpURL

main :: IO ()
main = runWithConfiguration mainInfo $ \conf -> do
        $ "http://"
        <> (_user . _auth) conf
        <> ":"
        <> (_pwd . _auth) conf
        <> "@"
        <> _domain conf
        <> "/"
        <> _path conf

Using Sum Types as Configuration Types

Sum types can not be used as configuration types in the same way as product types. The reason is that the nondeterminism in the choice of a term for the type is not restricted to the chosen constructor arguments but in addition there is non-determinism in the choice of the constructor, too.

An update function for a product type can be defined point-wise as a mapping from constructor parameters to values. An update for a sum type must take the constructor context into account. Moreover, when applied to a given default value the function may not be applicable at all if the default value uses a different constructor context than what the update assumes.

For the future we plan to provide a general solution for configurations of sum types which would be based on the possibility to define default values for more than a single constructor. For now one must restrict configurations of sum types to yield constant values instead of point-wise (partial) updates. In practice this means that for a type a one has to provide an FromJSON instance for a and use the ..: operator. Similarly for the option parser one has to define a parser that yields an a and use it with the .:: operator.

The module Configuration.Utils.Maybe provides tools for dealing with Maybe values.

Package and Build Information

The module Configuration.Utils.Setup an example Setup.hs script that hooks into the cabal build process at the end of the configuration phase and generates a module with package information for each component of the cabal package.

The modules are created in the autogen build directory where also the Path_ module is created by cabal's simple build setup. This is usually the directory ./dist/build/autogen.

For a library component the module is named just PkgInfo. For all other components the module is named PkgInfo_COMPONENT_NAME where COMPONENT_NAME is the name of the component with - characters replaced by _.

For instance, if a cabal package contains a library and an executable that is called my-app, the following modules are created: PkgInfo and PkgInfo_my_app.

Usage as Setup Script

There are two ways how this module can be used:

  1. Copy the code of this module into a file called Setup.hs in the root directory of your package.

  2. If the configuration-tools package is already installed in the system where the build is done, following code can be used as Setup.hs script:

    module Main (main) where
    import Configuration.Utils.Setup

With both methods the field Build-Type in the package description (cabal) file must be set to Custom:

Build-Type: Custom

Integration With Configuration.Utils

You can integrate the information provided by the PkgInfo modules with the command line interface of an application by importing the respective module for the component and using the runWithPkgInfoConfiguration function from the module Configuration.Utils as show in the following example:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}

module Main
( main
) where

import Configuration.Utils
import PkgInfo

instance FromJSON (() -> ()) where parseJSON _ = pure id

mainInfo :: ProgramInfo ()
mainInfo = programInfo "Hello World" (pure id) ()

main :: IO ()
main = runWithPkgInfoConfiguration mainInfo pkgInfo . const $ putStrLn "hello world"

With that the resulting application supports the following additional command line options:

Here is the example output of --long-info for the example examples/Trivial.hs from this package:

trivial-0.2.6 (package configuration-tools-0.2.6 revision 2cc860c)
Copyright (c) 2014 AlephCloud, Inc.

Author: Lars Kuhtz <>
License: MIT
Build with: ghc-7.8.3 (x86_64-osx)
Build flags:
Optimisation: normal

    Cabal- [BSD3, 2003-2006, Isaac Jones 2005-2011, Duncan Coutts]
    MonadRandom-0.3 [OtherLicense]
    aeson- [BSD3, (c) 2011-2014 Bryan O'Sullivan (c) 2011 MailRank, Inc.]
    ansi-terminal- [BSD3]
    ansi-wl-pprint- [BSD3]
    array- [BSD3]
    attoparsec- [BSD3]
    base- [BSD3]
    base-unicode-symbols- [BSD3, 2009–2011 Roel van Dijk <>]
    bifunctors- [BSD3, Copyright (C) 2008-2013 Edward A. Kmett]
    rts-1.0 [BSD3]
    bytestring- [BSD3, Copyright (c) Don Stewart 2005-2009, (c) Duncan Coutts 2006-2013, (c) David Roundy 2003-2005, (c) Jasper Van der Jeugt 2010, (c) Simon Meier 2010-2013.]
    case-insensitive- [BSD3, 2011 Bas van Dijk]
    comonad-4.2.2 [BSD3, Copyright (C) 2008-2014 Edward A. Kmett, Copyright (C) 2004-2008 Dave Menendez]
    conduit- [MIT]
    containers- [BSD3]
    contravariant-1.2 [BSD3, Copyright (C) 2007-2014 Edward A. Kmett]
    deepseq- [BSD3]
    directory- [BSD3]
    distributive-0.4.4 [BSD3, Copyright (C) 2011-2014 Edward A. Kmett]
    dlist-0.7.1 [BSD3, 2006-2009 Don Stewart, 2013 Sean Leather]
    either-4.3.1 [BSD3, Copyright (C) 2008-2014 Edward A. Kmett]
    errors-1.4.7 [BSD3, 2012, 2013 Gabriel Gonzalez]
    exceptions-0.6.1 [BSD3, Copyright (C) 2013-2014 Edward A. Kmett Copyright (C) 2012 Google Inc.]
    filepath- [BSD3]
    free-4.9 [BSD3, Copyright (C) 2008-2013 Edward A. Kmett]
    ghc-prim- [BSD3]
    hashable- [BSD3]
    integer-gmp- [BSD3]
    lifted-base- [BSD3, (c) 2011-2012 Bas van Dijk, Anders Kaseorg]
    mmorph-1.0.4 [BSD3, 2013 Gabriel Gonzalez]
    monad-control- [BSD3, (c) 2011 Bas van Dijk, Anders Kaseorg]
    mtl-2.2.1 [BSD3]
    nats-0.2 [BSD3, Copyright (C) 2011-2014 Edward A. Kmett]
    old-locale- [BSD3]
    optparse-applicative- [BSD3, (c) 2012-2014 Paolo Capriotti <>]
    prelude-extras-0.4 [BSD3, Copyright (C) 2011-2014 Edward A. Kmett]
    pretty- [BSD3]
    primitive- [BSD3, (c) Roman Leshchinskiy 2009-2012]
    process- [BSD3]
    profunctors- [BSD3, Copyright (C) 2011-2014 Edward A. Kmett]
    random- [BSD3]
    resourcet- [BSD3]
    safe-0.3.6 [BSD3, Neil Mitchell 2007-2014]
    scientific- [BSD3]
    semigroupoids-4.2 [BSD3, Copyright (C) 2011-2013 Edward A. Kmett]
    semigroups-0.15.3 [BSD3, Copyright (C) 2011-2014 Edward A. Kmett]
    stm-2.4.3 [BSD3]
    syb-0.4.2 [BSD3]
    tagged-0.7.2 [BSD3, 2009-2013 Edward A. Kmett]
    template-haskell- [BSD3]
    text- [BSD3, 2009-2011 Bryan O'Sullivan, 2008-2009 Tom Harper]
    time-1.4.2 [BSD3]
    transformers- [BSD3]
    transformers-base-0.4.3 [BSD3, 2011 Mikhail Vorozhtsov <>, Bas van Dijk <>]
    transformers-compat- [BSD3, Copyright (C) 2012 Edward A. Kmett]
    unix- [BSD3]
    unordered-containers- [BSD3, 2010-2014 Johan Tibell 2010 Edward Z. Yang]
    vector- [BSD3, (c) Roman Leshchinskiy 2008-2012]
    void-0.6.1 [BSD3, Copyright (C) 2008-2013 Edward A. Kmett]
    yaml- [BSD3]

Configuration Types for HTTP Services and Clients

The module Configuration.Utils.Http contains some types for configuring HTTP services and clients. Currently these types only provide the most basic configuration settings. This will probably be extended in the future. Feel free to submit patches for missing settings.


This package is in an early stage of development and more features are planned.