DEV Community

Cover image for Customizing IEx for Elixir Nerves target device
Masatoshi Nishiguchi
Masatoshi Nishiguchi

Posted on

Customizing IEx for Elixir Nerves target device

Today I talk about how I customize things that are printed when shelling into a Nerves target board (e.g., Raspberry Pi Zero W).

にほんご

customizing-iex-in-nerves 2021-05-08 at 11 06 14 AM

TL;DR

  • Find the rootfs_overlay/etc/iex.exs file in your Nerves project directory.
  • Edit it as you want.

Here is an example.

IO.puts("""
\e[34m████▄▖    \e[36m▐███
\e[34m█▌  ▀▜█▙▄▖  \e[36m▐█
\e[34m█▌ \e[36m▐█▄▖\e[34m▝▀█▌ \e[36m▐█   \e[39mN  E  R  V  E  S
\e[34m█▌   \e[36m▝▀█▙▄▖ ▐█
\e[34m███▌    \e[36m▀▜████\e[0m
""")

# Add Toolshed helpers to the IEx session
use Toolshed

if RingLogger in Application.get_env(:logger, :backends, []) do
  IO.puts("""
  RingLogger is collecting log messages from Elixir and Linux. To see the
  messages, either attach the current IEx session to the logger:

    RingLogger.attach

  or print the next messages in the log:

    RingLogger.next
  """)
end

# Print information about the running system
IO.puts("")
uname

# Print information about the connected I2C devices
if Code.ensure_loaded?(Circuits.I2C) do
  IO.puts("")
  Circuits.I2C.detect_devices()
end

# Print information about local weather
try do
  IO.puts("")
  weather()
rescue
  exception -> nil
end

# Alias some modules to save typing later on
alias HelloNerves.Worker
Enter fullscreen mode Exit fullscreen mode

.iex.exs file in Elixir

It is a script that is loaded when IEx is started. For details, IEx documentation has a section that explains the iex.exs file.

.iex.exs file in Nerves target device

The template for the .iex.exs file is defined in nerves_bootstrap.

When we make a new Nerves project running the mix nerves.new command, that .iex.exs template is copied into the project's rootfs_overlay/etc directory.

The Nerves logo Ascii art was added in this PR in February 2021 so if you have any Nerves projects that are created before that, you do not see the logo in the IEx. In such a case you can manually paste the Ascii art into your rootfs_overlay/etc/.iex.exs file.

Some use cases

We can do whatever we want in our rootfs_overlay/etc/.iex.exs file, but here are some ideas I came up with.

  • Print Toolshed functions, such as weather/0, uname/0
  • Print information about connected I2C devices invoking Circuits.I2C.detect_devices/0.
  • Aliasing some modules

weather/0 required the Internet connection. Please make sure that :inets is added to extra_application in your mix.exs file.

def application do
  [
    mod: {HelloNerves.Application, []},
    extra_applications: [:logger, :runtime_tools, :inets]
  ]
end
Enter fullscreen mode Exit fullscreen mode

That's it!

Top comments (0)