|
1 | 1 | inputs: |
2 | 2 | let |
3 | | - inherit (inputs.nixpkgs-unstable) lib; |
| 3 | + |
| 4 | + inherit (builtins) |
| 5 | + readDir |
| 6 | + ; |
| 7 | + |
| 8 | + inherit (inputs.nixpkgs-lib) lib; |
4 | 9 |
|
5 | 10 | inherit (lib.attrsets) |
| 11 | + attrNames |
| 12 | + filterAttrs |
6 | 13 | mapAttrs' |
7 | 14 | nameValuePair |
8 | 15 | ; |
9 | 16 |
|
| 17 | + inherit (lib.fixedPoints) |
| 18 | + fix |
| 19 | + ; |
| 20 | + |
| 21 | + inherit (lib.lists) |
| 22 | + toList |
| 23 | + ; |
| 24 | + |
10 | 25 | inherit (lib.strings) |
| 26 | + concatMapStrings |
| 27 | + concatStringsSep |
11 | 28 | splitString |
12 | 29 | substring |
13 | 30 | toLower |
14 | 31 | toUpper |
15 | | - concatMapStrings |
16 | 32 | ; |
| 33 | + |
| 34 | + inherit (lib.trivial) |
| 35 | + const |
| 36 | + flip |
| 37 | + pipe |
| 38 | + ; |
| 39 | + |
17 | 40 | in |
18 | | -rec { |
| 41 | +fix (finalLibrary: { |
| 42 | + |
| 43 | + path = fix (finalPath: { |
| 44 | + |
| 45 | + /** |
| 46 | + Filter the contents of a directory path for directories only. |
| 47 | +
|
| 48 | + # Inputs |
| 49 | +
|
| 50 | + `contents` |
| 51 | +
|
| 52 | + : 1\. The contents of a directory path. |
| 53 | +
|
| 54 | + # Type |
| 55 | +
|
| 56 | + ``` |
| 57 | + filterDirectories :: AttrSet -> AttrSet |
| 58 | + ``` |
| 59 | +
|
| 60 | + # Examples |
| 61 | + :::{.example} |
| 62 | + ## `lib.path.filterDirectories` usage example |
| 63 | +
|
| 64 | + ```nix |
| 65 | + x = { |
| 66 | + "default.nix" = "regular"; |
| 67 | + djacu = "directory"; |
| 68 | + programs = "directory"; |
| 69 | + services = "directory"; |
| 70 | + } |
| 71 | + filterDirectories x |
| 72 | + => { |
| 73 | + djacu = "directory"; |
| 74 | + programs = "directory"; |
| 75 | + services = "directory"; |
| 76 | + } |
| 77 | + ``` |
| 78 | +
|
| 79 | + ::: |
| 80 | + */ |
| 81 | + filterDirectories = filterAttrs (const (fileType: fileType == "directory")); |
| 82 | + |
| 83 | + /** |
| 84 | + Get list of directories names under parent. |
| 85 | +
|
| 86 | + # Inputs |
| 87 | +
|
| 88 | + `path` |
| 89 | +
|
| 90 | + : 1\. The parent path. |
| 91 | +
|
| 92 | + # Type |
| 93 | +
|
| 94 | + ``` |
| 95 | + getDirectoryNames :: Path -> [String] |
| 96 | + ``` |
| 97 | +
|
| 98 | + # Examples |
| 99 | + :::{.example} |
| 100 | + ## `lib.path.getDirectoryNames` usage example |
| 101 | +
|
| 102 | + ```nix |
| 103 | + getDirectoryNames ./home-modules |
| 104 | + => [ |
| 105 | + "djacu" |
| 106 | + "programs" |
| 107 | + "services" |
| 108 | + ] |
| 109 | + ``` |
| 110 | + */ |
| 111 | + getDirectoryNames = flip pipe [ |
| 112 | + finalPath.getDirectories |
| 113 | + attrNames |
| 114 | + ]; |
| 115 | + |
| 116 | + /** |
| 117 | + Get attribute set of directories under parent. |
| 118 | +
|
| 119 | + # Inputs |
| 120 | +
|
| 121 | + `path` |
| 122 | +
|
| 123 | + : 1\. The parent path. |
| 124 | +
|
| 125 | + # Type |
| 126 | +
|
| 127 | + ``` |
| 128 | + getDirectories :: Path -> AttrSet |
| 129 | + ``` |
| 130 | +
|
| 131 | + # Examples |
| 132 | + :::{.example} |
| 133 | + ## `lib.path.getDirectories` usage example |
| 134 | +
|
| 135 | + ```nix |
| 136 | + getDirectories ./home-modules |
| 137 | + => { |
| 138 | + djacu = "directory"; |
| 139 | + programs = "directory"; |
| 140 | + services = "directory"; |
| 141 | + } |
| 142 | + ``` |
| 143 | + */ |
| 144 | + getDirectories = flip pipe [ |
| 145 | + readDir |
| 146 | + finalPath.filterDirectories |
| 147 | + ]; |
| 148 | + |
| 149 | + /** |
| 150 | + Join a parent path to one or more children. |
| 151 | +
|
| 152 | + # Inputs |
| 153 | +
|
| 154 | + `parent` |
| 155 | +
|
| 156 | + : 1\. A parent path. |
| 157 | +
|
| 158 | + `paths` |
| 159 | +
|
| 160 | + : 2\. The paths to append. |
| 161 | +
|
| 162 | + # Type |
| 163 | +
|
| 164 | + ``` |
| 165 | + joinParentToPaths :: Path -> String | [ String ] -> String |
| 166 | + ``` |
| 167 | +
|
| 168 | + # Examples |
| 169 | + :::{.example} |
| 170 | + ## `lib.path.joinParentToPaths` usage example |
| 171 | +
|
| 172 | + ```nix |
| 173 | + joinParentToPaths ./home-modules "users" |
| 174 | + => /home/djacu/dev/djacu/theonecfg/home-modules/users |
| 175 | + joinParentToPaths ./home-modules [ "users" djacu" "module.nix" "] |
| 176 | + => /home/djacu/dev/djacu/theonecfg/home-modules/users/djacu/module.nix |
| 177 | + ``` |
| 178 | +
|
| 179 | + ::: |
| 180 | + */ |
| 181 | + joinParentToPaths = parent: paths: parent + ("/" + concatStringsSep "/" (toList paths)); |
| 182 | + |
| 183 | + }); |
| 184 | + |
| 185 | + strings = fix (finalStrings: { |
| 186 | + |
| 187 | + mutFirstChar = |
| 188 | + f: s: |
| 189 | + let |
| 190 | + firstChar = f (substring 0 1 s); |
| 191 | + rest = substring 1 (-1) s; |
| 192 | + in |
| 193 | + firstChar + rest; |
19 | 194 |
|
20 | | - mutFirstChar = |
21 | | - f: s: |
22 | | - let |
23 | | - firstChar = f (substring 0 1 s); |
24 | | - rest = substring 1 (-1) s; |
25 | | - in |
26 | | - firstChar + rest; |
| 195 | + kebabToCamel = |
| 196 | + s: |
| 197 | + finalStrings.mutFirstChar toLower ( |
| 198 | + concatMapStrings (finalStrings.mutFirstChar toUpper) (splitString "-" s) |
| 199 | + ); |
27 | 200 |
|
28 | | - kebabToCamel = |
29 | | - s: mutFirstChar toLower (concatMapStrings (mutFirstChar toUpper) (splitString "-" s)); |
| 201 | + attrNamesKebabToCamel = mapAttrs' ( |
| 202 | + name: value: nameValuePair (finalStrings.kebabToCamel name) value |
| 203 | + ); |
30 | 204 |
|
31 | | - attrNamesKebabToCamel = mapAttrs' (name: value: nameValuePair (kebabToCamel name) value); |
| 205 | + }); |
32 | 206 |
|
33 | | -} |
| 207 | +}) |
0 commit comments