Basic

Basic example to create add(a, b) NIF. We will use nimler to automatically generate Elixir wrapper module, then test via elixir CLI.

import nimler
import nimler/codec

func add(env: ptr ErlNifEnv, a1: int, a2: int): int {.xnif.} =
  return a1 + a2

exportNifs "Elixir.NumberAdder", [ add ]
  1. Compile nim c --app:lib -d:nimlerGenWrapper nif.nim. Produces libnif.so and NumberAdder.ex

  2. Run elixir -r NumberAdder.ex -e "IO.inspect %{add: NumberAdder.add(1, 2)}"

Note: exportNifs() is a compile-time template that exports an ErlNifEntry to be loaded by Erlang or Elixir