Usage of Mix.env() discouraged in Phoenix router?


I’m curious about the usage of Mix.env() in Phoenix router. Bamboo recommends to add a conditional route for :dev environment to see emails that are kept in memory when using Bamboo.LocalAdapter.

# In your Router
defmodule MyApp.Router do
  use Phoenix.Router # or use Plug.Router if you're not using Phoenix

  if Mix.env == :dev do
    # If using Phoenix
    forward "/sent_emails", Bamboo.SentEmailViewerPlug

    # If using Plug.Router, make sure to add the `to`
    forward "/sent_emails", to: Bamboo.SentEmailViewerPlug

Others have asked similar questions here and the conclusion seems to be that it’s not a bad practice because routes are compiled. But since Mix is a build tool, I’m wondering if this holds true when using releases for example, so it will be a better idea to use Application config values.

1 Like

The Mix.env call won’t land in your release, because that if expression is executed when compiling the module to a beam file – a.k.a. when creating the release. Only the resulting beam files are included in the release itself. Therefore it’s not problematic in terms of Mix being not available at runtime.


Thanks! That was my intuition too :slightly_smiling_face: