如何为 lua 设置 nix-shell 脚本?

如何为 lua 设置 nix-shell 脚本?

我正在尝试设置一个包含 lua 5.2、lua 套接字库和一些其他库的 nix-shell 脚本。但是,当我加载 shell 时,最终只显示 Lua。

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

当我运行 shell 时,我得到的结果如下:

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:~  $ 

答案1

你必须设置LUA_PATHLUA_CPATH

shell.nix应该可行,

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)}"
  '';
}

要检查这些路径是否已导出,

$ 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

答案2

在 2020 年有一种更简单的方法可以做到这一点:

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

这会将修改后的lua派生项放入您的环境中,其中包括luasocketcjson派生项。luaPackagesLUA_PATH

所以你没必要去惹LUA_PATHshellHooks自己。

编辑:

lua.withPackages您也可以在 nix 文件中使用该函数:

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

相关内容