mmzk-env: Read environment variables into a user-defined data type

[ data, environment, library, mit, program ] [ Propose Tags ] [ Report a vulnerability ]

mmzk-env is a Haskell library that provides functionality to read environment variables into user-defined data types, allowing for flexible and type-safe configuration management.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.1.0, 0.1.1.1, 0.1.2.0, 0.2.0.0, 0.2.1.0, 0.2.1.1, 0.3.0.0, 0.4.0.0
Change log CHANGELOG.md
Dependencies base (>=4.16 && <5), containers (>=0.6.7 && <0.7), gigaparsec (>=0.3.1 && <0.4), mmzk-env, text (>=2.1.3 && <2.2) [details]
License MIT
Author MMZK1526
Maintainer MMZK1526
Uploaded by MMZK1526 at 2026-04-16T17:24:24Z
Category Data, Environment
Home page https://github.com/MMZK1526/mmzk-env
Bug tracker https://github.com/MMZK1526/mmzk-env/issues
Distributions
Executables witness-example, newtype-example, enum-example, custom-mapping-example, quickstart-example
Downloads 150 total (24 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for mmzk-env-0.4.0.0

[back to package description]

mmzk-env

mmzk-env is a library for reading environment variables into a user-defined data type. It provides a type-safe way to parse and validate environment variables, ensuring that they conform to the expected types.

Contents

Quick Start

Full example →

{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeApplications #-}

import Data.Env
import GHC.Generics

-- | Example: Define an environment schema
data Config = Config
    { port     :: Int
    , name     :: String
    , mainHost :: String
    , debug    :: Maybe Bool }
    deriving (Show, Generic, EnvSchema)

-- | Run the validation
main :: IO ()
main = do
  errOrEnv <- validateEnv @Config
  case errOrEnv of
    Left err  -> putStrLn $ "Validation failed:\n" ++ renderParseError err
    Right cfg -> putStrLn $ "Config loaded successfully: " ++ show cfg

With this setup, it requires the environment variables PORT, NAME, MAIN_HOST, and DEBUG to be set according to the types defined in the Config data type. The library will automatically parse these variables and validate them against the schema.

If any variable is missing or has an incorrect type, the validation will fail, and an error message will be printed.

Custom Environment Variable Mapping

Full example →

By default, the library converts camelCase field names to UPPER_SNAKE_CASE (e.g., mainHostMAIN_HOST).

If you want to use uppercase environment variable names without underscores (like MAINHOST instead of MAIN_HOST), you can use validateEnvWith (or validateEnvWWith for witness types) with a custom mapping function:

data Config = Config
    { port      :: Int
    , name      :: String
    , main_host :: String  -- Will map to "MAINHOST" with custom mapping
    , debug     :: Maybe Bool }
    deriving (Show, Generic, EnvSchema)

main :: IO ()
main = do
  errOrEnv <- validateEnvWith @Config (map toUpper)
  case errOrEnv of
    Left err  -> putStrLn $ "Validation failed:\n" ++ renderParseError err
    Right cfg -> putStrLn $ "Config loaded successfully: " ++ show cfg

With validateEnvWith (map toUpper), the field main_host will look for the environment variable MAINHOST instead of MAIN_HOST.

You can provide any custom mapping function to validateEnvWith to transform field names to environment variable names according to your needs.

Error Handling

validateEnv (and its variants) returns Either ParseError a. Unlike a simple Either String approach, ParseError collects all field failures in a single pass — so every invalid field is reported at once, not just the first one.

Use renderParseError to format the error for display:

case errOrEnv of
  Left err  -> putStrLn $ "Validation failed:\n" ++ renderParseError err
  Right cfg -> ...

A single-field failure shows the field name, then the detail indented below:

port: invalid field
  (line 1, column 1):
    unexpected "n"
    expected an integer
    >not-a-number
     ^

A missing required field gives a clear message instead of a parser error:

name: invalid field
  missing required environment variable

Multiple failures are numbered in field-declaration order:

2 fields failed to parse:
  1. port: invalid field
     (line 1, column 1):
       unexpected "n"
       expected an integer
       >not-a-number
        ^
  2. name: invalid field
     missing required environment variable

ParseError and FieldError are both exported from Data.Env, so you can also inspect them programmatically:

import Data.Env (ParseError(..), FieldError(..))

case errOrEnv of
  Right cfg -> ...
  Left (ParseError errs) ->
    mapM_ (\fe -> putStrLn $ fe.errField ++ " is invalid") errs

Enum Support

Full example →

The library also supports automatic parsing of enumerated types. You can define an enum and derive the TypeParser instance using the helper type EnumParser.

The extension DerivingVia is required for this feature.

{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE TypeApplications #-}

data Gender = Male | Female
  deriving (Show, Eq, Enum, Bounded)
  deriving TypeParser via (EnumParser Gender)

parseType @Gender "Male"    -- Right Male
parseType @Gender "Female"  -- Right Female
parseType @Gender "male"    -- Left "invalid value \"male\"; expected one of: Male, Female"

Enum parsing is case-sensitive and the error message lists all valid constructors.

Witness Types: Avoiding Newtype Boilerplate

The library provides a "witness" pattern that allows you to enhance parsing behaviour without wrapping values in newtypes. This is useful when you need features like default values, validation, or transformation but want to keep your final data types simple.

The Problem: Newtype Boilerplate

Full example →

Let's say you want to parse a PostgreSQL port that defaults to 5432. Without witnesses, you might create a newtype wrapper:

{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TypeApplications #-}

import Data.Env
import Data.Env.TypeParser
import Data.Word
import GHC.Generics

-- Define a newtype wrapper for the port
newtype PsqlPort = PsqlPort Word16
  deriving (Show, Eq)

-- Implement custom parsing with default value
instance TypeParser PsqlPort where
  parseMissing = Right (PsqlPort 5432)  -- Default to 5432 when absent
  parseType str = case parseType str of
    Right port -> Right (PsqlPort port)
    Left err   -> Left err

data Config = Config
  { psqlPort :: PsqlPort
  , dbName   :: String }
  deriving (Show, Generic, EnvSchema)

Now when you use your config, you have to constantly unwrap the value:

unpackPort :: PsqlPort -> Word16
unpackPort (PsqlPort port) = port

connectToDatabase :: Config -> IO Connection
connectToDatabase cfg = connect $ defaultConnectInfo
  { connectPort = unpackPort (psqlPort cfg)  -- Annoying unpacking!
  , connectDatabase = dbName cfg }

The Solution: Witnesses

Full example →

With witness types, you can specify parsing behaviour at the type level while keeping the final value unwrapped:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}

import Data.Env
import Data.Env.RecordParserW
import Data.Env.TypeParserW
import Data.Env.Witness.DefaultNum
import Data.Word
import GHC.Generics

data Config c = Config
  { psqlPort :: Column c (DefaultNum 5432 Word16) Word16  -- Defaults to 5432
  , dbName   :: Column c (Solo String) String }
  deriving (Generic)

instance EnvSchemaW (Config 'Dec)
deriving stock instance Show (Config 'Res)  -- For printing the result

-- Validate environment variables with defaults
main :: IO ()
main = do
  errOrConfig <- validateEnvW @(Config 'Dec)
  case errOrConfig of
    Left err  -> putStrLn $ "Validation failed:\n" ++ renderParseError err
    Right cfg -> connectToDatabase cfg  -- cfg :: Config 'Res

The magic happens with the Column type family and the ColumnType phantom type:

  • Config 'Dec (Declaration): The type used for parsing, where each field is (witness, value)
    • This works under the hood for the generic instances and users typically don't interact with it directly
  • Config 'Res (Result): The type you work with, where each field is just value
  • Column c witness a: Expands to (witness, a) when c = 'Dec, or just a when c = 'Res

Now your final config has no wrappers:

connectToDatabase :: Config 'Res -> IO Connection
connectToDatabase cfg = connect $ defaultConnectInfo
  { connectPort = psqlPort cfg  -- Direct access to Word16!
  , connectDatabase = dbName cfg }

Key Benefits

  1. No Unpacking: Your final data type contains raw values (Word16, String, etc.), not newtypes
  2. Type-Level Defaults: Default values are specified in the type signature using type-level naturals
  3. Flexible Parsing: Different witness types provide different parsing strategies (defaults, validation, transformation)

Available Witnesses

  • Solo a: Standard parsing without special behaviour (equivalent to TypeParser)
  • DefaultNum n a: Numeric types with a type-level default value n
  • DefaultString s a: String types with a type-level default value s
  • DefaultBool b a: Bool with a type-level default; also accepts true/false, t/f, 1/0
  • Custom witnesses: You can define your own by implementing the TypeParserW class

More built-in witnesses will be provided.

For more complex parsing needs, witnesses provide a way to augment behaviour without polluting your domain types with wrapper noise.