Skip to content

Commit

Permalink
refactor: use lib.generators.toLua (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekowinston authored Mar 12, 2024
1 parent 3384f3c commit 286fcc8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 63 deletions.
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 2 additions & 26 deletions modules/lib.nix
Original file line number Diff line number Diff line change
@@ -1,36 +1,12 @@
{lib}: let
inherit (builtins) isFunction typeOf;
inherit (lib) concatStringsSep concatMapStringsSep isDerivation mapAttrsToList mkOption types;
in rec {
inherit (lib) mkOption types;
in {
mkLuaOption = type:
mkOption {
type = types.nullOr (types.oneOf [type (types.functionTo type)]);
default = null;
};

toLuaHashMap = v: "{ ${concatStringsSep "," (mapAttrsToList (name: value: ''["${name}"] = ${toLua value}'') v)} }";

toLuaArray = v: "{ ${concatMapStringsSep "," toLua v} }";

# TODO: Surely there is a better way?
toLua = v:
if v == null
then "nil"
else if (typeOf v) == "string"
then ''"${v}"''
else if (typeOf v) == "bool"
then
if v
then "true"
else "false"
else if (typeOf v) == "set"
then toLuaHashMap v
else if (typeOf v) == "list"
then toLuaArray v
else if isFunction v
then v {}
else toString v;

# TODO: Maybe?
# usage: neovim-lib.importPluginFromSpec ./plugins.nix {inherit ...};
importPluginFromSpec = path: attrs: let
Expand Down
38 changes: 7 additions & 31 deletions modules/plugins/default.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
{
lib,
flake-parts-lib,
neovim-lib,
...
}:
with lib; let
inherit (builtins) typeOf;
inherit (flake-parts-lib) mkPerSystemOption;
inherit (neovim-lib) toLua;

pluginSpec = with types; {
options = {
src = mkOption {
Expand Down Expand Up @@ -156,34 +152,14 @@ in {
in
attrValues deps;
}
// optionalAttrs (isDerivation attrs.init || typeOf attrs.init == "path") {
init = _: ''
dofile "${attrs.init}"
'';
// optionalAttrs (isDerivation attrs.init || isPath attrs.init) {
init = lib.generators.mkLuaInline ''dofile "${attrs.init}"'';
}
# // optionalAttrs (typeOf attrs.init == "path") {
# TODO: This is better, but... stack overflow
# init = pkgs.writeTextFile {
# name = "${name}-init.lua";
# text = ''
# dofile "${attrs.init}"
# '';
# };
# }
// optionalAttrs (typeOf attrs.config == "bool") {
// optionalAttrs (isBool attrs.config) {
inherit (attrs) config;
}
// optionalAttrs (isDerivation attrs.config || typeOf attrs.config == "path") {
# TODO: This is better, but... stack overflow
# config = pkgs.writeTextFile {
# name = "${name}-config.lua";
# text = ''
# dofile "${attrs.config}"
# '';
# };
config = _: ''
dofile "${attrs.config}"
'';
// optionalAttrs (isDerivation attrs.config || isPath attrs.config) {
config = lib.generators.mkLuaInline ''dofile "${attrs.config}"'';
}
// optionalAttrs (builtins.isAttrs attrs.config) {
config = true;
Expand All @@ -194,8 +170,8 @@ in {
// optionalAttrs (attrs.keys != null) {inherit (attrs) keys;}
// optionalAttrs (attrs.priority != null) {inherit (attrs) priority;};

spec = toLua (mapAttrsToList toPlugin' cfg.plugins);
opts = toLua ({performance.rtp.reset = false;} // cfg.settings);
spec = lib.generators.toLua {} (mapAttrsToList toPlugin' cfg.plugins);
opts = lib.generators.toLua {} ({performance.rtp.reset = false;} // cfg.settings);
in {
inherit spec opts;
};
Expand Down

0 comments on commit 286fcc8

Please sign in to comment.