mirror of
https://github.com/NotAShelf/nvf.git
synced 2025-09-06 10:21:31 +00:00
treewide: refactor custom lib, merge lists in hm/nixos module (#323)
* treewide: refactor custom lib, merge lists in hm/nixos module * lib/types(custom): clarify anythingConcatLists code --------- Co-authored-by: raf <raf@notashelf.dev>
This commit is contained in:
parent
0c444830f6
commit
901363d1ac
13 changed files with 307 additions and 256 deletions
53
lib/types/custom.nix
Normal file
53
lib/types/custom.nix
Normal file
|
@ -0,0 +1,53 @@
|
|||
{lib}: let
|
||||
inherit (lib) isStringLike showOption showFiles getFiles mergeOneOption mergeEqualOption;
|
||||
inherit (lib.types) anything attrsOf;
|
||||
inherit (lib.nvim.types) anythingConcatLists;
|
||||
inherit (builtins) typeOf isAttrs any head concatLists;
|
||||
in {
|
||||
# HACK: Does this break anything in our case?
|
||||
# A modified version of the nixpkgs anything type that concatenates lists
|
||||
# This isn't the default because the order in which the lists are concatenated depends on the order in which the modules are imported,
|
||||
# which makes it non-deterministic
|
||||
anythingConcatLists =
|
||||
anything
|
||||
// {
|
||||
merge = loc: defs: let
|
||||
getType = value:
|
||||
if isAttrs value && isStringLike value
|
||||
then "stringCoercibleSet"
|
||||
else typeOf value;
|
||||
|
||||
# Throw an error if not all defs have the same type
|
||||
checkType = getType (head defs).value;
|
||||
commonType =
|
||||
if any (def: getType def.value != checkType) defs
|
||||
then throw "The option `${showOption loc}' has conflicting option types in ${showFiles (getFiles defs)}"
|
||||
else checkType;
|
||||
|
||||
mergeFunctions = {
|
||||
# Recursively merge attribute sets
|
||||
set = (attrsOf anythingConcatLists).merge;
|
||||
|
||||
# Overridden behavior for lists, that concatenates lists
|
||||
list = _: defs: concatLists (map (e: e.value) defs);
|
||||
|
||||
# This means it's a package, only accept a single definition
|
||||
stringCoercibleSet = mergeOneOption;
|
||||
|
||||
# This works by passing the argument to the functions,
|
||||
# and merging their returns values instead
|
||||
lambda = loc: defs: arg:
|
||||
anythingConcatLists.merge
|
||||
(loc ++ ["<function body>"])
|
||||
(map (def: {
|
||||
inherit (def) file;
|
||||
value = def.value arg;
|
||||
})
|
||||
defs);
|
||||
};
|
||||
in
|
||||
# Merge the defs with the correct function from above, if available
|
||||
# otherwise only allow equal values
|
||||
(mergeFunctions.${commonType} or mergeEqualOption) loc defs;
|
||||
};
|
||||
}
|
|
@ -6,8 +6,10 @@
|
|||
typesDag = import ./dag.nix {inherit lib;};
|
||||
typesPlugin = import ./plugins.nix {inherit inputs lib;};
|
||||
typesLanguage = import ./languages.nix {inherit lib;};
|
||||
typesCustom = import ./custom.nix {inherit lib;};
|
||||
in {
|
||||
inherit (typesDag) dagOf;
|
||||
inherit (typesPlugin) pluginsOpt extraPluginType mkPluginSetupOption luaInline pluginType;
|
||||
inherit (typesLanguage) diagnostics mkGrammarOption;
|
||||
inherit (typesCustom) anythingConcatLists;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue