mayl

mayl

Circuits_sim in a release

I have a project using circuits_i2c and I want to be able to run the project on my PC with circuits_sim. Following the documentation I wound up with this in my config.exs (qemu is the MIX_TARGET I want to use the simulated i2c bus for:

if config_target() == :qemu do
  config :circuits_i2c, default_backend: CircuitsSim.I2C.Backend
  config :circuits_sim,
  config: [
    {CircuitsSim.Device.MCP23008, bus_name: "i2c-0", address: 0x20},
  ]
end

This works for iex and I can see the MIX_TARGET take effect:

✦ ❯ iex -S mix
Generated circuits_sim_test app
Erlang/OTP 26 [erts-14.2.3] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.16.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Circuits.I2C.bus_names
[]
✦ ❯ MIX_TARGET=qemu iex -S mix
Erlang/OTP 26 [erts-14.2.3] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Interactive Elixir (1.16.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> Circuits.I2C.bus_names
["i2c-0"]
iex(2)>

However, for releases built like this:

✦ ❯ MIX_TARGET=qemu mix release

Running the resulting release gives the following error, where circuits_i1c appears to try to load the NIF despite being apparently configured to use the circuits_sim backend:

✦ ❯ _build/qemu_dev/rel/circuits_sim_test/bin/circuits_sim_test start_iex
=CRASH REPORT==== 13-Aug-2025::18:14:17.622342 ===
  crasher:
    initial call: kernel:init/1
    pid: <0.675.0>
    registered_name: []
    exception exit: {on_load_function_failed,'Elixir.Circuits.I2C.Nif',
                        {error,
                            {load_failed,
                                "Failed to load NIF library: '/home/larry/projects/circuitsSimIssue/circuits_sim_test/_build/qemu_dev/rel/circuits_sim_test/lib/circuits_i2c-2.1.0/priv/i2c_nif.so: cannot open shared object file: No such file or directory'"}}}
      in function  init:run_on_load_handlers/0
      in call from kernel:init/1 (kernel.erl, line 227)
    ancestors: [kernel_sup,<0.668.0>]
    message_queue_len: 0
    messages: []
    links: [<0.670.0>]
    dictionary: []
    trap_exit: false
    status: running
    heap_size: 610
    stack_size: 28
    reductions: 234
  neighbours:

=SUPERVISOR REPORT==== 13-Aug-2025::18:14:17.622504 ===
    supervisor: {local,kernel_sup}
    errorContext: start_error
    reason: {on_load_function_failed,'Elixir.Circuits.I2C.Nif',
                {error,
                    {load_failed,
                        "Failed to load NIF library: '/home/larry/projects/circuitsSimIssue/circuits_sim_test/_build/qemu_dev/rel/circuits_sim_test/lib/circuits_i2c-2.1.0/priv/i2c_nif.so: cannot open shared object file: No such file or directory'"}}}
    offender: [{pid,undefined},
               {id,on_load},
               {mfargs,{proc_lib,start_link,[kernel,init,[on_load]]}},
               {restart_type,transient},
               {significant,false},
               {shutdown,2000},
               {child_type,worker}]

=CRASH REPORT==== 13-Aug-2025::18:14:17.623120 ===
  crasher:
    initial call: application_master:init/4
    pid: <0.667.0>
    registered_name: []
    exception exit: {{shutdown,
                      {failed_to_start_child,on_load,
                       {on_load_function_failed,'Elixir.Circuits.I2C.Nif',
                        {error,
                         {load_failed,
                          "Failed to load NIF library: '/home/larry/projects/circuitsSimIssue/circuits_sim_test/_build/qemu_dev/rel/circuits_sim_test/lib/circuits_i2c-2.1.0/priv/i2c_nif.so: cannot open shared object file: No such file or directory'"}}}}},
                     {kernel,start,[normal,[]]}}
      in function  application_master:init/4 (application_master.erl, line 142)
    ancestors: [<0.666.0>]
    message_queue_len: 1
    messages: [{'EXIT',<0.668.0>,normal}]
    links: [<0.666.0>,<0.665.0>]
    dictionary: []
    trap_exit: true
    status: running
    heap_size: 987
    stack_size: 28
    reductions: 208
  neighbours:

=INFO REPORT==== 13-Aug-2025::18:14:17.623386 ===
    application: kernel
    exited: {{shutdown,
                 {failed_to_start_child,on_load,
                     {on_load_function_failed,'Elixir.Circuits.I2C.Nif',
                         {error,
                             {load_failed,
                                 "Failed to load NIF library: '/home/larry/projects/circuitsSimIssue/circuits_sim_test/_build/qemu_dev/rel/circuits_sim_test/lib/circuits_i2c-2.1.0/priv/i2c_nif.so: cannot open shared object file: No such file or directory'"}}}}},
             {kernel,start,[normal,[]]}}
    type: permanent

Kernel pid terminated (application_controller) ("{application_start_failure,kernel,{{shutdown,{failed_to_start_child,on_load,{on_load_function_failed,'Elixir.Circuits.I2C.Nif',{error,{load_failed,\"Failed to load NIF library: '/home/larry/projects/circuitsSimIssue/circuits_sim_test/_build/qemu_dev/rel/circuits_sim_test/lib/circuits_i2c-2.1.0/priv/i2c_nif.so: cannot open shared object file: No such file or directory'\"}}}}},{kernel,start,[normal,[]]}}}")

Crash dump is being written to: erl_crash.dump...done

I want to be able to build a release using circuits_sim, because I am testing more than just this elixir app in qemu and my higher level nix build that produces that VM needs a release as input. Is there a way to make that work?

Marked As Solved

fhunleth

fhunleth

Co-author of Nerves

Thanks. I understand your setup now what was happening.

Could you update your mix dependencies to use the branch at Support builds that completely exclude I2CDev support by fhunleth · Pull Request #211 · elixir-circuits/circuits_i2c · GitHub and confirm that it works like you were expecting? E.g.,

{:circuits_i2c, github: "elixir-circuits/circuits_i2c", branch: "dont-load-nif-if-not-built", override: true}

Also Liked

fhunleth

fhunleth

Co-author of Nerves

Yes, they’ll have the exact same issue.

What you ran into turned out to be due to the logic controlling whether to build the NIF when it’s not used. The Elixir module using the NIF used to lazy load it and then give you an error. I had to remove the lazy NIF loading since there were a lot of issues with it in practice. I didn’t realize that caused the regression with circuits_sim and releases. The PR completely removes everything involved with the NIF when it’s unused, so it’s not even possible to get the error.

As another benefit, non-Nerves/non-Linux users of Circuits.I2C (and eventually the others) won’t even have a single Elixir module that’s needed for Linux-support compiled now. The C code was already gone, but I got the feedback that just having the associated Elixir modules there wasn’t ideal.

Thanks for reporting the issue. The other Elixir Circuits libraries will get the same treatment as I get time.

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement