Mix v1.0.5 Mix.Config

Module for defining, reading and merging app configurations.

Most commonly, this module is used to define your own configuration:

use Mix.Config

config :plug,
  key1: "value1",
  key2: "value2"

import_config "#{Mix.env}.exs"

All config/* macros, including import_config/1, are used to help define such configuration files.

Furthermore, this module provides functions like read!/1, merge/2 and friends which help manipulate configurations in general.

Summary

Functions

Merges two configurations

Persists the given configuration by modifying the configured applications environment

Reads and validates a configuration file

Reads many configuration files given by wildcard into a single config

Validates a configuration

Macros

Configures the given application

Configures the given key for the given application

Imports configuration from the given file

Functions

merge(config1, config2)

Merges two configurations.

The configuration of each application is merged together with the values in the second one having higher preference than the first in case of conflicts.

Examples

iex> Mix.Config.merge([app: [k: :v1]], [app: [k: :v2]])
[app: [k: :v2]]

iex> Mix.Config.merge([app1: []], [app2: []])
[app1: [], app2: []]
persist(config)

Persists the given configuration by modifying the configured applications environment.

read!(file)

Reads and validates a configuration file.

read_wildcard!(config, path)

Reads many configuration files given by wildcard into a single config.

validate!(config)

Validates a configuration.

Macros

config(app, opts)

Configures the given application.

Keyword lists are always deep merged.

Examples

The given opts are merged into the existing configuration for the given app. Conflicting keys are overridden by the ones specified in opts. For example, the declaration below:

config :lager,
  log_level: :warn,
  mode: :truncate

config :lager,
  log_level: :info,
  threshold: 1024

Will have a final configuration of:

[log_level: :info, mode: :truncate, threshold: 1024]
config(app, key, opts)

Configures the given key for the given application.

Keyword lists are always deep merged.

Examples

The given opts are merged into the existing values for key in the given app. Conflicting keys are overridden by the ones specified in opts. For example, the declaration below:

config :ecto, Repo,
  log_level: :warn

config :ecto, Repo,
  log_level: :info,
  pool_size: 10

Will have a final value for Repo of:

[log_level: :info, pool_size: 10]
import_config(file)

Imports configuration from the given file.

The path is expected to be relative to the directory the current configuration file is on.

Examples

This is often used to emulate configuration across environments:

import_config "#{Mix.env}.exs"

Or to import files from children in umbrella projects:

import_config "../apps/*/config/config.exs"