Preamble: I am aware of other methods of dynamic runtime configuration (e.g.
Config.Provider
), and am currently a happy user of Dotenvy. The title of this thread is not “How to dynamically configure runtime config?”, but is specifically pertains to the usage ofCode.eval_file/1
as a strategy to do so.
I am experimenting with different strategies to dynamically import runtime config, similar to how Config.import_config/1
works for compile-time config.
I have come up with a solution that appears to work. I am bringing the topic here for discussion, to see if there are any valid technical reasons why I should not use this strategy.
The strategy
Here is an example config/runtime.exs
that uses Code.eval_file/1
to import a local runtime config file, if one exists:
config/runtime.exs
import Config
config :my_project, :hello, :world
# Maybe import local runtime config file(s)
for local_runtime_config_file_name <-
["runtime.local.exs", "runtime.#{config_env()}.local.exs"] do
local_runtime_config_file_path = Path.join(__DIR__, local_runtime_config_file_name)
if File.exists?(local_runtime_config_file_path) do
IO.puts("Importing local runtime config from `#{local_runtime_config_file_path}`...")
Code.eval_file(local_runtime_config_file_path)
end
end
This allows one or more (presumably gitignored) local runtime config files to be added, without having to modify the main runtime config file:
config/runtime.local.exs
import Config
config :my_project, :hello, :another_world
The above example shows how a local runtime config file could be loaded for 1) all configuration environments, and/or 2) a specific configuration environment (e.g. :dev
, :test
, etc.), and supports cascading (i.e. the global runtime.local.exs
would be overridden by e.g. runtime.dev.local.exs
). If any of the local files do not exist, then that is fine also, and no custom config would be loaded.
I have this running, and it appears to work. But I haven’t seen this strategy documented anywhere, so I’m a little wary of using it.
Does this appear to be a viable strategy, or is there some hidden footgun here that I am not aware of?