Application-wide configuration parameters in Phoenix

Is there a kind of equivalent of “initializers” in Phoenix? A place where an application-wide accessible parameters / settings could be stored and retrieved easily. In Rails that would be something like Rails.configuration.[...] for a good example.

What kind of configuration are you talking about? Configuration within the Elixir ecosystem is in general handled via the auto generated files under /config.

Parameters specific to a particular application / deployment. For example I can have a generic implementation of a particular functionality but don’t want to hardcode values, which may have to be altered. Say: min/max password length, max session time, max login attempts, etc for an authentication mechanism. Similar may apply to other parts of the application. /config looks like the place for this kind of things but how this is done so that the confguration params can be used by other parts of the application. A controller for example.

Application.get_env/3, Application.fetch_env/2, and Application.fetch_env!/2. In addition to that you can use config/config.exs, config/release.exs, use config providers, or just directly add sys.config to your releases. Everything is up to you and your needs.

For a moment I thought those are the “outer” env variables, which could do the trick but was not what I asked about. Still, after quick checking relevant hexdocs, this seems to actually be what I was asking for, thanks!

Just remember that config/config.exs is loaded only during compilation, so stuff like System.get_env/1 there can not work as expected in releases.

2 Likes