NIFs

Aliases
  • NIF
  • Native Implemented Functions
Image of Author
February 26, 2023 (last updated May 17, 2024)

NIFs, or Native Implemented Functions, are an Erlang concept that is implicitly a part of Elixir.

To quote from the Erlang NIFs Tutorial,

A NIF is a function that is implemented in C instead of Erlang. NIFs appear as any other functions to the callers. They belong to a module and are called like any other Erlang functions. The NIFs of a module are compiled and linked into a dynamic loadable, shared library (SO in UNIX, DLL in Windows). The NIF library must be loaded in runtime by the Erlang code of the module.

Other languages besides C can be supported with additional libraries. For example, Rustler let's you write NIFs in Rust,

Rustler is a library for writing Erlang NIFs in safe Rust code. That means there should be no ways to crash the BEAM (Erlang VM). The library provides facilities for generating the boilerplate for interacting with the BEAM, handles encoding and decoding of Erlang terms, and catches rust panics before they unwind into C.

Resources