Get current environment name

Chris picture Chris · Jan 26, 2016 · Viewed 8k times · Source

In Phoenix Framework, how can I get the current environment's name ?

I've already tried reading env variables with System.get_env("MIX_ENV"), but the value is not always set.

Answer

Sheharyar picture Sheharyar · Jun 25, 2017

Mix.env() doesn't work in production or other environments where you use compiled releases (built using Exrm / Distillery) or when Mix just isn't available.


The solution is to specify it in your config/config.exs file:

config :your_app, env: Mix.env()

You can then get the environment atom in your application like this:

Application.get_env(:your_app, :env)
#=> :prod