Skip to content

Commit 4eb2ad7

Browse files
committed
lib/lua: support nixpkg's "lua-inline" type
See #1935
1 parent dbf6f7b commit 4eb2ad7

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

lib/helpers.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ let
8787
inherit (helpers.extendedLib) maintainers;
8888

8989
toLuaObject = helpers.lua.toLua;
90+
mkLuaInline = helpers.lua.mkInline;
9091
};
9192
in
9293
helpers

lib/to-lua.nix

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ rec {
3232
# and contain only letters, digits, and underscores.
3333
isIdentifier = s: !(isKeyword s) && (builtins.match "[A-Za-z_][0-9A-Za-z_]*" s) == [ ];
3434

35+
# Alias for nixpkgs lib's `mkLuaInline`,
36+
# but can also convert rawLua to lua-inline
37+
mkInline = v: lib.generators.mkLuaInline (v.__raw or v);
38+
39+
# Whether the value is a lua-inline type
40+
isInline = v: v._type or null == "lua-inline";
41+
3542
# toLua' with default options, aliased as toLuaObject at the top-level
3643
toLua = toLua' { };
3744

@@ -141,6 +148,8 @@ rec {
141148
value
142149
else if allowRawValues && value ? __raw then
143150
value
151+
else if isInline value then
152+
value
144153
else if isDerivation value then
145154
value
146155
else if isList value then
@@ -227,8 +236,11 @@ rec {
227236
v.__pretty v.val
228237
# apply raw values if allowed
229238
else if allowRawValues && v ? __raw then
230-
# TODO: apply indentation to multiline raw values
239+
# TODO: deprecate in favour of inline-lua
231240
v.__raw
241+
else if isInline v then
242+
# TODO: apply indentation to multiline raw values?
243+
"(${v.expr})"
232244
else
233245
"{"
234246
+ introSpace

tests/lib-tests.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ let
6565
expected = "<lua code>";
6666
};
6767

68+
testToLuaObjectInlineLua = {
69+
expr = helpers.toLuaObject (lib.generators.mkLuaInline "<lua code>");
70+
expected = "(<lua code>)";
71+
};
72+
73+
testToLuaObjectInlineLuaNested = {
74+
expr = helpers.toLuaObject { lua = lib.generators.mkLuaInline "<lua code>"; };
75+
expected = "{ lua = (<lua code>) }";
76+
};
77+
6878
testToLuaObjectLuaTableMixingList = {
6979
expr = helpers.toLuaObject {
7080
"__unkeyed...." = "foo";

0 commit comments

Comments
 (0)