5

I am trying to set up a nix-shell script which contains lua 5.2, the lua sockets library, and a few other libraries. However, when I load the shell, only Lua ends up being present.

with import <nixpkgs> {}; stdenv.mkDerivation rec { name = "lua-env"; buildInputs = [ lua52Packages.lua lua52Packages.luasocket lua52Packages.luasec lua52Packages.cjson ]; } 

When I run the shell, this is what I get:

savanni@lapis:~ $ nix-shell lua.nix [nix-shell:~]$ lua Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio > require 'socket' stdin:1: module 'socket' not found: no field package.preload['socket'] no file '/usr/share/lua/5.2/socket.lua' no file '/usr/share/lua/5.2/socket/init.lua' no file '/usr/lib/lua/5.2/socket.lua' no file '/usr/lib/lua/5.2/socket/init.lua' no file './socket.lua' no file '/usr/lib/lua/5.2/socket.so' no file '/usr/lib/lua/5.2/loadall.so' no file './socket.so' stack traceback: [C]: in function 'require' stdin:1: in main chunk [C]: in ? > [nix-shell:~]$ exit savanni@lapis:~ $ 
3
  • Did you manage to get any progress on this? Commented Feb 19, 2017 at 0:27
  • No. I probably could, now, but I no longer work somewhere that lua is a thing and so haven't even tried. Would you like me to? Commented Feb 20, 2017 at 0:25
  • It would be helpful. I'm currently stumped with getting this to work. Commented Mar 16, 2017 at 10:43

2 Answers 2

5

You have to set LUA_PATH and LUA_CPATH

This shell.nix should work,

with import <nixpkgs> {}; with luaPackages; let libs = [lua cjson luasocket luasec]; in stdenv.mkDerivation rec { name = "lua-env"; buildInputs = libs; shellHook = '' export LUA_CPATH="${lib.concatStringsSep ";" (map getLuaCPath libs)}" export LUA_PATH="${lib.concatStringsSep ";" (map getLuaPath libs)}" ''; } 

To check that those paths are exported,

$ nix-shell --run 'echo $LUA_CPATH; echo $LUA_PATH' /nix/store/lp0ns0hjwx1klk6amnyic3f62bw7h8j7-lua-5.2.3/lib/lua/5.2/?.so;/nix/store/lp0ns0hjwx1klk6amnyic3f62bw7h8j7-lua-5.2.3/share/lua/5.2/?.so;/nix/store/ppdspfcm5nnfz0fk9zarjmpyv5lcmn18-lua5.2-cjson-2.1.0/lib/lua/5.2/?.so;/nix/store/ppdspfcm5nnfz0fk9zarjmpyv5lcmn18-lua5.2-cjson-2.1.0/share/lua/5.2/?.so;/nix/store/b71xyq7gkc8iccj6wr84zq31gqc3m9ix-lua5.2-socket-3.0-rc1/lib/lua/5.2/?.so;/nix/store/b71xyq7gkc8iccj6wr84zq31gqc3m9ix-lua5.2-socket-3.0-rc1/share/lua/5.2/?.so;/nix/store/6q8xn2bcfskmrfzql47jld5d2irvn5jr-lua5.2-sec-0.6/lib/lua/5.2/?.so;/nix/store/6q8xn2bcfskmrfzql47jld5d2irvn5jr-lua5.2-sec-0.6/share/lua/5.2/?.so /nix/store/lp0ns0hjwx1klk6amnyic3f62bw7h8j7-lua-5.2.3/lib/lua/5.2/?.lua;/nix/store/lp0ns0hjwx1klk6amnyic3f62bw7h8j7-lua-5.2.3/share/lua/5.2/?.lua;/nix/store/ppdspfcm5nnfz0fk9zarjmpyv5lcmn18-lua5.2-cjson-2.1.0/lib/lua/5.2/?.lua;/nix/store/ppdspfcm5nnfz0fk9zarjmpyv5lcmn18-lua5.2-cjson-2.1.0/share/lua/5.2/?.lua;/nix/store/b71xyq7gkc8iccj6wr84zq31gqc3m9ix-lua5.2-socket-3.0-rc1/lib/lua/5.2/?.lua;/nix/store/b71xyq7gkc8iccj6wr84zq31gqc3m9ix-lua5.2-socket-3.0-rc1/share/lua/5.2/?.lua;/nix/store/6q8xn2bcfskmrfzql47jld5d2irvn5jr-lua5.2-sec-0.6/lib/lua/5.2/?.lua;/nix/store/6q8xn2bcfskmrfzql47jld5d2irvn5jr-lua5.2-sec-0.6/share/lua/5.2/?.lua 
1
  • 1
    Apologies for the extremely long delay. This had stopped being relevant to me, and I clearly missed the notification until today. I just tried out the shell.nix that you wrote and it worked perfectly in an instant, though today I think we would use mkShell instead of mkDerivation. Commented Jan 9, 2020 at 20:24
0

There is a simpler way to do this in 2020:

nix-shell -p 'lua.withPackages(ps: [ps.luasocket ps.cjson])' 

This will put a modified lua derivation in your environment where the luasocket and cjson derivations from luaPackages are included in the LUA_PATH.

So you do not need to mess with LUA_PATH or shellHooks yourself.

EDIT:

You can use the lua.withPackages function in a nix file as well:

with import <nixpkgs> {}; stdenv.mkDerivation rec { name = "lua-env"; buildInputs = [ lua.withPackages(ps: with ps; [luasocket luasec cjson ]) ]; } 
2
  • These nix-shell commands, whilst short, really don't help the whole "declarative" and commitable nature of .nix files. Commented Mar 2, 2021 at 10:41
  • 1
    @Rebs I updated the answer on how to use this function declarative Commented Jul 1, 2021 at 10:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.