Using lua modules

Hello,

I have a lua seutp with a flake.nix that looks like this:

{
  description = "A very basic flake";

  inputs = {
    nixpkgs.url = github:NixOS/nixpkgs;
    utils.url = github:numtide/flake-utils;
  };

  outputs = { self, nixpkgs, utils }: utils.lib.eachDefaultSystem(system:
  let
    pkgs = nixpkgs.legacyPackages.${system};
    lib = pkgs.lib;
    lua = pkgs.lua5_3.withPackages(ps: with ps; [
      cjson
      http
    ]);
  in {
    devShells.default = pkgs.mkShell rec {
      buildInputs = [
        lua
      ];
    };
  });
}

I assumed that both the lua-http as well as the cjson would be available to call from within lua code. Trying to verify this in a REPL however throws an error (the example is copied from the lua-http docs):

Lua 5.3.6  Copyright (C) 1994-2020 Lua.org, PUC-Rio
> local http_request = require "http.request"
> local headers, stream = assert(http_request.new_from_uri("http://example.com"):go())
stdin:1: attempt to index a nil value (global 'http_request')
stack traceback:
	stdin:1: in main chunk
	[C]: in ?
> local body = assert(stream:get_body_as_string())
stdin:1: attempt to index a nil value (global 'stream')
stack traceback:
	stdin:1: in main chunk
	[C]: in ?
> if headers:get ":status" ~= "200" then
>>     error(body)
>> end
stdin:1: attempt to index a nil value (global 'headers')
stack traceback:
	stdin:1: in main chunk
	[C]: in ?
> print(body)
nil

The behavior when requiring cjson is the same, and the same thing happens across different versions of the lua package as well as the luajit packages. The modules seem to be installed fine:

which lua 
# /nix/store/f392f68bh95j6mz8hz2lx91jd6sf6cfh-lua-5.3.6-env/bin/lua
fd http /nix/store/f392f68bh95j6mz8hz2lx91jd6sf6cfh-lua-5.3.6-env/
# /nix/store/f392f68bh95j6mz8hz2lx91jd6sf6cfh-lua-5.3.6-env/http-0.3-0-rocks
# /nix/store/f392f68bh95j6mz8hz2lx91jd6sf6cfh-lua-5.3.6-env/share/lua/5.3/http
fd cjson /nix/store/f392f68bh95j6mz8hz2lx91jd6sf6cfh-lua-5.3.6-env/
# /nix/store/f392f68bh95j6mz8hz2lx91jd6sf6cfh-lua-5.3.6-env/lib/lua/5.3/cjson.so
# /nix/store/f392f68bh95j6mz8hz2lx91jd6sf6cfh-lua-5.3.6-env/lua-cjson-2.1.0.6-1-rocks
# /nix/store/f392f68bh95j6mz8hz2lx91jd6sf6cfh-lua-5.3.6-env/share/lua/5.3/cjson

What am I missing?

It’s possibly broken see harmonize lua wrapping across nixpkgs · Issue #169229 · NixOS/nixpkgs · GitHub lua: test the interpreter by teto · Pull Request #177556 · NixOS/nixpkgs · GitHub . Any help welcome

What could help look like? I don’t have any experience with Lua, I just wanted to try it out, but happy to help if I can

1 Like

to debug your issue you can look at your lua less $(readlink -f $(which lua)) to see how it wraps LUA_PATH.
If LUA_PATH is not correct then you can look at https://github.com/NixOS/nixpkgs/blob/1829c5b002d128c9c94f43d8b11c0added3863eb/pkgs/development/interpreters/lua-5/wrapper.nix to debug the issue (in that folder or the files touched by the PRs I mentioned in my previous post).

would you mind creating a ticket with the content of your first post ? to help track it etc

I created the issue in Lua modules are broken · Issue #179180 · NixOS/nixpkgs · GitHub and linked back to your other issue / PR and added some additional info about how LUA_PATH is set up.

1 Like