liveBook · Manning

Runtime config file

As explained in Elixir in Action, Third Edition

runtime.exs is an Elixir configuration script evaluated at runtime before an application starts so runtime-specific settings can be loaded.

runtime.exs is evaluated when the system starts, before the application runs. It converts runtime-specific inputs such as OS environment variables into application settings, avoiding hard-coded deployment values. section 11.4.1

Configuring an HTTP port

Read TODO_HTTP_PORT with a default of 5454, convert it to an integer, and store it as :http_port for :todo. Todo.Web can retrieve it with Application.fetch_env!/2; missing configuration raises an error. section 11.4.1

Listing 11.14 Configuring the HTTP port (todo_env/config/runtime.exs)
import Config                                           #1
 
http_port = System.get_env("TODO_HTTP_PORT", "5454")    #2
config :todo, http_port: String.to_integer(http_port)   #3

Shows the compact runtime.exs pipeline from OS environment variable to typed application setting. section 11.4.1

Use runtime configuration to:

  • Select different defaults and OS variables according to config_env(), such as separate development and test ports. section 11.4.2
  • Keep test data isolated by varying settings such as the database folder. section 11.4.2
  • Prefer runtime.exs over config.exs when values depend on the machine that runs the system; config.exs executes before compilation. section 11.4.3

How Elixir in Action, Third Edition covers it

How deep it goes
Explains environment lookup, type conversion, environment selection, application retrieval, and compile-time configuration trade-offs.
Examples
Uses configurable HTTP ports, separate test defaults, and an environment-specific database folder.
What it assumes
Assumes familiarity with Elixir applications, Mix environments, OS variables, and application environment lookup.
What you can do after
Operate/configure Elixir applications with runtime-provided ports and environment-specific settings.
How it connects
Connects OS environment variables, application environment, Mix environments, OTP applications, and config.exs.

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.