env_var_provider v0.5.1 EnvVar.Provider

A Config.Provider that reads a configuration schema from a map and reads configuration from environment variables.

Variable names are constructed from the field names directly, following a convention.

Usage

Define a function that returns a map representing the configuration. For example, you can have a module just for that:

defmodule MyApp.EnvVarConfig do
  def schema do
    %{
      my_app: %{
        port: %{type: :integer}
      }
    }
  end
end

Now you can add EnvVar.Provider as a config provider in your release configuration:

def project do
  [
    # ...,
    releases: [
      my_release: [
        config_providers: [
          {EnvVar.Provider,
           env_map: MyApp.EnvVarConfig.schema(),
           prefix: "",
           enforce: true}
        ]
      ]
    ]
  ]

Options

  • :enforce - (boolean) if true, raise an error if any environment variables are not present when reading the configuration. Required.

  • :prefix - (string or atom) prepended to the name of system environment variables. For example, if you pass prefix: "BEOWULF_" and you want to configure :port inside :my_app, the environment variable name will be BEOWULF_MY_APP_PORT. Required.

  • :env_map - (map or {module, function, args}) the configuration schema. Can be a map of configuration or a {module, function, args} tuple that returns a map of configuration when invoked (as module.function(args...)).

Configuration schema

The configuration schema is a map with applications as the top-level keys and maps of configuration as their values. The schema for each configuration option is a map with at least the :type key.

%{
  my_app: %{
    port: %{type: :integer}
  }
}

The supported schema properties are:

  • :type - see below

  • :default - the default value if no environment variable is found. This value will be parsed just like the environment variable would, so it should always be a string.

The supported types are:

  • simple types - :string, :integer, :float, or :boolean

  • {:tuple, TYPE, SEPARATOR} - complex type where the second field is one of the simple types above. SEPARATOR is used as the separator.

  • {:tuple, TYPE} - same as {:tuple, TYPE, ","}.

  • {:list, TYPE, SEPARATOR} and {:list, TYPE} - complex type that behaves like {:tuple, ...} but parsing to a list.

A note on boolean types. The following are supported syntax:

  • "true"
  • "1" -> true
  • "false"
  • "0" -> false

Variable name convention

EnvVar.Provider will look for system environment variables by upcasing configuration names and separating with underscores. For example, if you configure the :port key of the :my_app application, it will look for the MY_APP_PORT environment variable.

Link to this section Summary

Link to this section Functions

Link to this function

convert(env_value, arg2)