From 9e9e34ff7f28215fa9a275ae568a42aa0176c2cf Mon Sep 17 00:00:00 2001 From: Ryan Yin Date: Tue, 5 Mar 2024 14:36:14 +0800 Subject: [PATCH] feat: Why some packages are installed --- docs/nixos-with-flakes/other-useful-tips.md | 13 +++++++++---- docs/zh/nixos-with-flakes/other-useful-tips.md | 12 ++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/docs/nixos-with-flakes/other-useful-tips.md b/docs/nixos-with-flakes/other-useful-tips.md index 5819fc4..6b3ad1c 100644 --- a/docs/nixos-with-flakes/other-useful-tips.md +++ b/docs/nixos-with-flakes/other-useful-tips.md @@ -71,11 +71,16 @@ sudo nix profile wipe-history --older-than 7d --profile /nix/var/nix/profiles/sy sudo nix store gc --debug ``` -Another command that returns all packages installed in the system is: -```shell -nix-env -qa -``` +## Why some packages are installed? + +To find out why a package is installed, you can use the following command: + +1. Enter a shell with `nix-tree` available: `nix shell nixpkgs#nix-tree` +1. ` nix-store --gc --print-roots | rg -v '/proc/' | rg -Po '(?<= -> ).*' | xargs -o nix-tree` +1. `/` to find the package you want to check. +1. `w` to show the package is depended by which packages, and the full dependency chain. + ## Reducing Disk Usage diff --git a/docs/zh/nixos-with-flakes/other-useful-tips.md b/docs/zh/nixos-with-flakes/other-useful-tips.md index ee6e61e..52275b4 100644 --- a/docs/zh/nixos-with-flakes/other-useful-tips.md +++ b/docs/zh/nixos-with-flakes/other-useful-tips.md @@ -68,11 +68,15 @@ sudo nix profile wipe-history --older-than 7d --profile /nix/var/nix/profiles/sy sudo nix store gc --debug ``` -以及查看系统层面安装的所有软件包(这个貌似只能用 `nix-env`): +## 查询为什么某个包被安装了 {#why-some-packages-are-installed} + +查询为什么某个包被安装,当前环境中的谁依赖了它: + +1. 进入一个带有 `nix-tree` 的 shell:`nix shell nixpkgs#nix-tree` +1. ` nix-store --gc --print-roots | rg -v '/proc/' | rg -Po '(?<= -> ).*' | xargs -o nix-tree` +1. `/` 以查找到你想查询的包 +1. 输入 `w`,看看谁依赖了它(`why depends`),以及完整的依赖链。 -```shell -nix-env -qa -``` # 节约存储空间