Skip to content

lib/lua-types: init #3213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

MattSturgeon
Copy link
Member

@MattSturgeon MattSturgeon commented Apr 24, 2025

The types in lua-types are inspired by pkgs.formats, allowing for a more appropriate option type than types.anything.

The intention is that we will replace usage of types.anything in 90% of cases treewide with lua-types.anything (see #3208). Currently there are ~270 instances of anything treewide.

All types in lua-types support inline-lua and raw-lua.

lua-types.tableOf will also serve as a good place to implement merging lists defs and attrs defs into a mixed table, at some point in the future. Ideally we can upstream something similar to nixpkgs too. Again, see #3208 and also NixOS/nixpkgs@master...MattSturgeon:nixpkgs:table.

TODO

  • Move types.rawLua to lua-types
  • Use lua-types.anything in mkNvimPkugin's settings option
  • Use lua-types in defaultNullOpts (?)

An initial set of option types for representing values serialisable into
lua.
`defaultNullOpts` is generally used for representing lua-options.

We should probably a) rename it something like `luaOptions` and switch
out the primitive types to use the new `lua-types`.
Copy link
Member

@GaetanLepage GaetanLepage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall.
I have a question about your (ab)use of laziness. (I'm not saying 'abuse' in a negative way here).
Why not relying on lib.fix? Is this approach (importing it back in and changing it in place) preferable? Would lib.fix (or rec) work at all?

Comment on lines +18 to +21
anything = types.either lua-types.primitive (lua-types.tableOf lua-types.anything) // {
description = "lua value";
descriptionClass = "noun";
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure you thought about priorities, but I think adding parentheses would greatly help with readability.

@MattSturgeon
Copy link
Member Author

Why not relying on lib.fix? Is this approach (importing it back in and changing it in place) preferable? Would lib.fix (or rec) work at all?

I assume you mean the self-referencing recursive lua-types.anything?

As far as recursive laziness goes, both approaches are exactly the same. No matter how you write it, all self-referencing values do the same thing...

I originally wrote it with "local" recursion using lib.fix, but I found that clunkier due to the extra syntax.

lib.fix (self: types.oneOf [
  # ...
  (tableOf self)
])
// {
  description = "lua value";
  # ...
}

Then I realised that was unnecessary; we don't need lib.fix to get a self reference because we already have a lua-types reference in scope.

@GaetanLepage
Copy link
Member

Ok this makes sense. I was wondering if there was any fundamental difference.
All good on my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants