MCPcopy Index your code
hub / github.com/dotenv-rs/dotenv

github.com/dotenv-rs/dotenv @v0.15.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.15.0 ↗ · + Follow
85 symbols 189 edges 22 files 6 documented · 7%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

rust-dotenv

Build Status 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"));
}

Core symbols most depended-on inside this repo

assert_parsed_string
called by 13
dotenv/src/parse.rs
dotenv
called by 6
dotenv/src/lib.rs
find
called by 6
dotenv/src/find.rs
load
called by 6
dotenv/src/iter.rs
skip_whitespace
called by 4
dotenv/src/parse.rs
expect_equal
called by 3
dotenv/src/parse.rs
apply_substitution
called by 3
dotenv/src/parse.rs
var
called by 3
dotenv/src/lib.rs

Shape

Function 65
Method 15
Class 3
Enum 2

Languages

Rust96%
Python4%

Modules by API surface

dotenv/src/parse.rs34 symbols
dotenv/src/errors.rs12 symbols
dotenv/src/lib.rs8 symbols
dotenv/src/find.rs5 symbols
dotenv/src/iter.rs4 symbols
azure-pipelines/fix_coverage_for_cobertura.py3 symbols
dotenv_codegen_implementation/src/lib.rs2 symbols
dotenv_codegen/tests/basic_dotenv_macro.rs2 symbols
dotenv/tests/common/mod.rs2 symbols
dotenv/src/bin/dotenv.rs2 symbols
dotenv/tests/test-vars.rs1 symbols
dotenv/tests/test-variable-substitution.rs1 symbols

For agents

$ claude mcp add dotenv \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact