LuaRocksで環境ごとにパッケージをインストールしてパスを通す

# 環境確認
（luajit / luarocksが`/usr/share/luajit`にインストールされているケース）

```
$ luarocks
 ~~ 略 ~~
CONFIGURATION
	Lua version: 5.1
	Configuration files:
		System: /usr/share/luajit/etc/luarocks/config-5.1.lua (ok)
		User  : /root/.luarocks/config-5.1.lua (failed)

	Rocks trees in use: 
		/root/.luarocks ("user")
		/usr/share/luajit ("system")

$ cat /usr/share/luajit/etc/luarocks/config-5.1.lua
rocks_trees = {
   { name = [[user]], root = home..[[/.luarocks]] },
   { name = [[system]], root = [[/usr/share/luajit]] }
}
```
# 環境追加とインストール
configへ環境を追加

```/usr/share/luajit/etc/luarocks/config-5.1.lua

rocks_trees = {
   { name = [[sandbox]], root = [[/home/sandbox]] },
   { name = [[user]], root = home..[[/.luarocks]] },
   { name = [[system]], root = [[/usr/share/luajit]] }
}
```

# パス確認
```bash
$ luarocks path
export  LUA_PATH='/home/sandbox/share/lua/5.1/?.lua;/home/sandbox/share/lua/5.1/?/init.lua;/home/vagrant/.luarocks/share/lua/5.1/?.lua;/home/vagrant/.luarocks/share/lua/5.1/?/init.lua;/usr/share/luajit/share/lua/5.1/?.lua;/usr/share/luajit/share/lua/5.1/?/init.lua;/root/.luarocks/share/lua/5.1/?.lua;/root/.luarocks/share/lua/5.1/?/init.lua;./?.lua;/usr/share/luajit/share/luajit-2.1.0-beta2/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua'
export LUA_CPATH='/home/sandbox/lib/lua/5.1/?.so;/home/vagrant/.luarocks/lib/lua/5.1/?.so;/usr/share/luajit/lib/lua/5.1/?.so;/root/.luarocks/lib/lua/5.1/?.so;./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so'
```
`luarocks path` で確認するとすべてのパスが入っているので、実際に必要なのは
### LUA_PATH
- /home/sandbox/share/lua/5.1/?.lua
- /home/sandbox/share/lua/5.1/?/init.lua

### LUA_CPATH
- /home/sandbox/lib/lua/5.1/?.so

Openrestyなら上記を [lua_package_path](https://github.com/openresty/lua-nginx-module#lua_package_path), [lua_package_cpath](https://github.com/openresty/lua-nginx-module#lua_package_cpath) へ設定する感じに

# 指定の環境へインストール

```bash
$ luarocks --tree=sandbox install yaml
Installing https://luarocks.org/yaml-1.1.2-1.src.rock...
Using https://luarocks.org/yaml-1.1.2-1.src.rock... switching to 'build' mode

Missing dependencies for yaml:
lub >= 1.0.3, < 2

Using https://luarocks.org/lub-1.1.0-1.src.rock... switching to 'build' mode

Missing dependencies for lub:
luafilesystem >= 1.4.0

Using https://luarocks.org/luafilesystem-1.6.3-2.src.rock... switching to 'build' mode
gcc -O2 -fPIC -I/usr/share/luajit/include/luajit-2.1 -c src/lfs.c -o src/lfs.o
gcc -shared -o lfs.so -L/usr/share/luajit/lib src/lfs.o
Updating manifest for /home/sandbox/lib/luarocks/rocks
No existing manifest. Attempting to rebuild...
luafilesystem 1.6.3-2 is now built and installed in /home/sandbox (license: MIT/X11)

Updating manifest for /home/sandbox/lib/luarocks/rocks
lub 1.1.0-1 is now built and installed in /home/sandbox (license: MIT)

gcc -O2 -fPIC -I/usr/share/luajit/include/luajit-2.1 -c src/api.c -o src/api.o -Isrc
gcc -O2 -fPIC -I/usr/share/luajit/include/luajit-2.1 -c src/b64.c -o src/b64.o -Isrc
gcc -O2 -fPIC -I/usr/share/luajit/include/luajit-2.1 -c src/dumper.c -o src/dumper.o -Isrc
gcc -O2 -fPIC -I/usr/share/luajit/include/luajit-2.1 -c src/emitter.c -o src/emitter.o -Isrc
gcc -O2 -fPIC -I/usr/share/luajit/include/luajit-2.1 -c src/loader.c -o src/loader.o -Isrc
gcc -O2 -fPIC -I/usr/share/luajit/include/luajit-2.1 -c src/lyaml.c -o src/lyaml.o -Isrc
gcc -O2 -fPIC -I/usr/share/luajit/include/luajit-2.1 -c src/parser.c -o src/parser.o -Isrc
gcc -O2 -fPIC -I/usr/share/luajit/include/luajit-2.1 -c src/reader.c -o src/reader.o -Isrc
gcc -O2 -fPIC -I/usr/share/luajit/include/luajit-2.1 -c src/scanner.c -o src/scanner.o -Isrc
gcc -O2 -fPIC -I/usr/share/luajit/include/luajit-2.1 -c src/strtod.c -o src/strtod.o -Isrc
gcc -O2 -fPIC -I/usr/share/luajit/include/luajit-2.1 -c src/writer.c -o src/writer.o -Isrc
gcc -shared -o yaml/core.so -L/usr/share/luajit/lib src/api.o src/b64.o src/dumper.o src/emitter.o src/loader.o src/lyaml.o src/parser.o src/reader.o src/scanner.o src/strtod.o src/writer.o
Updating manifest for /home/sandbox/lib/luarocks/rocks
yaml 1.1.2-1 is now built and installed in /home/sandbox (license: MIT)

```
# インストール結果
```bash
$ tree /home/sandbox/
/home/sandbox/
├── lib
│   ├── lua
│   │   └── 5.1
│   │       ├── lfs.so
│   │       └── yaml
│   │           └── core.so
│   └── luarocks
│       └── rocks
│           ├── luafilesystem
│           │   └── 1.6.3-2
│           │       ├── doc
│           │       │   └── us
│           │       │       ├── doc.css
│           │       │       ├── examples.html
│           │       │       ├── index.html
│           │       │       ├── license.html
│           │       │       ├── luafilesystem.png
│           │       │       └── manual.html
│           │       ├── luafilesystem-1.6.3-2.rockspec
│           │       ├── rock_manifest
│           │       └── tests
│           │           └── test.lua
│           ├── lub
│           │   └── 1.1.0-1
│           │       ├── doc
│           │       │   └── index.html
│           │       ├── lub-1.1.0-1.rockspec
│           │       └── rock_manifest
│           ├── manifest
│           └── yaml
│               └── 1.1.2-1
│                   ├── doc
│                   │   └── index.html
│                   ├── rock_manifest
│                   └── yaml-1.1.2-1.rockspec
└── share
    └── lua
        └── 5.1
            ├── lub
            │   ├── Autoload.lua
            │   ├── Dir.lua
            │   ├── init.lua
            │   ├── Param.lua
            │   └── Template.lua
            └── yaml
                └── init.lua
```

# 参考
- [Using LuaRocks to install packages in the current directory - http://leafo.net/](http://leafo.net/guides/customizing-the-luarocks-tree.html#the-install-locations/using-a-custom-directory)