This is Ecto specific, but you can provide compile-time config when defining a module as a repo.
defmodule MyApp.Repo do
use Ecto.Repo, otp_app: :my_app,
adapter: Ecto.Adapters.Postgres
end
You can then add runtime config in the repo's init/2
callback.
defmodule MyApp.Repo do
use Ecto.Repo, otp_app: :my_app,
adapter: Ecto.Adapters.Postgres
def init(_type, config) do
new_config = Keyword.put(config, :url, System.get_env("DATABASE_URL"))
{:ok, new_config}
end
end
A more generic solution would be to provide application config as part of mix.exs
's :env
option. I believe this is runtime only though (someone please correct me if I'm wrong).
Here's what mix help compile.app
provides on this.
:env - default values for the application environment. The application
environment is one of the most common ways to configure applications. See
the Application module for mechanisms to read and write to the application
environment.
Here's an example of application/0
in mix.exs
using :env
.
def application do
[extra_applications: [:logger, :crypto],
env: [key: :value],
registered: [MyServer]]
end