Configs
| config files | |
| Per-environment | |
| runtime.exs | |
| Application.fetch_env! |
config files
Settings live outside the code in files under config/. Each file groups values by application name, so ports, hosts, and feature flags stay visible in one place instead of hiding inside modules.
import Config
config(:hello, greeting: "hi")
Per-environment
Development and production rarely share settings. Keep common values in config/config.exs and overrides in files like config/dev.exs and config/prod.exs, selected by the current environment.
import Config
import_config("#{config_env()}.exs")
runtime.exs
Values baked at build time cannot follow the deploy environment. The file config/runtime.exs runs at boot, so it reads the machine around it and configures the release accordingly.
port = System.get_env("PORT", "4000")
config(:hello, port: port)
Application.fetch_env!
Read required settings with a strict call that raises when the key is missing. A loud boot failure beats a quiet wrong default that surfaces hours later.
Application.fetch_env!(:hello, :greeting)
Next: Releases
Article author: Arthur Isaev