{ lib ? import }: let inherit (builtins) baseNameOf dirOf length genList pathExists; inherit (lib) take splitString concatStringsSep last foldl foldr head tail singleton removePrefix hasSuffix removeSuffix flatten crossLists reverseList; butlast = list: take (length list - 1) list; doFileSuffix = "do"; genPatterns = path: let fn = baseNameOf path; in (map (x: "default." + x) (foldr (a: b: let x = (head b); in [ (a + "." + x) ] ++ b) [ doFileSuffix ] (tail (splitString "." fn)))); searchPath = path: let pathlist = (splitString "/" path); pat = (last pathlist); doFile = path + "." + doFileSuffix; patlist = genPatterns pat; foldedpaths = foldr (a: b: let h = if length b == 0 then "" else head b; in [ (h + a + "/") ] ++ b) [] (tail (reverseList pathlist)); pths = crossLists (p: pat: p + pat) [ foldedpaths patlist ]; in [ doFile ] ++ pths; cwd = "/home/spacefrogg/tmp"; path = cwd + "/foo/bar/baz.def.c"; whichdo = path: let pathlist = searchPath path; in foldl (res: p: if res == "" then if pathExists p then p else res else res) "" pathlist; d1 = path: pat: let dir = (dirOf pat) + "/"; in removePrefix dir path; d2 = path: pat: let dir = (dirOf pat) + "/"; _path = removePrefix dir path; _pat = removeSuffix ".${doFileSuffix}" (removePrefix "${dir}default" pat); _out = removeSuffix _pat _path; in if _out == "" then _path else _out; self = { inherit searchPath cwd path doFileSuffix whichdo d1 d2; }; in self