21 releases (breaking)

0.15.0 Oct 21, 2019
0.14.1 May 15, 2019
0.13.0 May 30, 2018
0.11.0 Feb 20, 2018
0.2.0 Nov 20, 2014

#424 in Configuration

Download history 109674/week @ 2023-10-25 111838/week @ 2023-11-01 106105/week @ 2023-11-08 119428/week @ 2023-11-15 105994/week @ 2023-11-22 114094/week @ 2023-11-29 112040/week @ 2023-12-06 110837/week @ 2023-12-13 81961/week @ 2023-12-20 63262/week @ 2023-12-27 98596/week @ 2024-01-03 104536/week @ 2024-01-10 123621/week @ 2024-01-17 123856/week @ 2024-01-24 115386/week @ 2024-01-31 95858/week @ 2024-02-07

477,777 downloads per month
Used in 1,251 crates (1,013 directly)

MIT license

35KB
847 lines

rust-dotenv

CI codecov Crates.io

Achtung! This is a v0.* version! Expect bugs and issues all around. Submitting pull requests and issues is highly encouraged!

Quoting bkeepers/dotenv:

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.

This library is meant to be used on development or testing environments in which setting environment variables is not practical. It loads environment variables from a .env file, if available, and mashes those with the actual environment variables provided by the operative system.

Usage

The easiest and most common usage consists on calling dotenv::dotenv when the application starts, which will load environment variables from a file named .env in the current directory or any of its parents; after that, you can just call the environment-related method you need as provided by std::os.

If you need finer control about the name of the file or its location, you can use the from_filename and from_path methods provided by the crate.

dotenv_codegen provides the dotenv! macro, which behaves identically to env!, but first tries to load a .env file at compile time.

Examples

A .env file looks like this:

# a comment, will be ignored
REDIS_ADDRESS=localhost:6379
MEANING_OF_LIFE=42

You can optionally prefix each line with the word export, which will conveniently allow you to source the whole file on your shell.

A sample project using Dotenv would look like this:

extern crate dotenv;

use dotenv::dotenv;
use std::env;

fn main() {
    dotenv().ok();

    for (key, value) in env::vars() {
        println!("{}: {}", key, value);
    }
}

Variable substitution

It's possible to reuse variables in the .env file using $VARIABLE syntax. The syntax and rules are similar to bash ones, here's the example:


VAR=one
VAR_2=two

# Non-existing values are replaced with an empty string
RESULT=$NOPE #value: '' (empty string)

# All the letters after $ symbol are treated as the variable name to replace
RESULT=$VAR #value: 'one'

# Double quotes do not affect the substitution
RESULT="$VAR" #value: 'one'

# Different syntax, same result 
RESULT=${VAR} #value: 'one'

# Curly braces are useful in cases when we need to use a variable with non-alphanumeric name
RESULT=$VAR_2 #value: 'one_2' since $ with no curly braces stops after first non-alphanumeric symbol 
RESULT=${VAR_2} #value: 'two'

# The replacement can be escaped with either single quotes or a backslash:
RESULT='$VAR' #value: '$VAR'
RESULT=\$VAR #value: '$VAR'

# Environment variables are used in the substutution and always override the local variables
RESULT=$PATH #value: the contents of the $PATH environment variable
PATH="My local variable value"
RESULT=$PATH #value: the contents of the $PATH environment variable, even though the local variable is defined

Dotenv will parse the file, substituting the variables the way it's described in the comments.

Using the dotenv! macro

Add dotenv_codegen to your dependencies, and add the following to the top of your crate:

#[macro_use]
extern crate dotenv_codegen;

Then, in your crate:

fn main() {
  println!("{}", dotenv!("MEANING_OF_LIFE"));
}

lib.rs:

This crate provides a configuration loader in the style of the ruby dotenv gem. This library is meant to be used on development or testing environments in which setting environment variables is not practical. It loads environment variables from a .env file, if available, and mashes those with the actual environment variables provided by the operating system.

Dependencies

~70KB