liveBook · Manning

Mix environment

As explained in Elixir in Action, Third Edition

The prod environment is the Mix configuration intended to produce a build close to the deployed production version, independently of the machine where it runs.

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:

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

How Elixir in Action, Third Edition covers it

How deep it goes
Explains compile-time branching, environment selection, artifact locations, and configuration variation, while deferring release internals.
Examples
Uses debug logging, test databases, alternate HTTP ports, conditional storage implementations, and _build/prod artifacts.
What it assumes
Assumes familiarity with Mix tasks and configuration, while distinguishing compile-time Mix settings from runtime application and OS environments.
What you can do after
Operate/configure builds with MIX_ENV, choose production compilation for realistic measurements, and vary settings without changing application code.
How it connects
Connects Mix environments with compile-time configuration, application environments, OS environments, OTP applications, and releases.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.