dotenv: Loads environment variables from dotenv files

[ configuration, library, mit, program ] [ Propose Tags ]

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.

To use, call loadFile from your application:

import Configuration.Dotenv
loadFile False "/my/dotenvfile"

This package also includes an executable that can be used to inspect the results of applying one or more Dotenv files to the environment, or for invoking your executables with an environment after one or more Dotenv files is applied.

See the Github page for more information on this package.


[Skip to Readme]

Flags

Manual Flags

NameDescriptionDefault
dev

Turn on development settings.

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.1.0.4, 0.1.0.5, 0.1.0.6, 0.1.0.7, 0.1.0.8, 0.1.0.9, 0.2.0.0, 0.3.0.0, 0.3.0.1, 0.3.0.2, 0.3.0.3, 0.3.1.0, 0.3.2.0, 0.3.3.0, 0.3.4.0, 0.4.0.0, 0.5.0.0, 0.5.0.1, 0.5.0.2, 0.5.1.0, 0.5.1.1, 0.5.2.0, 0.5.2.1, 0.5.2.2, 0.5.2.3, 0.5.2.4, 0.5.2.5, 0.6.0.1, 0.6.0.2, 0.6.0.3, 0.7.0.0, 0.8.0.0, 0.8.0.1, 0.8.0.2, 0.8.0.3, 0.8.0.4, 0.8.0.6, 0.8.0.7, 0.9.0.0, 0.9.0.1, 0.9.0.2, 0.9.0.3, 0.10.0.0, 0.10.0.1, 0.10.1.0, 0.11.0.0, 0.11.0.1, 0.11.0.2, 0.12.0.0 (info)
Change log CHANGELOG.md
Dependencies base (>=4.7 && <5.0), base-compat (>=0.4), dotenv, exceptions (>=0.8 && <0.9), megaparsec (>=6.0 && <7.0), optparse-applicative (>=0.11 && <0.15), process, text, transformers (>=0.4 && <0.6), void (>=0.7 && <0.8) [details]
License MIT
Copyright 2015-2017 Stack Builders Inc.
Author Justin Leitgeb
Maintainer hackage@stackbuilders.com
Category Configuration
Home page https://github.com/stackbuilders/dotenv-hs
Bug tracker https://github.com/stackbuilders/dotenv-hs/issues
Source repo head: git clone git@github.com:stackbuilders/dotenv-hs.git
Uploaded by camm at 2017-08-25T23:37:34Z
Distributions LTSHaskell:0.11.0.2, NixOS:0.11.0.2, Stackage:0.12.0.0
Reverse Dependencies 5 direct, 1 indirect [details]
Executables dotenv
Downloads 29696 total (187 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2017-08-25 [all 1 reports]

Readme for dotenv-0.5.0.1

[back to package description]

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.

NOTE: Empty environment variables

If you need to have empty environment variables in your configuration, you can use something like the code below:

fromMaybe "" <$> lookupEnv "ENV_VAR"

Currently, dotenv-hs doesn't allow you to set empty environment variables, because of setEnv from our System.Environment. This is bug reported in GHC ticket. We have had many dicussions about this. Fortunately, there is already some work for this issue in GHC Phabricator.

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 -e mydotenvfile myprogram

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

$ dotenv -e mydotenvfile myprogram -- --myflag myargument

or:

$ dotenv -e mydotenvfile "myprogram --myflag myargument"

Also, you can use a --example flag to use dotenv-safe functionality so that you can have a list of strict envs that should be defined in the environment or in your dotenv files before the execution of your program. For instance:

$ cat .env.example
DOTENV=
FOO=
BAR=

$ cat .env
DOTENV=123

$ echo $FOO
123

This will fail:

$ dotenv -e .env --example .env.example "myprogram --myflag myargument"
> dotenv: Missing env vars! Please, check (this/these) var(s) (is/are) set: BAR

This will succeed:

$ export BAR=123 # Or you can do something like: "echo 'BAR=123' >> .env"
$ dotenv -e .env --example .env.example "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

(C) 2015-2017 Stack Builders Inc.