fireproofsocks

fireproofsocks

Limits on what code modules you can call from inside config files

I was trying to add some code to one of my config files in order to convert the value from a string (as it would be read from an ENV variable) to a boolean – I had a custom is_truthy? function for this. However, I got compilation errors saying that MyApp is not available. I didn’t realize that you weren’t allowed to run your application’s modules and call its functions from config files. Is this really the case? And what’s the reasoning behind it?

Thanks for explanations!

Most Liked

Nicd

Nicd

Just an addendum to this older post: In runtime config (config/runtime.exs), you can use your modules, as they have already been compiled. No applications are running at that point, but for example you can write config helpers like this:

Nicd

Nicd

Your modules are compiled using the generated config, not the other way around. So when the config is being processed, you cannot use the modules because they have not been compiled. Otherwise there would be cyclic dependencies.

What you can do is use MFA (module, function, arguments) syntax to refer to functions that you will later run at runtime. So for example callback_function: {MyApp.MyModule, :function, [1, "foo"]} is fine.

As for the release config files that are evaluated during runtime at system startup, I think @LostKobrakai’s post covers that case. But I’m not an expert on them.

LostKobrakai

LostKobrakai

Both answers come down to: Your App depends on your configuration, so your configuration cannot depend on your app.

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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

We're in Beta

About us Mission Statement