This is effectively a port of dotenv, whose README explains it best:
Storing configuration in the environment is one of the tenets of a
twelve-factor app. Anything that is likely to change between deployment
environments–such as resource handles for databases or credentials for
external services–should be extracted from the code into environment
variables.
But it is not always practical to set environment variables on development
machines or continuous integration servers where multiple projects are run.
dotenv loads variables from a .env file into ENV when the environment is
bootstrapped.
This library exposes functions for doing just that.
Usage
import LoadEnv
import System.Environment (lookupEnv)
main :: IO ()
main = do
loadEnv
print =<< lookupEnv "FOO"
% cat .env
FOO=bar
% runhaskell main.hs
Just "bar"
Don’t override values already set in the environment
Given a hypothetical program load-env, which uses one of our loadEnv
functions on stdin:
Previously,
FOO=bar load-env <<EOM
FOO=bat
EOM
would override FOO to bat when load-env ran. But now, it will see FOO
is already bar and leave it.
This is better behavior under the assumption that a .env file is meant to
specify defaults in the case of nothing explicit. When there are explicit
values in the environment, it’s most likely that our user indeed wants them
respected.