Get current environment name

You can use Mix.env/0:

iex(1)> Mix.env
:dev

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

Update (March 2022):

Recent versions of Elixir (v1.13.x+) recommend using config_env() instead of Mix.env(), so do this:

config :your_app, env: config_env()

An alternative to putting the Mix.env/0 into your applications configuration is to use a module attribute. This also works in production because module attributes are evaluated at compile time.

defmodule MyModule do
  @env Mix.env()

  def env, do: @env
end

If you only need the environment in a specific place - for example when starting up the application supervisor - this tends to be the easier solution.