Dotenv

Small lib to allow storing config separate from code.

Dotenv loads variables from a file (named by default .env, hence the name) into the application environment. They can be read in Sys.env.

This allows for the user to reproduce the deployed environments locally, as if the application was deployed in said environments.

This way we keep the secrets out of the committed code, and the application ready to access them when deployed. Please don't commit the environment files ;)

This is a port of JavaScript's Dotenv (https://github.com/motdotla/dotenv).

I have adapted it a little, but the result should be similar enough so that nothing from the JS world is missed.

Install

With opam:

opam install dotenv

Usage

Dotenv should be the first thing to be run on you program's entry function.

Ex:

let _ =
  Dotenv.export () |> ignore
  let whatever_secret = Sys.get_env_opt "secret" in
  rest of the app...

This will read from a .env file at the base of your app and populate the environment with the variables that it find in export format (VAR=value).

Methods

Dotenv has two methods: parse and export.

export will run through the env file and add the valid variables to the environment. They are then accessible via Sys.get_env exactly as if they were on the deployment (or development) environment when the program started.

parse will run through the env file and get all the variables onto an association list, so that it can then be used without having the variables on the environment. This is probably not used in most cases. Can be useful for loading files that aren't secret or for some types of debugging. Don't forget the debug option, there's a lot of good info there.

Arguments to the functions:

export and parse have three possible arguments, all optional:

Parse Rules

The parse rules used are pretty much the same as dotenv for JS - from which I partly used the text below, with some edits -, with some small differences that are presented next. The parsing will be the same for parse or export methods, but the latter will add VAR to the application environment with value VALUE: