dotenv 0.1.1

dotenv implementation for D. Simplifies handling environment variables.

To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:


dotenv implementation for D. Simplifies handling environment variables.

Environment variables are one of the factors of the 12 Factor App. dotenv helps you manage different environments, and extract data that is sensitive or likely to change into environment variables.

Getting Started

Create a .env file at the root directory of your app. An example might look like,

# MySQL credentials.
MYSQL_HOST = localhost

Variables are declared in NAME=value format, and lines starting with '#' are treated as comments.

Loading Env

Once you've created your file, it can be loaded using Env.load. Env.load also takes additional arguments if you need to customize its behaviour.

import dotenv;

void main()
    // Loads the .env file

    // Loads from a different file name

    // Do not load system environment variables
    Env.load(".env", false);

    // Load only system environment variables

By default, Env will also load the system environment variables. Multiple calls to Env.load can be made to load multiple dotenv files. If multiple variables are loaded under the same name, the newer value will override the previous one.

Accessing Variables

Once loaded, Env behaves like an associative-array. Variables are accessed by name (and are case-insensitive), and their values returned as strings.

Connection connectDB()
    return new Connection(
        Env["MYSQL_HOST"], Env["MYSQL_PORT"],
        Env["MYSQL_USER"], Env["MYSQL_PASS"]

Env also provides property-like access (using opDispatch) to variables, but only to their uppercase names. The properties also accept an optional template argument, to perform a type conversion.

void dbInfo()
    string host = Env.MYSQL_HOST;
    ushort port = Env.MYSQL_PORT!ushort;

    // . . .

Modifying Variables

Since Env behaves like an associative-array, it can also be modified in much the same way.


    Env["MYSQL_USER"] = "test_user";

    // . . .

It also provides empty, length, keys, and values properties that behave as expected.

Different Environments

dotenv is great for managing different environments. For example, if you have a local development environment where you needed to load your configuration exclusively from .env, and a remote production environment where there is no .env file, but only system environment variables, you could do something like,

import dotenv;

shared static this()
    // Try to load .env file without system variables.
    Env.load!((e) {
        import std.exception : ErrnoException;

        if(cast(ErrnoException) e)
            // .env file was not found, load system.
    })(".env", false);

Env.load takes optional callback functions (which must be callable with an Exception object as a parameter), to which any exceptions throw during initialization are forwarded. Here we check if it was an ErrnoException, and assume that if it is, no .env file was found, and that we're not in our local development environment.

Planned Features

  • Support for JSON/SDLang dotenv files, for more complex variables
  • More customization during initialization



  • Mihail-K
0.1.1 2016-May-16
0.1.0 2016-May-12
~master 2016-May-16
Show all 3 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 2 downloads this month

  • 353 downloads total

Short URL: