Reputation: 281
I have two mix
projects P1 and P2.
P1 depends on ecto and provides a genserver interface. It is also added as a dependency in P2 only to have access to some client functions. So P2 does not try to start P1. It is started independently.
The problem is that P2 cannot be compiled. mix compile
says that the the adapter configuration for P1.Repo
is missing.
I do not want to include any repo configuration for P1 inside P2. Is there any way to use functionality provided by P1's modules inside P2 under these circumstances?
Upvotes: 0
Views: 1063
Reputation: 5191
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
Upvotes: 1
Reputation: 9299
If P1 is dependency of P2, P2 is responsible for configuration and nothing can change that, but you have couple of other options:
use umbrella project The umbrella project can have its own config with P1.Repo. This way you keep P1 and P2 configs cleaner.
additionally you can tear out meaningful chunk from P1 that is used by P2 and move it to P3. Both P1 and P2 depend on P3.
Upvotes: 0