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