summaryrefslogtreecommitdiffstats
path: root/doc/doc-support/lib-function-locations.nix
blob: 64a320f3f0ccfc1f948e50342a5c87228f12d1ed (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{
  nixpkgsPath,
  revision,
  libsetsJSON,
}:
let
  lib = import (nixpkgsPath + "/lib");
  libsets = builtins.fromJSON libsetsJSON;

  libDefPos =
    prefix: set:
    builtins.concatMap (
      name:
      [
        {
          name = builtins.concatStringsSep "." (prefix ++ [ name ]);
          location = builtins.unsafeGetAttrPos name set;
        }
      ]
      ++ lib.optionals (builtins.length prefix == 0 && builtins.isAttrs set.${name}) (
        libDefPos (prefix ++ [ name ]) set.${name}
      )
    ) (builtins.attrNames set);

  libset =
    toplib:
    map (subsetname: {
      subsetname = subsetname;
      functions = libDefPos [ ] toplib.${subsetname};
    }) (map (x: x.name) libsets);

  flattenedLibSubset =
    { subsetname, functions }:
    map (fn: {
      name = "lib.${subsetname}.${fn.name}";
      value = fn.location;
    }) functions;

  locatedlibsets = libs: map flattenedLibSubset (libset libs);
  removeFilenamePrefix =
    prefix: filename:
    let
      prefixLen = (builtins.stringLength prefix) + 1; # +1 to remove the leading /
      filenameLen = builtins.stringLength filename;
      substr = builtins.substring prefixLen filenameLen filename;
    in
    substr;

  removeNixpkgs = removeFilenamePrefix (toString nixpkgsPath);

  liblocations = builtins.filter (elem: elem.value != null) (lib.lists.flatten (locatedlibsets lib));

  fnLocationRelative =
    { name, value }:
    {
      inherit name;
      value = value // {
        file = removeNixpkgs value.file;
      };
    };

  relativeLocs = (map fnLocationRelative liblocations);
  sanitizeId = builtins.replaceStrings [ "'" ] [ "-prime" ];

  urlPrefix = "https://github.com/NixOS/nixpkgs/blob/${revision}";
  jsonLocs = builtins.listToAttrs (
    map (
      { name, value }:
      {
        name = sanitizeId name;
        value =
          let
            text = "${value.file}:${toString value.line}";
            target = "${urlPrefix}/${value.file}#L${toString value.line}";
          in
          "[${text}](${target}) in `<nixpkgs>`";
      }
    ) relativeLocs
  );

in
jsonLocs