Elixir v1.9.0-rc.0 Config View Source
A simple keyword-based configuration API.
Example
This module is most commonly used to define application
configuration, typically in config/config.exs:
import Config
config :some_app,
key1: "value1",
key2: "value2"
import_config "#{Mix.env()}.exs"
import Config will import the functions config/2, config/3
and import_config/1 to help you manage your configuration.
Once Mix starts, it will automatically evaluate the configuration
file and persist it into :some_app's application environment, which
can be accessed in as follows:
"value1" = Application.fetch_env!(:some_app, :key1)
See Config.Reader for evaluating and reading configuration
files.
Important: if you are writing a library to be used by other developers, it is generally recommended to avoid the application environment, as the application environment is effectively a global storage. For more information, read our library guidelines.
Migrating from use Mix.Config
The Config module in Elixir was introduced in v1.9 as a
replacement to Mix.Config, which was specific to Mix and
has been deprecated.
You can leverage Config instead of Mix.Config in two steps.
The first step is to replace use Mix.Config at the top of
your config files by import Config.
The second is to make sure your import_config/1 calls do
not have a wildcard character. If so, you need to perform
the wildcard lookup manually. For example, if you did:
import_config "../apps/*/config/config.exs"
It has to be replaced by:
for config <- "apps/*/config/config.exs" |> Path.expand() |> Path.wildcard() do
import_config config
end
Link to this section Summary
Functions
Configures the given root_key.
Configures the given key for the given root_key.
Imports configuration from the given file.
Link to this section Functions
Configures the given root_key.
Keyword lists are always deep-merged.
Examples
The given opts are merged into the existing configuration
for the given root_key. Conflicting keys are overridden by the
ones specified in opts. For example, the application
configuration below
config :logger,
level: :warn,
backends: [:console]
config :logger,
level: :info,
truncate: 1024
will have a final configuration for :logger of:
[level: :info, backends: [:console], truncate: 1024]
Configures the given key for the given root_key.
Keyword lists are always deep merged.
Examples
The given opts are merged into the existing values for key
in the given root_key. Conflicting keys are overridden by the
ones specified in opts. For example, the application
configuration below
config :ecto, Repo,
log_level: :warn,
adapter: Ecto.Adapters.Postgres
config :ecto, Repo,
log_level: :info,
pool_size: 10
will have a final value of the configuration for the Repo
key in the :ecto application of:
[log_level: :info, pool_size: 10, adapter: Ecto.Adapters.Postgres]
Imports configuration from the given file.
In case the file doesn't exist, an error is raised.
If file is a relative, it will be expanded relatively to the directory the current configuration file is in.
Examples
This is often used to emulate configuration across environments:
import_config "#{Mix.env()}.exs"