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.
		
	
			
		
			
				
	
	
		
			107 lines
		
	
	
	
		
			3.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			107 lines
		
	
	
	
		
			3.4 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#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());
 | 
						|
 | 
						|
        if (json) {
 | 
						|
            JSONPlaceholder jsonRoot(std::cout, true);
 | 
						|
            store->pathInfoToJSON(jsonRoot,
 | 
						|
                // FIXME: preserve order?
 | 
						|
                PathSet(storePaths.begin(), storePaths.end()),
 | 
						|
                true, showClosureSize);
 | 
						|
        }
 | 
						|
 | 
						|
        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) << store->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>());
 |