treewide: cleanup

This commit is contained in:
NotAShelf 2023-11-06 12:33:38 +03:00
parent 84fc8eb860
commit c1f449137f
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29
9 changed files with 220 additions and 197 deletions

View file

@ -4,4 +4,5 @@
types = import ./types {inherit lib;}; types = import ./types {inherit lib;};
languages = import ./languages.nix {inherit lib;}; languages = import ./languages.nix {inherit lib;};
lua = import ./lua.nix {inherit lib;}; lua = import ./lua.nix {inherit lib;};
vim = import ./vim.nix {inherit lib;};
} }

View file

@ -1,11 +1,8 @@
# Helpers for converting values to lua # Helpers for converting values to lua
{lib}: rec { {lib}: let
# yes? no. inherit (lib) mapAttrsToList filterAttrs concatStringsSep concatMapStringsSep stringToCharacters boolToString;
yesNo = value: inherit (builtins) hasAttr head;
if value in rec {
then "yes"
else "no";
# Convert a null value to lua's nil # Convert a null value to lua's nil
nullString = value: nullString = value:
if value == null if value == null
@ -46,4 +43,44 @@
+ " }"; + " }";
# Convert a list of lua expressions to a lua table. The difference to listToLuaTable is that the elements here are expected to be lua expressions already, whereas listToLuaTable converts from nix types to lua first # Convert a list of lua expressions to a lua table. The difference to listToLuaTable is that the elements here are expected to be lua expressions already, whereas listToLuaTable converts from nix types to lua first
luaTable = items: ''{${builtins.concatStringsSep "," items}}''; luaTable = items: ''{${builtins.concatStringsSep "," items}}'';
toLuaObject = args:
if builtins.isAttrs args
then
if hasAttr "__raw" args
then args.__raw
else if hasAttr "__empty" args
then "{ }"
else
"{"
+ (concatStringsSep ","
(mapAttrsToList
(n: v:
if head (stringToCharacters n) == "@"
then toLuaObject v
else "[${toLuaObject n}] = " + (toLuaObject v))
(filterAttrs
(
_: v:
(v != null) && (toLuaObject v != "{}")
)
args)))
+ "}"
else if builtins.isList args
then "{" + 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 "${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 "";
} }

26
lib/vim.nix Normal file
View file

@ -0,0 +1,26 @@
{lib}: let
inherit (builtins) isInt isBool toJSON;
in rec {
# yes? no.
yesNo = value:
if value
then "yes"
else "no";
# convert a boolean to a vim compliant boolean string
mkVimBool = val:
if val
then "1"
else "0";
# convert a literal value to a vim compliant value
valToVim = val:
if (isInt val)
then (builtins.toString val)
else
(
if (isBool val)
then (mkVimBool val)
else (toJSON val)
);
}

View file

@ -1,27 +0,0 @@
{
config,
lib,
...
}:
with lib; let
cfg = config.vim;
in {
config = {
assertions = mkMerge [
{
assertion = cfg.kommentary.enable;
message = "Kommentary has been deprecated in favor of comments-nvim";
}
{
assertion = cfg.utility.colorizer.enable;
message = "config.utility.colorizer has been renamed to config.utility.ccc";
}
mkIf
(config.programs.neovim-flake.enable)
{
assertion = !config.programs.neovim.enable;
message = "You cannot use `programs.neovim-flake.enable` with `programs.neovim.enable`";
}
];
};
}

View file

@ -2,9 +2,9 @@
lib, lib,
config, config,
... ...
}: }: let
with lib; inherit (lib) optionalString mkIf nvim;
with builtins; let
cfg = config.vim; cfg = config.vim;
in { in {
config = { config = {
@ -57,8 +57,8 @@ in {
}; };
vim.configRC.basic = nvim.dag.entryAfter ["globalsScript"] '' vim.configRC.basic = nvim.dag.entryAfter ["globalsScript"] ''
" Debug mode settings
${optionalString cfg.debugMode.enable '' ${optionalString cfg.debugMode.enable ''
" Debug mode settings
set verbose=${toString cfg.debugMode.level} set verbose=${toString cfg.debugMode.level}
set verbosefile=${cfg.debugMode.logFile} set verbosefile=${cfg.debugMode.logFile}
''} ''}

View file

@ -2,9 +2,10 @@
pkgs, pkgs,
lib, lib,
... ...
}: }: let
with lib; inherit (lib) mkEnableOption mkOption;
with builtins; { inherit (lib.types) types;
in {
options.vim = { options.vim = {
package = mkOption { package = mkOption {
type = types.package; type = types.package;

View file

@ -3,8 +3,11 @@
lib, lib,
... ...
}: }:
with lib;
with builtins; let with builtins; let
inherit (lib) mkOption types mapAttrsFlatten filterAttrs optionalString getAttrs;
inherit (lib) nvim;
inherit (nvim.lua) toLuaObject;
cfg = config.vim; cfg = config.vim;
wrapLuaConfig = luaConfig: '' wrapLuaConfig = luaConfig: ''
@ -20,7 +23,7 @@ with builtins; let
mkOption { mkOption {
type = types.bool; type = types.bool;
default = value; default = value;
description = description; inherit description;
}; };
# Most of the keybindings code is highly inspired by pta2002/nixvim. Thank you! # Most of the keybindings code is highly inspired by pta2002/nixvim. Thank you!
@ -67,7 +70,7 @@ with builtins; let
normalizeAction = action: let normalizeAction = action: let
# Extract the values of the config options that have been explicitly set by the user # Extract the values of the config options that have been explicitly set by the user
config = config =
filterAttrs (n: v: v != null) filterAttrs (_: v: v != null)
(getAttrs (attrNames mapConfigOptions) action); (getAttrs (attrNames mapConfigOptions) action);
in { in {
config = config =
@ -85,8 +88,8 @@ with builtins; let
normalizedAction = normalizeAction action; normalizedAction = normalizeAction action;
in { in {
inherit (normalizedAction) action config; inherit (normalizedAction) action config;
key = key; inherit key;
mode = mode; inherit mode;
}) })
maps); maps);
@ -117,169 +120,140 @@ with builtins; let
default = {}; default = {};
}; };
in { in {
options.vim = { options = {
viAlias = mkOption { assertions = lib.mkOption {
description = "Enable vi alias"; type = with types; listOf unspecified;
type = types.bool;
default = true;
};
vimAlias = mkOption {
description = "Enable vim alias";
type = types.bool;
default = true;
};
configRC = mkOption {
description = "vimrc contents";
type = types.oneOf [(nvim.types.dagOf types.lines) types.str];
default = {};
};
luaConfigRC = mkOption {
description = "vim lua config";
type = types.oneOf [(nvim.types.dagOf types.lines) types.str];
default = {};
};
builtConfigRC = mkOption {
internal = true; internal = true;
type = types.lines;
description = "The built config for neovim after resolving the DAG";
};
startPlugins = nvim.types.pluginsOpt {
default = []; default = [];
description = "List of plugins to startup."; example = lib.literalExpression [
{
assertion = false;
message = "you can't enable this for that reason";
}
];
}; };
optPlugins = nvim.types.pluginsOpt { warnings = mkOption {
internal = true;
default = []; default = [];
description = "List of plugins to optionally load"; type = with types; listOf str;
}; example = ["The `foo' service is deprecated and will go away soon!"];
description = lib.mdDoc ''
extraPlugins = mkOption { This option allows modules to show warnings to users during
type = types.attrsOf nvim.types.extraPluginType; the evaluation of the system configuration.
default = {};
description = ''
List of plugins and related config.
Note that these are setup after builtin plugins.
''; '';
example = literalExpression ''
with pkgs.vimPlugins; {
aerial = {
package = aerial-nvim;
setup = "require('aerial').setup {}";
};
harpoon = {
package = harpoon;
setup = "require('harpoon').setup {}";
after = ["aerial"];
};
}'';
}; };
globals = mkOption { vim = {
default = {}; viAlias = mkOption {
description = "Set containing global variable values"; description = "Enable vi alias";
type = types.attrs; type = types.bool;
}; default = true;
maps = mkOption {
type = types.submodule {
options = {
normal = mapOptions "normal";
insert = mapOptions "insert";
select = mapOptions "select";
visual = mapOptions "visual and select";
terminal = mapOptions "terminal";
normalVisualOp = mapOptions "normal, visual, select and operator-pending (same as plain 'map')";
visualOnly = mapOptions "visual only";
operator = mapOptions "operator-pending";
insertCommand = mapOptions "insert and command-line";
lang = mapOptions "insert, command-line and lang-arg";
command = mapOptions "command-line";
};
}; };
default = {};
description = ''
Custom keybindings for any mode.
For plain maps (e.g. just 'map' or 'remap') use maps.normalVisualOp. vimAlias = mkOption {
''; description = "Enable vim alias";
type = types.bool;
default = true;
};
example = '' configRC = mkOption {
maps = { description = "vimrc contents";
normal."<leader>m" = { type = types.oneOf [(nvim.types.dagOf types.lines) types.str];
silent = true; default = {};
action = "<cmd>make<CR>"; };
}; # Same as nnoremap <leader>m <silent> <cmd>make<CR>
luaConfigRC = mkOption {
description = "vim lua config";
type = types.oneOf [(nvim.types.dagOf types.lines) types.str];
default = {};
};
builtConfigRC = mkOption {
internal = true;
type = types.lines;
description = "The built config for neovim after resolving the DAG";
};
startPlugins = nvim.types.pluginsOpt {
default = [];
description = "List of plugins to startup.";
};
optPlugins = nvim.types.pluginsOpt {
default = [];
description = "List of plugins to optionally load";
};
extraPlugins = mkOption {
type = types.attrsOf nvim.types.extraPluginType;
default = {};
description = ''
List of plugins and related config.
Note that these are setup after builtin plugins.
'';
example = literalExpression ''
with pkgs.vimPlugins; {
aerial = {
package = aerial-nvim;
setup = "require('aerial').setup {}";
};
harpoon = {
package = harpoon;
setup = "require('harpoon').setup {}";
after = ["aerial"];
};
}'';
};
globals = mkOption {
default = {};
description = "Set containing global variable values";
type = types.attrs;
};
maps = mkOption {
type = types.submodule {
options = {
normal = mapOptions "normal";
insert = mapOptions "insert";
select = mapOptions "select";
visual = mapOptions "visual and select";
terminal = mapOptions "terminal";
normalVisualOp = mapOptions "normal, visual, select and operator-pending (same as plain 'map')";
visualOnly = mapOptions "visual only";
operator = mapOptions "operator-pending";
insertCommand = mapOptions "insert and command-line";
lang = mapOptions "insert, command-line and lang-arg";
command = mapOptions "command-line";
};
}; };
''; default = {};
description = ''
Custom keybindings for any mode.
For plain maps (e.g. just 'map' or 'remap') use maps.normalVisualOp.
'';
example = ''
maps = {
normal."<leader>m" = {
silent = true;
action = "<cmd>make<CR>";
}; # Same as nnoremap <leader>m <silent> <cmd>make<CR>
};
'';
};
}; };
}; };
config = let config = let
mkVimBool = val:
if val
then "1"
else "0";
valToVim = val:
if (isInt val)
then (builtins.toString val)
else
(
if (isBool val)
then (mkVimBool val)
else (toJSON val)
);
filterNonNull = mappings: filterAttrs (_name: value: value != null) mappings; filterNonNull = mappings: filterAttrs (_name: value: value != null) mappings;
globalsScript = globalsScript =
mapAttrsFlatten (name: value: "let g:${name}=${valToVim value}") mapAttrsFlatten (name: value: "let g:${name}=${valToVim value}")
(filterNonNull cfg.globals); (filterNonNull cfg.globals);
toLuaObject = args:
if builtins.isAttrs args
then
if hasAttr "__raw" args
then args.__raw
else if hasAttr "__empty" args
then "{ }"
else
"{"
+ (concatStringsSep ","
(mapAttrsToList
(n: v:
if head (stringToCharacters n) == "@"
then toLuaObject v
else "[${toLuaObject n}] = " + (toLuaObject v))
(filterAttrs
(
n: v:
!isNull v && (toLuaObject v != "{}")
)
args)))
+ "}"
else if builtins.isList args
then "{" + 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 "${boolToString args}"
else if builtins.isFloat args
then "${toString args}"
else if builtins.isInt args
then "${toString args}"
else if isNull args
then "nil"
else "";
toLuaBindings = mode: maps: toLuaBindings = mode: maps:
builtins.map (value: '' builtins.map (value: ''
vim.keymap.set(${toLuaObject mode}, ${toLuaObject value.key}, ${toLuaObject value.action}, ${toLuaObject value.config}) vim.keymap.set(${toLuaObject mode}, ${toLuaObject value.key}, ${toLuaObject value.action}, ${toLuaObject value.config})
@ -304,7 +278,7 @@ in {
mapResult, mapResult,
}: let }: let
# When the value is a string, default it to dag.entryAnywhere # When the value is a string, default it to dag.entryAnywhere
finalDag = lib.mapAttrs (name: value: finalDag = lib.mapAttrs (_: value:
if builtins.isString value if builtins.isString value
then nvim.dag.entryAnywhere value then nvim.dag.entryAnywhere value
else value) else value)
@ -378,6 +352,13 @@ in {
}; };
builtConfigRC = let builtConfigRC = let
failedAssertions = map (x: x.message) (filter (x: !x.assertion) config.assertions);
baseSystemAssertWarn =
if failedAssertions != []
then throw "\nFailed assertions:\n${concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}"
else lib.showWarnings config.warnings;
mkSection = r: '' mkSection = r: ''
" SECTION: ${r.name} " SECTION: ${r.name}
${r.data} ${r.data}
@ -389,7 +370,7 @@ in {
inherit mapResult; inherit mapResult;
}; };
in in
vimConfig; baseSystemAssertWarn vimConfig;
}; };
}; };
} }

View file

@ -5,7 +5,7 @@ inputs: {
check ? true, check ? true,
extraSpecialArgs ? {}, extraSpecialArgs ? {},
}: let }: let
inherit (pkgs) neovim-unwrapped wrapNeovim vimPlugins; inherit (pkgs) wrapNeovim vimPlugins;
inherit (builtins) map filter isString toString getAttr; inherit (builtins) map filter isString toString getAttr;
inherit (pkgs.vimUtils) buildVimPlugin; inherit (pkgs.vimUtils) buildVimPlugin;
@ -21,6 +21,8 @@ inputs: {
specialArgs = {modulesPath = toString ./.;} // extraSpecialArgs; specialArgs = {modulesPath = toString ./.;} // extraSpecialArgs;
}; };
vimOptions = module.config.vim;
buildPlug = {pname, ...} @ args: buildPlug = {pname, ...} @ args:
assert lib.asserts.assertMsg (pname != "nvim-treesitter") "Use buildTreesitterPlug for building nvim-treesitter."; assert lib.asserts.assertMsg (pname != "nvim-treesitter") "Use buildTreesitterPlug for building nvim-treesitter.";
buildVimPlugin (args buildVimPlugin (args
@ -31,8 +33,6 @@ inputs: {
buildTreesitterPlug = grammars: vimPlugins.nvim-treesitter.withPlugins (_: grammars); buildTreesitterPlug = grammars: vimPlugins.nvim-treesitter.withPlugins (_: grammars);
vimOptions = module.config.vim;
buildConfigPlugins = plugins: buildConfigPlugins = plugins:
map map
(plug: ( (plug: (

View file

@ -34,10 +34,14 @@
pkgsModule = {config, ...}: { pkgsModule = {config, ...}: {
config = { config = {
_module.args.baseModules = modules; _module = {
_module.args.pkgsPath = lib.mkDefault pkgs.path; inherit check;
_module.args.pkgs = lib.mkDefault pkgs; args = {
_module.check = check; baseModules = modules;
pkgsPath = lib.mkDefault pkgs.path;
pkgs = lib.mkDefault pkgs;
};
};
}; };
}; };
in in