見出し画像

devbox環境でnode.js入れてみた

ご挨拶・背景

こんにちは!
現在、SHIFTの開発支援プロダクト推進部でTB Projectに参加しております、yasudaです。

今回は、2022年のクリスマス前にver0.2.0が出た、devboxという開発用の独立したシェルを作れるコマンドラインツールを触っていこうと考えております。

過去記事:


想定している対象読者

  • devboxが気になる人

  • Nixが気になる人

devboxとは?

公式のドキュメント曰く、Instantでeasyなpredictible 開発環境とのことです。実態としては、jetpack.ioというバックエンド開発ツールを作っている2020年設立の会社が、NixOSのパッケージマネジャーであるNixのwrapperとして作られています。

Virtual Boxなどの仮想環境を用意せずとも、Dockerのような環境構築ができるのが魅力ですね。

参照記事:


devboxを使うメリットとは?

5つあります。

  1. プロジェクトで必要なツールのリストをdevbox.jsonファイルで宣言して、devbox shellで実行できるので、プロジェクトメンバー全員が同じ環境で作業できる。

  2. 開発環境がノートPC内の他のものから隔離されているので、PCが汚れない。

  3. 仮想化レイヤーを追加せず、PC上で孤立した環境を作るため、ファイルシステムやコマンドの処理速度が低下しない。

  4. プロジェクトごとに隔離された環境なので、複数バージョンの対立などが起きない。

  5. vscodeのdevcontainer.jsonやDockerfileを出力できる。

devboxをインストールするには?

curl -fsSL https://get.jetpack.io/devbox | bash

でインストールできます。

ちなみにcurlのoptionの-fsSLは、 サーバーへのリクエストに失敗してエラーが返ってきても標準出力に何も表示させない -f
進捗は表示されないが、エラーは表示されるの -sS
リダイレクト設定されているURLにアクセスした時に、リダイレクト先のURLに対してもリクエストを発行する -L
の組み合わせになっております。

参照記事:

インストールすると、下記のようにインストール場所の確認をされるので、良ければYと答えます。

Devbox 📦 by jetpack.io
  Instant and predictable development environments and containers.

  This script downloads and installs the latest devbox binary.

Confirm Installation Details
  Location:     /usr/local/bin/devbox
  Download URL: <https://releases.jetpack.io/devbox>

? Install devbox to /usr/local/bin (requires sudo)? [Y/n]
Downloading and Installing
✓ Downloading devbox binary... [DONE]
→ Installing in /usr/local/bin/devbox (requires sudo)...
✓ Installing in /usr/local/bin/devbox... [DONE]
✓ Successfully installed devbox 🚀

インストールが完了しました。
devboxと入力すると下記が出ます。

$devbox
Instant, easy, predictable development environments

Usage:
  devbox [flags]
  devbox [command]

Available Commands:
  add         Add a new package to your devbox
  completion  Generate the autocompletion script for the specified shell
  generate    
  help        Help about any command
  info        Display package info
  init        Initialize a directory as a devbox project
  rm          Remove a package from your devbox
  run         Starts a new devbox shell and runs the target script
  services    Interact with devbox services
  shell       Start a new shell or run a command with access to your packages
  version     Print version information

Flags:
  -h, --help   help for devbox

Use "devbox [command] --help" for more information about a command.
$devbox version 
0.2.0

devboxにnode.jsをインストールしてみる

  1. まずは任意の名前で空のフォルダを作成

  2. $ devbox init でdevbox.jsonファイルを作成
    初期設定は下記のようになっています。

{
  "packages": [],
  "shell": {
    "init_hook": null
  },
  "nixpkgs": {
    "commit": "52e3e80afff4b16ccb7c52e9f0f5220552f03d04"
  }
}

3.$ devbox add nodejs でNix Packagesからnode.jsをインストール
ちなみにこの時、nodejsをnode.jsと書くとパッケージが見つからないので注意。

また、ここで、Nixが手元にインストールされていないと、自動的にインストールされる流れになります。
そして、nodejsがインストール成功すると下記のように表示が出ます。

Installing nix packages. This may take a while... done.
nodejs (nodejs-18.12.1) is now installed.

4.$ devbox add yarn でyarnもインストールする

  同じく下記の表示が出ます。

Installing nix packages. This may take a while... done.
yarn (yarn-1.22.19) is now installed.

5.$ devbox shell でdevbox環境の新しいシェルを起動します。

この段階で、実際に必要なパッケージをnix/storeにfetchしてシェルを起動してくれます。

Installing nix packages. This may take a while... done.
these 25 paths will be fetched (80.56 MiB download, 500.09 MiB unpacked):
  /nix/store/039c4vhb9s848cqyh9gwixxc6a31rm5v-libSystem-11.0.0
  /nix/store/17590a5bp47rym08i2h70a2ji1z18q6w-expand-response-params
  /nix/store/33yb640sh8ac5ky9ak7fjnrb3mw559q1-cctools-binutils-darwin-wrapper-973.0.1
  /nix/store/3f0g6j019vy5njy80ya5g6k8843vyzbw-compiler-rt-libc-11.1.0
  /nix/store/3ns8fjjfv67h30g341757j60bpx8fing-llvm-11.1.0
  /nix/store/4w2rv6s96fwsb4qyw8b9w394010gxriz-stdenv-darwin
  /nix/store/5ni656fm1mig17801hs14f9wdqbih4ri-libtapi-1100.0.11
  /nix/store/9m49xk9l6dn16b515clqbzv3fyj27pxr-openssl-3.0.7
  /nix/store/c85j441hrgg8b63k9iq0m6q5gkqpsvmz-libobjc-11.0.0
  /nix/store/d8hcl0rdr85qjhk5hwwpk5mmz3yf93cc-bash-interactive-5.1-p16
  /nix/store/g8b81w9kpb82gm7702ca692mp5flm7pf-apple-framework-CoreFoundation-11.0.0
  /nix/store/gqvp3m95nbnzlinvbhdwqwfh6bh1bwj1-sigtool-0.1.3
  /nix/store/i92bgxw5ns8zbv8yk3b0anvip0dlak3g-binutils-2.39
  /nix/store/jmxf2kh5f96s2gw8kvqlq3vckpx7lh2k-cctools-port-973.0.1
  /nix/store/kn8ahinf4kbh9zwnw44a0hhps6ia1swy-clang-11.1.0
  /nix/store/lqiqj6s5jx196bpx0mr53d232b57y768-clang-wrapper-11.1.0
  /nix/store/nlrnyahaj2aw7kgzcslmkf65g10d6syr-libxml2-2.10.1
  /nix/store/nm5f2lmgn13058mim1m6n7gk7q6cxm8j-ncurses-6.3-p20220507
  /nix/store/qk7lhszzq0c96vysk4y3z2rapwsjnsyd-readline-8.1p2
  /nix/store/ra4asalsj40viq45x408v49y748anrg4-clang-11.1.0-lib
  /nix/store/sgbfgh7abygpwycn9vmd9f0idfnwcs32-signing-utils
  /nix/store/x0b1kda81p0w0qqp7abvsprcf7s7qp64-cctools-binutils-darwin-973.0.1
  /nix/store/y4w6ssx5i5qk6zw4nl4k2qyw8m8p3i5i-llvm-11.1.0-lib
  /nix/store/ykkl3w3z4v661mjj47lfb6ryxicjrlyv-post-link-sign-hook
  /nix/store/zidx8v13phsbzg3pjwjqil95iazq9l5y-compiler-rt-libc-11.1.0-dev
copying path '/nix/store/nm5f2lmgn13058mim1m6n7gk7q6cxm8j-ncurses-6.3-p20220507' from '<https://cache.nixos.org>'...
copying path '/nix/store/i92bgxw5ns8zbv8yk3b0anvip0dlak3g-binutils-2.39' from '<https://cache.nixos.org>'...
copying path '/nix/store/3f0g6j019vy5njy80ya5g6k8843vyzbw-compiler-rt-libc-11.1.0' from '<https://cache.nixos.org>'...
copying path '/nix/store/17590a5bp47rym08i2h70a2ji1z18q6w-expand-response-params' from '<https://cache.nixos.org>'...
copying path '/nix/store/zidx8v13phsbzg3pjwjqil95iazq9l5y-compiler-rt-libc-11.1.0-dev' from '<https://cache.nixos.org>'...
copying path '/nix/store/039c4vhb9s848cqyh9gwixxc6a31rm5v-libSystem-11.0.0' from '<https://cache.nixos.org>'...
copying path '/nix/store/c85j441hrgg8b63k9iq0m6q5gkqpsvmz-libobjc-11.0.0' from '<https://cache.nixos.org>'...
copying path '/nix/store/5ni656fm1mig17801hs14f9wdqbih4ri-libtapi-1100.0.11' from '<https://cache.nixos.org>'...
copying path '/nix/store/g8b81w9kpb82gm7702ca692mp5flm7pf-apple-framework-CoreFoundation-11.0.0' from '<https://cache.nixos.org>'...
copying path '/nix/store/jmxf2kh5f96s2gw8kvqlq3vckpx7lh2k-cctools-port-973.0.1' from '<https://cache.nixos.org>'...
copying path '/nix/store/nlrnyahaj2aw7kgzcslmkf65g10d6syr-libxml2-2.10.1' from '<https://cache.nixos.org>'...
copying path '/nix/store/9m49xk9l6dn16b515clqbzv3fyj27pxr-openssl-3.0.7' from '<https://cache.nixos.org>'...
copying path '/nix/store/y4w6ssx5i5qk6zw4nl4k2qyw8m8p3i5i-llvm-11.1.0-lib' from '<https://cache.nixos.org>'...
copying path '/nix/store/qk7lhszzq0c96vysk4y3z2rapwsjnsyd-readline-8.1p2' from '<https://cache.nixos.org>'...
copying path '/nix/store/ra4asalsj40viq45x408v49y748anrg4-clang-11.1.0-lib' from '<https://cache.nixos.org>'...
copying path '/nix/store/d8hcl0rdr85qjhk5hwwpk5mmz3yf93cc-bash-interactive-5.1-p16' from '<https://cache.nixos.org>'...
copying path '/nix/store/kn8ahinf4kbh9zwnw44a0hhps6ia1swy-clang-11.1.0' from '<https://cache.nixos.org>'...
copying path '/nix/store/3ns8fjjfv67h30g341757j60bpx8fing-llvm-11.1.0' from '<https://cache.nixos.org>'...
copying path '/nix/store/gqvp3m95nbnzlinvbhdwqwfh6bh1bwj1-sigtool-0.1.3' from '<https://cache.nixos.org>'...
copying path '/nix/store/x0b1kda81p0w0qqp7abvsprcf7s7qp64-cctools-binutils-darwin-973.0.1' from '<https://cache.nixos.org>'...
copying path '/nix/store/ykkl3w3z4v661mjj47lfb6ryxicjrlyv-post-link-sign-hook' from '<https://cache.nixos.org>'...
copying path '/nix/store/sgbfgh7abygpwycn9vmd9f0idfnwcs32-signing-utils' from '<https://cache.nixos.org>'...
copying path '/nix/store/33yb640sh8ac5ky9ak7fjnrb3mw559q1-cctools-binutils-darwin-wrapper-973.0.1' from '<https://cache.nixos.org>'...
copying path '/nix/store/lqiqj6s5jx196bpx0mr53d232b57y768-clang-wrapper-11.1.0' from '<https://cache.nixos.org>'...
copying path '/nix/store/4w2rv6s96fwsb4qyw8b9w394010gxriz-stdenv-darwin' from '<https://cache.nixos.org>'...
Starting a devbox shell...

6.devbox内にて、node.jsのバージョンを確認します。無事、devbox環境に入れました。

(devbox) $ node -v
v18.12.1

最後に

いかがだったでしょうか。かなり簡単にnode.jsをdevboxに追加できてしまいますね。恐るべし。

どうやらバージョンを分離させているのはNixの機能らしく、その辺りの詳細や、実際にちゃんとした開発環境を作るとなった場合の問題についてもまた次の機会に掘り下げていければと思います。

この記事が皆様のdevboxに対する理解に一役買うことができたら幸いです。

この記事を最後まで読んだ方は是非、ハートボタンからスキしていただけたら嬉しいです(^O^)/

ご清覧ありがとうございました!

参考文献


執筆者プロフィール:Yutaro Yasuda
22新卒としてSHIFTに入社。趣味は散歩、読書、食事。愛用座布団はtempur.

お問合せはお気軽に
https://service.shiftinc.jp/contact/

SHIFTについて(コーポレートサイト)
https://www.shiftinc.jp/

SHIFTのサービスについて(サービスサイト)
https://service.shiftinc.jp/

SHIFTの導入事例
https://service.shiftinc.jp/case/

お役立ち資料はこちら
https://service.shiftinc.jp/resources/

SHIFTの採用情報はこちら
https://recruit.shiftinc.jp/career/

みんなにも読んでほしいですか?

オススメした記事はフォロワーのタイムラインに表示されます!