coen.bakker

coen.bakker

Recommended way to set env variables when using Ueberauth in development?

I am currently using Ueberauth to allow users to use third-parties to authenticate themselves. I can make the *_CLIENT_ID and *_CLIENT_SECRET available in my dev/test env with, for example:

export GOOGE_CLIENT_ID = "..."
export GOOGLE_CLIENT_ID = "..."

But how can I prevent having to export the variables with each new shell session?

Maybe I have taken a wrong approach to setting the env variables altogether? What is the recommended way to make them available to a dev/test environment?

First 10 of 27 Posts Switch mode

coen.bakker

coen.bakker OP

Is it common to add a shell script somewhere? I see that Fly.io generated an env.sh.eex to export some variables. Should I do the same? Not sure how to run that before each compile, however…

sodapopcan

sodapopcan

For development, the answer is probably to use direnv.

Personally, I just make myself a shell script in the root directory that I call dev that has the following as a bare minimum.

#!/usr/bin/env bash

source .env

case $@ in
  c)
    iex -S mix
    ;;

  *)
    iex -S mix phx.server
    ;;
esac

Then just ./dev to start the dev server.

For production is dependent on how you deploy, of course.

coen.bakker

coen.bakker OP

This is great. Thank you.

For production I use Fly.io. You can set “secrets” with their cli and on their website.

D4no0

D4no0

Or you can create an additional config file that you will add to gitignore and import from dev.exs or test.exs.

coen.bakker

coen.bakker OP

Is that was dev.secrets.exs was/is?

D4no0

D4no0

I mean there is no convention, but yeah, I’ve seen this used in a few projects.

coen.bakker

coen.bakker OP

I’ve seen it around also.

I got the impression that it used to be generated in Phoenix apps in a previous version of the phx generator. See for example here: https://stackoverflow.com/q/68239156.

But thanks again. :raised_hands:

pdgonzalez872

pdgonzalez872

My 2c: I tent to prefer explicitness for env vars, rather than having things magically get loaded. I’ve seen and used this mechanism before:

export $(cat .env | xargs) && mix phx.server

direnv works well, but assumes folks have it set up (most do). The above is more explicit in my opinion. I don’t mind the verbosity, mostly due to me rarely having to type that out (always on a readme or in a shell history somewhere).

When working with others, there is usually something on the readme that will help (including yourself when you forget :slight_smile: ) As far as the mechanics:

  • There is usually a .env.example that is committed to the repo (example)
  • an entry for an .env file in .gitignore (example)
  • instructions to create your .env file in the readme (example)

If you don’t have a lot of env vars, I’d start with SOME_VAR=your_var mix phx.server and build from that. Then, consider the above if that sounds interesting.

sodapopcan

sodapopcan

I don’t know why I never thought of this—it seems so obvious, lol.

The dev script still comes in handy, at least for one project, where I need to start up multiple nodes. Of course, Docker is probably the “real” answer there, but I’m stubborn. Maybe there is another way? Though this is getting a little off topic of purely ENV vars.

arcanemachine

arcanemachine

I use (and love) direnv, but for working with others, I find that dotenvy is a good solution with less friction than direnv.

It’s another dependency, but it gets rolled in with the others when you run mix deps.get. Plus it works in all environments, instead of only ones where direnv is installed (CI, shared dev boxes, etc.).

I’ve encountered the custom config.exs strategy mentioned by @D4no0, and I’m not a fan of the concept. Dotenv files are closer to a “universal” configuration system, so that’s a big plus for me. It allows me to share a config system between my Elixir project, my Docker Compose config, etc.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement