treewide: refactor custom lib, merge lists in hm/nixos module

This commit is contained in:
diniamo 2024-07-07 19:37:46 +02:00
commit a376eed0df
13 changed files with 308 additions and 256 deletions

18
lib/configuration.nix Normal file
View file

@ -0,0 +1,18 @@
{
inputs,
lib,
}: let
modulesWithInputs = import ../modules inputs;
in
{
modules ? [],
pkgs,
check ? true,
extraSpecialArgs ? {},
extraModules ? [],
...
}:
modulesWithInputs {
inherit pkgs lib check extraSpecialArgs extraModules;
configuration.imports = modules;
}

View file

@ -12,4 +12,5 @@
lists = import ./lists.nix {inherit lib;};
lua = import ./lua.nix {inherit lib;};
vim = import ./vim.nix;
neovimConfiguration = import ./configuration.nix {inherit inputs lib;};
}

View file

@ -1,7 +1,7 @@
# Convenience function that returns the given Nixpkgs standard library
# extended with our functions using `lib.extend`.
nixpkgsLib: inputs:
nixpkgsLib.extend (self: super: {
inputs:
inputs.nixpkgs.lib.extend (self: super: {
# WARNING: New functions should not be added here, but to files
# imported by `./default.nix` under their own categories. If your
# function does not fit to any of the existing categories, create

54
lib/types/custom.nix Normal file
View file

@ -0,0 +1,54 @@
{lib}: let
inherit (lib) isStringLike showOption showFiles getFiles mergeOneOption mergeEqualOption;
inherit (lib.types) anything attrsOf;
inherit (lib.nvim.types) anythingConcatLists;
inherit (builtins) isAttrs foldl' head concatLists;
in {
# 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
# HACK: Does this break anything in our case?
anythingConcatLists =
anything
// {
merge = loc: defs: let
getType = value:
if isAttrs value && isStringLike value
then "stringCoercibleSet"
else builtins.typeOf value;
# Returns the common type of all definitions, throws an error if they
# don't have the same type
commonType =
foldl' (
type: def:
if getType def.value == type
then type
else throw "The option `${showOption loc}' has conflicting option types in ${showFiles (getFiles defs)}"
) (getType (head defs).value)
defs;
mergeFunction =
{
# Recursively merge attribute sets
set = (attrsOf anythingConcatLists).merge;
# Overridden behavior for lists
list = _: defs: concatLists (map (e: e.value) defs);
# This is the type of packages, only accept a single definition
stringCoercibleSet = mergeOneOption;
lambda = loc: defs: arg:
anythingConcatLists.merge
(loc ++ ["<function body>"])
(map (def: {
inherit (def) file;
value = def.value arg;
})
defs);
# Otherwise fall back to only allowing all equal definitions
}
.${commonType}
or mergeEqualOption;
in
mergeFunction loc defs;
};
}

View file

@ -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;
inherit (typesLanguage) diagnostics mkGrammarOption;
inherit (typesCustom) anythingConcatLists;
}