1# Functions for copying sources to the Nix store.
2{ lib }:
3
4let
5 inherit (lib.strings)
6 hasPrefix
7 ;
8in
9
10{
11 /*
12 A map of all haskell packages defined in the given path,
13 identified by having a cabal file with the same name as the
14 directory itself.
15
16 Type: Path -> Map String Path
17 */
18 haskellPathsInDir =
19 # The directory within to search
20 root:
21 let # Files in the root
22 root-files = builtins.attrNames (builtins.readDir root);
23 # Files with their full paths
24 root-files-with-paths =
25 map (file:
26 { name = file; value = root + "/${file}"; }
27 ) root-files;
28 # Subdirectories of the root with a cabal file.
29 cabal-subdirs =
30 builtins.filter ({ name, value }:
31 builtins.pathExists (value + "/${name}.cabal")
32 ) root-files-with-paths;
33 in builtins.listToAttrs cabal-subdirs;
34 /*
35 Find the first directory containing a file matching 'pattern'
36 upward from a given 'file'.
37 Returns 'null' if no directories contain a file matching 'pattern'.
38
39 Type: RegExp -> Path -> Nullable { path : Path; matches : [ MatchResults ]; }
40 */
41 locateDominatingFile =
42 # The pattern to search for
43 pattern:
44 # The file to start searching upward from
45 file:
46 let go = path:
47 let files = builtins.attrNames (builtins.readDir path);
48 matches = builtins.filter (match: match != null)
49 (map (builtins.match pattern) files);
50 in
51 if builtins.length matches != 0
52 then { inherit path matches; }
53 else if path == /.
54 then null
55 else go (dirOf path);
56 parent = dirOf file;
57 isDir =
58 let base = baseNameOf file;
59 type = (builtins.readDir parent).${base} or null;
60 in file == /. || type == "directory";
61 in go (if isDir then file else parent);
62
63
64 /*
65 Given a directory, return a flattened list of all files within it recursively.
66
67 Type: Path -> [ Path ]
68 */
69 listFilesRecursive =
70 # The path to recursively list
71 dir:
72 lib.flatten (lib.mapAttrsToList (name: type:
73 if type == "directory" then
74 lib.filesystem.listFilesRecursive (dir + "/${name}")
75 else
76 dir + "/${name}"
77 ) (builtins.readDir dir));
78
79}