nvf/lib/types/dag.nix
alfarel 88d1e1c902
lib/types: use payload instead of wrapper in dagOf
This is the more modern approach: https://github.com/NixOS/nixpkgs/pull/375084
The above PR does this for similar cases in nixpkgs.

This fixes a warning that shows up when using the lib in weird ways:

```
trace: evaluation warning: The deprecated `functor.wrapped` attribute is accessed, use `nestedTypes.elemType` instead.
```

The warning is thrown by the module system when merging types
(at least in some cases).
It shouldn't be exposed at all in the current codebase.
2025-09-27 01:11:48 +00:00

78 lines
2.4 KiB
Nix

# From home-manager: https://github.com/nix-community/home-manager/blob/master/modules/lib/types-dag.nix
# Used for ordering configuration text.
{lib}: let
inherit
(lib)
nvim
mkIf
mkOrder
mkOption
mkOptionType
types
;
dagEntryOf = elemType: let
submoduleType = types.submodule ({name, ...}: {
options = {
data = mkOption {type = elemType;};
after = mkOption {type = with types; listOf str;};
before = mkOption {type = with types; listOf str;};
};
config = mkIf (elemType.name == "submodule") {
data._module.args.dagName = name;
};
});
maybeConvert = def:
if nvim.dag.isEntry def.value
then def.value
else
nvim.dag.entryAnywhere (
if def ? priority
then mkOrder def.priority def.value
else def.value
);
in
mkOptionType {
name = "dagEntryOf";
description = "DAG entry of ${elemType.description}";
# leave the checking to the submodule type
merge = loc: defs:
submoduleType.merge loc (map (def: {
inherit (def) file;
value = maybeConvert def;
})
defs);
};
in rec {
# A directed acyclic graph of some inner type.
#
# Note, if the element type is a submodule then the `name` argument
# will always be set to the string "data" since it picks up the
# internal structure of the DAG values. To give access to the
# "actual" attribute name a new submodule argument is provided with
# the name `dagName`.
dagOf = elemType: let
attrEquivalent = types.attrsOf (dagEntryOf elemType);
in
mkOptionType rec {
name = "dagOf";
description = "DAG of ${elemType.description}";
inherit (attrEquivalent) check merge emptyValue;
inherit (elemType) getSubModules;
getSubOptions = prefix: elemType.getSubOptions (prefix ++ ["<name>"]);
substSubModules = m: dagOf (elemType.substSubModules m);
# Based on the result of calling the nixpkgs types.nix elemTypeFunctor helper function.
functor = {
inherit name;
payload.elemType = elemType;
type = payload: types.${name} payload.elemType;
binOp = a: b: let
merged = a.elemType.typeMerge b.elemType.functor;
in
if merged == null
then null
else {elemType = merged;};
};
nestedTypes.elemType = elemType;
};
}