Loading additional config files from runtime.exs for test and dev

I know that import_config/1 cannot be used in runtime.exs (docs), I am guessing that’s because of how runtime.exs has special handling for being copied into a release, and this does not extend to extra files. Config.Provider can be used to load additional files on release boot time.

But what’s the recommended approach for loading additional files in dev and test environments, assuming that releases are not used? Other than for aestethics (compared to nesting in if config_env() == :dev), also to be able to have git-ignored files for customizing the runtime config by individual devs (e.g. runtime.dev.local.exs).

I came up with something like this, but I don’t know if it’s fine to use Config.Reader inside runtime.exs:

# inside runtime.exs

# import_config does not work for runtime.exs as a safeguard
# because it would potentially fail to find the file when running a release
# so we implement our own similar functionality for non release envs
import_non_release_config_when_present = fn env, relative_path ->
  if env == :prod, do: raise "this would fail when building a release"

  path = Path.expand(relative_path, __DIR__)

  if config_env() == env && File.exists?(path) do
    path
    |> Config.Reader.read!()
    |> Enum.each(fn {root_key, configs} ->
      Enum.each(configs, fn {key, value} ->
        config root_key, key, value
      end)
    end)
  end
end

for env <- [:dev, :test] do
  import_non_release_config_when_present.(env, "runtime.#{env}.exs")
  import_non_release_config_when_present.(env, "runtime.#{env}.local.exs")
end
1 Like

Perhaps Code.require_file(config_path) ? I’ve used this in the past but I’m not sure if there’s a better way either.

1 Like