• Stars
    star
    226
  • Rank 176,514 (Top 4 %)
  • Language
    Elixir
  • License
    MIT License
  • Created almost 11 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A port of dotenv to Elixir

Dotenv for Elixir

Build Module Version Hex Docs Total Download License Last Updated

This is a port of @bkeepers' dotenv project to Elixir. You can read more about dotenv on that project's page. The short version is that it simplifies developing projects where configuration is stored in environment variables (e.g. projects intended to be deployed to Heroku).

WARNING: Not compatible with Elixir releases

Elixir has an excellent configuration system and this dotenv implementation has a serious limitation in that it isn't available at compile time. It fits very poorly into a deployment setup using Elixir releases, distillery, or similar.

Configuration management should be built around Elixir's existing configuration system. A good example is Phoenix which generates a project where the production config imports the "secrets" from a file stored outside of version control. Even if you're using this for development, the same approach could be taken.

However, if you are using Heroku, Dokku, or another deployment process that does not use releases, read on!

Quick Start

The simplest way to use Dotenv is with the included OTP application. This will automatically load variables from a .env file in the root of your project directory into the process environment when started.

First add :dotenv to your dependencies.

For the latest release:

defp deps do
  [
    {:dotenv, "~> 3.0.0"}
  ]
end

Most likely, if you are deploying in a Heroku-like environment, you'll want to only load the package in a non-production environment:

{:dotenv, "~> 3.0.0", only: [:dev, :test]}

For master:

{:dotenv, github: "avdi/dotenv_elixir"}

Fetch your dependencies with mix deps.get.

Now, when you load your app in a console with iex -S mix, your environment variables will be set automatically.

Using Environment Variables in Configuration

Mix loads configuration before loading any application code. If you want to use .env variables in your application configuration, you'll need to load dotenv manually on application start and reload your application config:

defmodule App.Application do
  use Application

  def start(_type, _args) do
    unless Mix.env == :prod do
      Dotenv.load
      Mix.Task.run("loadconfig")
    end

    # ... the rest of your application startup
  end
end

Elixir 1.9 and older

If you are running an old version of Elixir, you'll need to add the :dotenv application to your applications list when running in the :dev environment:

# Configuration for the OTP application
def application do
  [
    mod: { YourApp, [] },
    applications: app_list(Mix.env)
  ]
end

defp app_list(:dev), do: [:dotenv | app_list]
defp app_list(_), do: app_list
defp app_list, do: [...]

Reloading the .env file

The Dotenv.reload!/0 function will reload the variables defined in the .env file.

More examples of the server API usage can be found in dotenv_app_test.exs.

Serverless API

If you would like finer-grained control over when variables are loaded, or would like to inspect them, Dotenv also provides a serverless API for interacting with .env files.

The load!/1 function loads variables into the process environment, and can be passed a path or list of paths to read from.

Alternately, load/1 will return a data structure of the variables read from the .env file:

iex> Dotenv.load
%Dotenv.Env{paths: ["/elixir/dotenv_elixir/.env"],
 values: %{"APP_TEST_VAR" => "HELLO"}}

For further details, see the inline documentation. Usage examples can be found in dotenv_test.exs.

Copyright and License

Copyright (c) 2014 Avdi Grimm

This library is released under the MIT License. See the LICENSE.md file for further details.

More Repositories

1

naught

A toolkit for building Null Object classes in Ruby
Ruby
1,049
star
2

quarto

Ruby
469
star
3

sbpprb

Smalltalk Best Practice Patterns in Ruby
Ruby
456
star
4

ppwm

A site to promote diverse pair-programming
CSS
223
star
5

hammertime

Exception debugging console for Ruby
Ruby
208
star
6

greenletters

Ruby console automation a la Expect
Ruby
116
star
7

alter-ego

Avdi's personal fork of AlterEgo
Ruby
58
star
8

.emacs24.d

A from-scratch rebuild of my Emacs configuration
Emacs Lisp
46
star
9

cowsay

Enter the cow, baby!
Ruby
41
star
10

firetower

A command-line interface to Campfire chats
Ruby
39
star
11

keyword_params

Declarative Keyword Paramaters for Ruby
Ruby
34
star
12

orgpress

A collection of Makefiles and scripts for generating formatted eBooks.
Ruby
34
star
13

nulldb

NOTE: This is no longer the official Github home of NullDB.
Ruby
32
star
14

leadlight

Rose colored stained glass windows for HTTP
Ruby
30
star
15

rpcfn-interactive-fiction

Ruby Programming Challenge for Newbies: Interactive Fiction
Ruby
28
star
16

brainguy

Ruby
27
star
17

Xcast

Ruby
26
star
18

hookr

A publish/subscribe callback hook facility for Ruby.
Ruby
19
star
19

cow

Ruby
19
star
20

org-rtm

Emacs org-mode/Remember the Milk Integration
Emacs Lisp
19
star
21

lockstep

Ruby
18
star
22

refactoring-in-ruby

My notebook as I work through the exercises in Refactoring in Ruby
15
star
23

gem-love

Show your love for your favorite gems
Ruby
13
star
24

fp_oo_elx

Elixir
13
star
25

walrus

Shell
12
star
26

fail-fast

Avdi's personal fork of FailFast
JavaScript
11
star
27

dm-google-sheet-adapter

DataMappr adapter for Google Sheet
Ruby
9
star
28

tapas-queue

A thread-safe, optionally bounded, timeout-capable queue based on RubyTapas episodes 137-143 (and a few others).
Ruby
8
star
29

gem-isntall

Ruby
7
star
30

tapas-dotfiles

Emacs Lisp
6
star
31

spartan

Butt-naked Ruby tests.
Ruby
6
star
32

handcar

Debugging utility for Rails
Ruby
5
star
33

clafamatt

CLAss FAMily ATTributes gives your classes class-inheritable attributes without all that tedious mucking about with @inheritable_attributes
Ruby
5
star
34

curator

A system for managing all of your digital artifacts
Ruby
5
star
35

mythbashers

JavaScript
4
star
36

feedupdater

Bob "sporkmonger" Aman's Feed Updater Utility
Ruby
4
star
37

gilded_rose_kata

Ruby
4
star
38

preen

Ruby
3
star
39

aws-sam

Playing around with AWS SAM
Python
3
star
40

bewarethemoose

Beware of the Moose
Shell
3
star
41

rack-stereoscope

TODO: one-line summary of your gem
Ruby
3
star
42

zero-zero

An ejection seat for Ruby apps
Ruby
3
star
43

polyglop

POLYglot Grand Language Omniscience Project
Ruby
3
star
44

hookah

Ruby
3
star
45

http_client

A replacement for net/http
Ruby
3
star
46

raffle

Ruby
3
star
47

rack_base_uri

A Rack middleware for automatically setting [X]HTML document base URIs.
Ruby
2
star
48

rudyard

Ruby Refactoring Framework
2
star
49

gem-lovefest

The server side of gem-love
Ruby
2
star
50

lmcr

The example code for Let's Make a Chatbot in Ruby
Ruby
2
star
51

favo5

Ruby
2
star
52

tick

Ruby
2
star
53

dacs

Devver Application Configuration System
Ruby
2
star
54

webscripts

1
star
55

odin

A project for learning TypeScript
TypeScript
1
star
56

rake-server

An experiment at turning Rake into a task server
Ruby
1
star
57

methodical

Automation framework for sequential operations
Ruby
1
star
58

daemon-herder

Ruby
1
star
59

flock-block

Block helper for file locking
Ruby
1
star
60

prototypes-in-ruby

Ruby
1
star
61

pulse-ffi

Ruby
1
star
62

tapas_shot_renumber

A tool for renumbering shots in RubyTapas-style scripts
Dockerfile
1
star
63

rails-synctest

Temporary
Ruby
1
star
64

articles

1
star
65

funwithgit

1
star
66

rut

Ruby
1
star