liveBook · Manning

Application environment

As explained in Elixir in Action, Third Edition

An application environment is an in-memory key-value store associated with an OTP application, where atom keys map to Elixir terms.

An application environment is an in-memory key-value store associated with an OTP application. Atom keys map to Elixir terms, and configuration can be loaded from Elixir scripts before the application starts. section 11.4

Configuring an HTTP port

Read TODO_HTTP_PORT at runtime, default to 5454, convert it to an integer, and store it as :http_port for :todo. Retrieve it with Application.fetch_env!(:todo, :http_port); use Application.get_env/3 when a default is preferable. section 11.4.1

Keep application, Mix, and OS environments distinct. Mix-specific configuration can select different OS variables and defaults, allowing development and test processes to use separate ports or database folders. section 11.4.2

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 runtime configuration script that reads an OS variable and writes the converted port into the application environment. section 11.4.1

Prefer runtime.exs for externally supplied settings. config.exs runs before compilation, so it reads values from the build machine; libraries should generally accept parameters as function arguments instead. section 11.4.3

How Elixir in Action, Third Edition covers it

How deep it goes
Explains storage, runtime retrieval, environment variation, and compile-time pitfalls without detailing internal implementation.
Examples
Uses configurable HTTP ports, separate test settings, and a configurable database folder.
What it assumes
Assumes familiarity with Elixir applications and Mix while distinguishing application, Mix, and OS environments explicitly.
What you can do after
Operate runtime configuration by loading OS variables, storing typed settings, and retrieving them from application code.
How it connects
Connects OTP applications with config scripts, Mix environments, OS environment variables, and runtime configuration.

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.