Starting an application callback module only if the application is the top-level one

Hello everyone! :wave:

I have an application that defines a module callback in its mix.exs’s application/0 function:

mix.exs:

def application do
    [
        mod: {MyApp.Application, []},
        extra_applications: [:logger, :runtime_tools]
    ]
end

However, I would like the supervisor spawned by MyApp.Application to only be started if my application is started as a “top-level” application in dev mode when running mix run in the cloned application repository.

On the other hand, if my application is loaded as the dependency of another application, I don’t want the supervision tree to be spawned.

Is there any way to achieve this? Can MyApp.Application find out or being told whether it’s being started as “top-level” app?

Thanks!

You can use configuration, which default to not starting things, but have the projects configuration set to start them. That config will be ignored when used as a dependency.

1 Like

thanks @LostKobrakai . I’m pretty sure I thought of that before, but for some reason I must have thought that the config isn’t yet loaded when calling the application/0 callback, which is nonsense of course. This works :+1: