Skip to main content

Command Palette

Search for a command to run...

Customizing IEx for Elixir Nerves target device

Published
2 min read
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

.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

That's it!

More from this blog

Raspberry Pi TensorFlow Liteで物体検出を楽しむ

この記事について Raspberry Pi、TensorFlow、Pythonのいずれにも詳しくない筆者が、物体検出をやって楽しんだ成果の記録です。 TensorFlow公式の物体検出のサンプルプログラムを実行します。 動作環境 ボード Raspberry Pi 4 Model B OS Raspberry Pi OS (32-bit または 64-bit) デスクトップ環境 カメラ Raspberry Pi カメラモジュール v2 Python Python ...

Apr 23, 20231 min read

Elixir Circuits.I2C with Mox

This is written in Japanese. I might convert it to English later, maybe. はじめに Elixirのテストでモックを用意するときに利用するElixirパッケージとして、moxが人気です。Elixir作者のJosé Valimさんが作ったからということもありますが、ただモックを用意するだけではなくElixirアプリの構成をより良くするためのアイデアにまで言及されているので、教科書のようなものと思っています。 一言でいうと「その場...

Dec 3, 20213 min read
M

Masatoshi Nishiguchi's Blog

62 posts