Provides a generic way to construct values from environment variables.
This package exposes a class FromEnv
that works with GHC.Generics
to provide a generic way to construct values from environment variables.
In many applications you'll have a configuration object holding a connection string to your database, the url of an S3 bucket, you name it. It can be tedious to have to construct this configuration object manually. With this package, you just derive Generic
and then create an instance of FromEnv
for your configuration type and you're done. Just call fromEnv
and you got your configuration.
import System.Environment.FromEnv
import GHC.Generics
newtype Config = Config { configS3BucketUrl :: String } deriving Generic
instance FromEnv Config
config <- fromEnv
The default behaviour is to convert field names like configS3BucketUrl
into environment variables like CONFIG_S3_BUCKET_URL
, but it can be overriden by providing a custom instance of FromEnv
.
from-env
Haskell package to construct datatypes from environment variables.
In many applications you'll have an .env
file or set environment variables some way. These environment variables usually contain configuration data such as database connection urls, secrets; etc.
Next, you make a configuration data type to hold this variables so your application can access them. Normally you'd have a bunch of calls to lookupEnv
in order to build your data type. This is tedious and error-prone. Thankfully, in Haskell we can do better!
import GHC.Generics
import System.Environment.FromEnv
data Config = Config
{ configDbUrl :: !String
, configApiSecret :: !String
}
deriving Generic
instance FromEnv Config
main = do
config <- fromEnv
-- do something with config
And that's it! By deriving Generic
you can now create an instance of FromEnv
for free. Check out the haddocks for more.
License
MIT.