Giter VIP home page Giter VIP logo

dotenv_elixir's Introduction

WARNING: This isn't the Elixir way.

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 typical deployment setup using exrm or similar. Configuration management should be built around Elixir's existing system, not environment variables.

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.

If you're sure this is the correct solution to a problem in your development/deployment workflow, read on!

Dotenv for Elixir

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).

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:

{:dotenv, "~> 3.0.0"}

For master:

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

Fetch your dependencies with mix deps.get.

Now, 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: [...]

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

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(1)> 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.

dotenv_elixir's People

Contributors

alxndr avatar drouchy avatar gullitmiranda avatar jarednorman avatar jgandt avatar lsimoneau avatar maurogeorge avatar mitchellhenke avatar tsubery avatar zorbash avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.