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