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!

1 Like

Without configuration your App cannot be started. Without your App started you cannot use functions of your Application.

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.


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

1 Like

Makes sense when you think about it in that light. Thank you!

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: