modules: explicit lib usage

This commit is contained in:
raf 2024-02-20 02:05:36 +03:00
commit caf342adb1
No known key found for this signature in database
GPG key ID: 02D1DD3FA08B6B29
27 changed files with 673 additions and 684 deletions

105
lib/binds.nix Normal file
View file

@ -0,0 +1,105 @@
{lib}: let
inherit (lib.options) mkOption;
inherit (lib.modules) mkIf;
inherit (lib.types) nullOr str submodule bool;
inherit (lib.attrsets) isAttrs mapAttrs attrsOf;
in rec {
# mkLuaBinding creates a binding with Lua and silent flags.
#
# Arguments:
# - key: The name of the binding.
# - action: The action to be performed when the binding is activated.
# - desc: The description of the binding.
mkLuaBinding = key: action: desc:
mkIf (key != null) {
"${key}" = {
inherit action desc;
lua = true;
silent = true;
};
};
# mkExprBinding creates a binding with Lua, silent, and expr flags.
#
# Arguments:
# - key: The name of the binding.
# - action: The action to be performed when the binding is activated.
# - desc: The description of the binding.
mkExprBinding = key: action: desc:
mkIf (key != null) {
"${key}" = {
inherit action desc;
lua = true;
silent = true;
expr = true;
};
};
# mkBinding creates a binding with silent flag.
#
# Arguments:
# - key: The name of the binding.
# - action: The action to be performed when the binding is activated.
# - desc: The description of the binding.
mkBinding = key: action: desc:
mkIf (key != null) {
"${key}" = {
inherit action desc;
silent = true;
};
};
# mkMappingOption creates an option that can be null or a string.
#
# Arguments:
# - description: The description of the option.
# - default: The default value of the option.
mkMappingOption = description: default:
mkOption {
type = nullOr str;
inherit default description;
};
# Utility function that takes two attrsets:
# { someKey = "some_value" } and
# { someKey = { description = "Some Description"; }; }
# and merges them into
# { someKey = { value = "some_value"; description = "Some Description"; }; }
addDescriptionsToMappings = actualMappings: mappingDefinitions:
mapAttrs (name: value: let
isNested = isAttrs value;
returnedValue =
if isNested
then addDescriptionsToMappings actualMappings."${name}" mappingDefinitions."${name}"
else {
inherit value;
inherit (mappingDefinitions."${name}") description;
};
in
returnedValue)
actualMappings;
# mkSetBinding creates a binding with the provided action and description.
#
# Arguments:
# - binding: The binding to be set.
# - action: The action to be performed when the binding is activated.
mkSetBinding = binding: action:
mkBinding binding.value action binding.description;
# mkSetExprBinding creates an expression binding with the provided action and description.
#
# Arguments:
# - binding: The binding to be set.
# - action: The action to be performed when the binding is activated.
mkSetExprBinding = binding: action:
mkExprBinding binding.value action binding.description;
# mkSetLuaBinding creates a Lua binding with the provided action and description.
#
# Arguments:
# - binding: The binding to be set.
# - action: The action to be performed when the binding is activated.
mkSetLuaBinding = binding: action:
mkLuaBinding binding.value action binding.description;
}

View file

@ -1,8 +1,10 @@
{lib}: {
modules = import ./modules.nix {inherit lib;};
dag = import ./dag.nix {inherit lib;};
types = import ./types {inherit lib;};
binds = import ./binds.nix {inherit lib;};
dag = import ./dag.nix {inherit lib;};
languages = import ./languages.nix {inherit lib;};
lua = import ./lua.nix {inherit lib;};
modules = import ./modules.nix {inherit lib;};
vim = import ./vim.nix {inherit lib;};
}

View file

@ -4,70 +4,9 @@
nixpkgsLib: let
mkNvimLib = import ./.;
in
nixpkgsLib.extend (self: super: rec {
nixpkgsLib.extend (self: super: {
nvim = mkNvimLib {lib = self;};
mkLuaBinding = key: action: desc:
self.mkIf (key != null) {
"${key}" = {
inherit action desc;
lua = true;
silent = true;
};
};
mkExprBinding = key: action: desc:
self.mkIf (key != null) {
"${key}" = {
inherit action desc;
lua = true;
silent = true;
expr = true;
};
};
mkBinding = key: action: desc:
self.mkIf (key != null) {
"${key}" = {
inherit action desc;
silent = true;
};
};
mkMappingOption = description: default:
self.mkOption {
type = self.types.nullOr self.types.str;
inherit default description;
};
# Utility function that takes two attrsets:
# { someKey = "some_value" } and
# { someKey = { description = "Some Description"; }; }
# and merges them into
# { someKey = { value = "some_value"; description = "Some Description"; }; }
addDescriptionsToMappings = actualMappings: mappingDefinitions:
self.attrsets.mapAttrs (name: value: let
isNested = self.isAttrs value;
returnedValue =
if isNested
then addDescriptionsToMappings actualMappings."${name}" mappingDefinitions."${name}"
else {
value = value;
description = mappingDefinitions."${name}".description;
};
in
returnedValue)
actualMappings;
mkSetBinding = binding: action:
mkBinding binding.value action binding.description;
mkSetExprBinding = binding: action:
mkExprBinding binding.value action binding.description;
mkSetLuaBinding = binding: action:
mkLuaBinding binding.value action binding.description;
# For forward compatibility.
literalExpression = super.literalExpression or super.literalExample;
})