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

:wave:

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

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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 ...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New

We're in Beta

About us Mission Statement