wslでnerves その3

# 概要
wsl(wsl2じゃない)でnervesやってみる。
qemu(x86_64エミュレータ、ラズパイじゃない)でやってみた。
shell server、実装してみた。

# サンプルコード

```
defmodule HelloNerves do
  @moduledoc """
  Documentation for HelloNerves.
  """

  @doc """
  Hello world.

  ## Examples

      iex> HelloNerves.hello
      :world

  """
  def hello do
    :world
  end
	require Logger
	use GenServer
	@port 5300
	def start do
		GenServer.start(__MODULE__, %{socket: nil})
	end
	def init(state) do
		{:ok, socket} = :gen_tcp.listen(@port, [:binary, active: true])
		send(self(), :accept)
		Logger.info "Listenn port: #{@port}"
		{:ok, %{state | socket: socket}}
	end
	def handle_info(:accept, %{socket: socket} = state) do
		{:ok, _} = :gen_tcp.accept(socket)
		Logger.info "Connected"
		{:noreply, state}
	end
	def handle_info({:tcp, socket, data}, state) do
		Logger.info "Received: #{String.length(data)}"
		Logger.info data
		Logger.info "Send"
		{result, 0} = System.shell(data)
		:ok = :gen_tcp.send(socket, result)
		:gen_tcp.close(socket)
		send(self(), :accept)
		{:noreply, state}
	end
	def handle_info({:tcp_closed, _}, state) do
		Logger.info "Close"
		{:stop, :normal, state}
	end
	def handle_info({:tcp_error, _}, state) do
		Logger.info "Error"
		{:stop, :normal, state}
	end
	def run() do
		cmd = IO.gets "> "
		{:ok, socket} = :gen_tcp.connect('localhost', 5300, [:binary, active: false])
		:ok = :gen_tcp.send(socket, cmd)
		{:ok, data} = :gen_tcp.recv(socket, 0)
		data
		|> IO.puts
		run()
	end
	def main(args \\ "") do
		HelloNerves.start
		run()
	end
end



```

# コンパイル手順

```
$ export MIX_TARGET=x86_64

$ mix firmware.image

==> nerves
==> hello_nerves

Nerves environment
  MIX_TARGET:   x86_64
  MIX_ENV:      dev

Compiling 1 file (.ex)
warning: variable "args" is unused (if the variable is not meant to be used, prefix it with an underscore)
  lib/hello_nerves.ex:62: HelloNerves.main/1

warning: Rserver.start/0 is undefined (module Rserver is not available or is yet to be defined)
  lib/hello_nerves.ex:63: HelloNerves.main/1

Generated hello_nerves app
|nerves| Building OTP Release...

* skipping runtime configuration (config/runtime.exs not found)
* creating _build/x86_64_dev/rel/hello_nerves/releases/0.1.0/vm.args
Updating base firmware image with Erlang release...
Copying rootfs_overlay: /home/user/elixir/hello_nerves/_build/x86_64_dev/nerves/rootfs_overlay
Copying rootfs_overlay: /home/user/elixir/hello_nerves/rootfs_overlay
Parallel mksquashfs: Using 2 processors
Creating 4.0 filesystem on /home/user/elixir/hello_nerves/_build/_nerves-tmp/combined.squashfs, block size 131072.

Exportable Squashfs 4.0 filesystem, gzip compressed, data block size 131072
        compressed data, compressed metadata, compressed fragments, compressed xattrs
        duplicates are removed
Filesystem size 23591.25 Kbytes (23.04 Mbytes)
        52.40% of uncompressed filesystem size (45019.42 Kbytes)
Inode table size 19831 bytes (19.37 Kbytes)
        28.58% of uncompressed inode table size (69379 bytes)
Directory table size 22064 bytes (21.55 Kbytes)
        39.90% of uncompressed directory table size (55292 bytes)
Number of duplicate files found 9
Number of inodes 2106
Number of files 1731
Number of fragments 129
Number of symbolic links  140
Number of device nodes 0
Number of fifo nodes 0
Number of socket nodes 0
Number of directories 235
Number of ids (unique uids + gids) 3
Number of uids 3
        root (0)
        www-data (33)
        ore (1000)
Number of gids 3
        root (0)
        www-data (33)
        ore (1000)
Building /home/user/elixir/hello_nerves/_build/x86_64_dev/nerves/images/hello_nerves.fw...
Firmware built successfully! ?

Now you may install it to a MicroSD card using `mix burn` or upload it
to a device with `mix upload` or `mix firmware.gen.script`+`./upload.sh`.

Writing to /home/user/elixir/hello_nerves/hello_nerves.img...
100% [====================================] 24.25 MB in / 24.72 MB out
Success!
Elapsed time: 0.322 s

```

# 実行結果

![hello.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/ee2c8d20-ae45-8896-b1f9-d5360c8f1b8d.png)


以上。