|
18 | 18 | ; |
19 | 19 |
|
20 | 20 | inherit (lib.filesystem) |
| 21 | + pathIsDirectory |
| 22 | + pathIsRegularFile |
21 | 23 | pathType |
| 24 | + packagesFromDirectoryRecursive |
22 | 25 | ; |
23 | 26 |
|
24 | 27 | inherit (lib.strings) |
|
361 | 364 | ... |
362 | 365 | }: |
363 | 366 | let |
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); |
| 367 | + inherit (lib.path) append; |
| 368 | + defaultPath = append directory "package.nix"; |
409 | 369 | in |
410 | | - packagesFromDirectory directory; |
| 370 | + if pathExists defaultPath then |
| 371 | + # if `${directory}/package.nix` exists, call it directly |
| 372 | + callPackage defaultPath {} |
| 373 | + else lib.concatMapAttrs (name: type: |
| 374 | + # otherwise, for each directory entry |
| 375 | + let path = append directory name; in |
| 376 | + if type == "directory" then { |
| 377 | + # recurse into directories |
| 378 | + "${name}" = packagesFromDirectoryRecursive { |
| 379 | + inherit callPackage; |
| 380 | + directory = path; |
| 381 | + }; |
| 382 | + } else if type == "regular" && hasSuffix ".nix" name then { |
| 383 | + # call .nix files |
| 384 | + "${lib.removeSuffix ".nix" name}" = callPackage path {}; |
| 385 | + } else if type == "regular" then { |
| 386 | + # ignore non-nix files |
| 387 | + } else throw '' |
| 388 | + lib.filesystem.packagesFromDirectoryRecursive: Unsupported file type ${type} at path ${toString path} |
| 389 | + '' |
| 390 | + ) (builtins.readDir directory); |
411 | 391 | } |
0 commit comments