generated from nix-community/nur-packages-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread-by-name.nix
62 lines (56 loc) · 2.31 KB
/
read-by-name.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Derived from https://github.com/NixOS/nixpkgs/blob/86d0c37bba6b7f8ca20ebd834e793d05be51e40e/pkgs/top-level/read-by-name.nix
/*
See https://github.com/NixOS/nixpkgs/blob/86d0c37bba6b7f8ca20ebd834e793d05be51e40e/pkgs/by-name/README.md for more details.
Type: { lib: decltype(pkgs.lib) } -> Path -> AttrsOf Path
Example:
import ./read-by-name.nix nixpkgs.lib ../by-name
=> {
hello = ../by-name/he/hello/package.nix;
hey = ../by-name/he/hey/package.nix;
foo = ../by-name/fo/foo/package.nix;
}
*/
{ lib }:
baseDirectory:
let
inherit (builtins)
readDir
;
inherit (lib.attrsets)
mapAttrs
mapAttrsToList
mergeAttrsList
;
# Package files for a single shard
# Type: String -> String -> AttrsOf Path
namesForShard = shard: type:
if type != "directory" then
# Ignore non-directories. Technically only README.md is allowed as a file in the base directory, so we could alternatively:
# - Assume that README.md is the only file and change the condition to `shard == "README.md"` for a minor performance improvement.
# This would however cause very poor error messages if there's other files.
# - Ensure that README.md is the only file, throwing a better error message if that's not the case.
# However this would make for a poor code architecture, because one type of error would have to be duplicated in the validity checks and here.
# Additionally in either of those alternatives, we would have to duplicate the hardcoding of "README.md"
{ }
else
mapAttrs
(name: _:
let
directory = "${baseDirectory}/${shard}/${name}";
candidates.package = "${directory}/package.nix";
candidates.dream2nix = "${directory}/module.nix";
kind =
if builtins.pathExists candidates.package then "package"
else if builtins.pathExists candidates.dream2nix then "dream2nix"
else throw "No package.nix or module.nix in ${baseDirectory}/${shard}/${name}!";
in
{
inherit directory kind;
path =
if kind == "package"
then candidates.package
else candidates.dream2nix;
})
(readDir (baseDirectory + "/${shard}"));
in
mergeAttrsList (mapAttrsToList namesForShard (readDir baseDirectory))