aboutsummaryrefslogtreecommitdiff
path: root/src/nix/path-info.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/nix/path-info.cc')
-rw-r--r--src/nix/path-info.cc30
1 files changed, 27 insertions, 3 deletions
diff --git a/src/nix/path-info.cc b/src/nix/path-info.cc
index 47caa401d..916ed360e 100644
--- a/src/nix/path-info.cc
+++ b/src/nix/path-info.cc
@@ -4,7 +4,6 @@
#include "json.hh"
#include "common-args.hh"
-#include <iomanip>
#include <algorithm>
using namespace nix;
@@ -13,12 +12,14 @@ struct CmdPathInfo : StorePathsCommand, MixJSON
{
bool showSize = false;
bool showClosureSize = false;
+ bool humanReadable = false;
bool showSigs = 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('h', "human-readable", "with -s and -S, print sizes like 1K 234M 5.67G etc.", &humanReadable);
mkFlag(0, "sigs", "show signatures", &showSigs);
}
@@ -40,6 +41,10 @@ struct CmdPathInfo : StorePathsCommand, MixJSON
"nix path-info -rS /run/current-system | sort -nk2"
},
Example{
+ "To show a package's closure size and all its dependencies with human readable sizes:",
+ "nix path-info -rsSh nixpkgs.rust"
+ },
+ 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/"
},
@@ -58,6 +63,25 @@ struct CmdPathInfo : StorePathsCommand, MixJSON
};
}
+ void printSize(unsigned long long value)
+ {
+ if (!humanReadable) {
+ std::cout << fmt("\t%11d", value);
+ return;
+ }
+
+ static constexpr std::array<char, 9> idents = {
+ ' ', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
+ };
+ size_t power = 0;
+ double res = value;
+ while (res > 1024 && power < idents.size()) {
+ ++power;
+ res /= 1024;
+ }
+ std::cout << fmt("\t%6.1f%c", res, idents.at(power));
+ }
+
void run(ref<Store> store, Paths storePaths) override
{
size_t pathLen = 0;
@@ -81,10 +105,10 @@ struct CmdPathInfo : StorePathsCommand, MixJSON
std::cout << storePath << std::string(std::max(0, (int) pathLen - (int) storePath.size()), ' ');
if (showSize)
- std::cout << '\t' << std::setw(11) << info->narSize;
+ printSize(info->narSize);
if (showClosureSize)
- std::cout << '\t' << std::setw(11) << store->getClosureSize(storePath).first;
+ printSize(store->getClosureSize(storePath).first);
if (showSigs) {
std::cout << '\t';