Read env variable in mix.exs to conditionally add a package

Hi,

Is it ok to read an env variable like this in mix.exs?

System.get_env("NOTESCLUB_IS_OBAN_WEB_PRO_ENABLED")

I’m using it to conditionally load Oban Web & Pro packages.
It works in development.
Yet, in production, it’s like the env variable doesn’t exist — and it does.

Any idea? Thanks!

If you’re using releases, then mix.exs won’t be executed. You must set the environment variable before compiling a production release.

2 Likes

Yes, I am using releases on fly.io

How should I set the env variable?
Do you mean something like this in Dockerfile?

RUN NOTESCLUB_IS_OBAN_WEB_PRO_ENABLED=true \
  mix deps.get --only $MIX_ENV

If it helps, you can see the full Dockerfile here.

It still doesn’t find the env variable.
What am I missing?
Thanks!

You’ve sprinkled NOTESCLUB_IS_OBAN_WEB_PRO_ENABLED in a few RUN statements, but you could set it with ENV instead.

1 Like

Thanks, it’s working now.

Besides using ENV, I also set the env variable to "true" instead of true.

As a reference, I updated the Dockerfile

1 Like