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

[ data, environment, library, mit ] [ 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
Change log CHANGELOG.md
Dependencies base (>=4.16 && <5), containers (>=0.6.7 && <0.7), gigaparsec (>=0.3.1 && <0.4) [details]
License MIT
Author Yitang Chen <mmzk1526@outlook.com>
Maintainer Yitang Chen <mmzk1526@outlook.com>
Category Data, Environment
Uploaded by MMZK1526 at 2025-08-03T13:23:46Z
Distributions
Downloads 3 total (3 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.1.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.

Quick Start

module Data.Env where

import           Control.Monad.IO.Class
import           Data.Env.ExtractFields
import           Data.Env.RecordParser
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: " ++ 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.