emadalam

emadalam

Hey all :wave:

I just published mahaul package for streamlining the environment variables usage in your Elixir apps. Parse and validate your environment variables easily in Elixir with the following benefits.

  • Compile time access guarantees
  • Parsed values with accurate elixir data types
  • Validation of required values before app boot
  • mix environment specific defaults and fallbacks

Read more for understanding why to use this package and its benefits. The complete documentation for mahaul is available online at HexDocs.

We have been using this in production and it has already saved us from bad releases where we deployed to production and forgot to set the needed environment variables after an update. Though it seems to work nicely for a variety of use cases, I’m relatively new to Elixir (only a few months) so I’d love your inputs and reviews on this.

Thanks to this wonderful Elixir community, I’ve never come across anything like this from a community standpoint, so hats off, I’m enjoying every moment of it since the day I started learning Elixir :heavy_heart_exclamation:

https://github.com/emadalam/mahaul

Showing Posts 15 to 6

stevensonmt

stevensonmt

LIGHTBULB
I get it now. Thanks!

emadalam

emadalam OP

Can you give some brief example of how such a dynamic pattern matching can work at compile time? Form what I can think of, the following will give you runtime errors without any compile time warnings.

defmodule Env do
  def get_env("VAR_1"), do: "..."
  def get_env("VAR_2"), do: "..."
end

defmodule SomeModule do
  def something() do
    Env.get_env("VAR_1") # works
    Env.get_env("VAR_2") # works
    Env.get_env("VAR_3") # runtime error with no compile time warnings
  end
end

vs

defmodule Env do
  use Mahaul,
    VAR_1: [type: :str],
    VAR_2: [type: :num]
end

defmodule SomeModule do
  def something() do
    Env.var_1() # works
    Env.var_2() # works
    Env.var_3() # compile time warnings for non-existing method call
  end
end

:heavy_heart_exclamation: :heavy_heart_exclamation: :heavy_heart_exclamation:

stevensonmt

stevensonmt

Thanks for clarifying as I had indeed misunderstood.

I’m still not sure I follow here though. This seems like with pattern matching you could hard code the strings for the env variables of interest and have the same sort of confidence. I can see that mahaul provides a single interface with the system env where you handle checking for the variables and providing defaults or handling errors rather than needing to do so everywhere you use the env variable in the application. Mahaul also seems to make it a lot easier to use different variables for different environments and to update them easily when the names of variables change between versions or something.

This sounds like a true win for me.

D4no0

D4no0

Well, the point of configuration is to use them in your code later, just as libraries use them and you can do that easily by calling get_env/3 or fetch_env/2, however taking in consideration the limitation of the configuration/compilation it seems that the only way to enforce something at compile-time is to make a custom module.

It would be great if such a functionality were to be introduced somewhere in the elixir internals so it could be used in all configurations.

emadalam

emadalam OP

I certainly use environment variables in my code beyond just config files. So even though the idea of macros in runtime.exs is certainly nice, I still want to be able to access those environment variables values independently in my code as needed. This is why a separately initialized module where I can see/configure all the environment variables used by my app in one glance, makes more sense to me from flexibility and ease of use standpoint.

D4no0

D4no0

Ah true, quite sad but it makes sense. Well at least for runtime.exs it would be nice to have the check.

LostKobrakai

LostKobrakai

You can only do that with Config.Provider implementations (e.g. runtime.exs), but that won’t be able to provide compile time configuration. The chicken/egg situation means config first then applications, because you need to be able to configure said applications. So applications cannot be available or be used in config/config.exs.

D4no0

D4no0

The usage of environment variables is not in question here, the thing that I try to point out is that I would like to use this library, however I would like the environment variable specification defined in the config.exs directly, not with a separate module.

In my opinion there is no reason to create your own wrappers over the config way of storing those values, however a check of the environment value type is a great thing.

To make my point cleaner, I will illustrate an dev.exs example:

import Config
# Import or use your module here
import MagicMacro

config :my_app, MyApp.Endpoint,
    http: [port: safe_env(PORT: [type: :port, default: "4000"])]
emadalam

emadalam OP

@D4no0 IMHO I don’t think elixir configs are limiting in any way, if anything I’ve found it to be very well thought and structured with perfect balance of standardization vs flexibility. I’ve rarely come across anything so flexible and feature complete that’s baked in as part of the core language feature, as elixir configs.

I feel it’s important to understand there’s a difference between app configuration and environment variables and they are not a replacement to each other rather environment variables compliment your app’s configurability. You can choose to configure your app with static values or environment variables, the choice is totally up to you. The fact that elixir configs lets you achieve this in a feature complete way, in itself is wonderful to me.

D4no0

D4no0

Oh I see, this is a limitation of elixir configs. It would be nice to also have a macro or sigil to define a typed/checked environment variable directly into config, without generating the function at compile-time of course, as I hate having to look into 10 different files to find the actual config.

Where Next? Top

Trending in Announcing Top

type1fool
WebAuthnLiveComponent WebAuthnComponents See this post about renaming the package. Passwordless authentication for Phoenix LiveView app...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
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
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New
quatermain
Hello, I’m sharing my plugin here in forum after some time so it has time to mature and proof yourself. I use Claude Code daily on a pr...
New
gtcode
Aloha gang, There’s a new class of AI model out from TypeSafe. Jev is their flagship model. You can learn about it here: ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
CodeSync
Code BEAM Europe 2026 – Call for Talks is Open! We’re excited to announce that Code BEAM Europe 2026 is now open for talk submissions :t...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews