Execute runtime.exs in custom mix task

Hi,

I need to select a local SQLite database outside my Phoenix project.
So I set some configuration options based on enviroment variables in runtime.exs.
For BASE_PATH=/path/to/my/working/dir mix phx.server it works great.

And now I need a custom mix task (tests.create) for some low level file stuff.

When I exectue BASE_PATH=/path/to/my/working/dir mix tests.create my config in runtime.exs is ignored.

How execute runtime.exs in my mix task?

A student of mine (jojofreddy) pointed me to this:

This magic line

def run(args) do
    Mix.Task.run("app.config")
  # do work
end

solved the problem.

You can also use @requirements module attribute for this purpose:

@requirements ["app.config"]
3 Likes