Lua: Difference between revisions
Appearance
imported>Samueldr m Drop the spaces after `#!` they bug me |
Furanchuchu (talk | contribs) m Add newline to improve readability. |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 19: | Line 19: | ||
* nix-shell doesn't require its magic lines to be right after the first line. | * nix-shell doesn't require its magic lines to be right after the first line. | ||
</blockquote> | </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;
}