Lua: Difference between revisions
Appearance
Furanchuchu (talk | contribs) Add an overlay template that gets applied to all Lua interpreters. |
m added Category:Languages |
||
| (One intermediate revision by one other user not shown) | |||
| Line 64: | Line 64: | ||
}; | }; | ||
}) (filterAttrs isLua interpreters); | }) (filterAttrs isLua interpreters); | ||
extendedLuaInterpreters = final.lib.makeOverridable mkExtendedLuaInterpreters {}; | extendedLuaInterpreters = final.lib.makeOverridable mkExtendedLuaInterpreters {}; | ||
in { | in { | ||
| Line 74: | Line 75: | ||
* [https://nixos.org/manual/nixpkgs/stable/#users-guide-to-lua-infrastructure Lua user guide in the nixpkgs manual] | * [https://nixos.org/manual/nixpkgs/stable/#users-guide-to-lua-infrastructure Lua user guide in the nixpkgs manual] | ||
[[Category:Languages]] | |||
Latest revision as of 21:44, 20 May 2026
#! 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;
}