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.

3 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

Sorry for reviving this thread after a few months, but I wonder if conditionally adding dependencies with an environment variable is really the right approach with the Oban Pro dependencies. Is this the recommended approach? If not, what would be an alternative? I’m asking since the notesclub project isn’t tracking mix.lock due to those optional dependencies.

What do you think @sorentwo? Thank you for your input :slight_smile:

2 Likes

I just want to say thank you for sharing your dockerfile. Deployment is the hardest thing for me to find resources on.

1 Like

Welcome :wink:

By the way, @stevensonmt, if you saw the gist, the latest dockerfile in the repo might help as it is more up to date.

1 Like