jononomo

jononomo

Why can't I do configuration in runtime.exs in my dev environment?

So I’m trying to understand how config/runtime.exs works. If I put:

IO.inspect("in runtime.exs")

at the top of the file, then when I start my application on my local machine with mix phx.server I can see it print out “in runtime.exs” in my terminal, and I can add other print statements to confirm that runtime.exs is being executed after dev.exs, which is being executed after config.exs.

But if I set some configuration in runtime.exs then it does not override the configuration set in config.exs, and of course it also does not override the configuation set in dev.exs. However, if I put some configuration in dev.exs then it does override the configuration set in config.exs.

My understanding is that runtime.exs is the last file executed of these three, and the first file executed when the compiled application starts up, so I would think that configuration set in runtime.exs would override configuration set in dev.exs. But this is not happening.

Does anyone have any idea what is going on here?

I’m using Phoenix 1.6-rc.0, by the way.

Marked As Solved

Nicd

Nicd

This is an implementation detail: the config ... line actually does not set the application env at that point, it puts values to the process dictionary with Process.put. The application env is created later when the system starts proper. Thus if you want to share some common values inside runtime.exs (or other config files), you must put them in regular variables.

Try inspecting the values inside your application.ex start-callback. That function is executed at runtime and you should see the correct values there (and any other function executed at runtime).

Also Liked

John-Goff

John-Goff

The releases.exs file was released in elixir 1.9, this was the first attempt at runtime config but it only applied to releases and not in dev. Elixir 1.11 introduced runtime.exs which runs in both dev and release mode.

LostKobrakai

LostKobrakai

To clarify this further. In elixir any code, which is not within a function definition (or moved to such via a macro) will be executed at compile time.

Nicd

Nicd

The issue is here. These lines that are at the bottom of the file will be executed at compile time. Thus runtime.exs will not be evaluated yet. Any values set in runtime.exs are only available at runtime.

I wrote this blog post that attempts to clarify the differences between the configurations (in the article I call “compile time” “build time” but it means the same thing): Elixir: Time for Some Configuration — Random Notes

To simplify, if you want to see your values from runtime.exs, you need to grab them at runtime, i.e. inside a function that is called at runtime.

Last Post!

John-Goff

John-Goff

The releases.exs file was released in elixir 1.9, this was the first attempt at runtime config but it only applied to releases and not in dev. Elixir 1.11 introduced runtime.exs which runs in both dev and release mode.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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

Other popular topics 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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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

We're in Beta

About us Mission Statement