about summary refs log tree commit diff
path: root/src/nix/path-info.cc
blob: a9b33e1877ddd2c6d967fa618a7e812fddbd1822 (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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "command.hh"
#include "shared.hh"
#include "store-api.hh"
#include "json.hh"

#include <iomanip>
#include <algorithm>

using namespace nix;

struct CmdPathInfo : StorePathsCommand
{
    bool showSize = false;
    bool showClosureSize = false;
    bool showSigs = false;
    bool json = false;

    CmdPathInfo()
    {
        mkFlag('s', "size", "print size of the NAR dump of each path", &showSize);
        mkFlag('S', "closure-size", "print sum size of the NAR dumps of the closure of each path", &showClosureSize);
        mkFlag(0, "sigs", "show signatures", &showSigs);
        mkFlag(0, "json", "produce JSON output", &json);
    }

    std::string name() override
    {
        return "path-info";
    }

    std::string description() override
    {
        return "query information about store paths";
    }

    Examples examples() override
    {
        return {
            Example{
                "To show the closure sizes of every path in the current NixOS system closure, sorted by size:",
                "nix path-info -rS /run/current-system | sort -nk2"
            },
            Example{
                "To check the existence of a path in a binary cache:",
                "nix path-info -r /nix/store/7qvk5c91...-geeqie-1.1 --store https://cache.nixos.org/"
            },
            Example{
                "To print the 10 most recently added paths (using --json and the jq(1) command):",
                "nix path-info --json --all | jq -r 'sort_by(.registrationTime)[-11:-1][].path'"
            },
            Example{
                "To show the size of the entire Nix store:",
                "nix path-info --json --all | jq 'map(.narSize) | add'"
            },
            Example{
                "To show every path whose closure is bigger than 1 GB, sorted by closure size:",
                "nix path-info --json --all -S | jq 'map(select(.closureSize > 1e9)) | sort_by(.closureSize) | map([.path, .closureSize])'"
            },
        };
    }

    void run(ref<Store> store, Paths storePaths) override
    {
        size_t pathLen = 0;
        for (auto & storePath : storePaths)
            pathLen = std::max(pathLen, storePath.size());

        auto getClosureSize = [&](const Path & storePath) -> unsigned long long {
            unsigned long long totalSize = 0;
            PathSet closure;
            store->computeFSClosure(storePath, closure, false, false);
            for (auto & p : closure)
                totalSize += store->queryPathInfo(p)->narSize;
            return totalSize;
        };

        if (json) {
            JSONList jsonRoot(std::cout, true);

            for (auto storePath : storePaths) {
                auto info = store->queryPathInfo(storePath);
                storePath = info->path;

                auto jsonPath = jsonRoot.object();
                jsonPath
                    .attr("path", storePath)
                    .attr("narHash", info->narHash.to_string())
                    .attr("narSize", info->narSize);

                if (showClosureSize)
                    jsonPath.attr("closureSize", getClosureSize(storePath));

                if (info->deriver != "")
                    jsonPath.attr("deriver", info->deriver);

                {
                    auto jsonRefs = jsonPath.list("references");
                    for (auto & ref : info->references)
                        jsonRefs.elem(ref);
                }

                if (info->registrationTime)
                    jsonPath.attr("registrationTime", info->registrationTime);

                if (info->ultimate)
                    jsonPath.attr("ultimate", info->ultimate);

                if (info->ca != "")
                    jsonPath.attr("ca", info->ca);

                if (!info->sigs.empty()) {
                    auto jsonSigs = jsonPath.list("signatures");
                    for (auto & sig : info->sigs)
                        jsonSigs.elem(sig);
                }
            }
        }

        else {

            for (auto storePath : storePaths) {
                auto info = store->queryPathInfo(storePath);
                storePath = info->path; // FIXME: screws up padding

                std::cout << storePath << std::string(std::max(0, (int) pathLen - (int) storePath.size()), ' ');

                if (showSize)
                    std::cout << '\t' << std::setw(11) << info->narSize;

                if (showClosureSize)
                    std::cout << '\t' << std::setw(11) << getClosureSize(storePath);

                if (showSigs) {
                    std::cout << '\t';
                    Strings ss;
                    if (info->ultimate) ss.push_back("ultimate");
                    if (info->ca != "") ss.push_back("ca:" + info->ca);
                    for (auto & sig : info->sigs) ss.push_back(sig);
                    std::cout << concatStringsSep(" ", ss);
                }

                std::cout << std::endl;
            }

        }

    }
};

static RegisterCommand r1(make_ref<CmdPathInfo>());