Lua: Difference between revisions

imported>Samueldr
m Drop the spaces after `#!` they bug me
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]