How to include optional dependencies using Rebar3?

Hello!

I’m looking for a way to include an optional dependency into my library project. This is about including brotli which is a stateless library (not an OTP app).

The use case is to allow brotli compression for an HTTP caching library, but since this library compiles native code and gzip as an alternative also exists (with 15-20% size overhead compared with brotli), I don’t want to force the users to have to compile native code if they’re fine with gzip.

Elixir’s mix allows that by doing:

{:foobar, "~> 1.0", optional: true}

but I couldn’t find such an option with rebar3. Have I missed something and, if not, what is the typical workaround/pattern for rebar projects?

Cheers!

1 Like

Instead of having a literal dependency to any specific compression library you could allow users to configure a callback module for compression. That means you don’t have to worry about supporting building NIFs in various environments.

3 Likes

There isn’t anything to do in rebar.config. But you’ll want to include the brotli application in optional_applications of your .app.src file, Erlang -- app

The user then includes brotli in their rebar.config the same as the user would have to do in their mix.exs.

6 Likes

Thanks for your answers!

Since this brotli dependency is not an OTP app, I’m not even sure this is needed. But keeping it in case this library turns into an app in the future.

Neither the compiler nor dialyzer complain about brotli module not being found, which is nice.

For tests I guess the way to go is to use a profile:
rebar.config:

{profiles,
 [{test, [{deps, [brotli]}]}]}.

Cheers!

1 Like

The library you posted is an OTP application.

3 Likes

Indeed I misunderstood what an OTP application is. In my mind an OTP app was a program that implements the application callbacks but it is wrong:

mod

Specifies the application callback module and a start argument, see application(3).

Key mod is necessary for an application implemented as a supervision tree, otherwise the application controller does not know how to start it. mod can be omitted for applications without processes, typically code libraries, for example, STDLIB.

Thanks for clarifying!

2 Likes

Right, this type of application is known as a library application.

There is a brief section about library applications in the LYSEFGG book if you want to read more.

2 Likes

Yup!

(Says I need 6 characters to make a post, so adding this)

2 Likes