Can I share sections of mix.exs files between umbrella child apps?

I have a lot of duplication between the mix.exs files in my umbrella child apps.

defmodule Foo.Mixfile do
  use Mix.Project

  def project do
    [
      some_key: %{
        some_big_data_structure: {
          ...
        }
    ]
  end
end

I have tried to import the shared code into them from a module:

defmodule Foo.Mixfile do
  use Mix.Project
  import SharedStuffModule
...

But I get:

module SharedStuffModule is not loaded and could not be found

I suppose the config must get read before loading other modules or similar…

Is there any way around this in Elixir? I’d very much like to DRY a few bigs of very long duplication between mix.exs files.

Thanks!

Probably not what you are looking for, but you can create yet another app which would export the shared data as a function?

# in the new `_shared` app
defmodule _Shared do
  def common_project(some, variables) do
    # ...
  end
end

Or maybe you can read the shared configs from the filesystem in some way?

Because it is a script file (.exs), any dependency needs to be explicitly required/loaded.

So you can put the SharedStuffModule in the umbrella root in a file named “shared_stuff.exs” and then on each umbrella child you can add at the top:

Code.require_file "../../shared_stuff.exs", __DIR__

Note though that the umbrella children were designed so they don’t depend on the parent, so this will slightly break this assumption.

3 Likes

Thanks very much @josevalim! That worked perfectly.