# 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](https://hexdocs.pm/nerves/targets.html) (e.g., [Raspberry Pi Zero W](https://www.raspberrypi.org/products/raspberry-pi-zero-w/)).

![customizing-iex-in-nerves 2021-05-08 at 11 06 14 AM](https://user-images.githubusercontent.com/7563926/117544080-9e20ed00-afed-11eb-9e1c-033afa8ede3b.gif)

## TL;DR

- Find the `rootfs_overlay/etc/iex.exs` file in your [Nerves](https://www.nerves-project.org/) project directory.
- Edit it as you want.

Here is an example.

```elixir
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](https://hexdocs.pm/iex/IEx.html#module-the-iex-exs-file) has a section that explains the iex.exs file.

## `.iex.exs` file in Nerves target device

The [template for the `.iex.exs` file](https://github.com/nerves-project/nerves_bootstrap/blob/main/templates/new/rootfs_overlay/etc/iex.exs) is defined in [nerves_bootstrap](https://hexdocs.pm/nerves_bootstrap).

When we make a new Nerves project running the [`mix nerves.new` command](https://github.com/nerves-project/nerves_bootstrap#mix-nervesnew), 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](https://github.com/nerves-project/nerves_bootstrap/pull/170) 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](https://hexdocs.pm/toolshed/Toolshed.html) 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`](http://erlang.org/doc/man/inets.html) is added to `extra_application` in your `mix.exs` file.

```elixir
def application do
  [
    mod: {HelloNerves.Application, []},
    extra_applications: [:logger, :runtime_tools, :inets]
  ]
end
```

That's it!

