5.4 KiB
nix2lua
This is a small but functional library that converts your nix configurations into Lua format.
This library was initially designed for my personal Neovim flake.
Installation
Add nix2lua
as input to your flake.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;
}
References
toLua expr
Returns a string containing Lua representation of
expr
. Strings, integers, floats, boolean, lists and sets are mapped to their Lua equivalents.Null will be skipped. This is useful when you want to use an optional value. To render
nil
you should use theLuaNil
function.toLua { foo = "bar"; } # { foo = "bar" } toLua [ 10 "foo" [ "bar" ] ] #`{ 10, "foo", { "bar" } }
LuaNil
Creates a type that will be mapped by the
toLua
asnil
toLua LuaNil # nil
raw expr
Creates a type that instructs
toLua
not to change the passed expressionexpr
.toLua (raw "require('bar').baz") # require('bar').baz
namedField name expr
(alias: nf
)
Creates a type that represents a named field in the Lua table. This type cannot exist outside a list or set.
This is useful to create table with some named fields.
toLua [ "foo" (namedField "bar" "baz") ] # { "foo", bar = "baz" }
join sep expr
Join expressions with a separator
toLua (join "=" ["vim.opt.line" true]) # vim.opt.line = true
concat expr
Joins expressions with an empty string separator
This is alias for
join ""
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.
toLua (pipe ["vim" "opt" "line"]); # vim.opt.line
toLua (pipe ["luasnip" (call0 "setup")]); # luasnip.setup()
pipe1 lhs rhs
Useful alias for pipe that allow only two arguments.
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 " "
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.toLua (call "setup" [ "foo" "bar" ]); # setup("foo", "bar")
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 []
.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]
.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
.toLua (require "luasnip") # require("luasnip")
op operation lhs rhs
Make Lua basic operation.
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; not = expr: spaceBetween [ kw_not expr ];
set varName expr
Set an expression to a Lua variable.
toLua (set "vim.opt.number" true); # vim.opt.number = true
Useful aliases:
lset = l: r: local (set l r);
func fnName params body
Define a function with specific name, parameters and body.
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);
lambda params body
Define a lambda function with specific parameters and body. Useful to use Lua function inline.
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.
toLua (local (set "luasnip" (require "luasnip"))); # local set luasnip = require("luasnip")
toLua (local (func "foo" ["bar"] (pipe1 "bar" (call0 "baz"))); # local function foo(bar) bar.baz() end
License
GNU General Public License v3.0 or later
See COPYING to see the full text.