dirs.nix (1728B)
1 { lib, ... }: 2 3 with lib; 4 5 rec { 6 7 /* 8 * Recursively read a directory 9 * Ugly code warning! 10 */ 11 readDirRec = dir: 12 let d = builtins.readDir dir; in 13 foldAttrs (n: a: n) [ ] (attrValues (mapAttrs 14 (n: v: 15 if v == "directory" then 16 # Add the directory name to the start of the file 17 # e.g. file.nix in directory called dir -> dir/file.nix 18 mapAttrs' 19 (fName: fVal: 20 nameValuePair "${n}/${fName}" "${n}/${fName}") 21 (readDirRec "${toString dir}/${n}") 22 23 # Else it is a normal file 24 # Yeah dont mind this weird stuff it's so that the 25 # directory stuff works lol 26 else { ${n} = n; }) 27 d)); 28 29 /* 30 * Map the function fn to every file in dir 31 */ 32 mapOnDir = dir: fn: 33 let d = builtins.readDir dir; in 34 mapAttrs (fn) d; 35 36 mapOnDir' = dir: fn: 37 let d = builtins.readDir dir; in 38 mapAttrs' (fn) d; 39 40 mapOnDirRec = dir: fn: 41 let d = readDirRec dir; in 42 mapAttrs (fn) d; 43 44 mapOnDirRec' = dir: fn: 45 let d = readDirRec dir; in 46 mapAttrs' (fn) d; 47 48 importDir = dir: 49 map (file: "${toString dir}/${file}") (filter (f: f != "default.nix") (attrValues (mapOnDir dir (name: a: 50 name)))); 51 52 importDir' = dir: 53 map (file: "${toString dir}/${file}") (filter (f: f != "default.nix") (attrValues (mapOnDir' dir (name: a: 54 name)))); 55 56 importDirRec = dir: 57 map (file: "${toString dir}/${file}") (filter (f: f != "default.nix" && hasSuffix ".nix" f) (attrValues (mapOnDirRec dir (name: a: 58 name)))); 59 60 importDirRec' = dir: 61 map (file: "${toString dir}/${file}") (filter (f: f != "default.nix" && hasSuffix ".nix" f) (attrValues (mapOnDirRec' dir (name: a: 62 name)))); 63 }