infra/flake/den/nix/den-brackets.nix
2025-12-01 13:23:24 +07:00

55 lines
1.3 KiB
Nix

# __findFile implementation to resolve deep aspects.
# inspired by https://fzakaria.com/2025/08/10/angle-brackets-in-a-nix-flake-world
{
lib,
config,
...
}:
_nixPath: name:
let
findAspect =
path:
let
head = lib.head path;
tail = lib.tail path;
notFound = "Aspect not found: ${lib.concatStringsSep "." path}";
headIsDen = head == "den";
readFromDen = lib.getAttrFromPath ([ "den" ] ++ tail) config;
headIsAspect = builtins.hasAttr head config.den.aspects;
aspectsPath = [
"den"
"aspects"
] ++ path;
readFromAspects = lib.getAttrFromPath aspectsPath config;
headIsDenful = lib.hasAttrByPath [ "ful" head ] config.den;
denfulTail = if lib.head tail == "provides" then lib.tail tail else tail;
denfulPath = [
"den"
"ful"
head
] ++ denfulTail;
readFromDenful = lib.getAttrFromPath denfulPath config;
found =
if headIsDen then
readFromDen
else if headIsAspect then
readFromAspects
else if headIsDenful then
readFromDenful
else
throw notFound;
in
found;
in
lib.pipe name [
(lib.strings.replaceStrings [ "/" ] [ ".provides." ])
(lib.strings.splitString ".")
(findAspect)
]