#config #env #macro #configuration #environment

macro envconfig_derive

Build a config object from environment variables without boilerplate

1 unstable release

0.2.0 Sep 12, 2018

#61 in Configuration

Download history 7/week @ 2018-09-12

2 downloads per month
Used in 1 crate

MIT license

158 lines


Build Status License Documentation

Build a config structure from environment variables in Rust without boilerplate.


Let's say you application relies on the following environment variables:


And you want to initialize Config structure like this one:

struct Config {
    host: String,
    port: u16,

You can achieve this with the following code without boilerplate:

extern crate envconfig_derive;
extern crate envconfig;

use envconfig::Envconfig;

pub struct Config {
    #[envconfig(from = "DB_HOST")]
    pub db_host: String,

    #[envconfig(from = "DB_PORT", default = "5432")]
    pub db_port: u16,

fn main() {
    // Assuming the following environment variables are set
    std::env::set_var("DB_HOST", "");

    // Initialize config from environment variables or terminate the process.
    let config = Config::init().unwrap();

    assert_eq!(config.db_host, "");
    assert_eq!(config.db_port, 5432);

Running tests

Tests do some manipulation with environment variables, so to prevent flaky tests they have to be executed in a single thread:

cargo test -- --test-threads=1


  • - migrate to the latest versions of syn and quote
  • - support Option<T> (issue)
  • - support default attribute (issue)
  • - support nested structures?


MIT © Sergey Potapov


  • greyblake Potapov Sergey - creator, maintainer.


~21K SLoC