nix2lua/README.md

325 lines
6 KiB
Markdown
Raw Normal View History

2022-11-18 00:17:19 +03:00
# nix2lua
2022-11-19 04:41:28 +03:00
This is a small but functional library that converts your nix configurations
2024-03-13 00:10:08 +03:00
into Lua format.
2022-11-19 04:41:28 +03:00
This library was initially designed for my personal
2024-03-13 00:10:08 +03:00
[Neovim flake](https://git.pleshevski.ru/mynix/neovim).
2022-11-19 04:41:28 +03:00
2022-11-19 04:46:37 +03:00
# Installation
2022-11-19 04:41:28 +03:00
2024-03-13 00:10:08 +03:00
Add `nix2lua` as input to your `flake.nix`
2022-11-19 04:41:28 +03:00
```nix
{
inputs.nix2lua.url = "git+https://git.pleshevski.ru/mynix/nix2lua";
outputs = { nix2lua }:
let
luaTable = nix2lua.lib.toLua {
foo = "bar";
nvimTree.settings = {
open_on_setup = true;
renderer = {
group_empty = true;
full_name = true;
};
};
};
in luaTable;
}
```
2022-11-19 04:46:37 +03:00
# References
2022-11-19 04:41:28 +03:00
`toLua expr`
> Returns a string containing Lua representation of `expr`. Strings, integers,
2024-03-13 00:10:08 +03:00
> floats, boolean, lists and sets are mapped to their Lua equivalents.
2022-11-19 04:41:28 +03:00
>
> Null will be skipped. This is useful when you want to use an optional value.
2024-03-13 00:10:08 +03:00
> To render `nil` you should use the `LuaNil` function.
2022-11-19 04:41:28 +03:00
>
> ```nix
2022-11-19 05:03:59 +03:00
> toLua { foo = "bar"; }
2024-05-17 18:10:01 +03:00
> # { foo = "bar" }
2022-11-19 05:03:59 +03:00
> toLua [ 10 "foo" [ "bar" ] ]
2024-05-17 18:10:01 +03:00
> #`{ 10, "foo", { "bar" } }
2022-11-19 04:41:28 +03:00
> ```
2024-03-13 00:10:08 +03:00
`LuaNil`
2022-11-19 04:41:28 +03:00
2024-03-13 00:10:08 +03:00
> Creates a type that will be mapped by the `toLua` as `nil`
2022-11-19 04:41:28 +03:00
>
> ```nix
2024-03-13 00:10:08 +03:00
> toLua LuaNil
2024-05-17 18:10:01 +03:00
> # nil
2022-11-19 04:41:28 +03:00
> ```
2024-05-17 18:10:01 +03:00
`raw expr`
2022-11-19 04:41:28 +03:00
> Creates a type that instructs `toLua` not to change the passed expression
> `expr`.
>
> ```nix
2024-05-17 18:10:01 +03:00
> toLua (raw "require('bar').baz")
> # require('bar').baz
2022-11-19 04:41:28 +03:00
> ```
2024-05-17 18:10:01 +03:00
`namedField name expr` (alias: `nf`)
2022-11-19 04:41:28 +03:00
2024-03-13 00:10:08 +03:00
> Creates a type that represents a named field in the Lua table. This type
> cannot exist outside a list or set.
2022-11-19 04:41:28 +03:00
>
2024-03-13 00:10:08 +03:00
> This is useful to create table with some named fields.
2022-11-19 04:41:28 +03:00
>
> ```nix
> toLua [
> "foo"
2024-05-17 18:10:01 +03:00
> (namedField "bar" "baz")
2022-11-19 05:03:59 +03:00
> ]
2024-05-17 18:10:01 +03:00
> # { "foo", bar = "baz" }
> ```
`join sep expr`
> Join expressions with a separator
>
> ```nix
> toLua (join "=" ["vim.opt.line" true])
> # vim.opt.line = true
> ```
`concat expr`
> Joins expressions with an empty string separator
>
> This is alias for `join ""`
>
> ```nix
> toLua (concat ["hello" "world"])
> # helloworld
> ```
`pipe expr`
> Joins expressions with a '.' (dot) separator.
> If list of expressions contains a string it will
> convert to the raw.
>
> ```nix
> toLua (pipe ["vim" "opt" "line"]);
> # vim.opt.line
> ```
> ```nix
> toLua (pipe ["luasnip" (call0 "setup")]);
> # luasnip.setup()
> ```
`pipe1 lhs rhs`
> Useful alias for pipe that allow only two arguments.
>
> ```nix
> toLua (pipe1 "luasnip" (call0 "setup"));
> # luasnip.setup()
> ```
`spaceBetween expr`
> Join expressions with a space separator. You can use it to separate
> all expressions in the Lua configuration.
>
> This is alias for `join " "`
>
> ```nix
> toLua (spaceBetween [
> (set "vim.opt.number" true)
> (set "vim.opt.relatednumber" true)
> ])
> # vim.opt.number = true vim.opt.relatednumber = true
> ```
`call fnName args`
> Util to call a Lua function with arguments.
>
> If `args` is list that each list item render as function argument.
> Otherwise, it will be rendered as first argument.
>
> ```nix
> toLua (call "setup" [ "foo" "bar" ]);
> # setup("foo", "bar")
> ```
> ```nix
> toLua (call "setup" { foo = "bar" });
> # or: toLua (call "setup" [{ foo = "bar" }]);
> # setup({ foo = "bar" })
> ```
`call0 fnName`
> Call a Lua function without arguments.
>
> This is alias for `call fnName []`.
>
> ```nix
> toLua (call0 "setup")
> # setup()
> ```
`call1 fnName arg`
> Call a Lua function with one argument.
> This is useful when a Lua function has only one argument of type list,
> and we want to avoid using an inner list.
>
> This is alias for `call fnName [arg]`.
>
> ```nix
> toLua (call1 "setup" [ "foo" "bar" ])
> # setup({ "foo", "bar" })
> ```
`require name`
> Call a require function to import module with `name`.
>
> This is alias for `call "require" name`.
> ```nix
> toLua (require "luasnip")
> # require("luasnip")
> ```
`op operation lhs rhs`
> Make Lua basic operation.
>
> ```nix
> toLua (op "+" 1 2)
> # (1 + 2)
> ```
>
> There are all aliases for basic Lua operations:
>
> ```
> add = op "+";
> sub = op "-";
> mul = op "*";
> div = op "/";
> mod = op "%";
> exp = op "^";
>
> eq = op "==";
> ne = op "~=";
> gt = op ">";
> lt = op "<";
> gte = op ">=";
> lte = op "<=";
>
> and = op kw_and;
> or' = op kw_or;
2024-05-17 18:10:01 +03:00
> not = expr: spaceBetween [ kw_not expr ];
> ```
`set varName expr`
> Set an expression to a Lua variable.
>
> ```nix
> toLua (set "vim.opt.number" true);
> # vim.opt.number = true
> ```
>
> Useful aliases:
> ```
> lset = l: r: local (set l r);
> ```
2024-05-17 18:10:01 +03:00
`func fnName params body`
> Define a function with specific name, parameters and body.
>
> ```nix
> toLua (func "foo" ["bar"] (pipe1 "bar" (call0 "baz"));
> # function foo(bar) bar.baz() end
> ```
>
> Useful aliases:
> ```
> func0 = fnName: func fnName [ ];
>
> lfunc = n: p: b: local (func n p b);
> lfunc0 = n: b: local (func0 n b);
2024-05-17 18:10:01 +03:00
> ```
`lambda params body`
> Define a lambda function with specific parameters and body.
> Useful to use Lua function inline.
>
> ```nix
> toLua (call "setup" {
> on_attach = lambda ["baz"] (pipe1 "baz" (call0 "bar"))
> });
> # setup { on_attach = function (baz) baz.bar() end }
> ```
>
> Useful aliases:
> ```
> lambda0 = lambda [ ];
> ```
`local expr`
> A modifier to define a variable or a function in local scope.
>
> ```nix
> toLua (local (set "luasnip" (require "luasnip")));
> # local set luasnip = require("luasnip")
> ```
> ```nix
> toLua (local (func "foo" ["bar"] (pipe1 "bar" (call0 "baz")));
> # local function foo(bar) bar.baz() end
2022-11-19 04:41:28 +03:00
> ```
2022-11-19 04:46:37 +03:00
2024-05-17 18:45:29 +03:00
`return expr`
> Return expression from a function.
>
> ```nix
> toLua (func "foo" ["bar"] (return (pipe1 "bar" "baz"));
> # function foo(bar) return bar.baz end
> ```
>
> Useful aliases:
> ```
> return_void = return null;
> ```
`ifelse condition trueBody falseBody`
> Make a Lua if else statement.
>
> ```nix
> toLua (ifelse (eq 10 10) (call "print" "yes") (call "print" "no"));
> # if (10 == 10) print("yes") else print("no") end
> ```
`if' condition trueBody`
> Make a Lua if statement without else.
>
> ```nix
> toLua (if' (eq 10 10) (call "print" "yes"));
> # if (10 == 10) print("yes") end
> ```
2022-11-19 04:46:37 +03:00
# License
GNU General Public License v3.0 or later
See [COPYING](./COPYING) to see the full text.