beardedeagle

beardedeagle

Best way to handle env vars that must be present at runtime but not compile time?

I’m trying to figure out the best way to handle an env var that MUST be present at runtime but not at compile time. The following is used to generate this env var:

  defp vault_key(_) do
    vault_key = :base64.encode(:crypto.strong_rand_bytes(16))
    IO.puts("#{vault_key}")
  end

Effectively I have an env var called VAULT_KEY which must be present at runtime to encrypt and decrypt values in mnesia, but I don’t want to require it at compile time, as if it’s unset, you can’t fire off this mix task to generate and export the key in the first place. Using System.get_env("VAULT_KEY") allows it to compile, but of course sets it to nil if the env var wasn’t set prior to compiling, which is not the desired behavior. Using System.fetch_env!("VAULT_KEY") requires the key to be present, but won’t allow for compilation if not present. The behavior I’m trying to accomplish here is that if the key isn’t present at compile time, thats’s fine, but if it’s not present at runtime then I the application should bail and fail to start. And I wan’t to apply that to all env’s (dev, test, staging, prod). Any thought’s or pointers would be greatly appreciated.

Marked As Solved

lpil

lpil

Creator of Gleam

If the call to System.fetch_env! is inside a function (and that function is not called during compilation) then it won’t be evaluated at compile time, so your code will compile even if it is not set.

If the call to System.fetch_env! is not inside a function (for example if it is inside a module and being assigned to a module attribute) then it will be evaluated at compile time and will result in an error.

Also Liked

idi527

idi527

:waving_hand:

Just in case, there is also a way to do it via distillery’s (and probably elixir 1.9’s) config providers. I usually have something like

# in the config provider
use Mix.Config

# evaluated on init
config :vanguard, vault_key: System.get_env("VAULT_KEY") || raise("VAULT_KEY is not not set.")

Example

idi527

idi527

Sometimes there is a need to explain to the user why and how an env var should be set, not just that it’s missing.

Where Next?

Popular in Questions Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement