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

Tee
can someone please explain to me how Enum.reduce works with maps
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
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
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement