Enviable
- code :: https://github.com/halostatue/enviable
- issues :: https://github.com/halostatue/enviable/issues
Enviable is a small collection of functions to improve Elixir project configuration via environment variables as proposed under the 12-factor application model. It works well with configuration environment loaders like Dotenvy, Nvir, or Envious and provides robust value conversion like jetenv.
Enviable 2.0 removes deprecated functions and changes defaults as previously documented. Elixir 1.17 or later is required.
Usage
Enviable will typically be imported in config/runtime.exs after Config, but
may be used anywhere that environment variables are read.
Enviable with Nvir
import Nvir
import Enviable
client = fetch_env!("CLIENT")
dotenv!([".env", ".env.#{client}"])
config :my_app,
key: fetch_env!("SECRET_KEY"),
port: fetch_env_as_integer!("PORT"),
ssl: get_env_as_boolean("SSL_ENABLED")Enviable with Dotenvy
# config/runtime.exs
import Config
import Enviable
client = fetch_env!("CLIENT")
Dotenvy.source([".env", ".env.#{client}", get_env()], side_effect: &put_env/1)
config :my_app,
key: fetch_env!("SECRET_KEY"),
port: fetch_env_as_integer!("PORT"),
ssl: get_env_as_boolean("SSL_ENABLED")Info
When using Dotenvy, the use of a
side_effectthat callsSystem.put_env/1is required, as Enviable works with the system environment variable table. Future versions of Enviable may offer ways to work with the default Dotenvy behaviour.
Enviable with Envious
# config/runtime.exs
import Config
import Enviable
client = fetch_env!("CLIENT")
env_files = [".env", ".env.#{client}"]
loaded_env =
Enum.reduce(env_files, %{}, fn file, acc ->
with {:ok, contents} <- File.read(file),
{:ok, env} <- Envious.parse(contents) do
Map.merge(acc, env)
else
_ -> acc
end
end)
for {key, value} <- loaded_env, do: put_env_new(key, value)
config :my_app,
key: fetch_env!("SECRET_KEY"),
port: fetch_env_as_integer!("PORT"),
ssl: get_env_as_boolean("SSL_ENABLED")Installation
Enviable can be installed by adding enviable to your list of dependencies in
mix.exs:
def deps do
[
{:enviable, "~> 2.1"},
# Optional, required for decimal conversion
{:decimal, "~> 2.3"},
# Optional, required only if :json or JSON aren't available or another
# JSON engine is not configured
{:jason, "~> 1.4"},
# Optional, if using the Enviable credo checks
{:credo, "~> 1.7", only: [:dev, :test], runtime: false}
]
endDocumentation is found on HexDocs.
Semantic Versioning
Enviable follows Semantic Versioning 2.0.