1{ # haskellPathsInDir : Path -> Map String Path
2 # A map of all haskell packages defined in the given path,
3 # identified by having a cabal file with the same name as the
4 # directory itself.
5 haskellPathsInDir = root:
6 let # Files in the root
7 root-files = builtins.attrNames (builtins.readDir root);
8 # Files with their full paths
9 root-files-with-paths =
10 map (file:
11 { name = file; value = root + "/${file}"; }
12 ) root-files;
13 # Subdirectories of the root with a cabal file.
14 cabal-subdirs =
15 builtins.filter ({ name, value }:
16 builtins.pathExists (value + "/${name}.cabal")
17 ) root-files-with-paths;
18 in builtins.listToAttrs cabal-subdirs;
19 # locateDominatingFile : RegExp
20 # -> Path
21 # -> Nullable { path : Path;
22 # matches : [ MatchResults ];
23 # }
24 # Find the first directory containing a file matching 'pattern'
25 # upward from a given 'file'.
26 # Returns 'null' if no directories contain a file matching 'pattern'.
27 locateDominatingFile = pattern: file:
28 let go = path:
29 let files = builtins.attrNames (builtins.readDir path);
30 matches = builtins.filter (match: match != null)
31 (map (builtins.match pattern) files);
32 in
33 if builtins.length matches != 0
34 then { inherit path matches; }
35 else if path == /.
36 then null
37 else go (dirOf path);
38 parent = dirOf file;
39 isDir =
40 let base = baseNameOf file;
41 type = (builtins.readDir parent).${base} or null;
42 in file == /. || type == "directory";
43 in go (if isDir then file else parent);
44}