wslでnerves その6

# 概要
wsl(wsl2じゃない)でnervesやってみる。
qemu(x86_64エミュレータ、ラズパイじゃない)でやってみた。
練習問題やってみた。

# 練習問題
shell serverを自動起動せよ。

# 方針
mix.exsをいじる。

# mix.exs

```
defmodule HelloNerves.MixProject do
  use Mix.Project

  @app :hello_nerves
  @version "0.1.0"
  @all_targets [:rpi, :rpi0, :rpi2, :rpi3, :rpi3a, :rpi4, :bbb, :osd32mp1, :x86_64, :grisp2]

  def project do
    [
      app: @app,
      version: @version,
      elixir: "~> 1.11",
      archives: [nerves_bootstrap: "~> 1.11"],
      start_permanent: Mix.env() == :prod,
      deps: deps(),
      releases: [{@app, release()}],
      preferred_cli_target: [run: :host, test: :host]
    ]
  end

  # Run "mix help compile.app" to learn about applications.
  def application do
    [
      #mod: {HelloNerves.Application, []},
      mod: {HelloNerves, []},
      extra_applications: [:logger, :runtime_tools]
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      # Dependencies for all targets
      {:nerves, "~> 1.7.16 or ~> 1.8.0 or ~> 1.9.0", runtime: false},
      {:shoehorn, "~> 0.9.1"},
      {:ring_logger, "~> 0.8.5"},
      {:toolshed, "~> 0.2.26"},

      # Dependencies for all targets except :host
      {:nerves_runtime, "~> 0.13.0", targets: @all_targets},
      {:nerves_pack, "~> 0.7.0", targets: @all_targets},

      # Dependencies for specific targets
      # NOTE: It's generally low risk and recommended to follow minor version
      # bumps to Nerves systems. Since these include Linux kernel and Erlang
      # version updates, please review their release notes in case
      # changes to your application are needed.
      {:nerves_system_rpi, "~> 1.19", runtime: false, targets: :rpi},
      {:nerves_system_rpi0, "~> 1.19", runtime: false, targets: :rpi0},
      {:nerves_system_rpi2, "~> 1.19", runtime: false, targets: :rpi2},
      {:nerves_system_rpi3, "~> 1.19", runtime: false, targets: :rpi3},
      {:nerves_system_rpi3a, "~> 1.19", runtime: false, targets: :rpi3a},
      {:nerves_system_rpi4, "~> 1.19", runtime: false, targets: :rpi4},
      {:nerves_system_bbb, "~> 2.14", runtime: false, targets: :bbb},
      {:nerves_system_osd32mp1, "~> 0.10", runtime: false, targets: :osd32mp1},
      {:nerves_system_x86_64, "~> 1.19", runtime: false, targets: :x86_64},
      {:nerves_system_grisp2, "~> 0.3", runtime: false, targets: :grisp2}
    ]
  end

  def release do
    [
      overwrite: true,
      # Erlang distribution is not started automatically.
      # See https://hexdocs.pm/nerves_pack/readme.html#erlang-distribution
      cookie: "#{@app}_cookie",
      include_erts: &Nerves.Release.erts/0,
      steps: [&Nerves.Release.init/1, :assemble],
      strip_beams: Mix.env() == :prod or [keep: ["Docs"]]
    ]
  end
end

```

# サンプルコード

```
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
	def start(_type, _args) do
		main()
	end
end

```

# 実行結果
キー入力に反応しない。

![run.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/3e9e493f-6649-fac8-e1c6-d9e1ffa5c05e.png)

c#からは、つながる。
```
>tcp0

>
pwd
/srv/erlang


>tcp0

>
ls
releases
lib
erts-13.1.3


```

以上。