|
18 | 18 | ; |
19 | 19 |
|
20 | 20 | inherit (lib.filesystem) |
21 | | - pathIsDirectory |
22 | | - pathIsRegularFile |
23 | 21 | pathType |
24 | | - packagesFromDirectoryRecursive |
25 | 22 | ; |
26 | 23 |
|
27 | 24 | inherit (lib.strings) |
|
363 | 360 | directory, |
364 | 361 | ... |
365 | 362 | }: |
366 | | - assert pathIsDirectory directory; |
367 | 363 | let |
368 | | - inherit (lib.path) append; |
369 | | - defaultPath = append directory "package.nix"; |
| 364 | + # Determine if a directory entry from `readDir` indicates a package or |
| 365 | + # directory of packages. |
| 366 | + directoryEntryIsPackage = basename: type: |
| 367 | + type == "directory" || hasSuffix ".nix" basename; |
| 368 | + |
| 369 | + # List directory entries that indicate packages in the given `path`. |
| 370 | + packageDirectoryEntries = path: |
| 371 | + filterAttrs directoryEntryIsPackage (readDir path); |
| 372 | + |
| 373 | + # Transform a directory entry (a `basename` and `type` pair) into a |
| 374 | + # package. |
| 375 | + directoryEntryToAttrPair = subdirectory: basename: type: |
| 376 | + let |
| 377 | + path = subdirectory + "/${basename}"; |
| 378 | + in |
| 379 | + if type == "regular" |
| 380 | + then |
| 381 | + { |
| 382 | + name = removeSuffix ".nix" basename; |
| 383 | + value = callPackage path { }; |
| 384 | + } |
| 385 | + else |
| 386 | + if type == "directory" |
| 387 | + then |
| 388 | + { |
| 389 | + name = basename; |
| 390 | + value = packagesFromDirectory path; |
| 391 | + } |
| 392 | + else |
| 393 | + throw |
| 394 | + '' |
| 395 | + lib.filesystem.packagesFromDirectoryRecursive: Unsupported file type ${type} at path ${toString subdirectory} |
| 396 | + ''; |
| 397 | + |
| 398 | + # Transform a directory into a package (if there's a `package.nix`) or |
| 399 | + # set of packages (otherwise). |
| 400 | + packagesFromDirectory = path: |
| 401 | + let |
| 402 | + defaultPackagePath = path + "/package.nix"; |
| 403 | + in |
| 404 | + if pathExists defaultPackagePath |
| 405 | + then callPackage defaultPackagePath { } |
| 406 | + else mapAttrs' |
| 407 | + (directoryEntryToAttrPair path) |
| 408 | + (packageDirectoryEntries path); |
370 | 409 | in |
371 | | - if pathIsRegularFile defaultPath then |
372 | | - # if `${directory}/package.nix` exists, call it directly |
373 | | - callPackage defaultPath {} |
374 | | - else lib.concatMapAttrs (name: type: |
375 | | - # otherwise, for each directory entry |
376 | | - let path = append directory name; in |
377 | | - if type == "directory" then { |
378 | | - # recurse into directories |
379 | | - "${name}" = packagesFromDirectoryRecursive { |
380 | | - inherit callPackage; |
381 | | - directory = path; |
382 | | - }; |
383 | | - } else if type == "regular" && hasSuffix ".nix" name then { |
384 | | - # call .nix files |
385 | | - "${lib.removeSuffix ".nix" name}" = callPackage path {}; |
386 | | - } else if type == "regular" then { |
387 | | - # ignore non-nix files |
388 | | - } else throw '' |
389 | | - lib.filesystem.packagesFromDirectoryRecursive: Unsupported file type ${type} at path ${toString path} |
390 | | - '' |
391 | | - ) (builtins.readDir directory); |
| 410 | + packagesFromDirectory directory; |
392 | 411 | } |
0 commit comments