mirror of
https://github.com/NotAShelf/nvf.git
synced 2025-09-06 10:21:31 +00:00
docs/hacking: describe toLuaObject
syntax for mixed tables
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I6a6a696449aab94c06827ea4b1d6e6042cc97ee6
This commit is contained in:
parent
0383311826
commit
7f3fe5caa5
1 changed files with 22 additions and 4 deletions
|
@ -165,15 +165,33 @@ own fields!
|
||||||
|
|
||||||
## Details of toLuaObject {#sec-details-of-toluaobject}
|
## Details of toLuaObject {#sec-details-of-toluaobject}
|
||||||
|
|
||||||
As you've seen above, `toLuaObject` is used to convert our nix attrSet
|
As you've seen above, `toLuaObject` is used to convert our `cfg.setupOpts`, a
|
||||||
`cfg.setupOpts`, into a lua table. Here are some rules of the conversion:
|
Nix attribute set, into Lua tables across the codebase. Here are some rules of
|
||||||
|
the conversion:
|
||||||
|
|
||||||
1. Nix `null` converts to lua `nil`
|
1. Nix `null` converts to Lua `nil`
|
||||||
2. Number and strings convert to their lua counterparts
|
- `foo = null;` -> `foo = nil`
|
||||||
|
2. Number and strings convert to their Lua counterparts
|
||||||
3. Nix attribute sets (`{}`) and lists (`[]`) convert into Lua dictionaries and
|
3. Nix attribute sets (`{}`) and lists (`[]`) convert into Lua dictionaries and
|
||||||
tables respectively. Here is an example of Nix -> Lua conversion.
|
tables respectively. Here is an example of Nix -> Lua conversion.
|
||||||
- `{foo = "bar"}` -> `{["foo"] = "bar"}`
|
- `{foo = "bar"}` -> `{["foo"] = "bar"}`
|
||||||
- `["foo" "bar"]` -> `{"foo", "bar"}`
|
- `["foo" "bar"]` -> `{"foo", "bar"}`
|
||||||
|
- You may also write **mixed tables** using `toLuaObject`, using a special
|
||||||
|
syntax to describe a key's position in the table. Let's say you want to get
|
||||||
|
something like `{"foo", bar = "baz"}` expressed in Lua using Nix. The
|
||||||
|
appropriate Nix syntax to express mixed tables is as follows:
|
||||||
|
|
||||||
|
```nix
|
||||||
|
# Notice the position indicator, "@1"
|
||||||
|
{ "@1" = "foo"; bar = "baz"; };
|
||||||
|
```
|
||||||
|
|
||||||
|
This will result in a mixed Lua table that is as follows:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
{"foo", bar = "baz"}
|
||||||
|
```
|
||||||
|
|
||||||
4. You can write raw Lua code using `lib.generators.mkLuaInline`. This function
|
4. You can write raw Lua code using `lib.generators.mkLuaInline`. This function
|
||||||
is part of nixpkgs, and is accessible without relying on **nvf**'s extended
|
is part of nixpkgs, and is accessible without relying on **nvf**'s extended
|
||||||
library.
|
library.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue