Mix.env at the compile time always return :prod when in deps

I am quite confused with the behaviour of the Mix.env called in the compile time. Consider the following simple function:

defmodule Dep do
  @env Mix.env
  def env(), do: @env

which works as expected:

iex(1)> Dep.env

So far, so good. But if you try to use it as an external library in another project

defp deps do
  [{:dep, ">= 0.0.0", path: "../dep"}]

It returns :prod

iex(1)> Dep.env

Setting MIX_ENV does not change this behaviour. Is it expected to be like this?

1 Like

The same is with unquote

defmodule Dep do
  def env(), do: unquote(Mix.env())

Dependencies are always compiled in their respective production environment.

I’m sure it’s documented somewhere but I can’t find it right now.


It is documented here https://hexdocs.pm/mix/Mix.Tasks.Deps.html#module-dependency-definition-options and it also shows how to override it. Dependencies are in the prod environment because dev is used when you are developing the project and you are not developing a dependency.


I thought so. OK, that explains everything, thanks!

1 Like

Right, it is clearly documented there. I must have missed it. Thanks for explanation!