What is the exact location each env vars is retrieved when building my application?

Hello everyone!
I was re-reading the Phoenix release documentation today, more specifically the section about runtime configuration:

You may have noticed that, in order to assemble our release, we had to set both SECRET_KEY_BASE and DATABASE_URL . That’s because config/config.exs , config/prod.exs , and friends are executed when the release is assembled (or more generally speaking, whenever you run a mix command).

However, in many cases, we don’t want to set the values for SECRET_KEY_BASE and DATABASE_URL when assembling the release but only when starting the system in production. In particular, you may not even have those values easily accessible, and you may have to reach out to another system to retrieve those.

So, I know I can pull those variables at runtime using config/releases.ex, but know I’m curious where is the exact place those vars are retrieved at build-time.
For instance, there’s a lot of System.get_env("MIX_ENV") scattered in the Mix source code, but what about: SECRET_KEY_BASE, DATABASE_URL and others? I couldn’t find any trace of those being used by Phoenix, Plug or Ecto (maybe I haven’t searched properly or Github search is just trolling me :sweat_smile:).

PS.: This is for learning purposes, so If there are any other common environment variables that I should know of, please feel free to share those too :smile:.

:wave:

but what about: SECRET_KEY_BASE , DATABASE_URL and others?

These would probably be defined in your own config files.

The documentation shows the usage of SECRET_KEY_BASE and DATABASE_URL before describing how to configure releases to load those vars at runtime, so I was under the impression that they were already retrieved somewhere (somehow). Perhaps, the following section was a hint I should have paid more attention:

The general recommendation is to keep those in environment variables and load them into your application. This is done in config/prod.secret.exs , which is responsible for loading secrets and configuration from environment variables.

PS.: One curious side effect I’m having, coming from a strictly static OO background and getting accustomed to everything being so explicit in Elixir is that sometimes when something is not directly too obvious I start getting a little superstitious about some hidden code magic :sweat_smile:.

Most of the guide evolved in various speeds over time, so such inconsistencies can come up. If you come to such points it would be great to look into doing a PR to bring it in a more approachable order. This is often easier to do as the person having the difficulties than as someone already knowing how it‘s supposed to work.

1 Like