about summary refs log tree commit diff
path: root/tools/nixery/group-layers/popcount.nix
blob: e21d7367724b312196ef1252622168b465159059 (plain) (blame)
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
{ pkgs ? import <nixpkgs> { config.allowUnfree = false; }
, target }:

let
  inherit (pkgs) coreutils runCommand writeText;
  inherit (builtins) replaceStrings readFile toFile fromJSON toJSON foldl' listToAttrs;

  path = [ pkgs."${target}" ];

  # graphJSON abuses feature in Nix that makes structured runtime
  # closure information available to builders. This data is imported
  # back via IFD to process it for layering data.
  graphJSON =
    path:
    runCommand "build-graph" {
      __structuredAttrs = true;
      exportReferencesGraph.graph = path;
      PATH = "${coreutils}/bin";
      builder = toFile "builder" ''
        . .attrs.sh
        cat .attrs.json > ''${outputs[out]}
      '';
    } "";

  buildClosures = paths: (fromJSON (readFile (graphJSON paths)));

  buildGraph = paths: listToAttrs (map (c: {
    name = c.path;
    value = {
    inherit (c) closureSize references;
    };
  }) (buildClosures paths));

  # Nix does not allow attrbute set keys to refer to store paths, but
  # we need them to for the purpose of the calculation. To work around
  # it, the store path prefix is replaced with the string 'closure/'
  # and later replaced again.
  fromStorePath = replaceStrings [ "/nix/store" ] [ "closure/" ];
  toStorePath = replaceStrings [ "closure/" ] [ "/nix/store/" ];

  buildTree = paths:
  let
    graph = buildGraph paths;
    top = listToAttrs (map (p: {
      name = fromStorePath (toString p);
      value = {};
    }) paths);
  in top;

  outputJson = thing: writeText "the-thing.json" (builtins.toJSON thing);
in outputJson (buildClosures path).graph