Adding Elixir to Erlang via Rebar3

Elixir and Erlang are functional programming languages both running on BEAM virtual machines. Programs in both languages get compiled into BEAM byte code. It’s possible to call Elixir code from Erlang and vice versa. Since Erlang modules are represented by atoms, Erlang library functions can easily be called from Elixir as follows —

Here, I am calling Erlang’s rand module’s uniform function from Elixir console. However, the focus of this article is the other way around — we want to access Elixir functions from Erlang code in rebar3 managed Erlang applications.

What’s rebar3?

As GitHub of rebar3 mentions —

“Rebar3 is an Erlang tool that makes it easy to create, develop, and release Erlang libraries, applications, and systems in a repeatable manner.”

I assume you are already somewhat familiar with rebar3 and have it installed on your system along with Elixir and Erlang. If you are not familiar with rebar3, this article will give a nice introduction.

Creating rebar3 project

I created a sample project using rebar3 command as below —

Right after creating new app, rebar.config will look as below —

There is no rebar.lock file yet, but running rebar3 get-deps will generate one as below —

This is pretty much empty as there are no external dependencies yet. Let’s change that.

Installing rebar_mix

I installed and configured elixir dependencies in this project using rebar_mix. As github page says rebar_mix is — “A rebar plugin for building Elixir dependencies with mix.”

I added this plugin in the project by adding the below lines in rebar.config

{plugins, [rebar_mix]}.
{provider_hooks, [
  {post, [{compile, {mix, consolidate_protocols}}]}
]}.

Doing a rebar3 get-deps will now create a plugins directory under _build folder with rebar_mix inside —

Adding Elixir dependencies

Next, I added an Elixir hex package decimal in rebar.config by adding the below in deps section—

{deps, [
           {decimal, "2.0.0"}
]}.

Full rebar.config now should look as below —

Doing a rebar3 get-deps will fetch the new dependency. At this point rebar.lock file should look as below —

Application code

Now I can add some application code to access Elixir libraries from the app. After editing elixirapp_app.erl, it should look as below —

A sample run from rebar3 shell (command: rebar3 shell) shows these newly added 3 functions can access Elixir libraries.

All Elixir modules start with Elixir prefix followed by the regular module name. Here, I am accessing Elixir’s String and List modules from the standard library and Decimal module from the newly added decimal package.

Conclusion

In this article, I showed how to add Elixir hex package dependencies in rebar3 managed Erlang application to access Elixir libraries from Erlang code.

Full source code for the app is here — https://github.com/imeraj/erlang_playground/tree/main/elixirapp

For more elaborate and in-depth future technical posts please follow me here or subscribe to my newsletter.

References

Did you find this article valuable?

Support Meraj Molla by becoming a sponsor. Any amount is appreciated!