From 3407a5d9361611ea8b5da9e02a7a1e2f90c928cd Mon Sep 17 00:00:00 2001 From: Benjamin Hipple Date: Sat, 25 Aug 2018 18:35:15 -0400 Subject: [PATCH] Add human readable closure sizes to nix path-info Unfortunately, -h is already taken as a short option by --help, so we have to use a different letter or the capitalized version. Resolves #2363 --- src/nix/path-info.cc | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/nix/path-info.cc b/src/nix/path-info.cc index 47caa401d..ee48d483c 100644 --- a/src/nix/path-info.cc +++ b/src/nix/path-info.cc @@ -13,12 +13,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); } @@ -39,6 +41,10 @@ struct CmdPathInfo : StorePathsCommand, MixJSON "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 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 +64,25 @@ struct CmdPathInfo : StorePathsCommand, MixJSON }; } + void printSize(int value) + { + if (!humanReadable) { + std::cout << '\t' << std::setw(11) << value; + return; + } + + static constexpr std::array idents = { + ' ', 'K', 'M', 'G', 'T', 'P' + }; + size_t power = 0; + double res = value; + while (res > 1024) { + ++power; + res /= 1024; + } + std::cout << '\t' << std::setw(11) << std::setprecision(3) << res << idents[power]; + } + void run(ref store, Paths storePaths) override { size_t pathLen = 0; @@ -81,10 +106,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';