Jump to content

Lua: Difference between revisions

From NixOS Wiki
imported>Makefu
Created page with "== #! nix-shell with lua interpreter == To use a nix-shell shebang with lua you will need to use following structure: <syntaxHighlight lang=lua> #! /usr/bin/env nix-shell --[..."
 
m Add newline to improve readability.
 
(4 intermediate revisions by 3 users not shown)
Line 3: Line 3:


<syntaxHighlight lang=lua>
<syntaxHighlight lang=lua>
#! /usr/bin/env nix-shell
#!/usr/bin/env nix-shell
--[[
--[[
#! nix-shell -i lua -p lua
#!nix-shell -i lua -p lua
]]
]]


Line 11: Line 11:
</syntaxHighlight>
</syntaxHighlight>


Some background by [[User:Samueldr|samueldr]]:
Some background by [[User:Samueldr|samueldr]]<ref>https://reddit.com/r/NixOS/comments/q7ns3l/interpreter_with_lua/</ref>:
    Two inter-twined fun facts.
    *    Lua only skips one shebang line.
    *    nix-shell doesn't require its magic lines to be right after the first line.


<blockquote>
Two inter-twined fun facts.


* Reference: https://reddit.com/r/NixOS/comments/q7ns3l/interpreter_with_lua/
* Lua only skips one shebang line.
* nix-shell doesn't require its magic lines to be right after the first line.
</blockquote>
 
== Override a Lua package for all available Lua interpreters ==
 
The nixpkgs reference manual suggests using the following overlay template:
 
<syntaxhighlight lang="nix" line="1">
final: prev: {
  lua = prev.lua.override {
    packageOverrides = luafinal: luaprev: {
      luarocks-nix = luaprev.luarocks-nix.overrideAttrs (oa: {
        pname = "my-custom-name";
      });
    };
  };
 
  luaPackages = final.lua.pkgs;
}
</syntaxhighlight>
 
However, this approach only overrides the default Lua interpreter.
 
The following template applies the overlay for all available Lua interpreters in nixpkgs:
 
<syntaxhighlight lang="nix" line="1">
final: prev: let
  inherit (final.lib.attrsets) filterAttrs mapAttrs;
  isLua = pkg-name: pkg: pkg ? luaversion;
 
  # I can just add my modifications by using `mapAttrs` on `luaInterpreters`.
  # However, after doing this, calling `luaInterpreters.override` will undo my
  # modifications. This is due to the way `lib.makeOverridable` works. It
  # keeps a reference to the original function and calls it with modified
  # arguments.
  mkExtendedLuaInterpreters = {...} @ args: let
    # Support overrides
    interpreters = prev.luaInterpreters.override args;
  in
    mapAttrs (k: v:
      v.override {
        packageOverrides = luafinal: luaprev: {
          luarocks-nix = luaprev.luarocks-nix.overrideAttrs (oa: {
            pname = "my-custom-name";
          });
        };
      }) (filterAttrs isLua interpreters);
 
  extendedLuaInterpreters = final.lib.makeOverridable mkExtendedLuaInterpreters {};
in {
  # Override all available Lua interpreters
  luaInterpreters = extendedLuaInterpreters;
}
</syntaxhighlight>
 
== See Also ==
 
* [https://nixos.org/manual/nixpkgs/stable/#users-guide-to-lua-infrastructure Lua user guide in the nixpkgs manual]

Latest revision as of 21:29, 2 September 2025

#! nix-shell with lua interpreter

To use a nix-shell shebang with lua you will need to use following structure:

#!/usr/bin/env nix-shell
--[[
#!nix-shell -i lua -p lua
]]

print("this is from lua")

Some background by samueldr[1]:

Two inter-twined fun facts.

  • Lua only skips one shebang line.
  • nix-shell doesn't require its magic lines to be right after the first line.

Override a Lua package for all available Lua interpreters

The nixpkgs reference manual suggests using the following overlay template:

final: prev: {
  lua = prev.lua.override {
    packageOverrides = luafinal: luaprev: {
      luarocks-nix = luaprev.luarocks-nix.overrideAttrs (oa: {
        pname = "my-custom-name";
      });
    };
  };

  luaPackages = final.lua.pkgs;
}

However, this approach only overrides the default Lua interpreter.

The following template applies the overlay for all available Lua interpreters in nixpkgs:

final: prev: let
  inherit (final.lib.attrsets) filterAttrs mapAttrs;
  isLua = pkg-name: pkg: pkg ? luaversion;

  # I can just add my modifications by using `mapAttrs` on `luaInterpreters`.
  # However, after doing this, calling `luaInterpreters.override` will undo my
  # modifications. This is due to the way `lib.makeOverridable` works. It
  # keeps a reference to the original function and calls it with modified
  # arguments.
  mkExtendedLuaInterpreters = {...} @ args: let
    # Support overrides
    interpreters = prev.luaInterpreters.override args;
  in
    mapAttrs (k: v:
      v.override {
        packageOverrides = luafinal: luaprev: {
          luarocks-nix = luaprev.luarocks-nix.overrideAttrs (oa: {
            pname = "my-custom-name";
          });
        };
      }) (filterAttrs isLua interpreters);

  extendedLuaInterpreters = final.lib.makeOverridable mkExtendedLuaInterpreters {};
in {
  # Override all available Lua interpreters
  luaInterpreters = extendedLuaInterpreters;
}

See Also