A Mix environment is a compile-time option that changes the shape of compiled Elixir code. The standard environments are dev, test, and prod. section 11.1.6 section 13.1.3
Use the environments to separate operational concerns:
- Use dev for development convenience, including extra logging and less-optimized compiled code. section 11.1.6 section 13.1.3
- Use test for reduced logging, isolated databases, alternate ports, or test-specific implementations. section 11.1.6 section 11.4.2
- Use prod when compiling a deployable version or measuring behavior under heavier load; it is not restricted to the production machine. section 11.1.6 section 13.1.3
Selecting the production compilation
Set MIX_ENV=prod before compiling or starting the system: MIX_ENV=prod mix compile, MIX_ENV=prod iex -S mix, or MIX_ENV=prod elixir -S mix run --no-halt. Mix recompiles the project and dependencies, storing BEAM files under _build/prod. section 11.1.6 section 13.1.3
Listing 11.16 Mix environment-specific settings (todo_env/config/runtime.exs)
import Config
http_port =
if config_env() != :test, #1
do: System.get_env("TODO_HTTP_PORT", "5454"), #1
else: System.get_env("TODO_TEST_HTTP_PORT", "5455") #1
config :todo, http_port: String.to_integer(http_port)
Configure runtime settings conditionally with config_env/0 while preserving stable application setting names. section 11.4.2
