MyNixOS website logo
Description

Reduced parser for configurator-ng config files.

This module provides a simplified and updated interface to the configuration file format of configurator and configurator-ng. Its aim is primarily to allow updating programs that depend on configurator-ng to new versions of GHC without changing the configuration file format.

What is this?

This is a simplified version of the resting configurator-ng, aimed particularly to offer users of configurator-ng such as PostgREST an easy path to migrate to a package that compiles with modern GHC versions and that continues to read existing configuration files.

Changes

configurator-pg skips some of configurator-ng's features, and changes the API in other places:

  • No configuration file reloading.
  • Simplified parsing API:
    • There is no type-class based value parsing; you need to supply explicit value parsers.
    • There's only load to read and evaluate a configuration file, and runParser to extract configuration values.
  • Simpler error handling and improved logging of parse errors.
  • Simplified handling of configuration subsets. There's subassocs and the unit tests pass, but the author didn't attempt to understand the original implementation fully.

Credits

The original configurator-ng is due to MailRank, Inc., Bryan O'Sullivan and Leon P Smith.

The low-level parser (Data.Configurator.Syntax) was initially mostly unchanged, but has since been rewritten with Megaparsec for better error messages. The evaluation (Data.Configurator.Load) is still close to the original. The high-level parser (Data.Configurator.Parser) is original.

File format

In short, the file format supports:

  • A simple but flexible configuration language that supports several of the most commonly needed types of data, along with interpolation of strings from the configuration or the system environment (e.g. $(HOME)).

  • An import directive allows the configuration of a complex application to be split across several smaller files, or common configuration data to be shared across several applications.

The format is more fully documented in the packages configurator and configurator-ng.

Here's an example:

# listen address
hostname = "localhost"
port = 8000

logdir = "$(HOME)/logs"
logfile = "$(logdir)/log.txt"
loglevels = [1, 4, 5]

users {
  alice = "[email protected]"
  bob   = "[email protected]"
}

# passwords.txt might contain
#   alice = "12345"
#   bob   = "sesame"
passwords {
  import "secrets/passwords.txt"
}

Usage

The following code can be used to parse the example above.

import Data.Configurator

data Settings = Settings
  { hostname  :: Text
  , port      :: Int
  , logfile   :: Maybe FilePath
  , loglevels :: Maybe [Int]
  , users     :: [(Text, Text)]
  , passwords :: [(Text, Text)]
  }

settingsParser :: Parser Config Settings
settingsParser =
  Settings
    <$> required "hostname" string
    <*> (Maybe.withDefault 1234 <$> optional "port" int)
    <*> optional "logfile" (pack <$> string)
    <*> optional "loglevels" (list int)
    <*> subassocs "users" string
    <*> subassocs "passwords" string

loadSettings :: IO Settings
loadSettings = do
  cfg <- load "settings.cfg"
  case runParser settingsParser cfg of
    Left err       -> die $ "reading config: " <> err
    Right settings -> return settings

Though note that for no apparent reason, subassocs returns the full key, whence the parsed list of users will be

    [ ("users.alice", "[email protected]")
    , ("users.bob", "[email protected]")
    ]
Metadata

Version

0.2.10

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows