1

Add toLuaObject generator from nixvim

This commit is contained in:
2024-06-01 13:59:34 +02:00
parent 1155053c8f
commit 95e88cbea7
2 changed files with 50 additions and 0 deletions

View File

@ -9,4 +9,5 @@
networking = import ./networking.nix {inherit inputs pkgs lib;};
virtualisation = import ./virtualisation.nix {inherit inputs pkgs lib;};
rofi = import ./rofi.nix {inherit inputs pkgs lib;};
generators = import ./generators.nix {inherit inputs pkgs lib;};
}

49
lib/generators.nix Normal file
View File

@ -0,0 +1,49 @@
{
inputs,
pkgs,
lib,
...
}: rec {
# NOTE: Taken from NixVim https://github.com/nix-community/nixvim/blob/main/lib/to-lua.nix
toLuaObject = args:
if builtins.isAttrs args
then
if lib.hasAttr "__raw" args
then args.__raw
else if lib.hasAttr "__empty" args
then "{ }"
else
"{"
+ (lib.concatStringsSep "," (
lib.mapAttrsToList (
n: v: let
valueString = toLuaObject v;
in
if lib.hasPrefix "__unkeyed" n
then valueString
else if lib.hasPrefix "__rawKey__" n
then ''[${lib.removePrefix "__rawKey__" n}] = '' + valueString
else if n == "__emptyString"
then "[''] = " + valueString
else "[${toLuaObject n}] = " + valueString
) (lib.filterAttrs (n: v: v != null && (toLuaObject v != "{}")) args)
))
+ "}"
else if builtins.isList args
then "{" + lib.concatMapStringsSep "," toLuaObject args + "}"
else if builtins.isString args
then
# This should be enough!
builtins.toJSON args
else if builtins.isPath args
then builtins.toJSON (toString args)
else if builtins.isBool args
then "${lib.boolToString args}"
else if builtins.isFloat args
then "${toString args}"
else if builtins.isInt args
then "${toString args}"
else if (args == null)
then "nil"
else "";
}