wslでnerves その42

# 概要
wsl(wsl2じゃない)でnervesやってみる。
qemu(x86_64エミュレータ、ラズパイじゃない)でやってみた。
生成したnerves_livebook.imgを、quemで起動してテストしてみた。
データの永続化、やってみた。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/079fb9fd-46f7-7e14-1eca-f8d5a48b9473.png)


# セットアップ

```
Mix.install([
  {:jason, "~> 1.4"}
])
```

# サンプルコード

```

defmodule Ecto2 do
	def insert(db, v) do
		db = db ++ [v]
	end
	def update(db, v) do
		i = Enum.filter_map(Enum.with_index(db), fn {x, _} -> 
			x.id == v.id 
		end, fn {_, i} -> 
			i 
		end)
		|> Enum.at(0)
		db = List.replace_at(db, i, v)
	end
	def delete(db, v) do
		db = List.delete(db, v)
	end
	def print(db) do
		Enum.map(db, fn x -> 
			[ x.id,  x.name]
		end)
		|>IO.inspect
	end
  def load(db) do
    File.read!("/data/db.json")
	  |> Jason.decode!(keys: :atoms)
  end
  def save(db) do
    json = Jason.encode!(db)
    File.write!("/data/db.json", json)
  end
end
now = NaiveDateTime.utc_now()
	|> NaiveDateTime.truncate(:second)
	
db = [
  %{
	id: 1,
	name: "ryu",
	age: 10,
	gaku: "s",
	inserted_at: ~N[2023-10-31 05:35:23],
	updated_at: ~N[2023-11-30 05:35:37]
  },
  %{
	id: 2,
	name: "ryo",
	age: 20,
	gaku: "s",
	inserted_at: ~N[2023-10-31 05:35:23],
	updated_at: ~N[2023-11-30 05:35:37]
  },
  %{
	id: 3,
	name: "alice",
	age: 20,
	gaku: "s",
	inserted_at: ~N[2023-10-31 05:35:23],
	updated_at: ~N[2023-11-30 05:35:37]
  },
  %{
	id: 4,
	name: "kent",
	age: 30,
	gaku: "s",
	inserted_at: ~N[2023-10-31 05:35:23],
	updated_at: ~N[2023-11-30 05:35:37]
  }
]
#Ecto2.save(db)
db = Ecto2.load(db)


Ecto2.print(db)
db = Ecto2.insert(db,   %{
	id: 5,
	name: "kent",
	age: 30,
	gaku: "s",
	inserted_at: now,
	updated_at: now
  }
)
Ecto2.print(db)




db = Ecto2.delete(db,   %{
	id: 5,
	name: "kent",
	age: 30,
	gaku: "s",
	inserted_at: now,
	updated_at: now
  }
)
Ecto2.print(db)

db = Ecto2.update(db,   %{
	id: 1,
	name: "yoko",
	age: 30,
	gaku: "s",
	inserted_at: now,
	updated_at: now
  }
)
Ecto2.print(db)
```

# 実行結果

```
warning: variable "db" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)
  /data/livebook/db0.livemd#cell:3jaaguuztxxlzebx32n3ivxnshzlhfwy:5: Ecto2.insert/2

warning: variable "db" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)
  /data/livebook/db0.livemd#cell:3jaaguuztxxlzebx32n3ivxnshzlhfwy:14: Ecto2.update/2

warning: variable "db" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)
  /data/livebook/db0.livemd#cell:3jaaguuztxxlzebx32n3ivxnshzlhfwy:17: Ecto2.delete/2

warning: variable "db" is unused (if the variable is not meant to be used, prefix it with an underscore)
  /data/livebook/db0.livemd#cell:3jaaguuztxxlzebx32n3ivxnshzlhfwy:25: Ecto2.load/1

[[1, "ryu"], [2, "ryo"], [3, "alice"], [4, "kent"]]
[[1, "ryu"], [2, "ryo"], [3, "alice"], [4, "kent"], [5, "kent"]]
[[1, "ryu"], [2, "ryo"], [3, "alice"], [4, "kent"]]
[[1, "yoko"], [2, "ryo"], [3, "alice"], [4, "kent"]]
warning: Enum.filter_map/3 is deprecated. Use Enum.filter/2 + Enum.map/2 or for comprehensions instead
  /data/livebook/db0.livemd#cell:3jaaguuztxxlzebx32n3ivxnshzlhfwy:8: Ecto2.update/2


```

以上。