Configs

A program needs values that are not code: a port, a greeting, the name of the database. In Elixir they live in the application environment, a key-value store that every process can read, and Application.get_env/3 is the call that reads one. The values come from the files of a Mix project, and the lesson shows both the call and the two files.

The application environment

The environment is a map per application. Application.put_env/3 writes a value and Application.get_env/3 reads it; the third argument is the default, so a key that is not set does not stop the program:

env.exs

Application.put_env(:demo, :greeting, "hello") Application.put_env(:demo, :port, 4000) IO.inspect(Application.get_env(:demo, :greeting)) IO.inspect(Application.get_env(:demo, :port)) IO.inspect(Application.get_env(:demo, :missing, "the default"))

elixir env.exs

"hello"
4000
"the default"

The values are keyed by an atom and belong to an application name, which is why the first argument is :demo here. A process that reads the value does not need the process that wrote it; the store belongs to the runtime, and a supervised server or a test can set a value before the code reads it.

config.exs and runtime.exs

A Mix project puts the values in config/config.exs, which is read at build time, and config/runtime.exs, which is read when the application starts. Both are scripts with import Config; the first holds what every environment shares, and the second holds what the machine decides:

In config/config.exs: config :demo, greeting: "hello". In config/runtime.exs: config :demo, port: String.to_integer(System.get_env("PORT", "4000")). The files are configuration scripts, not programs, so this page does not list them as code blocks; the shape of the call is the whole content.

Fetch and default

Application.fetch_env/2 answers {:ok, value} or :error, which a pattern can tell apart. The bang version raises when the key is missing, which is the right choice for a value the program cannot run without:

fetch.exs

Application.put_env(:demo, :port, 4000) IO.inspect(Application.fetch_env(:demo, :port)) IO.inspect(Application.fetch_env(:demo, :missing)) case Application.fetch_env(:demo, :missing) do {:ok, value} -> IO.puts("got #{value}") :error -> IO.puts("no value, use the default") end

elixir fetch.exs

{:ok, 4000}
:error
no value, use the default

The case is the pattern a library uses when a missing value is allowed: the caller decides whether the default is enough. fetch_env!/2 is the strict form, and the next section shows its report.

The value that moves to the machine

A value that differs between the laptop, the test machine and the server belongs in runtime.exs, read from System.get_env/2. The program below does the same thing without a project, so the reader sees the call that turns an environment variable into a value:

port.exs

port = System.get_env("PORT", "4000") IO.inspect(port) IO.inspect(String.to_integer(port))

PORT=5000 elixir port.exs

"5000"
5000

elixir port.exs

"4000"
4000

The first run exports PORT for one command; the second uses the default. A release reads the same variable at boot, which is why the value is not compiled into the beam: the same build runs on every machine and the environment decides the port.

The mistake: a missing value

Application.fetch_env!/2 raises when the key is not set. The message names the application and the key, and the stack points at the caller:

missing.exs

IO.inspect(Application.fetch_env!(:demo, :missing))

elixir missing.exs

** (ArgumentError) could not fetch application environment :missing for application :demo because the application was not loaded nor configured
    (elixir 1.19.7) lib/application.ex:775: Application.fetch_env!/2
    missing.exs:1: (file)

The fix is the default in get_env/3, the :error branch of fetch_env/2, or the key in config.exs or runtime.exs. A strict fetch is right at boot, where a missing value should stop the application before it serves a request with the wrong port.

The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Where does an Elixir program read a configured value?

    Answer

    From the application environment with Application.get_env/3

    The application environment is the runtime key-value store per application.

  2. What is the difference between config.exs and runtime.exs?

    Answer

    config.exs is read at build time, runtime.exs when the application starts

    compile-time configuration is in config.exs; the machine-specific values are in runtime.exs.

  3. What does Application.get_env(:demo, :missing, "x") return when the key is absent?

    Answer

    "x"

    The third argument is the default returned for a missing key.

  4. What does Application.fetch_env/2 answer for a key that is set?

    Answer

    {:ok, value}

    fetch_env returns an ok tuple or :error, which a pattern can match.

  5. What does this program print? Application.put_env(:demo, :name, "Ada") IO.inspect(Application.get_env(:demo, :name))

    Answer

    "Ada"

    The stored value is the binary "Ada", and inspect prints the quotes.

  6. What does this program print? Application.put_env(:demo, :port, 4000) IO.inspect(Application.fetch_env(:demo, :port))

    Answer

    {:ok, 4000}

    A set key is reported as an ok tuple with the value.

  7. What does this program print? IO.inspect(Application.fetch_env(:demo, :missing))

    Answer

    :error

    fetch_env answers :error for a key that is not set, without raising.

  8. What does this program print? port = System.get_env("PORT", "4000") IO.inspect(String.to_integer(port))

    Answer

    4000

    The default string is converted to the integer 4000.

  9. Why does a release read the port from the environment at boot?

    Answer

    So the same build runs on every machine with its own value

    The value is not compiled in; the machine decides it when the application starts.

  10. What does Application.fetch_env!/2 do when the key is missing?

    Answer

    Raises an ArgumentError that names the application and the key

    The bang version is the strict form and raises instead of returning an error tuple.

Автор статьи: Arthur Isaev