dotenv

Loads environment variables from `.env`.

Readme

dotenv.nim Build Status CircleCI

dotenv implementation for Nim. Loads environment variables from .env

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.

Installation

dotenv can be installed using Nimble:

nimble install dotenv

Or add the following to your .nimble file:

# Dependencies

requires "dotenv >= 1.1.0"

Documentation

Usage

Create a .env file

Create a .env file in the root of your project (or anywhere else - just be sure to specify the path when loading).

DB_HOST=localhost
DB_USER=root
DB_PASS=""
DB_NAME=test

Variables values are always strings, and can be either wrapped in quotes or left without.

Multiline strings can also be used using the """ syntax:

SOME_LONG_STRING="""This string is very long.

It will span multiple lines.
"""

You can also add comments, using the # symbol:

# Comments can fill a whole line
DB_NAME=test # Or they can follow an assignment

Loading the .env file

You can load the .env file from the current working directory as follows:

import dotenv

let env = initDotEnv()
env.load()

# You can now access the variables using os.getEnv()

Or, you can specify the path to the directory and/or file:

import dotenv

let env = initDotEnv("/some/directory/path", "custom_file_name.env")
env.load()

# You can now access the variables using os.getEnv()

By default, dotenv does not overwrite existing environment variables, though this can be done using overload rather than load:

import dotenv

let env = initDotEnv()
env.overload()

# You can now access the variables using os.getEnv()

Loading from a string

You can also load environment variables directly from a string without instantiating a DotEnv instance:

import dotenv, os

loadEnvFromString("""hello = world
    foo = bar
    """)
assert getEnv("foo") == "bar"

Planned features

  • Allow the usage of other environment variables inside variable values.
  • Add validation of variable values, specifying variables have to be integer, or boolean, or a value from a predefined set.
Need help? Read Nimble
Author:
euantorano
Available versions:
License: BSD 3-Clause

Project website Hosted docs