mat-hek

mat-hek

Membrane Core Team

Unifex - same code for NIFs and C nodes

Hello everyone,

pleased to announce Unifex, a tool that simplifies integrating elixir with C/C++. Unifex has been out for some time already but since v0.3 released today, it supports generating C nodes - lightweight programs acting as Erlang nodes. Though communication with NIFs is way faster than with separate nodes, a failing node doesn’t crash the entire VM, moreover, it can be supervised like a normal process. Unifex allows choosing between NIFs and C nodes at any point, even in runtime.

Approach

Here are some key facts about Unifex:

  • Native code is kept separate from elixir code
  • Native code doesn’t depend on erlang libraries, but on Unifex abstraction, that aims to be more user friendly than erlang APIs
  • Unifex works by generating boilerplate code in the compile-time, based on DSL in elixir
  • Unifex aims to support the intersection of sets of data types from Elixir and C
  • Unifex relies on Bundlex, our tool for compiling native code

Here’s how it looks like in short: firstly, you describe the interface with spec-like DSL:

module Example

interface [NIF, CNode]

spec foo(num :: int) :: {:ok :: label, answer :: int}

then you provide implementation:

#include "_generated/example.h"

UNIFEX_TERM foo(UnifexEnv* env, int num) {
  return foo_result_ok(env, num);
}

and finally, you use it either as NIF

defmodule Example do
  use Unifex.Loader
end
iex> Example.foo(10)
{:ok, 10}

or as CNode

iex> require Unifex.CNode
iex> {:ok, cnode} = Unifex.CNode.start_link(:example)
iex> Unifex.CNode.call(cnode, :foo, [10])
{:ok, 10}

A detailed description of how to place that in a Mix project can be found here.

Use cases

We’re using Unifex heavily at Membrane, mainly for wrapping some complex, stateful libs, and that’s where Unifex suits the most IMHO.

Status

Though we’ve been using Unifex for NIFs for some time, adding C nodes required lots of changes that are quite fresh. We’re validating them while developing Membrane ICE plugin.

Feedback and contributions are most welcome :wink:

Credits: @Feliks, @mickel8, @bblaszkow06, @mkaput, Software Mansion

Most Liked

mat-hek

mat-hek

Membrane Core Team

Unifex v0.4.0 released :tada:

This version adds the ability to define custom types and use them in Unifex specs. For now, enums and structs are supported. Declaring them generates C enums and C structs, respectively, for example:

type position :: :manager | :developer | :intern | :product_owner

type personal_data :: %PersonalData{
  age: int,
  expirience: int,
  name: string
}

will generate

enum Position_t { MANAGER, DEVELOPER, INTERN, PRODUCT_OWNER };
typedef enum Position_t Position;

struct personal_data_t {
  int age;
  int expirience;
  char *name;
};
typedef struct personal_data_t personal_data;

and can be used like

spec get_salary(person :: personal_data, person_position :: position) ::
       {:ok :: label, salary :: int}
       | {:error :: label, :unemployed :: label}

Docs, GitHub

Credits go to @Feliks, thanks!

Where Next?

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
385 14863 120
New
GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
kip
Following on from my CLDR lbraries I started work on Unicode transforms. But like everything related to CLDR there is a lot of yak-shavin...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
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
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

We're in Beta

About us Mission Statement