LoginSignup
2
1

More than 1 year has passed since last update.

IoT開発フレームワーク「Nerves」の開発環境をWSL2上に作る

Last updated at Posted at 2021-12-30

IoT開発フレームワーク「Nerves」の開発環境を、WSL2上に作る方法です。

前提環境とゴール

  • Windows 11 Pro / 21H2
  • WSL2 (Ubuntu 20.04)

ゴール

WSL2 上の Linux で Nerves による開発ができるようになります。
※ Nerves アプリの作成方法(いわゆる Getting Started)以降の解説はしません。

環境構築のポイント

公式の Installation にも書かれている通り、fwup(組み込みLinux向けファームウェアバイナリ作成ツール)を WSL2(Linux 側) だけでなく、Windows(ホスト側)にもインストールします。

fwup.exe を Windows(ホスト側) にインストール

公式の Installation では Chocolatey を使ってインストールするように書かれていますが、実は Release からダウンロードできる fwup.exe を、パスが通っているディレクトリに配置するだけで OK です。

私は %USERPROFILE%\AppData\Local\fwup\ というディレクトリを作り、そこに fwup.exe をコピーしました。
image.png
その後、環境変数 Path へ同パスを追加しています。
image.png

fwup.exe のアップデート

Chocolatey でアップデートするか、 Release ページからダウンロードしてきてファイルを上書きします。

残りの作業は WSL2 上で行う

残りの作業は WSL2 上の Linux で行います。以下は 公式からコマンドライン の抜粋です。
※一部、バージョン番号を変えたり、ダイアログが出ないようにするオプションを追加しています。

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.9.0
echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.bashrc
echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.bashrc # optional
source ~/.bashrc
asdf plugin-add erlang
asdf plugin-add elixir
asdf install erlang 24.1.5
asdf install elixir 1.12.3-otp-24
asdf global erlang 24.1.5
asdf global elixir 1.12.3-otp-24
mix local.hex --force
mix local.rebar --force
mix archive.install hex nerves_bootstrap --force

最終的には mix のサブコマンドに nerves.** がいれば OK です。

❯ mix help | grep nerves
mix local.nerves        # Checks for updates to nerves_bootstrap
mix nerves.clean        # Cleans dependencies and build artifacts
mix nerves.new          # Creates a new Nerves application
mix nerves.system.shell # Enter a shell to configure a custom system

以上で完了です。
この後は Getting Started にお進みください。

Appendix: asdf とは?

asdfとは、言語ランタイムのバージョン管理ツールです。言語に特化ものに rbenv 等がありますが、それらの汎用版になります。このツール自体は Nerves や Elixir とは関係ありませんが、インストールが楽になるので使っているようです。

asdf plugin-add LANG_NAME で管理できる言語ランタイムを追加して asdf install LANG_NAME で実際にインストールするといったスタイルです。

対応言語はかなりあります。これを機にasdfに一本化してもいいかもしれませんね。

$ asdf plugin list all | grep -P '(ruby|nodejs|rust|golang)'
golang                        https://github.com/kennyp/asdf-golang.git
golangci-lint                 https://github.com/hypnoglow/asdf-golangci-lint.git
nodejs                        https://github.com/asdf-vm/asdf-nodejs.git
ruby                          https://github.com/asdf-vm/asdf-ruby.git
rust                          https://github.com/code-lever/asdf-rust.git

asdf 自体のアップデートは asdf update でできます。

Tips: asdf.sh が存在するときだけ asdf を有効化するスクリプト

.bashrc を複数ホストで共有してる場合、条件分岐したくなることがあると思うので、そのスクリプトです。
echo -e ... > ~/.bashrc で流し込んでいる部分の代わりにお使いください。(echo ... はお好みです)

# asdf
if [ -f "$HOME/.asdf/asdf.sh" ]; then
        echo -n "asdf ... " > /dev/stderr
        . $HOME/.asdf/asdf.sh
        . $HOME/.asdf/completions/asdf.bash
        echo "done" > /dev/stderr
fi

あとがき

良いお年を。

EoT

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1