Mix release does not include Mix - is there a way to use Mix.env?

Hi,

So I’m investigating mix release and I understand that the release does not include Mix. Unfortunately there are a number of places in the code where Mix.env is called. Is there a recommended workaround for this? I’m thinking of creating a prod only dependency, basically this:

defmodule Mix do

  def env, do: :prod

end

Is there a better solution?

Thanks

Just do not use Mix.env/0 in your runtime code.

The general solution here is to, instead of having code like this:

if Mix.env == :prod do
  # prod stuff
else
  # other stuff
end

Do this:

if Application.get_env(:my_app, :some_relevant_flag) do
  # stuff related to that flag
else
 # other stuff
end

Then in your configuration you can choose to set that flag to true or false.

8 Likes

If you don’t call Mix.env() at runtime, you can set it up at compile time.

There are multiple ways to do that.

use module attribute

@env Mix.env()

Then, read @env as a normal module attribute.

use config

config :my_app, compile_env: Mix.env()

Then, get compile_env with Application.get_env(:my_app, :compile_env).


If you want to call Mix.env() at runtime, just do not do that, like @hauleth said.

5 Likes