Add configs dynamic

Good morning people.
I am having a question about how I could do a function that would do an external access and then I would add this setting in the config.exs file.

config.exs

config :myapp_web, MyappWeb.Endpoint,
  test: [host: MyappWeb.External.test()]

external.ex

defmodule MyappWeb.External do

  def test do
    response = "localhost"
    response
  end

end

Not found MyappWeb.External.test.

Thanks.

You can’t, configs are read before compiling the modules.

So you’ll need to inline that module/function into your config file.

Things might change when 1.9 is released though. Configuration works differently then, but I haven’t yet looked into the details…

It will probably still not change much as some of the release configuration must be done before startup.

1 Like

I’m not only speaking about releases, as far as I understand the “old” way to do configuration has been deprecated:

https://hexdocs.pm/mix/1.9.0/Mix.Config.html

This module is deprecated. Use Config and Config.Reader instead.

While there is now Config available.

But yeah, from a quick glance, usage of those modules pretty much the same, and also its documentation says, that mix will evaluate the config on each run, meaning before actual compiling/loading/running the application takes place

1 Like