dotenv

Loads environment variables from dotenv files

https://github.com/stackbuilders/dotenv-hs

Version on this page:0.3.4.0
LTS Haskell 22.43:0.11.0.2
Stackage Nightly 2024-12-08:0.12.0.0
Latest on Hackage:0.12.0.0

See all snapshots dotenv appears in

MIT licensed by Justin Leitgeb
Maintained by [email protected]
This version can be pinned in stack with:dotenv-0.3.4.0@sha256:d97858d99668affd62f01e7cf403d2c4f320acdab2493a9a3e05f303d209caf3,4325

Build Status Hackage

Dotenv files for Haskell

In most applications, configuration should be separated from code. While it usually works well to keep configuration in the environment, there are cases where you may want to store configuration in a file outside of version control.

“Dotenv” files have become popular for storing configuration, especially in development and test environments. In Ruby, Python and Javascript there are libraries to facilitate loading of configuration options from configuration files. This library loads configuration to environment variables for programs written in Haskell.

Installation

In most cases you will just add dotenv to your cabal file. You can also install the library and executable by invoking stack install dotenv.

Usage

Set configuration variables in a file following the format below:

S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE

Then, calling Dotenv.load from your Haskell program reads the above settings into the environment:

import qualified Configuration.Dotenv as Dotenv
Dotenv.loadFile False "/path/to/your/file"

After calling Dotenv.load, you are able to read the values set in your environment using standard functions from System.Environment such as lookupEnv and getEnv.

Configuration

The first argument to loadFile specifies whether you want to override system settings. False means Dotenv will respect already-defined variables, and True means Dotenv will overwrite already-defined variables.

Advanced Dotenv File Syntax

You can add comments to your Dotenv file, on separate lines or after values. Values can be wrapped in single or double quotes. Multi-line values can be specified by wrapping the value in double-quotes, and using the “\n” character to represent newlines.

The spec file is the best place to understand the nuances of Dotenv file parsing.

Command-Line Usage

You can call dotenv from the command line in order to load settings from one or more dotenv file before invoking an executable:

dotenv -f mydotenvfile myprogram

Aditionally you can pass arguments and flags to the program passed to Dotenv:

dotenv -f mydotenvfile myprogram -- --myflag myargument

Hint: The env program in most Unix-like environments prints out the current environment settings. By invoking the program env in place of myprogram above you can see what the environment will look like after evaluating multiple Dotenv files.

Author

Justin Leitgeb

License

MIT

Copyright

(C) 2015-2017 Stack Builders Inc.

Changes

MASTER

Dotenv 0.3.4.0

  • Allow optparse-applicative 0.14

Dotenv 0.3.3.0

  • Add support for variable expansion. Thanks to حبيب الامين (GitHub: habibalamin) for making this contribution.

Dotenv 0.3.2.0

  • Add the option to pass arguments to the program passed to Dotenv. Thanks to Oleg Grenrus (GitHub: phadej) for making this contribution.

Dotenv 0.3.1.0

  • Made interface more polymorphic so the functions works in any instance of MonadIO, not only IO. This should reduce amount of lifting in some cases.

  • Added onMissingFile helper to deal with possibly missing files.

  • Parser was rewritten to take full advantage of Megaparsec. hspec-megaparsec is now used for testing of the parser.

  • Dropped support for GHC 7.4.

Dotenv 0.3.0.3

  • Allow optparse-applicative 0.13

Dotenv 0.3.0.1

  • Remove unnecessary package dependencies.

Dotenv 0.3.0.0

  • Reverted change to Data.Text in favor of String, for maintaining compatibility with common Haskell system libraries. Added separate interface for parsing a file into tuples containing Data.Text values. Thanks to Daisuke Fujimura (GitHub: fujimura).
  • Fixed parsing of CRLF characters for Windows users.

Dotenv 0.2.0.0 (deprecated)

  • Changed public interfaces to use Data.Text.
  • Added function parseFile to read dotenv file without modifying the environment. Thanks to Daisuke Fujimura (GitHub: fujimura) for making this contribution.

Dotenv 0.1.0.0

  • First public release.