diff --git a/lib/helpers.nix b/lib/helpers.nix index 5e35ad67..9c920d63 100644 --- a/lib/helpers.nix +++ b/lib/helpers.nix @@ -87,6 +87,7 @@ let inherit (helpers.extendedLib) maintainers; toLuaObject = helpers.lua.toLua; + mkLuaInline = helpers.lua.mkInline; }; in helpers diff --git a/lib/to-lua.nix b/lib/to-lua.nix index 05c2ab45..002c7dcd 100644 --- a/lib/to-lua.nix +++ b/lib/to-lua.nix @@ -32,6 +32,13 @@ rec { # and contain only letters, digits, and underscores. isIdentifier = s: !(isKeyword s) && (builtins.match "[A-Za-z_][0-9A-Za-z_]*" s) == [ ]; + # Alias for nixpkgs lib's `mkLuaInline`, + # but can also convert rawLua to lua-inline + mkInline = v: lib.generators.mkLuaInline (v.__raw or v); + + # Whether the value is a lua-inline type + isInline = v: v._type or null == "lua-inline"; + # toLua' with default options, aliased as toLuaObject at the top-level toLua = toLua' { }; @@ -141,6 +148,8 @@ rec { value else if allowRawValues && value ? __raw then value + else if isInline value then + value else if isDerivation value then value else if isList value then @@ -227,8 +236,11 @@ rec { v.__pretty v.val # apply raw values if allowed else if allowRawValues && v ? __raw then - # TODO: apply indentation to multiline raw values + # TODO: deprecate in favour of inline-lua v.__raw + else if isInline v then + # TODO: apply indentation to multiline raw values? + "(${v.expr})" else "{" + introSpace diff --git a/tests/lib-tests.nix b/tests/lib-tests.nix index dc9fcae4..fde833c2 100644 --- a/tests/lib-tests.nix +++ b/tests/lib-tests.nix @@ -65,6 +65,16 @@ let expected = ""; }; + testToLuaObjectInlineLua = { + expr = helpers.toLuaObject (lib.generators.mkLuaInline ""); + expected = "()"; + }; + + testToLuaObjectInlineLuaNested = { + expr = helpers.toLuaObject { lua = lib.generators.mkLuaInline ""; }; + expected = "{ lua = () }"; + }; + testToLuaObjectLuaTableMixingList = { expr = helpers.toLuaObject { "__unkeyed...." = "foo";