mblagusz

mblagusz

Runtime configuration for a dependent library

We are in the process of moving some shared functionality (mail sending) into a separate application that’s a dependency of our main app. That is, a shared library.

The problem we’re running into is that the mail configuration currently lives inside config/runtime.exs in our main application. If we move it to config/runtime.exs inside the dependent library, then Application.get_env(:mailer, …) doesn’t find it anymore. It would seem that only the “main” runtime.exs file is being loaded by mix run.

I see some libraries use files like config/prod.exs, but I want the shared configuration to read environment variables at startup. Is there a pattern that will let me do this?

Most Liked

LostKobrakai

LostKobrakai

Not just runtime.exs. All config can only come from the top level mix project you’re on. Config of dependencies is ignored.

mayel

mayel

Not only that, but it is generally discouraged to use the config for libraries (see the infobox at Config — Elixir v1.16.2).

I personally consider one’s “internal” dependencies to be an exception, and so this is my approach to runtime config for such libraries:

in the main app’s runtime.exs:

[SubApp1.RuntimeConfig]
|> Enum.each(&apply(&1, :config))

in the dependency (at lib/runtime_config.exs):

defmodule SubApp1.RuntimeConfig do
  def config do
    import Config
    
    config :sub_app1,
    	key: System.get_env("SUB_APP1_key", "default_val")

Last Post!

mayel

mayel

Yeah that’s why I only suggested it for “internal” libraries, with the advantage of keeping the config in the same repo as the code rather than copy/pasting it all in runtime.exs but YYMV…

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement