Skip to content

Commit

Permalink
refactor: major plugins cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelroses committed Aug 4, 2024
1 parent 760c380 commit 42235c9
Showing 1 changed file with 105 additions and 113 deletions.
218 changes: 105 additions & 113 deletions modules/neovim.nix
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,6 @@ in
type = attrsOf (submodule pluginSpec);
default = { };
};

corePlugins = mkOption {
type = attrsOf (submodule pluginSpec);
default =
builtins.removeAttrs
(builtins.mapAttrs (_: val: { inherit (val) src; }) (
pkgs.callPackage ../_sources/generated.nix { }
))
[
"override"
"overrideDerivation"
"chaivim"
];
};
};

build = {
Expand All @@ -202,119 +188,125 @@ in
};
};

config =
config.neovim =
let
allPlugins = cfg.plugins // cfg.corePlugins;
mapPluginsRec =
fn:
mapAttrsToList (name: attrs: (fn name attrs) ++ (mapAttrsToList fn attrs.dependencies)) cfg.plugins;
in
{
neovim =
# you can override these but they are the defaults
chaivim.plugins =
builtins.removeAttrs
(builtins.mapAttrs (_: val: { inherit (val) src; }) (
pkgs.callPackage ../_sources/generated.nix { }
))
[
"override"
"overrideDerivation"
"chaivim"
];

build =
let
mapPluginsRec =
fn:
mapAttrsToList (name: attrs: (fn name attrs) ++ (mapAttrsToList fn attrs.dependencies)) allPlugins;
inherit (config.neovim) build;
inherit (pkgs.vimUtils) buildVimPlugin;

mkPlugin =
name: attrs:
if attrs.package != null then
attrs.package
else
buildVimPlugin {
inherit name;
inherit (attrs) src;
leaveDotGit = true; # So some lazy features (commands) work properly
};
in
{
build =
chaivim =
let
inherit (config.neovim) build;
inherit (pkgs.vimUtils) buildVimPlugin;

mkPlugin =
toPlugin' =
name: attrs:
if attrs.package != null then
attrs.package
else
buildVimPlugin {
inherit name;
inherit (attrs) src;
leaveDotGit = true; # So some lazy features (commands) work properly
};
in
{
chaivim =
let
toPlugin' =
name: attrs:
let
package = mkPlugin name attrs;
in
{
inherit name;
dir = "${package}";
url = "a required field that isn't imporant to set with nix";
}
// optionalAttrs (isBool attrs.enabled) { inherit (attrs) enabled; }
// optionalAttrs (isString attrs.enabled) { enabled = lib.generators.mkLuaInline attrs.enabled; }
// optionalAttrs (isBool attrs.dev) { inherit (attrs) dev; }
// optionalAttrs (attrs.lazy != null) { inherit (attrs) lazy; }
// optionalAttrs (attrs.dependencies != { }) {
dependencies =
let
deps = mapAttrs toPlugin' attrs.dependencies;
in
attrValues deps;
}
// optionalAttrs (isDerivation attrs.init || isPath attrs.init) {
init = lib.generators.mkLuaInline ''dofile "${attrs.init}"'';
}
// optionalAttrs (isString attrs.init) { init = lib.generators.mkLuaInline attrs.init; }
// optionalAttrs (isBool attrs.config) { inherit (attrs) config; }
// optionalAttrs (isString attrs.config) { config = lib.generators.mkLuaInline attrs.config; }
// optionalAttrs (isDerivation attrs.config || isPath attrs.config) {
config = lib.generators.mkLuaInline ''dofile "${attrs.config}"'';
}
// optionalAttrs (isAttrs attrs.config) {
config = true;
opts = attrs.config;
}
// optionalAttrs (attrs.event != null) { inherit (attrs) event; }
// optionalAttrs (attrs.cmd != null) { inherit (attrs) cmd; }
// optionalAttrs (attrs.ft != null) { inherit (attrs) ft; }
// optionalAttrs (attrs.main != null) { inherit (attrs) main; }
// optionalAttrs (attrs.priority != null) { inherit (attrs) priority; };
package = mkPlugin name attrs;
in
{
config = toLua { } ({ inputs = mapAttrsToList toPlugin' allPlugins; } // cfg.config);
modules = toLua { } cfg.modules;
};

plugins =
pkgs.runCommand "plugins.lua"
{
nativeBuildInputs = with pkgs; [ stylua ];
passAsFile = [ "text" ];
preferLocalBuild = true;
allowSubstitutes = false;
text = ''
-- Generated by Nix (github:isabelroses/chainix)
vim.opt.rtp:prepend "${cfg.package}"
require("ch").setup(${build.chaivim.config}, ${build.chaivim.modules})
'';
}
''
target=$out
mkdir -p "$(dirname "$target")"
if [ -e "$textPath" ]; then
mv "$textPath" "$target"
else
echo -n "$text" > "$target"
fi
stylua --config-path ${../stylua.toml} $target
'';
inherit name;
dir = "${package}";
url = "a required field that isn't imporant to set with nix";
}
// optionalAttrs (isBool attrs.enabled) { inherit (attrs) enabled; }
// optionalAttrs (isString attrs.enabled) { enabled = lib.generators.mkLuaInline attrs.enabled; }
// optionalAttrs (isBool attrs.dev) { inherit (attrs) dev; }
// optionalAttrs (attrs.lazy != null) { inherit (attrs) lazy; }
// optionalAttrs (attrs.dependencies != { }) {
dependencies =
let
deps = mapAttrs toPlugin' attrs.dependencies;
in
attrValues deps;
}
// optionalAttrs (isDerivation attrs.init || isPath attrs.init) {
init = lib.generators.mkLuaInline ''dofile "${attrs.init}"'';
}
// optionalAttrs (isString attrs.init) { init = lib.generators.mkLuaInline attrs.init; }
// optionalAttrs (isBool attrs.config) { inherit (attrs) config; }
// optionalAttrs (isString attrs.config) { config = lib.generators.mkLuaInline attrs.config; }
// optionalAttrs (isDerivation attrs.config || isPath attrs.config) {
config = lib.generators.mkLuaInline ''dofile "${attrs.config}"'';
}
// optionalAttrs (isAttrs attrs.config) {
config = true;
opts = attrs.config;
}
// optionalAttrs (attrs.event != null) { inherit (attrs) event; }
// optionalAttrs (attrs.cmd != null) { inherit (attrs) cmd; }
// optionalAttrs (attrs.ft != null) { inherit (attrs) ft; }
// optionalAttrs (attrs.main != null) { inherit (attrs) main; }
// optionalAttrs (attrs.priority != null) { inherit (attrs) priority; };
in
{
config = toLua { } ({ inputs = mapAttrsToList toPlugin' cfg.plugins; } // cfg.config);
modules = toLua { } cfg.modules;
};

cpaths =
let
cpaths = mapPluginsRec (_: attrs: optional (attrs.cpath != null) attrs.cpath);
in
mkAfter (flatten cpaths);
paths =
let
paths = mapPluginsRec (_: attrs: attrs.paths);
in
mkAfter (flatten paths);
plugins =
pkgs.runCommand "plugins.lua"
{
nativeBuildInputs = with pkgs; [ stylua ];
passAsFile = [ "text" ];
preferLocalBuild = true;
allowSubstitutes = false;
text = ''
-- Generated by Nix (github:catgardens/chainix)
vim.opt.rtp:prepend "${cfg.package}"
require("ch").setup(${build.chaivim.config}, ${build.chaivim.modules})
'';
}
''
target=$out
mkdir -p "$(dirname "$target")"
if [ -e "$textPath" ]; then
mv "$textPath" "$target"
else
echo -n "$text" > "$target"
fi
stylua --config-path ${../stylua.toml} $target
'';
};

cpaths =
let
cpaths = mapPluginsRec (_: attrs: optional (attrs.cpath != null) attrs.cpath);
in
mkAfter (flatten cpaths);
paths =
let
paths = mapPluginsRec (_: attrs: attrs.paths);
in
mkAfter (flatten paths);
};
}
);
Expand Down

0 comments on commit 42235c9

Please sign in to comment.