2016-04-21 15:59:50 +03:00
|
|
|
#include "command.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
2016-08-26 19:55:55 +03:00
|
|
|
#include "json.hh"
|
2017-04-24 15:21:36 +03:00
|
|
|
#include "common-args.hh"
|
2016-08-26 19:55:55 +03:00
|
|
|
|
2016-04-21 18:53:47 +03:00
|
|
|
#include <algorithm>
|
2018-08-31 20:20:08 +03:00
|
|
|
#include <array>
|
2016-04-21 15:59:50 +03:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2017-04-24 15:21:36 +03:00
|
|
|
struct CmdPathInfo : StorePathsCommand, MixJSON
|
2016-04-21 15:59:50 +03:00
|
|
|
{
|
|
|
|
bool showSize = false;
|
|
|
|
bool showClosureSize = false;
|
2018-08-26 01:35:15 +03:00
|
|
|
bool humanReadable = false;
|
2016-04-22 15:39:37 +03:00
|
|
|
bool showSigs = false;
|
2016-04-21 15:59:50 +03:00
|
|
|
|
|
|
|
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);
|
2018-08-30 17:18:53 +03:00
|
|
|
mkFlag('h', "human-readable", "with -s and -S, print sizes like 1K 234M 5.67G etc.", &humanReadable);
|
2016-04-22 15:39:37 +03:00
|
|
|
mkFlag(0, "sigs", "show signatures", &showSigs);
|
2016-04-21 15:59:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string description() override
|
|
|
|
{
|
|
|
|
return "query information about store paths";
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:18:23 +03:00
|
|
|
Category category() override { return catSecondary; }
|
|
|
|
|
2016-04-21 15:59:50 +03:00
|
|
|
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"
|
|
|
|
},
|
2018-08-26 01:35:15 +03:00
|
|
|
Example{
|
|
|
|
"To show a package's closure size and all its dependencies with human readable sizes:",
|
2020-07-15 21:28:16 +03:00
|
|
|
"nix path-info -rsSh nixpkgs#rust"
|
2018-08-26 01:35:15 +03:00
|
|
|
},
|
2016-04-21 15:59:50 +03:00
|
|
|
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/"
|
|
|
|
},
|
2016-08-26 19:55:55 +03:00
|
|
|
Example{
|
|
|
|
"To print the 10 most recently added paths (using --json and the jq(1) command):",
|
2016-09-14 19:20:11 +03:00
|
|
|
"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])'"
|
2016-08-26 19:55:55 +03:00
|
|
|
},
|
2016-04-21 15:59:50 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-30 17:18:53 +03:00
|
|
|
void printSize(unsigned long long value)
|
2018-08-26 01:35:15 +03:00
|
|
|
{
|
|
|
|
if (!humanReadable) {
|
2018-08-30 17:18:53 +03:00
|
|
|
std::cout << fmt("\t%11d", value);
|
2018-08-26 01:35:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:30:45 +03:00
|
|
|
static const std::array<char, 9> idents{{
|
2018-08-27 01:12:06 +03:00
|
|
|
' ', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
|
2018-08-31 18:30:45 +03:00
|
|
|
}};
|
2018-08-26 01:35:15 +03:00
|
|
|
size_t power = 0;
|
|
|
|
double res = value;
|
2018-08-27 01:12:06 +03:00
|
|
|
while (res > 1024 && power < idents.size()) {
|
2018-08-26 01:35:15 +03:00
|
|
|
++power;
|
|
|
|
res /= 1024;
|
|
|
|
}
|
2018-08-30 17:18:53 +03:00
|
|
|
std::cout << fmt("\t%6.1f%c", res, idents.at(power));
|
2018-08-26 01:35:15 +03:00
|
|
|
}
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
void run(ref<Store> store, StorePaths storePaths) override
|
2016-04-21 15:59:50 +03:00
|
|
|
{
|
|
|
|
size_t pathLen = 0;
|
|
|
|
for (auto & storePath : storePaths)
|
2019-12-05 20:11:09 +02:00
|
|
|
pathLen = std::max(pathLen, store->printStorePath(storePath).size());
|
2016-04-21 15:59:50 +03:00
|
|
|
|
2016-08-26 19:55:55 +03:00
|
|
|
if (json) {
|
2017-11-14 15:27:01 +02:00
|
|
|
JSONPlaceholder jsonRoot(std::cout);
|
exportReferencesGraph: Export more complete info in JSON format
This writes info about every path in the closure in the same format as
‘nix path-info --json’. Thus it also includes NAR hashes and sizes.
Example:
[
{
"path": "/nix/store/10h6li26i7g6z3mdpvra09yyf10mmzdr-hello-2.10",
"narHash": "sha256:0ckdc4z20kkmpqdilx0wl6cricxv90lh85xpv2qljppcmz6vzcxl",
"narSize": 197648,
"references": [
"/nix/store/10h6li26i7g6z3mdpvra09yyf10mmzdr-hello-2.10",
"/nix/store/27binbdy296qvjycdgr1535v8872vz3z-glibc-2.24"
],
"closureSize": 20939776
},
{
"path": "/nix/store/27binbdy296qvjycdgr1535v8872vz3z-glibc-2.24",
"narHash": "sha256:1nfn3m3p98y1c0kd0brp80dn9n5mycwgrk183j17rajya0h7gax3",
"narSize": 20742128,
"references": [
"/nix/store/27binbdy296qvjycdgr1535v8872vz3z-glibc-2.24"
],
"closureSize": 20742128
}
]
Fixes #1134.
2017-01-26 21:36:20 +02:00
|
|
|
store->pathInfoToJSON(jsonRoot,
|
|
|
|
// FIXME: preserve order?
|
2020-06-16 23:20:18 +03:00
|
|
|
StorePathSet(storePaths.begin(), storePaths.end()),
|
2020-02-12 00:50:16 +02:00
|
|
|
true, showClosureSize, SRI, AllowInvalid);
|
2016-08-26 19:55:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
for (auto & storePath : storePaths) {
|
2016-08-26 19:55:55 +03:00
|
|
|
auto info = store->queryPathInfo(storePath);
|
2019-12-05 20:11:09 +02:00
|
|
|
auto storePathS = store->printStorePath(storePath);
|
2016-04-21 15:59:50 +03:00
|
|
|
|
2019-12-05 20:11:09 +02:00
|
|
|
std::cout << storePathS;
|
2018-09-01 01:04:57 +03:00
|
|
|
|
|
|
|
if (showSize || showClosureSize || showSigs)
|
2019-12-05 20:11:09 +02:00
|
|
|
std::cout << std::string(std::max(0, (int) pathLen - (int) storePathS.size()), ' ');
|
2016-08-26 19:55:55 +03:00
|
|
|
|
|
|
|
if (showSize)
|
2018-08-26 01:35:15 +03:00
|
|
|
printSize(info->narSize);
|
2016-08-26 19:55:55 +03:00
|
|
|
|
|
|
|
if (showClosureSize)
|
2019-12-05 20:11:09 +02:00
|
|
|
printSize(store->getClosureSize(info->path).first);
|
2016-08-26 19:55:55 +03:00
|
|
|
|
|
|
|
if (showSigs) {
|
|
|
|
std::cout << '\t';
|
|
|
|
Strings ss;
|
|
|
|
if (info->ultimate) ss.push_back("ultimate");
|
2020-06-02 03:37:43 +03:00
|
|
|
if (info->ca) ss.push_back("ca:" + renderContentAddress(*info->ca));
|
2016-08-26 19:55:55 +03:00
|
|
|
for (auto & sig : info->sigs) ss.push_back(sig);
|
|
|
|
std::cout << concatStringsSep(" ", ss);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
2016-04-22 15:39:37 +03:00
|
|
|
}
|
|
|
|
|
2016-04-21 15:59:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-06-18 17:01:35 +03:00
|
|
|
static auto r1 = registerCommand<CmdPathInfo>("path-info");
|