Last Updated: February 25, 2016
·
3.397K
· losinggeneration

Append to Lua's search paths from the command line

I tend to put packages in my home directory rather than system wide. Specifically, LuaRocks I tend to prefer --local to make cleanup easier if ever needed. This leads to things like needing the luarocks.loader. One trick I decided to do is just fix up my calls to lua from my shell. The method I've chosen is to define lua as a shell (tested in BASH and ZSH) function that appends to the LUA_PATH & LUA_CPATH and then calls the lua command.

function lua()
{
    # Get Lua's version
    local lua_version=$(command lua -e "print(string.sub(_VERSION, -3))")
    # Append to Lua's paths using normal shell variable interpolation
    LUA_PATH=$(command lua -e "print(package.path..';$HOME/.luarocks/share/lua/$lua_version/?.lua')") \
    LUA_CPATH=$(command lua -e "print(package.cpath..';$HOME/.luarocks/lib/lua/$lua_version/?.so')") \
    command lua $*
}

For me this is fairly ideal, it doesn't however apply to commands not run from a shell.