Muhammad Lukman Low
Muhammad Lukman Low

Reputation: 8523

Is it possible to have a custom mix file for Elixir?

In my projects involving Elixir, I often have stuff I want to add to my mix.exs file for example adding my own ENV etc. It's usually a pain to add these items and have to add them to my .gitignore. My question is, is it possible to have something like custom_mix.exs which I can call from mix where I can put in all of my custom settings which will overwrite the settings that are defined in mix.exs ? The custom_mix.exs file will not be put under git and can be permanently put under .gitignore and mix.exs will not error out if no custom_mix.exs is found.

Upvotes: 1

Views: 246

Answers (2)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 120990

While the answer by Steve Pallen is perfectly correct, you are doing it wrong in the first place. This definitely looks like an X/Y problem.

Why would you put settings in mix.exs file, while you have config/config.exs for this particular purpose? config.exs has a built-in support for what your’re looking for out of the box, check how e.g. does different configurations for different environments.

mix.exs file is intended to specify the general application behaviour and what you are currently trying to achieve is to make the application behaviour undetermined.


The possible problem you could try to resolve this way, that I could think of, is versioning of dependent applications or doing some patchwork on external mix.exs code. Still, the approach to tackle and fight against the core elixir project file is a bad idea. You might create a wrapper for the original mix.exs file, calling it’s functions from your version and forcing Elixir to use your version with some shell wrappers, or like.

Upvotes: 4

Steve Pallen
Steve Pallen

Reputation: 4507

Yes, very easy to do. Add the following to the very top of your mix.exs file

if File.exists?("./custom_mix.exs"), do: Code.require_file("./custom_mix.exs", __DIR__)

defmodule MyApp.Mixfile do
  # ...
  if function_exported?(CustomMix, :some_stuff, 0), 
    do: CustomMix.some_stuff()
end

Then create your custom_mix.exs file like this:

defmodule CustomMix do
  def some_stuff do
    #
  end
end

Upvotes: 1

Related Questions