Most straight forward way is like this:

if System.get_env("MIX_ENV") == "dev" do
  # ...
end

…or

Mix.env == :dev

But the problem is that if you are deploying with Exrm or Distillery then Mix doesn’t work in production source.

This may not be an issue if you use Docker image that includes Mix (which depends on usecase may/may not be good enough for you) but in general it’s recommended to do following (solution from SO user Sheharyar :

Create custom config/config.exs:

# config/config.exs
config :your_app, env: Mix.env

Then you can get environment:

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