init
This commit is contained in:
24 files changed
+3822
No files matched your search
@@ -0,0 +1,2 @@
|
||||
result
|
||||
result-*
|
||||
@@ -0,0 +1,26 @@
|
||||
# i-am-gcroot
|
||||
|
||||
Go tool (TUI via bubbletea, optional htmx web UI) to list, filter and delete Nix GC roots. User docs are in README.md.
|
||||
|
||||
## Layout
|
||||
|
||||
- `main.go`: subcommands (`tui` default, `list`, `rm`, `gc`, `web`, `version`) with stdlib `flag`.
|
||||
- `internal/roots`: discovery (`scan.go`, mirrors `findRoots` in libstore `gc.cc`: gcroots tree + `auto/` indirect roots + profiles dir; runtime roots via `nix-store --gc --print-roots`), `Set` (thread-safe shared state used by TUI and web, background size computation), `size.go` (`nix path-info --json -S`, accepts both object and array JSON formats), `delete.go` (only removes symlinks; profile generations via `nix-env --delete-generations`; protected roots need `Force`), `collect.go` (targeted GC: closure of deleted targets minus closure of all `--print-roots` targets, then `nix-store --delete`, retrying without paths nix reports alive). `Root.Hidden` marks nix cache roots (`hiddenPatterns` in `scan.go`); `filter.Apply` drops them unless `showHidden` or the query has `is:hidden`.
|
||||
- `internal/filter`: query language and sorting shared by all frontends.
|
||||
- `internal/tui`: bubbletea model (`model.go`) and rendering (`view.go`).
|
||||
- `internal/web`: HTTP server, `templates/index.html` (page + `rows` fragment), `static/htmx.min.js` (vendored htmx 2.0.4, embedded).
|
||||
- `nix/vm-test.nix` + `nix/vm-test.py`: NixOS VM integration test. The Python lives in its own file because nix `''` strings do not strip tab indentation.
|
||||
|
||||
`roots.Config` holds store/state dirs and binary names so tests run against a fake tree in a temp dir.
|
||||
|
||||
## Development
|
||||
|
||||
```
|
||||
nix develop # go, gopls (CGO_ENABLED=0)
|
||||
go test ./...
|
||||
nix build # runs go tests too
|
||||
nix build .#checks.x86_64-linux.vm -L # VM test
|
||||
nix flake check
|
||||
```
|
||||
|
||||
When Go dependencies change, set `vendorHash` in `flake.nix` to a fake hash, run `nix build`, and copy the `got:` hash. Nix files are indented with tabs.
|
||||
@@ -0,0 +1,103 @@
|
||||
# i-am-gcroot
|
||||
|
||||
List, filter and delete Nix garbage collector roots from a TUI, an optional web UI, or the command line.
|
||||
|
||||
```
|
||||
nix run github:illustris/i-am-gcroot # TUI
|
||||
nix run . -- --web # TUI + web UI on 127.0.0.1:8377
|
||||
nix run . -- list -q 'under:/tmp older:7d' --sizes
|
||||
```
|
||||
|
||||
## What it finds
|
||||
|
||||
| Kind | What it is | Delete does |
|
||||
|---|---|---|
|
||||
| `indirect` | a link outside the gcroots tree (`./result`, `.gcroots/foo`) registered via `/nix/var/nix/gcroots/auto` | removes the link (and the `auto/` entry if writable; `nix-store --gc` prunes it otherwise) |
|
||||
| `dangling` | an `auto/` entry whose link is already gone | removes the `auto/` entry (needs root) |
|
||||
| `profile` | a profile generation, `<profile>-<N>-link`, system or user | `nix-env --profile <p> --delete-generations N` |
|
||||
| `direct` | any other symlink in `/nix/var/nix/gcroots` | removes the link |
|
||||
| `runtime` | store paths held by running processes (`/proc/...`), grouped per PID | never deletable; hidden unless asked for |
|
||||
|
||||
Protected roots (marked `!`) are refused unless `--force` is given: the current generation of every profile, and `gcroots/current-system` / `gcroots/booted-system`.
|
||||
|
||||
Only symlinks are ever removed. Nothing in `/nix/store` is touched until you run GC: either a full `nix-store --gc` (`g` in the TUI, the "run gc" button, `i-am-gcroot gc`) or a targeted collect right after deleting (`X` in the TUI, "delete + collect", `rm -c`).
|
||||
|
||||
Targeted collect takes the closures of the roots just deleted, subtracts everything still reachable from any GC root (including runtime roots), and runs `nix-store --delete` on the rest. Nix re-checks liveness itself, so a path that turns out to be alive (for example through `keep-derivations`) is kept and reported. It takes tens of seconds rather than a walk of the whole store.
|
||||
|
||||
Roots that nix creates for its own caches (anything under `.cache/nix/`, like `~/.cache/nix/flake-registry.json`) are hidden by default. Show them with `h` in the TUI, the "hidden" checkbox, `--all`, or the `is:hidden` query term.
|
||||
|
||||
Closure sizes come from `nix path-info -S` and load in the background. The "up to" figure for a selection is the sum of closures; shared paths may stay alive through other roots, so the real amount freed is usually less.
|
||||
|
||||
## Filter syntax
|
||||
|
||||
Terms are whitespace separated and ANDed. Prefix any term with `-` to negate it.
|
||||
|
||||
| Term | Matches |
|
||||
|---|---|
|
||||
| `foo` | case-insensitive substring of link, target, via or command |
|
||||
| `/re/` | regex over the same fields (no spaces, use `\s`; terms with more than two `/` are substrings) |
|
||||
| `kind:indirect,dangling` | kinds (prefixes like `kind:prof` work) |
|
||||
| `owner:me`, `owner:alice`, `owner:1000` | owner of the link |
|
||||
| `under:/tmp/claude-` | link starts with this prefix |
|
||||
| `older:30d`, `newer:2h` | link mtime (`s m h d w y`) |
|
||||
| `size>1G`, `size<10M` | closure size (`K M G T`) |
|
||||
| `is:protected`, `is:current`, `is:dangling`, `is:hidden` | flags (`is:hidden` also makes hidden roots visible) |
|
||||
|
||||
## TUI keys
|
||||
|
||||
| Key | Action |
|
||||
|---|---|
|
||||
| `j/k`, arrows, `pgup/pgdn`, `home/end` | move |
|
||||
| `space` / `x` | mark / unmark |
|
||||
| `a` / `A` | mark all shown (non-protected) / clear marks |
|
||||
| `d` | delete marked roots (or the one under the cursor), with confirmation |
|
||||
| `X` | delete, then garbage collect only the store paths those roots kept alive |
|
||||
| `D` | dry run |
|
||||
| `g` | run `nix-store --gc` |
|
||||
| `/` | edit filter (`enter`/`esc` to leave, `esc` again clears it) |
|
||||
| `1`-`5`, `0` | toggle kinds, reset kinds |
|
||||
| `h` | show/hide nix cache roots |
|
||||
| `s` / `S` | cycle sort key (age, size, target, kind, link) / reverse |
|
||||
| `enter` | details |
|
||||
| `R` | full results of the last delete or dry run |
|
||||
| `r` | rescan |
|
||||
| `?` | help |
|
||||
|
||||
Below the key hints, an activity line shows the running operation with a timer, then a one-line outcome (failures included); `R` shows the per-root details. The list is rescanned from disk after every delete.
|
||||
|
||||
## CLI
|
||||
|
||||
```
|
||||
i-am-gcroot [tui] [-q QUERY] [--web] [--addr ADDR] [--no-auth] [--runtime] [--force]
|
||||
i-am-gcroot list [-q QUERY] [--kind K,..] [--sort KEY] [--desc] [--sizes] [--json] [--runtime] [--all]
|
||||
i-am-gcroot rm [-n] [-y] [-c] [--force] [--all] (-q QUERY | LINK...)
|
||||
i-am-gcroot gc
|
||||
i-am-gcroot web [--addr ADDR] [--no-auth] [--force] [--runtime]
|
||||
```
|
||||
|
||||
`rm` asks for confirmation unless `-y`; `-n` is a dry run; `-c` runs the targeted collect afterwards; `--all` lets `-q` match hidden roots (explicit link arguments always work).
|
||||
|
||||
## Web UI
|
||||
|
||||
`i-am-gcroot web` (headless) or `--web` alongside the TUI. It binds to `127.0.0.1:8377` and generates a random token at startup; open the printed URL (`?token=...`), which sets a cookie. POST requests additionally require the token in an `X-Token` or `Authorization: Bearer` header (the page sends it), so a cross-site form cannot trigger deletes. `--no-auth` disables this; only use it on a trusted socket.
|
||||
|
||||
JSON API (with `Authorization: Bearer <token>`):
|
||||
|
||||
- `GET /api/roots?q=...&kind=indirect&kind=profile&sort=size&desc=1&hidden=1`
|
||||
- `POST /api/delete` with `{"links": ["/home/me/result"], "dryRun": false, "collect": true}`, returns `{"results": [...], "collected": {...}}`
|
||||
- `POST /api/gc`
|
||||
|
||||
## Permissions
|
||||
|
||||
Run as your user to manage your own roots. Root-owned roots (system profile generations, `auto/` entries of dangling roots, other users' links) fail with a hint to rerun under `sudo`. Runtime roots of other users' processes show as `{censored}` unless run as root.
|
||||
|
||||
## NixOS
|
||||
|
||||
```nix
|
||||
{
|
||||
inputs.i-am-gcroot.url = "github:illustris/i-am-gcroot";
|
||||
# ...
|
||||
imports = [ inputs.i-am-gcroot.nixosModules.default ];
|
||||
programs.i-am-gcroot.enable = true;
|
||||
}
|
||||
```
|
||||
Generated
+27
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1792773339,
|
||||
"narHash": "sha256-rZLrzrC6i+jnTAlvPZHogKDulY7WWDEv9hJeHieO7Zw=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "3ec636362e1816bc2f0888fa0fa2c69f1bc9e504",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixos-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
{
|
||||
description = "i-am-gcroot: list, filter and delete Nix GC roots from a TUI or web UI";
|
||||
|
||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||
|
||||
outputs = { self, nixpkgs }:
|
||||
let
|
||||
systems = [ "x86_64-linux" "aarch64-linux" ];
|
||||
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f nixpkgs.legacyPackages.${system});
|
||||
version = "0.1.0";
|
||||
in {
|
||||
packages = forAllSystems (pkgs: rec {
|
||||
i-am-gcroot = pkgs.buildGoModule {
|
||||
pname = "i-am-gcroot";
|
||||
inherit version;
|
||||
src = pkgs.lib.fileset.toSource {
|
||||
root = ./.;
|
||||
fileset = pkgs.lib.fileset.unions [
|
||||
./go.mod
|
||||
./go.sum
|
||||
./main.go
|
||||
./internal
|
||||
];
|
||||
};
|
||||
vendorHash = "sha256-RNbS40G+8rtwlSJgYLN1puTCytGfXdagQTEs6sIXwnM=";
|
||||
env.CGO_ENABLED = "0";
|
||||
ldflags = [ "-s" "-w" "-X main.version=${version}" ];
|
||||
meta = {
|
||||
description = "TUI and web UI to list, filter and delete Nix GC roots";
|
||||
mainProgram = "i-am-gcroot";
|
||||
platforms = pkgs.lib.platforms.linux;
|
||||
};
|
||||
};
|
||||
default = i-am-gcroot;
|
||||
});
|
||||
|
||||
apps = forAllSystems (pkgs: {
|
||||
default = {
|
||||
type = "app";
|
||||
program = "${self.packages.${pkgs.system}.default}/bin/i-am-gcroot";
|
||||
};
|
||||
});
|
||||
|
||||
devShells = forAllSystems (pkgs: {
|
||||
default = pkgs.mkShell {
|
||||
packages = with pkgs; [ go gopls gotools ];
|
||||
CGO_ENABLED = "0";
|
||||
};
|
||||
});
|
||||
|
||||
nixosModules.default = { config, lib, pkgs, ... }: {
|
||||
options.programs.i-am-gcroot.enable = lib.mkEnableOption "i-am-gcroot";
|
||||
config = lib.mkIf config.programs.i-am-gcroot.enable {
|
||||
environment.systemPackages = [ self.packages.${pkgs.system}.default ];
|
||||
};
|
||||
};
|
||||
|
||||
checks = forAllSystems (pkgs: {
|
||||
package = self.packages.${pkgs.system}.default;
|
||||
vm = import ./nix/vm-test.nix { inherit pkgs self; };
|
||||
});
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
module github.com/illustris/i-am-gcroot
|
||||
|
||||
go 1.24.0
|
||||
|
||||
require (
|
||||
github.com/charmbracelet/bubbles v0.21.0
|
||||
github.com/charmbracelet/bubbletea v1.3.10
|
||||
github.com/charmbracelet/lipgloss v1.1.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/atotto/clipboard v0.1.4 // indirect
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect
|
||||
github.com/charmbracelet/x/ansi v0.10.1 // indirect
|
||||
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
|
||||
github.com/charmbracelet/x/term v0.2.1 // indirect
|
||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mattn/go-localereader v0.0.1 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.16 // indirect
|
||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
|
||||
github.com/muesli/cancelreader v0.2.2 // indirect
|
||||
github.com/muesli/termenv v0.16.0 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
|
||||
golang.org/x/sys v0.36.0 // indirect
|
||||
golang.org/x/text v0.3.8 // indirect
|
||||
)
|
||||
@@ -0,0 +1,47 @@
|
||||
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
|
||||
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
|
||||
github.com/charmbracelet/bubbles v0.21.0 h1:9TdC97SdRVg/1aaXNVWfFH3nnLAwOXr8Fn6u6mfQdFs=
|
||||
github.com/charmbracelet/bubbles v0.21.0/go.mod h1:HF+v6QUR4HkEpz62dx7ym2xc71/KBHg+zKwJtMw+qtg=
|
||||
github.com/charmbracelet/bubbletea v1.3.10 h1:otUDHWMMzQSB0Pkc87rm691KZ3SWa4KUlvF9nRvCICw=
|
||||
github.com/charmbracelet/bubbletea v1.3.10/go.mod h1:ORQfo0fk8U+po9VaNvnV95UPWA1BitP1E0N6xJPlHr4=
|
||||
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs=
|
||||
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk=
|
||||
github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY=
|
||||
github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30=
|
||||
github.com/charmbracelet/x/ansi v0.10.1 h1:rL3Koar5XvX0pHGfovN03f5cxLbCF2YvLeyz7D2jVDQ=
|
||||
github.com/charmbracelet/x/ansi v0.10.1/go.mod h1:3RQDQ6lDnROptfpWuUVIUG64bD2g2BgntdxH0Ya5TeE=
|
||||
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8=
|
||||
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs=
|
||||
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
|
||||
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
|
||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
|
||||
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
|
||||
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
|
||||
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
|
||||
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
|
||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
|
||||
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
||||
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
||||
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
|
||||
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
|
||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
|
||||
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
|
||||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
|
||||
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
|
||||
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
|
||||
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
|
||||
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
|
||||
@@ -0,0 +1,307 @@
|
||||
// Package filter parses the query language shared by the TUI and web UI.
|
||||
//
|
||||
// A query is a whitespace separated list of terms, all of which must match:
|
||||
//
|
||||
// foo substring of link, target, via or command (case-insensitive)
|
||||
// /re/ regular expression over the same fields (no spaces; use \s)
|
||||
// -term negates any term
|
||||
// kind:K indirect, profile, direct, dangling, runtime (comma separated)
|
||||
// owner:U owner name or uid; "me" is the current user
|
||||
// under:/path link starts with /path
|
||||
// older:30d modified more than 30 days ago (s, m, h, d, w, y)
|
||||
// newer:2h modified less than 2 hours ago
|
||||
// size>500M closure size bigger than 500 MiB (K, M, G, T; also <)
|
||||
// is:protected protected roots; is:current for current generations
|
||||
// is:hidden nix cache roots (~/.cache/nix/...), shown only when asked for
|
||||
package filter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/user"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/illustris/i-am-gcroot/internal/roots"
|
||||
)
|
||||
|
||||
type pred func(r *roots.Root, now time.Time) bool
|
||||
|
||||
type Query struct {
|
||||
Raw string
|
||||
preds []pred
|
||||
// ShowHidden is set when the query mentions is:hidden, which would
|
||||
// otherwise never match because hidden roots are dropped first.
|
||||
ShowHidden bool
|
||||
}
|
||||
|
||||
func Parse(q string) (*Query, error) {
|
||||
out := &Query{Raw: q}
|
||||
for _, tok := range strings.Fields(q) {
|
||||
neg := false
|
||||
if strings.HasPrefix(tok, "-") && len(tok) > 1 {
|
||||
neg, tok = true, tok[1:]
|
||||
}
|
||||
p, err := parseTerm(tok)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if tok == "is:hidden" {
|
||||
out.ShowHidden = true
|
||||
}
|
||||
if neg {
|
||||
inner := p
|
||||
p = func(r *roots.Root, now time.Time) bool { return !inner(r, now) }
|
||||
}
|
||||
out.preds = append(out.preds, p)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func haystack(r *roots.Root) string {
|
||||
return r.Link + "\x00" + r.Target + "\x00" + r.Via + "\x00" + r.Command
|
||||
}
|
||||
|
||||
func parseTerm(tok string) (pred, error) {
|
||||
if isRegex(tok) {
|
||||
re, err := regexp.Compile("(?i)" + tok[1:len(tok)-1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("bad regex %s: %w", tok, err)
|
||||
}
|
||||
return func(r *roots.Root, _ time.Time) bool { return re.MatchString(haystack(r)) }, nil
|
||||
}
|
||||
if strings.HasPrefix(tok, "size>") || strings.HasPrefix(tok, "size<") {
|
||||
n, err := ParseSize(tok[5:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if tok[4] == '>' {
|
||||
return func(r *roots.Root, _ time.Time) bool { return r.ClosureSize > n }, nil
|
||||
}
|
||||
return func(r *roots.Root, _ time.Time) bool { return r.ClosureSize >= 0 && r.ClosureSize < n }, nil
|
||||
}
|
||||
key, val, ok := strings.Cut(tok, ":")
|
||||
if ok && val != "" {
|
||||
switch key {
|
||||
case "kind", "k":
|
||||
kinds := map[roots.Kind]bool{}
|
||||
for _, k := range strings.Split(val, ",") {
|
||||
kk, err := ParseKind(k)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
kinds[kk] = true
|
||||
}
|
||||
return func(r *roots.Root, _ time.Time) bool { return kinds[r.Kind] }, nil
|
||||
case "owner", "user":
|
||||
if val == "me" {
|
||||
if u, err := user.Current(); err == nil {
|
||||
val = u.Username
|
||||
}
|
||||
}
|
||||
return func(r *roots.Root, _ time.Time) bool {
|
||||
return r.Owner == val || strconv.Itoa(r.UID) == val
|
||||
}, nil
|
||||
case "under", "in":
|
||||
return func(r *roots.Root, _ time.Time) bool { return strings.HasPrefix(r.Link, val) }, nil
|
||||
case "older", "newer":
|
||||
d, err := ParseDuration(val)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if key == "older" {
|
||||
return func(r *roots.Root, now time.Time) bool { return !r.ModTime.IsZero() && now.Sub(r.ModTime) > d }, nil
|
||||
}
|
||||
return func(r *roots.Root, now time.Time) bool { return now.Sub(r.ModTime) < d }, nil
|
||||
case "is":
|
||||
switch val {
|
||||
case "protected":
|
||||
return func(r *roots.Root, _ time.Time) bool { return r.Protected }, nil
|
||||
case "current":
|
||||
return func(r *roots.Root, _ time.Time) bool { return r.Current }, nil
|
||||
case "dangling":
|
||||
return func(r *roots.Root, _ time.Time) bool { return r.Kind == roots.KindDangling }, nil
|
||||
case "hidden":
|
||||
return func(r *roots.Root, _ time.Time) bool { return r.Hidden }, nil
|
||||
}
|
||||
return nil, fmt.Errorf("unknown is:%s (protected, current, dangling, hidden)", val)
|
||||
}
|
||||
}
|
||||
lower := strings.ToLower(tok)
|
||||
return func(r *roots.Root, _ time.Time) bool {
|
||||
return strings.Contains(strings.ToLower(haystack(r)), lower)
|
||||
}, nil
|
||||
}
|
||||
|
||||
// isRegex distinguishes /re/ from a plain path like /tmp/foo, which is
|
||||
// matched as a substring.
|
||||
func isRegex(tok string) bool {
|
||||
return len(tok) > 2 && tok[0] == '/' && tok[len(tok)-1] == '/' && strings.Count(tok, "/") == 2
|
||||
}
|
||||
|
||||
func ParseKind(s string) (roots.Kind, error) {
|
||||
for _, k := range roots.AllKinds {
|
||||
if strings.HasPrefix(string(k), s) && s != "" {
|
||||
return k, nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("unknown kind %q", s)
|
||||
}
|
||||
|
||||
func ParseDuration(s string) (time.Duration, error) {
|
||||
if len(s) < 2 {
|
||||
return 0, fmt.Errorf("bad duration %q", s)
|
||||
}
|
||||
n, err := strconv.ParseFloat(s[:len(s)-1], 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("bad duration %q", s)
|
||||
}
|
||||
unit := map[byte]time.Duration{
|
||||
's': time.Second, 'm': time.Minute, 'h': time.Hour,
|
||||
'd': 24 * time.Hour, 'w': 7 * 24 * time.Hour, 'y': 365 * 24 * time.Hour,
|
||||
}[s[len(s)-1]]
|
||||
if unit == 0 {
|
||||
return 0, fmt.Errorf("bad duration unit in %q (s, m, h, d, w, y)", s)
|
||||
}
|
||||
return time.Duration(n * float64(unit)), nil
|
||||
}
|
||||
|
||||
func ParseSize(s string) (int64, error) {
|
||||
s = strings.TrimSuffix(strings.TrimSuffix(strings.ToUpper(s), "B"), "I")
|
||||
mult := int64(1)
|
||||
if s != "" {
|
||||
switch s[len(s)-1] {
|
||||
case 'K':
|
||||
mult = 1 << 10
|
||||
case 'M':
|
||||
mult = 1 << 20
|
||||
case 'G':
|
||||
mult = 1 << 30
|
||||
case 'T':
|
||||
mult = 1 << 40
|
||||
}
|
||||
if mult > 1 {
|
||||
s = s[:len(s)-1]
|
||||
}
|
||||
}
|
||||
n, err := strconv.ParseFloat(s, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("bad size %q", s)
|
||||
}
|
||||
return int64(n * float64(mult)), nil
|
||||
}
|
||||
|
||||
func (q *Query) Match(r *roots.Root, now time.Time) bool {
|
||||
for _, p := range q.preds {
|
||||
if !p(r, now) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type SortKey int
|
||||
|
||||
const (
|
||||
SortLink SortKey = iota
|
||||
SortAge
|
||||
SortSize
|
||||
SortTarget
|
||||
SortKind
|
||||
)
|
||||
|
||||
var SortNames = []string{"link", "age", "size", "target", "kind"}
|
||||
|
||||
func (k SortKey) String() string { return SortNames[k] }
|
||||
|
||||
func ParseSort(s string) SortKey {
|
||||
for i, n := range SortNames {
|
||||
if n == s {
|
||||
return SortKey(i)
|
||||
}
|
||||
}
|
||||
return SortLink
|
||||
}
|
||||
|
||||
// Apply filters by query and kind set (nil means all kinds) and sorts the
|
||||
// result. Hidden roots are dropped unless showHidden or the query asks for
|
||||
// them.
|
||||
func Apply(rs []roots.Root, q *Query, kinds map[roots.Kind]bool, showHidden bool, key SortKey, desc bool) []roots.Root {
|
||||
showHidden = showHidden || (q != nil && q.ShowHidden)
|
||||
now := time.Now()
|
||||
out := make([]roots.Root, 0, len(rs))
|
||||
for i := range rs {
|
||||
r := &rs[i]
|
||||
if kinds != nil && !kinds[r.Kind] {
|
||||
continue
|
||||
}
|
||||
if r.Hidden && !showHidden {
|
||||
continue
|
||||
}
|
||||
if q != nil && !q.Match(r, now) {
|
||||
continue
|
||||
}
|
||||
out = append(out, *r)
|
||||
}
|
||||
less := func(a, b *roots.Root) bool {
|
||||
switch key {
|
||||
case SortAge:
|
||||
// "ascending" age means newest first
|
||||
return a.ModTime.After(b.ModTime)
|
||||
case SortSize:
|
||||
return a.ClosureSize < b.ClosureSize
|
||||
case SortTarget:
|
||||
return a.TargetName() < b.TargetName()
|
||||
case SortKind:
|
||||
if a.Kind != b.Kind {
|
||||
return a.Kind < b.Kind
|
||||
}
|
||||
}
|
||||
return a.Link < b.Link
|
||||
}
|
||||
sort.SliceStable(out, func(i, j int) bool {
|
||||
if desc {
|
||||
return less(&out[j], &out[i])
|
||||
}
|
||||
return less(&out[i], &out[j])
|
||||
})
|
||||
return out
|
||||
}
|
||||
|
||||
func HumanSize(n int64) string {
|
||||
switch {
|
||||
case n == -1:
|
||||
return "..."
|
||||
case n < 0:
|
||||
return "-"
|
||||
case n < 1<<10:
|
||||
return fmt.Sprintf("%dB", n)
|
||||
case n < 1<<20:
|
||||
return fmt.Sprintf("%.1fK", float64(n)/(1<<10))
|
||||
case n < 1<<30:
|
||||
return fmt.Sprintf("%.1fM", float64(n)/(1<<20))
|
||||
case n < 1<<40:
|
||||
return fmt.Sprintf("%.1fG", float64(n)/(1<<30))
|
||||
}
|
||||
return fmt.Sprintf("%.1fT", float64(n)/(1<<40))
|
||||
}
|
||||
|
||||
func HumanAge(t time.Time, now time.Time) string {
|
||||
if t.IsZero() {
|
||||
return "-"
|
||||
}
|
||||
d := now.Sub(t)
|
||||
switch {
|
||||
case d < time.Minute:
|
||||
return fmt.Sprintf("%ds", int(d.Seconds()))
|
||||
case d < time.Hour:
|
||||
return fmt.Sprintf("%dm", int(d.Minutes()))
|
||||
case d < 48*time.Hour:
|
||||
return fmt.Sprintf("%dh", int(d.Hours()))
|
||||
case d < 365*24*time.Hour:
|
||||
return fmt.Sprintf("%dd", int(d.Hours()/24))
|
||||
}
|
||||
return fmt.Sprintf("%.1fy", d.Hours()/24/365)
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package filter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/illustris/i-am-gcroot/internal/roots"
|
||||
)
|
||||
|
||||
func TestQuery(t *testing.T) {
|
||||
now := time.Now()
|
||||
r := roots.Root{
|
||||
Link: "/tmp/claude-1000/scratch/result",
|
||||
Target: "/nix/store/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello-2.12",
|
||||
Kind: roots.KindIndirect,
|
||||
Owner: "alice",
|
||||
UID: 1000,
|
||||
ModTime: now.Add(-40 * 24 * time.Hour),
|
||||
ClosureSize: 2 << 30,
|
||||
}
|
||||
cases := map[string]bool{
|
||||
"": true,
|
||||
"hello": true,
|
||||
"HELLO": true,
|
||||
"-hello": false,
|
||||
"hello world": false,
|
||||
"/hel+o-2/": true,
|
||||
"/^/tmp/": false, // more than two slashes: plain substring
|
||||
"/tmp/claude-": true, // substring
|
||||
"under:/tmp/claude-": true,
|
||||
"under:/home": false,
|
||||
"kind:indirect": true,
|
||||
"kind:prof,ind": true,
|
||||
"kind:profile": false,
|
||||
"owner:alice": true,
|
||||
"owner:1000": true,
|
||||
"owner:bob": false,
|
||||
"older:30d": true,
|
||||
"older:50d": false,
|
||||
"newer:1w": false,
|
||||
"size>1G": true,
|
||||
"size>3G": false,
|
||||
"size<3G": true,
|
||||
"is:protected": false,
|
||||
"-is:protected kind:in": true,
|
||||
}
|
||||
for q, want := range cases {
|
||||
p, err := Parse(q)
|
||||
if err != nil {
|
||||
t.Errorf("%q: %v", q, err)
|
||||
continue
|
||||
}
|
||||
if got := p.Match(&r, now); got != want {
|
||||
t.Errorf("%q: got %v want %v", q, got, want)
|
||||
}
|
||||
}
|
||||
for _, bad := range []string{"kind:nope", "older:3x", "size>abc", "/[/", "is:what"} {
|
||||
if _, err := Parse(bad); err == nil {
|
||||
t.Errorf("%q should fail to parse", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplySort(t *testing.T) {
|
||||
now := time.Now()
|
||||
rs := []roots.Root{
|
||||
{Link: "/b", Kind: roots.KindIndirect, ClosureSize: 5, ModTime: now.Add(-time.Hour)},
|
||||
{Link: "/a", Kind: roots.KindIndirect, ClosureSize: 10, ModTime: now.Add(-2 * time.Hour)},
|
||||
{Link: "/c", Kind: roots.KindRuntime, ClosureSize: 1},
|
||||
}
|
||||
all := map[roots.Kind]bool{roots.KindIndirect: true}
|
||||
out := Apply(rs, nil, all, false, SortLink, false)
|
||||
if len(out) != 2 || out[0].Link != "/a" {
|
||||
t.Errorf("link sort: %+v", out)
|
||||
}
|
||||
out = Apply(rs, nil, all, false, SortSize, true)
|
||||
if out[0].Link != "/a" {
|
||||
t.Errorf("size desc: %+v", out)
|
||||
}
|
||||
out = Apply(rs, nil, all, false, SortAge, false)
|
||||
if out[0].Link != "/b" {
|
||||
t.Errorf("age: newest first: %+v", out)
|
||||
}
|
||||
|
||||
rs[0].Hidden = true
|
||||
if out = Apply(rs, nil, all, false, SortLink, false); len(out) != 1 {
|
||||
t.Errorf("hidden root should be dropped: %+v", out)
|
||||
}
|
||||
if out = Apply(rs, nil, all, true, SortLink, false); len(out) != 2 {
|
||||
t.Errorf("showHidden should keep it: %+v", out)
|
||||
}
|
||||
q, _ := Parse("is:hidden")
|
||||
if out = Apply(rs, q, all, false, SortLink, false); len(out) != 1 || out[0].Link != "/b" {
|
||||
t.Errorf("is:hidden should show only hidden: %+v", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseSize(t *testing.T) {
|
||||
for in, want := range map[string]int64{"1K": 1024, "1.5M": 1572864, "2GiB": 2 << 30, "100": 100} {
|
||||
got, err := ParseSize(in)
|
||||
if err != nil || got != want {
|
||||
t.Errorf("%s: got %d %v want %d", in, got, err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
package roots
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CollectResult summarises a targeted garbage collection.
|
||||
type CollectResult struct {
|
||||
// Closure is the number of store paths in the closures of the targets.
|
||||
Closure int `json:"closure"`
|
||||
// Candidates is how many of those were not reachable from any other root.
|
||||
Candidates int `json:"candidates"`
|
||||
// Deleted and Freed come from nix-store's own report.
|
||||
Deleted int `json:"deleted"`
|
||||
Freed int64 `json:"freed"`
|
||||
// Kept lists paths nix refused to delete because they turned out to be
|
||||
// alive (e.g. via keep-derivations or a root created meanwhile).
|
||||
Kept []string `json:"kept,omitempty"`
|
||||
}
|
||||
|
||||
func (c CollectResult) String() string {
|
||||
s := fmt.Sprintf("collected %d store paths, %s freed", c.Deleted, humanBytes(c.Freed))
|
||||
if alive := c.Closure - c.Candidates + len(c.Kept); alive > 0 {
|
||||
s += fmt.Sprintf(" (%d of %d closure paths still alive via other roots)", alive, c.Closure)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
const argChunk = 2000
|
||||
|
||||
// Collect deletes the parts of the targets' closures that are no longer
|
||||
// reachable from any GC root, without walking the whole store the way
|
||||
// `nix-store --gc` does. Liveness is pre-computed from the closure of all
|
||||
// current roots (`--print-roots`); nix-store --delete re-checks it, so a
|
||||
// wrong guess only costs a retry, never a live path.
|
||||
func Collect(ctx context.Context, cfg Config, targets []string) (CollectResult, error) {
|
||||
var res CollectResult
|
||||
targets = existing(targets)
|
||||
if len(targets) == 0 {
|
||||
return res, nil
|
||||
}
|
||||
closure, err := queryClosure(ctx, cfg, targets)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Closure = len(closure)
|
||||
|
||||
out, err := exec.CommandContext(ctx, cfg.NixStoreBin, "--gc", "--print-roots").Output()
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("nix-store --print-roots: %w", cmdError(err))
|
||||
}
|
||||
var rootTargets []string
|
||||
seen := map[string]bool{}
|
||||
sc := bufio.NewScanner(bytes.NewReader(out))
|
||||
sc.Buffer(make([]byte, 64*1024), 1024*1024)
|
||||
for sc.Scan() {
|
||||
if _, t, ok := strings.Cut(sc.Text(), " -> "); ok && !seen[t] {
|
||||
seen[t] = true
|
||||
rootTargets = append(rootTargets, t)
|
||||
}
|
||||
}
|
||||
live, err := queryClosure(ctx, cfg, existing(rootTargets))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
var cand []string
|
||||
for p := range closure {
|
||||
if !live[p] {
|
||||
cand = append(cand, p)
|
||||
}
|
||||
}
|
||||
res.Candidates = len(cand)
|
||||
|
||||
for attempt := 0; len(cand) > 0 && attempt < 50; attempt++ {
|
||||
n, freed, alive, err := deletePaths(ctx, cfg, cand)
|
||||
res.Deleted += n
|
||||
res.Freed += freed
|
||||
if err != nil && alive == "" {
|
||||
return res, err
|
||||
}
|
||||
if alive == "" {
|
||||
break
|
||||
}
|
||||
res.Kept = append(res.Kept, alive)
|
||||
var next []string
|
||||
for _, p := range existing(cand) {
|
||||
if p != alive {
|
||||
next = append(next, p)
|
||||
}
|
||||
}
|
||||
cand = next
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func existing(paths []string) []string {
|
||||
var out []string
|
||||
seen := map[string]bool{}
|
||||
for _, p := range paths {
|
||||
if p == "" || seen[p] {
|
||||
continue
|
||||
}
|
||||
seen[p] = true
|
||||
if _, err := os.Lstat(p); err == nil {
|
||||
out = append(out, p)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func queryClosure(ctx context.Context, cfg Config, paths []string) (map[string]bool, error) {
|
||||
res := map[string]bool{}
|
||||
for i := 0; i < len(paths); i += argChunk {
|
||||
args := append([]string{"--query", "--requisites"}, paths[i:min(i+argChunk, len(paths))]...)
|
||||
out, err := exec.CommandContext(ctx, cfg.NixStoreBin, args...).Output()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("nix-store --query --requisites: %w", cmdError(err))
|
||||
}
|
||||
for _, l := range strings.Split(strings.TrimSpace(string(out)), "\n") {
|
||||
if l != "" {
|
||||
res[l] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
var (
|
||||
deletedRe = regexp.MustCompile(`(\d+) store paths deleted, ([0-9.]+) (\w+) freed`)
|
||||
aliveRe = regexp.MustCompile(`[Cc]annot delete path '([^']+)'`)
|
||||
)
|
||||
|
||||
// deletePaths runs nix-store --delete in chunks. If nix refuses a path
|
||||
// because it is alive, that path is returned so the caller can retry
|
||||
// without it.
|
||||
func deletePaths(ctx context.Context, cfg Config, paths []string) (n int, freed int64, alive string, err error) {
|
||||
for i := 0; i < len(paths); i += argChunk {
|
||||
args := append([]string{"--delete"}, paths[i:min(i+argChunk, len(paths))]...)
|
||||
out, cerr := exec.CommandContext(ctx, cfg.NixStoreBin, args...).CombinedOutput()
|
||||
if m := deletedRe.FindSubmatch(out); m != nil {
|
||||
c, _ := strconv.Atoi(string(m[1]))
|
||||
n += c
|
||||
freed += parseBytes(string(m[2]), string(m[3]))
|
||||
}
|
||||
if cerr != nil {
|
||||
if m := aliveRe.FindSubmatch(out); m != nil {
|
||||
return n, freed, string(m[1]), cerr
|
||||
}
|
||||
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
|
||||
return n, freed, "", fmt.Errorf("nix-store --delete: %v: %s", cerr, lines[len(lines)-1])
|
||||
}
|
||||
}
|
||||
return n, freed, "", nil
|
||||
}
|
||||
|
||||
func parseBytes(num, unit string) int64 {
|
||||
f, _ := strconv.ParseFloat(num, 64)
|
||||
mult := map[string]float64{"bytes": 1, "KiB": 1 << 10, "MiB": 1 << 20, "GiB": 1 << 30, "TiB": 1 << 40}[unit]
|
||||
if mult == 0 {
|
||||
mult = 1
|
||||
}
|
||||
return int64(f * mult)
|
||||
}
|
||||
|
||||
func humanBytes(n int64) string {
|
||||
units := []string{"B", "KiB", "MiB", "GiB", "TiB"}
|
||||
f := float64(n)
|
||||
i := 0
|
||||
for f >= 1024 && i < len(units)-1 {
|
||||
f /= 1024
|
||||
i++
|
||||
}
|
||||
if i == 0 {
|
||||
return fmt.Sprintf("%d B", n)
|
||||
}
|
||||
return fmt.Sprintf("%.1f %s", f, units[i])
|
||||
}
|
||||
|
||||
// CollectedTargets returns the store paths of roots whose deletion
|
||||
// succeeded, for passing to Collect.
|
||||
func CollectedTargets(results []Result) []string {
|
||||
var out []string
|
||||
for _, r := range results {
|
||||
if r.OK() && r.Target != "" {
|
||||
out = append(out, r.Target)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package roots
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Result struct {
|
||||
Link string `json:"link"`
|
||||
Target string `json:"target,omitempty"`
|
||||
Action string `json:"action"`
|
||||
Err string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func (r Result) OK() bool { return r.Err == "" }
|
||||
|
||||
type DeleteOpts struct {
|
||||
DryRun bool
|
||||
// Force allows deleting protected roots (current generations,
|
||||
// current-system/booted-system).
|
||||
Force bool
|
||||
}
|
||||
|
||||
// Delete removes the given roots. It only ever removes symlinks (or calls
|
||||
// nix-env for profile generations); nothing inside the store is touched.
|
||||
func Delete(ctx context.Context, cfg Config, rs []Root, opts DeleteOpts) []Result {
|
||||
res := make([]Result, 0, len(rs))
|
||||
for _, r := range rs {
|
||||
res = append(res, deleteOne(ctx, cfg, r, opts))
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func deleteOne(ctx context.Context, cfg Config, r Root, opts DeleteOpts) Result {
|
||||
res := Result{Link: r.Link, Target: r.Target}
|
||||
fail := func(err error) Result {
|
||||
res.Err = err.Error()
|
||||
if errors.Is(err, fs.ErrPermission) {
|
||||
res.Err += " (root-owned? rerun with sudo)"
|
||||
}
|
||||
return res
|
||||
}
|
||||
if !r.Deletable() {
|
||||
res.Action = "skip"
|
||||
return fail(errors.New("runtime roots are held by running processes and cannot be deleted"))
|
||||
}
|
||||
if r.Protected && !opts.Force {
|
||||
res.Action = "skip"
|
||||
return fail(fmt.Errorf("protected: %s", r.Reason))
|
||||
}
|
||||
|
||||
switch r.Kind {
|
||||
case KindProfile:
|
||||
gen := strconv.Itoa(r.Generation)
|
||||
res.Action = fmt.Sprintf("nix-env --profile %s --delete-generations %s", r.Profile, gen)
|
||||
if r.Current {
|
||||
// nix-env refuses to delete the current generation; with --force
|
||||
// fall back to removing the link itself.
|
||||
res.Action = "rm " + r.Link
|
||||
if opts.DryRun {
|
||||
return res
|
||||
}
|
||||
return orFail(res, removeSymlink(cfg, r.Link, true), fail)
|
||||
}
|
||||
if opts.DryRun {
|
||||
return res
|
||||
}
|
||||
out, err := exec.CommandContext(ctx, cfg.NixEnvBin, "--profile", r.Profile, "--delete-generations", gen).CombinedOutput()
|
||||
if err != nil {
|
||||
msg := strings.TrimSpace(string(out))
|
||||
if msg == "" {
|
||||
msg = err.Error()
|
||||
}
|
||||
if strings.Contains(msg, "Permission denied") {
|
||||
msg += " (rerun with sudo)"
|
||||
}
|
||||
res.Err = msg
|
||||
}
|
||||
return res
|
||||
case KindDangling:
|
||||
res.Action = "rm " + r.Via
|
||||
if opts.DryRun {
|
||||
return res
|
||||
}
|
||||
return orFail(res, removeSymlink(cfg, r.Via, false), fail)
|
||||
case KindIndirect:
|
||||
if r.Target == "" {
|
||||
res.Action = "skip"
|
||||
return fail(fmt.Errorf("cannot inspect %s: %s", r.Link, r.Reason))
|
||||
}
|
||||
res.Action = "rm " + r.Link
|
||||
if opts.DryRun {
|
||||
return res
|
||||
}
|
||||
if err := removeSymlink(cfg, r.Link, true); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
// The auto/ entry is now stale; nix-store --gc prunes it, but tidy it
|
||||
// up right away if we are allowed to.
|
||||
if r.Via != "" {
|
||||
_ = removeSymlink(cfg, r.Via, false)
|
||||
}
|
||||
return res
|
||||
default: // KindDirect
|
||||
res.Action = "rm " + r.Link
|
||||
if opts.DryRun {
|
||||
return res
|
||||
}
|
||||
return orFail(res, removeSymlink(cfg, r.Link, false), fail)
|
||||
}
|
||||
}
|
||||
|
||||
func orFail(res Result, err error, fail func(error) Result) Result {
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// removeSymlink removes p after checking it is a symlink and, if
|
||||
// mustPointToStore, that it points into the store.
|
||||
func removeSymlink(cfg Config, p string, mustPointToStore bool) error {
|
||||
if cfg.inStore(p) || p == cfg.StoreDir {
|
||||
return fmt.Errorf("refusing to remove %s: inside the store", p)
|
||||
}
|
||||
st, err := os.Lstat(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if st.Mode()&fs.ModeSymlink == 0 {
|
||||
return fmt.Errorf("refusing to remove %s: not a symlink", p)
|
||||
}
|
||||
if mustPointToStore {
|
||||
t, err := os.Readlink(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !cfg.inStore(t) {
|
||||
return fmt.Errorf("refusing to remove %s: points to %s, not the store", p, t)
|
||||
}
|
||||
}
|
||||
return os.Remove(p)
|
||||
}
|
||||
|
||||
var freedRe = regexp.MustCompile(`(\d+) store paths deleted, ([0-9.]+ \w+) freed`)
|
||||
|
||||
// GC runs nix-store --gc and returns its summary line.
|
||||
func GC(ctx context.Context, cfg Config) (string, error) {
|
||||
out, err := exec.CommandContext(ctx, cfg.NixStoreBin, "--gc").CombinedOutput()
|
||||
if m := freedRe.FindString(string(out)); m != "" {
|
||||
return m, err
|
||||
}
|
||||
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
|
||||
last := lines[len(lines)-1]
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("nix-store --gc: %v: %s", err, last)
|
||||
}
|
||||
return last, nil
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
// Package roots discovers, sizes and deletes Nix garbage collector roots.
|
||||
package roots
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Kind string
|
||||
|
||||
const (
|
||||
// KindIndirect is a symlink outside the gcroots tree (e.g. ./result)
|
||||
// registered through /nix/var/nix/gcroots/auto.
|
||||
KindIndirect Kind = "indirect"
|
||||
// KindDangling is an auto/ entry whose target link no longer exists.
|
||||
KindDangling Kind = "dangling"
|
||||
// KindProfile is a profile generation link (<profile>-<N>-link).
|
||||
KindProfile Kind = "profile"
|
||||
// KindDirect is a symlink inside the gcroots tree pointing into the store.
|
||||
KindDirect Kind = "direct"
|
||||
// KindRuntime is a root held by a running process (/proc/...).
|
||||
KindRuntime Kind = "runtime"
|
||||
)
|
||||
|
||||
var AllKinds = []Kind{KindIndirect, KindProfile, KindDirect, KindDangling, KindRuntime}
|
||||
|
||||
type Root struct {
|
||||
// Link is the symlink that keeps Target alive. For runtime roots it is
|
||||
// /proc/<pid>.
|
||||
Link string `json:"link"`
|
||||
// Via is the gcroots entry that registers Link (auto/<hash> for indirect
|
||||
// roots), empty when Link itself is inside the gcroots tree.
|
||||
Via string `json:"via,omitempty"`
|
||||
Target string `json:"target"`
|
||||
Kind Kind `json:"kind"`
|
||||
|
||||
UID int `json:"uid"`
|
||||
Owner string `json:"owner"`
|
||||
ModTime time.Time `json:"mtime"`
|
||||
|
||||
// Profile generation info, only for KindProfile.
|
||||
Profile string `json:"profile,omitempty"`
|
||||
Generation int `json:"generation,omitempty"`
|
||||
Current bool `json:"current,omitempty"`
|
||||
|
||||
// Hidden roots are nix's own bookkeeping (e.g. ~/.cache/nix/flake-registry.json)
|
||||
// and are left out of listings unless asked for.
|
||||
Hidden bool `json:"hidden,omitempty"`
|
||||
|
||||
// Protected roots are refused by Delete unless forced.
|
||||
Protected bool `json:"protected,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
|
||||
// Runtime roots: number of store paths held and the process command.
|
||||
Count int `json:"count,omitempty"`
|
||||
Command string `json:"command,omitempty"`
|
||||
|
||||
// Sizes in bytes, -1 while unknown.
|
||||
ClosureSize int64 `json:"closureSize"`
|
||||
NarSize int64 `json:"narSize"`
|
||||
}
|
||||
|
||||
func (r Root) Deletable() bool {
|
||||
return r.Kind != KindRuntime
|
||||
}
|
||||
|
||||
// TargetName is a short human label for the target store path.
|
||||
func (r Root) TargetName() string {
|
||||
t := r.Target
|
||||
if i := strings.LastIndexByte(t, '/'); i >= 0 {
|
||||
t = t[i+1:]
|
||||
}
|
||||
// strip the 32 char hash
|
||||
if len(t) > 33 && t[32] == '-' {
|
||||
t = t[33:]
|
||||
}
|
||||
return t
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
package roots
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type fakeTree struct {
|
||||
cfg Config
|
||||
home string
|
||||
}
|
||||
|
||||
func symlink(t *testing.T, target, link string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Dir(link), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(target, link); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func newFakeTree(t *testing.T) *fakeTree {
|
||||
dir := t.TempDir()
|
||||
ft := &fakeTree{
|
||||
cfg: Config{
|
||||
StoreDir: filepath.Join(dir, "store"),
|
||||
StateDir: filepath.Join(dir, "state"),
|
||||
NixStoreBin: "false",
|
||||
NixBin: "false",
|
||||
NixEnvBin: "true",
|
||||
},
|
||||
home: filepath.Join(dir, "home"),
|
||||
}
|
||||
store := func(name string) string {
|
||||
p := filepath.Join(ft.cfg.StoreDir, name)
|
||||
os.MkdirAll(p, 0o755)
|
||||
return p
|
||||
}
|
||||
gc := filepath.Join(ft.cfg.StateDir, "gcroots")
|
||||
prof := filepath.Join(ft.cfg.StateDir, "profiles")
|
||||
|
||||
// indirect root: auto/h1 -> home/result -> store
|
||||
symlink(t, store("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello")+"/bin/hello", filepath.Join(ft.home, "result"))
|
||||
symlink(t, filepath.Join(ft.home, "result"), filepath.Join(gc, "auto", "h1"))
|
||||
// dangling: auto/h2 -> home/gone
|
||||
symlink(t, filepath.Join(ft.home, "gone"), filepath.Join(gc, "auto", "h2"))
|
||||
// auto entry pointing to a regular file is not a root
|
||||
os.WriteFile(filepath.Join(ft.home, "file"), nil, 0o644)
|
||||
symlink(t, filepath.Join(ft.home, "file"), filepath.Join(gc, "auto", "h3"))
|
||||
// direct root
|
||||
symlink(t, store("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-direct"), filepath.Join(gc, "manual", "keep"))
|
||||
// gcroots/profiles -> profiles dir must not produce duplicates
|
||||
symlink(t, prof, filepath.Join(gc, "profiles"))
|
||||
// profile with two generations, 2 is current
|
||||
symlink(t, store("cccccccccccccccccccccccccccccccc-sys1"), filepath.Join(prof, "system-1-link"))
|
||||
symlink(t, store("dddddddddddddddddddddddddddddddd-sys2"), filepath.Join(prof, "system-2-link"))
|
||||
symlink(t, "system-2-link", filepath.Join(prof, "system"))
|
||||
// a user profile registered through auto/
|
||||
symlink(t, store("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-hm"), filepath.Join(ft.home, "profiles", "hm-3-link"))
|
||||
symlink(t, "hm-3-link", filepath.Join(ft.home, "profiles", "hm"))
|
||||
symlink(t, filepath.Join(ft.home, "profiles", "hm-3-link"), filepath.Join(gc, "auto", "h4"))
|
||||
// nix's own cache root, hidden by default
|
||||
symlink(t, store("ffffffffffffffffffffffffffffffff-flake-registry.json"), filepath.Join(ft.home, ".cache", "nix", "flake-registry.json"))
|
||||
symlink(t, filepath.Join(ft.home, ".cache", "nix", "flake-registry.json"), filepath.Join(gc, "auto", "h5"))
|
||||
return ft
|
||||
}
|
||||
|
||||
func byLink(rs []*Root) map[string]*Root {
|
||||
m := map[string]*Root{}
|
||||
for _, r := range rs {
|
||||
m[r.Link] = r
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func TestScan(t *testing.T) {
|
||||
ft := newFakeTree(t)
|
||||
rs, err := Scan(ft.cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := byLink(rs)
|
||||
if len(rs) != 7 {
|
||||
for _, r := range rs {
|
||||
t.Logf("%s %s -> %s", r.Kind, r.Link, r.Target)
|
||||
}
|
||||
t.Fatalf("want 7 roots, got %d", len(rs))
|
||||
}
|
||||
if r := m[filepath.Join(ft.home, ".cache", "nix", "flake-registry.json")]; r == nil || !r.Hidden {
|
||||
t.Errorf("cache root should be hidden: %+v", r)
|
||||
}
|
||||
if m[filepath.Join(ft.home, "result")].Hidden {
|
||||
t.Error("result should not be hidden")
|
||||
}
|
||||
gc := filepath.Join(ft.cfg.StateDir, "gcroots")
|
||||
prof := filepath.Join(ft.cfg.StateDir, "profiles")
|
||||
|
||||
r := m[filepath.Join(ft.home, "result")]
|
||||
if r == nil || r.Kind != KindIndirect || r.Via != filepath.Join(gc, "auto", "h1") ||
|
||||
r.Target != filepath.Join(ft.cfg.StoreDir, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello") {
|
||||
t.Errorf("indirect root wrong: %+v", r)
|
||||
}
|
||||
if r.TargetName() != "hello" {
|
||||
t.Errorf("TargetName = %q", r.TargetName())
|
||||
}
|
||||
if r := m[filepath.Join(ft.home, "gone")]; r == nil || r.Kind != KindDangling {
|
||||
t.Errorf("dangling root wrong: %+v", r)
|
||||
}
|
||||
if r := m[filepath.Join(gc, "manual", "keep")]; r == nil || r.Kind != KindDirect {
|
||||
t.Errorf("direct root wrong: %+v", r)
|
||||
}
|
||||
if r := m[filepath.Join(prof, "system-1-link")]; r == nil || r.Kind != KindProfile || r.Generation != 1 || r.Current {
|
||||
t.Errorf("profile gen 1 wrong: %+v", r)
|
||||
}
|
||||
if r := m[filepath.Join(prof, "system-2-link")]; r == nil || !r.Current || !r.Protected {
|
||||
t.Errorf("profile gen 2 should be current and protected: %+v", r)
|
||||
}
|
||||
if r := m[filepath.Join(ft.home, "profiles", "hm-3-link")]; r == nil || r.Kind != KindProfile || !r.Current || r.Via == "" {
|
||||
t.Errorf("indirect profile wrong: %+v", r)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
ft := newFakeTree(t)
|
||||
rs, err := Scan(ft.cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := byLink(rs)
|
||||
ctx := context.Background()
|
||||
prof := filepath.Join(ft.cfg.StateDir, "profiles")
|
||||
gc := filepath.Join(ft.cfg.StateDir, "gcroots")
|
||||
|
||||
result := filepath.Join(ft.home, "result")
|
||||
all := []Root{*m[result], *m[filepath.Join(ft.home, "gone")], *m[filepath.Join(prof, "system-1-link")], *m[filepath.Join(prof, "system-2-link")]}
|
||||
|
||||
// dry run touches nothing
|
||||
res := Delete(ctx, ft.cfg, all, DeleteOpts{DryRun: true})
|
||||
if _, err := os.Lstat(result); err != nil {
|
||||
t.Fatal("dry run removed link")
|
||||
}
|
||||
if !res[0].OK() || res[0].Action != "rm "+result {
|
||||
t.Errorf("dry run result: %+v", res[0])
|
||||
}
|
||||
if res[3].OK() {
|
||||
t.Errorf("current generation must be refused: %+v", res[3])
|
||||
}
|
||||
|
||||
res = Delete(ctx, ft.cfg, all, DeleteOpts{})
|
||||
for i, r := range res[:3] {
|
||||
if !r.OK() {
|
||||
t.Errorf("delete %d failed: %+v", i, r)
|
||||
}
|
||||
}
|
||||
for _, p := range []string{result, filepath.Join(gc, "auto", "h1"), filepath.Join(gc, "auto", "h2")} {
|
||||
if _, err := os.Lstat(p); err == nil {
|
||||
t.Errorf("%s still exists", p)
|
||||
}
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(ft.cfg.StoreDir, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello")); err != nil {
|
||||
t.Error("store path must not be touched")
|
||||
}
|
||||
if res[2].Action != "nix-env --profile "+filepath.Join(prof, "system")+" --delete-generations 1" {
|
||||
t.Errorf("profile action: %q", res[2].Action)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveSymlinkRefusesNonLinks(t *testing.T) {
|
||||
ft := newFakeTree(t)
|
||||
if err := removeSymlink(ft.cfg, filepath.Join(ft.home, "file"), false); err == nil {
|
||||
t.Error("removed a regular file")
|
||||
}
|
||||
if err := removeSymlink(ft.cfg, filepath.Join(ft.cfg.StoreDir, "x"), false); err == nil {
|
||||
t.Error("removed something in the store")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRuntime(t *testing.T) {
|
||||
cfg := DefaultConfig()
|
||||
out := []byte(`/proc/1/maps -> /nix/store/a-x
|
||||
/proc/1/environ -> /nix/store/b-y
|
||||
/proc/1/maps -> /nix/store/a-x
|
||||
/proc/22/exe -> /nix/store/c-z
|
||||
{censored} -> /nix/store/d-w
|
||||
/home/u/result -> /nix/store/e-v
|
||||
`)
|
||||
rs := parseRuntime(cfg, out, map[string]bool{})
|
||||
if len(rs) != 3 {
|
||||
t.Fatalf("want 3 runtime groups, got %d", len(rs))
|
||||
}
|
||||
m := byLink(rs)
|
||||
if m["/proc/1"].Count != 2 || m["/proc/22"].Target != "/nix/store/c-z" || m["{censored}"] == nil {
|
||||
t.Errorf("bad grouping: %+v %+v", m["/proc/1"], m["/proc/22"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsePathInfo(t *testing.T) {
|
||||
obj := parsePathInfo([]byte(`{"/nix/store/a-x":{"closureSize":10,"narSize":5},"/nix/store/b-y":null}`))
|
||||
if obj["/nix/store/a-x"].ClosureSize != 10 || len(obj) != 1 {
|
||||
t.Errorf("object format: %+v", obj)
|
||||
}
|
||||
arr := parsePathInfo([]byte(`[{"path":"/nix/store/a-x","closureSize":7,"narSize":3},{"path":"/nix/store/b-y","valid":false}]`))
|
||||
if arr["/nix/store/a-x"].ClosureSize != 7 || len(arr) != 1 {
|
||||
t.Errorf("array format: %+v", arr)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,519 @@
|
||||
package roots
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Config locates the Nix state on disk. Tests point it at a fake tree.
|
||||
type Config struct {
|
||||
StoreDir string // /nix/store
|
||||
StateDir string // /nix/var/nix
|
||||
// NixStoreBin is used for --print-roots and --gc.
|
||||
NixStoreBin string
|
||||
// NixBin is used for path-info.
|
||||
NixBin string
|
||||
// NixEnvBin is used to delete profile generations.
|
||||
NixEnvBin string
|
||||
}
|
||||
|
||||
func DefaultConfig() Config {
|
||||
c := Config{
|
||||
StoreDir: "/nix/store",
|
||||
StateDir: "/nix/var/nix",
|
||||
NixStoreBin: "nix-store",
|
||||
NixBin: "nix",
|
||||
NixEnvBin: "nix-env",
|
||||
}
|
||||
if v := os.Getenv("NIX_STORE_DIR"); v != "" {
|
||||
c.StoreDir = v
|
||||
}
|
||||
if v := os.Getenv("NIX_STATE_DIR"); v != "" {
|
||||
c.StateDir = v
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (c Config) gcrootsDir() string { return filepath.Join(c.StateDir, "gcroots") }
|
||||
func (c Config) autoDir() string { return filepath.Join(c.StateDir, "gcroots", "auto") }
|
||||
func (c Config) profilesDir() string { return filepath.Join(c.StateDir, "profiles") }
|
||||
|
||||
func (c Config) inStore(p string) bool {
|
||||
return strings.HasPrefix(p, c.StoreDir+"/")
|
||||
}
|
||||
|
||||
// storePathOf trims a path inside the store to the top level store path.
|
||||
func (c Config) storePathOf(p string) string {
|
||||
rest := strings.TrimPrefix(p, c.StoreDir+"/")
|
||||
if i := strings.IndexByte(rest, '/'); i >= 0 {
|
||||
rest = rest[:i]
|
||||
}
|
||||
return c.StoreDir + "/" + rest
|
||||
}
|
||||
|
||||
var protectedLinks = map[string]bool{
|
||||
"/run/current-system": true,
|
||||
"/run/booted-system": true,
|
||||
}
|
||||
|
||||
// hiddenPatterns mark roots that nix creates for its own caches.
|
||||
var hiddenPatterns = []string{"/.cache/nix/"}
|
||||
|
||||
func isHidden(link string) bool {
|
||||
for _, p := range hiddenPatterns {
|
||||
if strings.Contains(link, p) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var genLinkRe = regexp.MustCompile(`^(.*)-(\d+)-link$`)
|
||||
|
||||
type scanner struct {
|
||||
cfg Config
|
||||
seen map[string]*Root
|
||||
order []*Root
|
||||
users map[int]string
|
||||
}
|
||||
|
||||
// Scan finds all persistent roots: the gcroots tree (including indirect
|
||||
// roots registered under auto/) and the profiles tree. Runtime roots are
|
||||
// gathered separately by ScanRuntime because that requires running
|
||||
// nix-store and is slow.
|
||||
func Scan(cfg Config) ([]*Root, error) {
|
||||
s := &scanner{cfg: cfg, seen: map[string]*Root{}, users: map[int]string{}}
|
||||
if err := s.walk(cfg.gcrootsDir(), true); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// The profiles dir is not required to exist (e.g. in tests).
|
||||
if err := s.walk(cfg.profilesDir(), false); err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
return nil, err
|
||||
}
|
||||
for _, r := range s.order {
|
||||
s.classifyProfile(r)
|
||||
s.fillOwner(r)
|
||||
r.Hidden = isHidden(r.Link)
|
||||
}
|
||||
return s.order, nil
|
||||
}
|
||||
|
||||
// walk visits every symlink below dir. dir itself may be a symlink; paths are
|
||||
// reported under dir rather than its resolved location.
|
||||
func (s *scanner) walk(dir string, isGCRoots bool) error {
|
||||
real, err := filepath.EvalSymlinks(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return filepath.WalkDir(real, func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
// unreadable subdirectories are skipped, not fatal
|
||||
if d != nil && d.IsDir() && p != real {
|
||||
return fs.SkipDir
|
||||
}
|
||||
if p == real {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if d.Type()&fs.ModeSymlink == 0 {
|
||||
return nil
|
||||
}
|
||||
shown := dir + strings.TrimPrefix(p, real)
|
||||
s.visitLink(shown, p, isGCRoots)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *scanner) add(r *Root) {
|
||||
if _, ok := s.seen[r.Link]; ok {
|
||||
return
|
||||
}
|
||||
r.ClosureSize, r.NarSize = -1, -1
|
||||
s.seen[r.Link] = r
|
||||
s.order = append(s.order, r)
|
||||
}
|
||||
|
||||
// visitLink mirrors the logic of findRoots in libstore/gc.cc.
|
||||
func (s *scanner) visitLink(shown, real string, isGCRoots bool) {
|
||||
target, err := os.Readlink(real)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if s.cfg.inStore(target) {
|
||||
s.add(&Root{Link: shown, Target: s.cfg.storePathOf(target), Kind: KindDirect})
|
||||
return
|
||||
}
|
||||
if !filepath.IsAbs(target) {
|
||||
target = filepath.Join(filepath.Dir(shown), target)
|
||||
}
|
||||
inAuto := isGCRoots && strings.HasPrefix(shown, s.cfg.autoDir()+"/")
|
||||
|
||||
st, err := os.Lstat(target)
|
||||
if err != nil {
|
||||
if !inAuto {
|
||||
return
|
||||
}
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
s.add(&Root{Link: target, Via: shown, Kind: KindDangling})
|
||||
} else {
|
||||
s.add(&Root{Link: target, Via: shown, Kind: KindIndirect, Reason: "unreadable: " + errReason(err)})
|
||||
}
|
||||
return
|
||||
}
|
||||
if st.Mode()&fs.ModeSymlink == 0 {
|
||||
return
|
||||
}
|
||||
target2, err := os.Readlink(target)
|
||||
if err != nil || !s.cfg.inStore(target2) {
|
||||
return
|
||||
}
|
||||
if inAuto {
|
||||
s.add(&Root{Link: target, Via: shown, Target: s.cfg.storePathOf(target2), Kind: KindIndirect})
|
||||
} else if isGCRoots {
|
||||
// e.g. gcroots/booted-system -> /run/booted-system -> store.
|
||||
// Deleting the gcroots entry is what drops the root.
|
||||
r := &Root{Link: shown, Via: target, Target: s.cfg.storePathOf(target2), Kind: KindDirect}
|
||||
if protectedLinks[target] {
|
||||
r.Protected, r.Reason = true, "points to "+target
|
||||
}
|
||||
s.add(r)
|
||||
}
|
||||
// In the profiles tree, links like system -> system-38-link resolve to a
|
||||
// generation link that is visited on its own.
|
||||
}
|
||||
|
||||
func errReason(err error) string {
|
||||
var pe *fs.PathError
|
||||
if errors.As(err, &pe) {
|
||||
return pe.Err.Error()
|
||||
}
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
func (s *scanner) classifyProfile(r *Root) {
|
||||
if r.Kind == KindDangling {
|
||||
return
|
||||
}
|
||||
m := genLinkRe.FindStringSubmatch(r.Link)
|
||||
if m == nil {
|
||||
return
|
||||
}
|
||||
profile := m[1]
|
||||
st, err := os.Lstat(profile)
|
||||
if err != nil || st.Mode()&fs.ModeSymlink == 0 {
|
||||
return
|
||||
}
|
||||
gen, _ := strconv.Atoi(m[2])
|
||||
r.Kind = KindProfile
|
||||
r.Profile = profile
|
||||
r.Generation = gen
|
||||
if cur, err := os.Readlink(profile); err == nil && filepath.Base(cur) == filepath.Base(r.Link) {
|
||||
r.Current = true
|
||||
r.Protected = true
|
||||
r.Reason = "current generation of " + profile
|
||||
}
|
||||
}
|
||||
|
||||
func (s *scanner) fillOwner(r *Root) {
|
||||
p := r.Link
|
||||
if r.Kind == KindDangling {
|
||||
p = r.Via
|
||||
}
|
||||
st, err := os.Lstat(p)
|
||||
if err != nil {
|
||||
r.UID = -1
|
||||
return
|
||||
}
|
||||
r.ModTime = st.ModTime()
|
||||
if sys, ok := st.Sys().(*syscall.Stat_t); ok {
|
||||
r.UID = int(sys.Uid)
|
||||
r.Owner = s.userName(r.UID)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *scanner) userName(uid int) string {
|
||||
if n, ok := s.users[uid]; ok {
|
||||
return n
|
||||
}
|
||||
n := strconv.Itoa(uid)
|
||||
if u, err := user.LookupId(n); err == nil {
|
||||
n = u.Username
|
||||
}
|
||||
s.users[uid] = n
|
||||
return n
|
||||
}
|
||||
|
||||
// ScanRuntime returns one root per process that holds store paths alive,
|
||||
// based on `nix-store --gc --print-roots`. Roots that the persistent scan
|
||||
// already found are skipped via known.
|
||||
func ScanRuntime(ctx context.Context, cfg Config, known map[string]bool) ([]*Root, error) {
|
||||
out, err := exec.CommandContext(ctx, cfg.NixStoreBin, "--gc", "--print-roots").Output()
|
||||
if err != nil {
|
||||
return nil, cmdError(err)
|
||||
}
|
||||
return parseRuntime(cfg, out, known), nil
|
||||
}
|
||||
|
||||
func parseRuntime(cfg Config, out []byte, known map[string]bool) []*Root {
|
||||
type group struct {
|
||||
targets map[string]bool
|
||||
}
|
||||
groups := map[string]*group{}
|
||||
var keys []string
|
||||
sc := bufio.NewScanner(bytes.NewReader(out))
|
||||
sc.Buffer(make([]byte, 64*1024), 1024*1024)
|
||||
for sc.Scan() {
|
||||
link, target, ok := strings.Cut(sc.Text(), " -> ")
|
||||
if !ok || known[link] {
|
||||
continue
|
||||
}
|
||||
var key string
|
||||
switch {
|
||||
case strings.HasPrefix(link, "{"):
|
||||
key = link
|
||||
case strings.HasPrefix(link, "/proc/"):
|
||||
rest := strings.TrimPrefix(link, "/proc/")
|
||||
if i := strings.IndexByte(rest, '/'); i >= 0 {
|
||||
rest = rest[:i]
|
||||
}
|
||||
key = "/proc/" + rest
|
||||
default:
|
||||
continue
|
||||
}
|
||||
g := groups[key]
|
||||
if g == nil {
|
||||
g = &group{targets: map[string]bool{}}
|
||||
groups[key] = g
|
||||
keys = append(keys, key)
|
||||
}
|
||||
g.targets[target] = true
|
||||
}
|
||||
sort.Strings(keys)
|
||||
var res []*Root
|
||||
for _, k := range keys {
|
||||
g := groups[k]
|
||||
r := &Root{Link: k, Kind: KindRuntime, Count: len(g.targets), ClosureSize: -1, NarSize: -1, UID: -1}
|
||||
if len(g.targets) == 1 {
|
||||
for t := range g.targets {
|
||||
r.Target = t
|
||||
}
|
||||
}
|
||||
if strings.HasPrefix(k, "/proc/") {
|
||||
if b, err := os.ReadFile(k + "/cmdline"); err == nil {
|
||||
r.Command = strings.TrimSpace(strings.ReplaceAll(string(b), "\x00", " "))
|
||||
}
|
||||
if st, err := os.Stat(k); err == nil {
|
||||
r.ModTime = st.ModTime()
|
||||
if sys, ok := st.Sys().(*syscall.Stat_t); ok {
|
||||
r.UID = int(sys.Uid)
|
||||
if u, err := user.LookupId(strconv.Itoa(r.UID)); err == nil {
|
||||
r.Owner = u.Username
|
||||
} else {
|
||||
r.Owner = strconv.Itoa(r.UID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
res = append(res, r)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func cmdError(err error) error {
|
||||
var ee *exec.ExitError
|
||||
if errors.As(err, &ee) && len(ee.Stderr) > 0 {
|
||||
return errors.New(strings.TrimSpace(string(ee.Stderr)))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Set is a thread safe, refreshable collection of roots shared by the TUI
|
||||
// and the web server.
|
||||
type Set struct {
|
||||
Cfg Config
|
||||
|
||||
// refreshMu serialises Refresh so a scan that started before a delete
|
||||
// cannot land after it and resurrect the deleted roots.
|
||||
refreshMu sync.Mutex
|
||||
|
||||
mu sync.RWMutex
|
||||
roots []*Root
|
||||
scannedAt time.Time
|
||||
runtime bool
|
||||
sizing bool
|
||||
sizesDone int
|
||||
sizesTodo int
|
||||
sizeCache map[string][2]int64
|
||||
listeners []func()
|
||||
}
|
||||
|
||||
func NewSet(cfg Config) *Set {
|
||||
return &Set{Cfg: cfg, sizeCache: map[string][2]int64{}}
|
||||
}
|
||||
|
||||
// OnChange registers a callback invoked (from any goroutine) when roots or
|
||||
// sizes change.
|
||||
func (s *Set) OnChange(f func()) {
|
||||
s.mu.Lock()
|
||||
s.listeners = append(s.listeners, f)
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
func (s *Set) notify() {
|
||||
s.mu.RLock()
|
||||
ls := append([]func(){}, s.listeners...)
|
||||
s.mu.RUnlock()
|
||||
for _, f := range ls {
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
// Snapshot returns copies of the current roots so callers can sort and
|
||||
// filter without locking.
|
||||
func (s *Set) Snapshot() []Root {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
out := make([]Root, len(s.roots))
|
||||
for i, r := range s.roots {
|
||||
out[i] = *r
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
type Status struct {
|
||||
Total int
|
||||
ScannedAt time.Time
|
||||
Sizing bool
|
||||
SizesDone int
|
||||
SizesTodo int
|
||||
Runtime bool
|
||||
}
|
||||
|
||||
func (s *Set) Status() Status {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return Status{len(s.roots), s.scannedAt, s.sizing, s.sizesDone, s.sizesTodo, s.runtime}
|
||||
}
|
||||
|
||||
// SetRuntime toggles whether the next Refresh includes runtime roots.
|
||||
func (s *Set) SetRuntime(on bool) {
|
||||
s.mu.Lock()
|
||||
s.runtime = on
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
// Refresh rescans roots and starts computing sizes in the background.
|
||||
func (s *Set) Refresh(ctx context.Context) error {
|
||||
s.refreshMu.Lock()
|
||||
defer s.refreshMu.Unlock()
|
||||
rs, err := Scan(s.Cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.mu.RLock()
|
||||
wantRuntime := s.runtime
|
||||
s.mu.RUnlock()
|
||||
if wantRuntime {
|
||||
known := map[string]bool{}
|
||||
for _, r := range rs {
|
||||
known[r.Link] = true
|
||||
}
|
||||
rt, err := ScanRuntime(ctx, s.Cfg, known)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rs = append(rs, rt...)
|
||||
}
|
||||
s.mu.Lock()
|
||||
var todo []string
|
||||
pending := map[string]bool{}
|
||||
for _, r := range rs {
|
||||
if r.Target == "" {
|
||||
continue
|
||||
}
|
||||
if sz, ok := s.sizeCache[r.Target]; ok {
|
||||
r.ClosureSize, r.NarSize = sz[0], sz[1]
|
||||
} else if !pending[r.Target] {
|
||||
pending[r.Target] = true
|
||||
todo = append(todo, r.Target)
|
||||
}
|
||||
}
|
||||
s.roots = rs
|
||||
s.scannedAt = time.Now()
|
||||
s.sizesDone, s.sizesTodo = 0, len(todo)
|
||||
s.sizing = len(todo) > 0
|
||||
s.mu.Unlock()
|
||||
s.notify()
|
||||
if len(todo) > 0 {
|
||||
go s.computeSizes(ctx, todo)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Set) computeSizes(ctx context.Context, paths []string) {
|
||||
QuerySizes(ctx, s.Cfg, paths, func(batch map[string][2]int64, n int) {
|
||||
s.mu.Lock()
|
||||
for p, sz := range batch {
|
||||
s.sizeCache[p] = sz
|
||||
}
|
||||
for _, r := range s.roots {
|
||||
if sz, ok := batch[r.Target]; ok {
|
||||
r.ClosureSize, r.NarSize = sz[0], sz[1]
|
||||
}
|
||||
}
|
||||
s.sizesDone += n
|
||||
s.mu.Unlock()
|
||||
s.notify()
|
||||
})
|
||||
s.mu.Lock()
|
||||
s.sizing = false
|
||||
s.mu.Unlock()
|
||||
s.notify()
|
||||
}
|
||||
|
||||
// Remove drops roots from the set after a successful delete, without a full
|
||||
// rescan.
|
||||
func (s *Set) Remove(links map[string]bool) {
|
||||
s.mu.Lock()
|
||||
kept := s.roots[:0:0]
|
||||
for _, r := range s.roots {
|
||||
if !links[r.Link] {
|
||||
kept = append(kept, r)
|
||||
}
|
||||
}
|
||||
s.roots = kept
|
||||
s.mu.Unlock()
|
||||
s.notify()
|
||||
}
|
||||
|
||||
// Lookup returns the current roots with the given link paths.
|
||||
func (s *Set) Lookup(links []string) []Root {
|
||||
want := map[string]bool{}
|
||||
for _, l := range links {
|
||||
want[l] = true
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
var out []Root
|
||||
for _, r := range s.roots {
|
||||
if want[r.Link] {
|
||||
out = append(out, *r)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package roots
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os/exec"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const sizeBatch = 16
|
||||
|
||||
// QuerySizes asks `nix path-info -S` for closure and NAR sizes in batches,
|
||||
// calling cb with each batch's results ([closure, nar]) and batch length.
|
||||
// Paths that are not valid get -2 so they are not retried.
|
||||
func QuerySizes(ctx context.Context, cfg Config, paths []string, cb func(map[string][2]int64, int)) {
|
||||
batches := make(chan []string)
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < 4; w++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for b := range batches {
|
||||
cb(pathInfo(ctx, cfg, b), len(b))
|
||||
}
|
||||
}()
|
||||
}
|
||||
for i := 0; i < len(paths); i += sizeBatch {
|
||||
end := min(i+sizeBatch, len(paths))
|
||||
select {
|
||||
case batches <- paths[i:end]:
|
||||
case <-ctx.Done():
|
||||
}
|
||||
}
|
||||
close(batches)
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
type pathInfoEntry struct {
|
||||
Path string `json:"path"`
|
||||
Valid *bool `json:"valid"`
|
||||
ClosureSize int64 `json:"closureSize"`
|
||||
NarSize int64 `json:"narSize"`
|
||||
}
|
||||
|
||||
func pathInfo(ctx context.Context, cfg Config, paths []string) map[string][2]int64 {
|
||||
res := map[string][2]int64{}
|
||||
for _, p := range paths {
|
||||
res[p] = [2]int64{-2, -2}
|
||||
}
|
||||
args := append([]string{"--extra-experimental-features", "nix-command", "path-info", "--json", "-S"}, paths...)
|
||||
// path-info exits non-zero when some paths are invalid but still prints
|
||||
// JSON for the rest, so the exit status is ignored.
|
||||
out, _ := exec.CommandContext(ctx, cfg.NixBin, args...).Output()
|
||||
for p, e := range parsePathInfo(out) {
|
||||
res[p] = [2]int64{e.ClosureSize, e.NarSize}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// parsePathInfo accepts both the object-keyed-by-path format (Nix >= 2.19)
|
||||
// and the older array format.
|
||||
func parsePathInfo(out []byte) map[string]pathInfoEntry {
|
||||
res := map[string]pathInfoEntry{}
|
||||
var obj map[string]*pathInfoEntry
|
||||
if err := json.Unmarshal(out, &obj); err == nil {
|
||||
for p, e := range obj {
|
||||
if e != nil {
|
||||
res[p] = *e
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
var arr []pathInfoEntry
|
||||
if err := json.Unmarshal(out, &arr); err == nil {
|
||||
for _, e := range arr {
|
||||
if e.Path != "" && (e.Valid == nil || *e.Valid) {
|
||||
res[e.Path] = e
|
||||
}
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,517 @@
|
||||
// Package tui implements the interactive terminal interface.
|
||||
package tui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
"github.com/illustris/i-am-gcroot/internal/filter"
|
||||
"github.com/illustris/i-am-gcroot/internal/roots"
|
||||
)
|
||||
|
||||
type mode int
|
||||
|
||||
const (
|
||||
modeNormal mode = iota
|
||||
modeFilter
|
||||
modeConfirmDelete
|
||||
modeConfirmGC
|
||||
modeDetails
|
||||
modeResults
|
||||
modeHelp
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
Query string
|
||||
Force bool
|
||||
WebURL string // shown in the status bar when the web UI runs alongside
|
||||
}
|
||||
|
||||
type Model struct {
|
||||
ctx context.Context
|
||||
set *roots.Set
|
||||
opts Options
|
||||
|
||||
changed chan struct{}
|
||||
|
||||
all []roots.Root
|
||||
view []roots.Root
|
||||
cursor int
|
||||
offset int
|
||||
marks map[string]bool
|
||||
kinds map[roots.Kind]bool
|
||||
sortKey filter.SortKey
|
||||
desc bool
|
||||
hidden bool // show hidden (nix cache) roots
|
||||
|
||||
input textinput.Model
|
||||
query *filter.Query
|
||||
queryErr string
|
||||
|
||||
mode mode
|
||||
pending []roots.Root // roots awaiting delete confirmation
|
||||
collect bool // run a targeted GC after the pending delete
|
||||
dryRun bool
|
||||
results []roots.Result
|
||||
collected string
|
||||
status string
|
||||
statusOK bool
|
||||
busy string // long running operation, shown on the activity line
|
||||
since time.Time
|
||||
scanning bool
|
||||
width int
|
||||
height int
|
||||
quitting bool
|
||||
}
|
||||
|
||||
func New(ctx context.Context, set *roots.Set, opts Options) *Model {
|
||||
ti := textinput.New()
|
||||
ti.Prompt = "/ "
|
||||
ti.Placeholder = "filter: text, /re/, kind:, owner:me, under:/tmp, older:30d, size>1G"
|
||||
ti.SetValue(opts.Query)
|
||||
m := &Model{
|
||||
ctx: ctx,
|
||||
set: set,
|
||||
opts: opts,
|
||||
changed: make(chan struct{}, 1),
|
||||
marks: map[string]bool{},
|
||||
kinds: defaultKinds(),
|
||||
input: ti,
|
||||
sortKey: filter.SortAge,
|
||||
}
|
||||
m.query, _ = filter.Parse(opts.Query)
|
||||
set.OnChange(func() {
|
||||
select {
|
||||
case m.changed <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
})
|
||||
return m
|
||||
}
|
||||
|
||||
func defaultKinds() map[roots.Kind]bool {
|
||||
k := map[roots.Kind]bool{}
|
||||
for _, kind := range roots.AllKinds {
|
||||
k[kind] = kind != roots.KindRuntime
|
||||
}
|
||||
return k
|
||||
}
|
||||
|
||||
type changedMsg struct{}
|
||||
type tickMsg struct{}
|
||||
type refreshedMsg struct{ err error }
|
||||
type deletedMsg struct {
|
||||
results []roots.Result
|
||||
dryRun bool
|
||||
collected string // summary of the targeted GC, if one ran
|
||||
}
|
||||
type gcMsg struct {
|
||||
summary string
|
||||
err error
|
||||
}
|
||||
|
||||
func (m *Model) waitChange() tea.Cmd {
|
||||
return func() tea.Msg {
|
||||
select {
|
||||
case <-m.changed:
|
||||
return changedMsg{}
|
||||
case <-m.ctx.Done():
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Model) refresh() tea.Cmd {
|
||||
m.scanning = true
|
||||
return func() tea.Msg { return refreshedMsg{m.set.Refresh(m.ctx)} }
|
||||
}
|
||||
|
||||
// startBusy shows an operation on the activity line with a running timer.
|
||||
func (m *Model) startBusy(what string) tea.Cmd {
|
||||
m.busy, m.since = what, time.Now()
|
||||
return tick()
|
||||
}
|
||||
|
||||
func tick() tea.Cmd {
|
||||
return tea.Tick(time.Second, func(time.Time) tea.Msg { return tickMsg{} })
|
||||
}
|
||||
|
||||
func (m *Model) setStatus(s string, ok bool) {
|
||||
m.status, m.statusOK = s, ok
|
||||
}
|
||||
|
||||
func (m *Model) Init() tea.Cmd {
|
||||
return tea.Batch(m.refresh(), m.waitChange())
|
||||
}
|
||||
|
||||
func (m *Model) reload() {
|
||||
m.all = m.set.Snapshot()
|
||||
m.apply()
|
||||
}
|
||||
|
||||
func (m *Model) apply() {
|
||||
var curLink string
|
||||
if m.cursor < len(m.view) {
|
||||
curLink = m.view[m.cursor].Link
|
||||
}
|
||||
m.view = filter.Apply(m.all, m.query, m.kinds, m.hidden, m.sortKey, m.desc)
|
||||
m.cursor = 0
|
||||
for i := range m.view {
|
||||
if m.view[i].Link == curLink {
|
||||
m.cursor = i
|
||||
break
|
||||
}
|
||||
}
|
||||
// forget marks that are no longer present at all
|
||||
present := map[string]bool{}
|
||||
for _, r := range m.all {
|
||||
present[r.Link] = true
|
||||
}
|
||||
for l := range m.marks {
|
||||
if !present[l] {
|
||||
delete(m.marks, l)
|
||||
}
|
||||
}
|
||||
m.clamp()
|
||||
}
|
||||
|
||||
// footerLines is the number of lines below the list: kinds/sort, key hints,
|
||||
// the activity line when there is something to report, and the filter.
|
||||
func (m *Model) footerLines() int {
|
||||
n := 2
|
||||
if m.activity() != "" {
|
||||
n++
|
||||
}
|
||||
if m.mode == modeFilter || m.input.Value() != "" {
|
||||
n++
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (m *Model) listHeight() int {
|
||||
h := m.height - 2 - m.footerLines()
|
||||
if h < 1 {
|
||||
h = 1
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func (m *Model) clamp() {
|
||||
if m.cursor >= len(m.view) {
|
||||
m.cursor = len(m.view) - 1
|
||||
}
|
||||
if m.cursor < 0 {
|
||||
m.cursor = 0
|
||||
}
|
||||
h := m.listHeight()
|
||||
if m.cursor < m.offset {
|
||||
m.offset = m.cursor
|
||||
}
|
||||
if m.cursor >= m.offset+h {
|
||||
m.offset = m.cursor - h + 1
|
||||
}
|
||||
if m.offset > max(0, len(m.view)-h) {
|
||||
m.offset = max(0, len(m.view)-h)
|
||||
}
|
||||
}
|
||||
|
||||
// selection returns marked roots, or the root under the cursor if nothing is
|
||||
// marked.
|
||||
func (m *Model) selection() []roots.Root {
|
||||
var out []roots.Root
|
||||
for _, r := range m.all {
|
||||
if m.marks[r.Link] {
|
||||
out = append(out, r)
|
||||
}
|
||||
}
|
||||
if len(out) == 0 && m.cursor < len(m.view) {
|
||||
out = append(out, m.view[m.cursor])
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
switch msg := msg.(type) {
|
||||
case tea.WindowSizeMsg:
|
||||
m.width, m.height = msg.Width, msg.Height
|
||||
m.input.Width = max(10, msg.Width-4)
|
||||
m.clamp()
|
||||
return m, nil
|
||||
case changedMsg:
|
||||
m.reload()
|
||||
return m, m.waitChange()
|
||||
case tickMsg:
|
||||
if m.busy != "" {
|
||||
return m, tick()
|
||||
}
|
||||
return m, nil
|
||||
case refreshedMsg:
|
||||
m.scanning = false
|
||||
if msg.err != nil {
|
||||
m.setStatus("scan failed: "+msg.err.Error(), false)
|
||||
}
|
||||
m.reload()
|
||||
return m, nil
|
||||
case deletedMsg:
|
||||
m.busy = ""
|
||||
m.results = msg.results
|
||||
m.dryRun = msg.dryRun
|
||||
m.collected = msg.collected
|
||||
m.setStatus(m.summarize(msg), true)
|
||||
if !msg.dryRun {
|
||||
gone := map[string]bool{}
|
||||
for _, r := range msg.results {
|
||||
if r.OK() {
|
||||
gone[r.Link] = true
|
||||
delete(m.marks, r.Link)
|
||||
} else {
|
||||
m.statusOK = false
|
||||
}
|
||||
}
|
||||
m.set.Remove(gone)
|
||||
}
|
||||
return m, nil
|
||||
case gcMsg:
|
||||
m.busy = ""
|
||||
if msg.err != nil {
|
||||
m.setStatus("gc failed: "+msg.err.Error(), false)
|
||||
} else {
|
||||
m.setStatus("gc: "+msg.summary, true)
|
||||
}
|
||||
return m, m.refresh()
|
||||
case tea.KeyMsg:
|
||||
return m.handleKey(msg)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (m *Model) handleKey(k tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
key := k.String()
|
||||
if key == "ctrl+c" {
|
||||
m.quitting = true
|
||||
return m, tea.Quit
|
||||
}
|
||||
switch m.mode {
|
||||
case modeFilter:
|
||||
switch key {
|
||||
case "enter", "esc":
|
||||
m.mode = modeNormal
|
||||
m.input.Blur()
|
||||
return m, nil
|
||||
}
|
||||
var cmd tea.Cmd
|
||||
m.input, cmd = m.input.Update(k)
|
||||
q, err := filter.Parse(m.input.Value())
|
||||
if err != nil {
|
||||
m.queryErr = err.Error()
|
||||
} else {
|
||||
m.queryErr = ""
|
||||
m.query = q
|
||||
m.apply()
|
||||
}
|
||||
return m, cmd
|
||||
case modeConfirmDelete:
|
||||
switch key {
|
||||
case "y", "Y":
|
||||
m.mode = modeNormal
|
||||
rs, opts, collect := m.pending, roots.DeleteOpts{Force: m.opts.Force}, m.collect
|
||||
what := fmt.Sprintf("deleting %d root(s)", len(rs))
|
||||
if collect {
|
||||
what += " and collecting their garbage"
|
||||
}
|
||||
return m, tea.Batch(m.startBusy(what), func() tea.Msg {
|
||||
res := roots.Delete(m.ctx, m.set.Cfg, rs, opts)
|
||||
msg := deletedMsg{results: res}
|
||||
if collect {
|
||||
c, err := roots.Collect(m.ctx, m.set.Cfg, roots.CollectedTargets(res))
|
||||
msg.collected = c.String()
|
||||
if err != nil {
|
||||
msg.collected += "; gc error: " + err.Error()
|
||||
}
|
||||
}
|
||||
// rebuild the list from disk rather than trusting in-memory state
|
||||
_ = m.set.Refresh(m.ctx)
|
||||
return msg
|
||||
})
|
||||
case "n", "N", "esc", "q":
|
||||
m.mode = modeNormal
|
||||
}
|
||||
return m, nil
|
||||
case modeConfirmGC:
|
||||
switch key {
|
||||
case "y", "Y":
|
||||
m.mode = modeNormal
|
||||
return m, tea.Batch(m.startBusy("running nix-store --gc"), func() tea.Msg {
|
||||
s, err := roots.GC(m.ctx, m.set.Cfg)
|
||||
return gcMsg{s, err}
|
||||
})
|
||||
case "n", "N", "esc", "q":
|
||||
m.mode = modeNormal
|
||||
}
|
||||
return m, nil
|
||||
case modeDetails, modeResults, modeHelp:
|
||||
switch key {
|
||||
case "esc", "q", "enter", "?":
|
||||
m.mode = modeNormal
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
switch key {
|
||||
case "q":
|
||||
m.quitting = true
|
||||
return m, tea.Quit
|
||||
case "esc":
|
||||
if m.input.Value() != "" {
|
||||
m.input.SetValue("")
|
||||
m.query, _ = filter.Parse("")
|
||||
m.apply()
|
||||
}
|
||||
case "/":
|
||||
m.mode = modeFilter
|
||||
return m, m.input.Focus()
|
||||
case "up", "k":
|
||||
m.cursor--
|
||||
case "down", "j":
|
||||
m.cursor++
|
||||
case "pgup", "ctrl+b":
|
||||
m.cursor -= m.listHeight()
|
||||
case "pgdown", "ctrl+f":
|
||||
m.cursor += m.listHeight()
|
||||
case "home":
|
||||
m.cursor = 0
|
||||
case "end", "G":
|
||||
m.cursor = len(m.view) - 1
|
||||
case " ", "x":
|
||||
if m.cursor < len(m.view) {
|
||||
r := m.view[m.cursor]
|
||||
if r.Deletable() {
|
||||
if m.marks[r.Link] {
|
||||
delete(m.marks, r.Link)
|
||||
} else {
|
||||
m.marks[r.Link] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
case "a":
|
||||
for _, r := range m.view {
|
||||
if r.Deletable() && !r.Protected {
|
||||
m.marks[r.Link] = true
|
||||
}
|
||||
}
|
||||
case "A":
|
||||
m.marks = map[string]bool{}
|
||||
case "1", "2", "3", "4", "5":
|
||||
kind := roots.AllKinds[int(key[0]-'1')]
|
||||
m.kinds[kind] = !m.kinds[kind]
|
||||
if kind == roots.KindRuntime && m.kinds[kind] && !m.set.Status().Runtime {
|
||||
m.set.SetRuntime(true)
|
||||
m.apply()
|
||||
return m, m.refresh()
|
||||
}
|
||||
m.apply()
|
||||
case "0":
|
||||
m.kinds = defaultKinds()
|
||||
m.apply()
|
||||
case "s":
|
||||
m.sortKey = (m.sortKey + 1) % filter.SortKey(len(filter.SortNames))
|
||||
m.apply()
|
||||
case "S":
|
||||
m.desc = !m.desc
|
||||
m.apply()
|
||||
case "r":
|
||||
return m, m.refresh()
|
||||
case "enter":
|
||||
if m.cursor < len(m.view) {
|
||||
m.mode = modeDetails
|
||||
}
|
||||
case "d", "D", "X":
|
||||
sel := m.selection()
|
||||
if len(sel) == 0 {
|
||||
return m, nil
|
||||
}
|
||||
if key == "D" {
|
||||
return m, func() tea.Msg {
|
||||
return deletedMsg{results: roots.Delete(m.ctx, m.set.Cfg, sel, roots.DeleteOpts{DryRun: true, Force: m.opts.Force}), dryRun: true}
|
||||
}
|
||||
}
|
||||
if m.busy != "" {
|
||||
m.setStatus("wait for "+m.busy+" to finish", false)
|
||||
return m, nil
|
||||
}
|
||||
m.pending = sel
|
||||
m.collect = key == "X"
|
||||
m.mode = modeConfirmDelete
|
||||
case "h":
|
||||
m.hidden = !m.hidden
|
||||
m.apply()
|
||||
case "g":
|
||||
if m.busy != "" {
|
||||
m.setStatus("wait for "+m.busy+" to finish", false)
|
||||
return m, nil
|
||||
}
|
||||
m.mode = modeConfirmGC
|
||||
case "R":
|
||||
if m.results != nil {
|
||||
m.mode = modeResults
|
||||
}
|
||||
case "?":
|
||||
m.mode = modeHelp
|
||||
}
|
||||
m.clamp()
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func kindList(kinds map[roots.Kind]bool) string {
|
||||
var parts []string
|
||||
for i, k := range roots.AllKinds {
|
||||
mark := " "
|
||||
if kinds[k] {
|
||||
mark = "x"
|
||||
}
|
||||
parts = append(parts, fmt.Sprintf("%d[%s]%s", i+1, mark, k))
|
||||
}
|
||||
return strings.Join(parts, " ")
|
||||
}
|
||||
|
||||
// summarize turns a delete outcome into one status line; R shows the rest.
|
||||
func (m *Model) summarize(msg deletedMsg) string {
|
||||
var ok, failed []roots.Result
|
||||
for _, r := range msg.results {
|
||||
if r.OK() {
|
||||
ok = append(ok, r)
|
||||
} else {
|
||||
failed = append(failed, r)
|
||||
}
|
||||
}
|
||||
var s string
|
||||
if msg.dryRun {
|
||||
s = fmt.Sprintf("dry run: %d action(s)", len(ok))
|
||||
if len(ok) > 0 {
|
||||
s += ", e.g. " + ok[0].Action
|
||||
}
|
||||
if len(failed) > 0 {
|
||||
s += fmt.Sprintf("; %d would fail", len(failed))
|
||||
}
|
||||
} else {
|
||||
s = fmt.Sprintf("deleted %d/%d root(s)", len(ok), len(msg.results))
|
||||
if msg.collected != "" {
|
||||
s += "; " + msg.collected
|
||||
} else if len(ok) > 0 {
|
||||
s += "; g to collect garbage"
|
||||
}
|
||||
}
|
||||
if len(failed) > 0 {
|
||||
s += fmt.Sprintf("; failed %s: %s", filepath.Base(failed[0].Link), failed[0].Err)
|
||||
if len(failed) > 1 {
|
||||
s += fmt.Sprintf(" (+%d more)", len(failed)-1)
|
||||
}
|
||||
}
|
||||
return s + " [R details]"
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
|
||||
"github.com/illustris/i-am-gcroot/internal/filter"
|
||||
"github.com/illustris/i-am-gcroot/internal/roots"
|
||||
)
|
||||
|
||||
var (
|
||||
styleHeader = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("12"))
|
||||
styleColHeader = lipgloss.NewStyle().Bold(true).Underline(true)
|
||||
styleCursor = lipgloss.NewStyle().Reverse(true)
|
||||
styleMarked = lipgloss.NewStyle().Foreground(lipgloss.Color("11")).Bold(true)
|
||||
styleDim = lipgloss.NewStyle().Foreground(lipgloss.Color("8"))
|
||||
styleErr = lipgloss.NewStyle().Foreground(lipgloss.Color("9"))
|
||||
styleOK = lipgloss.NewStyle().Foreground(lipgloss.Color("10"))
|
||||
styleBox = lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).Padding(0, 1)
|
||||
|
||||
kindColor = map[roots.Kind]lipgloss.Color{
|
||||
roots.KindIndirect: "14",
|
||||
roots.KindProfile: "13",
|
||||
roots.KindDirect: "12",
|
||||
roots.KindDangling: "9",
|
||||
roots.KindRuntime: "8",
|
||||
}
|
||||
)
|
||||
|
||||
func (m *Model) View() string {
|
||||
if m.quitting {
|
||||
return ""
|
||||
}
|
||||
if m.width == 0 {
|
||||
return "loading..."
|
||||
}
|
||||
switch m.mode {
|
||||
case modeHelp:
|
||||
return m.overlay(helpText)
|
||||
case modeDetails:
|
||||
return m.overlay(m.details())
|
||||
case modeResults:
|
||||
return m.overlay(m.resultsView())
|
||||
case modeConfirmDelete:
|
||||
return m.overlay(m.confirmView())
|
||||
case modeConfirmGC:
|
||||
return m.overlay("Run nix-store --gc now?\n\nThis deletes every store path that is no longer reachable from a root.\n\n[y] yes [n] no")
|
||||
}
|
||||
return m.listView()
|
||||
}
|
||||
|
||||
func (m *Model) overlay(body string) string {
|
||||
box := styleBox.Width(min(m.width-4, 120)).Render(body)
|
||||
return lipgloss.Place(m.width, m.height, lipgloss.Center, lipgloss.Center, box)
|
||||
}
|
||||
|
||||
const (
|
||||
wMark = 2
|
||||
wKind = 9
|
||||
wOwner = 10
|
||||
wAge = 6
|
||||
wSize = 8
|
||||
)
|
||||
|
||||
func (m *Model) columns() (link, target int) {
|
||||
rest := m.width - wMark - wKind - wOwner - wAge - wSize - 6
|
||||
if rest < 20 {
|
||||
rest = 20
|
||||
}
|
||||
target = rest * 2 / 5
|
||||
return rest - target, target
|
||||
}
|
||||
|
||||
func (m *Model) listView() string {
|
||||
var b strings.Builder
|
||||
st := m.set.Status()
|
||||
|
||||
title := styleHeader.Render("i-am-gcroot")
|
||||
info := fmt.Sprintf(" %d roots, %d shown, %d marked (up to %s)", st.Total, len(m.view), len(m.marks), filter.HumanSize(m.markedSize()))
|
||||
if m.opts.WebURL != "" {
|
||||
info += styleDim.Render(" web: " + m.opts.WebURL)
|
||||
}
|
||||
b.WriteString(trunc(title+info, m.width+20) + "\n")
|
||||
|
||||
wl, wt := m.columns()
|
||||
hdr := fmt.Sprintf("%-*s %-*s %-*s %*s %*s %-*s %-*s", wMark, "", wKind, "KIND", wOwner, "OWNER", wAge, "AGE", wSize, "CLOSURE", wl, "LINK", wt, "TARGET")
|
||||
b.WriteString(styleColHeader.Render(trunc(hdr, m.width)) + "\n")
|
||||
|
||||
h := m.listHeight()
|
||||
now := time.Now()
|
||||
for i := m.offset; i < m.offset+h; i++ {
|
||||
if i >= len(m.view) {
|
||||
b.WriteString("\n")
|
||||
continue
|
||||
}
|
||||
b.WriteString(m.row(&m.view[i], i == m.cursor, wl, wt, now) + "\n")
|
||||
}
|
||||
|
||||
// footer
|
||||
sortDir := "asc"
|
||||
if m.desc {
|
||||
sortDir = "desc"
|
||||
}
|
||||
hidden := " "
|
||||
if m.hidden {
|
||||
hidden = "x"
|
||||
}
|
||||
line := fmt.Sprintf("%s h[%s]hidden sort:%s %s", kindList(m.kinds), hidden, m.sortKey, sortDir)
|
||||
if st.Sizing {
|
||||
line += fmt.Sprintf(" sizing %d/%d", st.SizesDone, st.SizesTodo)
|
||||
}
|
||||
if m.scanning {
|
||||
line += " scanning..."
|
||||
}
|
||||
b.WriteString(styleDim.Render(trunc(line, m.width)) + "\n")
|
||||
b.WriteString(styleDim.Render(trunc(hints, m.width)) + "\n")
|
||||
if a := m.activity(); a != "" {
|
||||
b.WriteString(a + "\n")
|
||||
}
|
||||
if m.mode == modeFilter || m.input.Value() != "" {
|
||||
b.WriteString(m.input.View())
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
const hints = "space mark a mark all d delete X delete+collect D dry-run g gc / filter s sort h hidden enter details R results ? help q quit"
|
||||
|
||||
// activity is the line under the key hints: a running operation, a filter
|
||||
// error, or the outcome of the last operation.
|
||||
func (m *Model) activity() string {
|
||||
switch {
|
||||
case m.busy != "":
|
||||
return styleMarked.Render(trunc(fmt.Sprintf("%s... %ds", m.busy, int(time.Since(m.since).Seconds())), m.width))
|
||||
case m.queryErr != "":
|
||||
return styleErr.Render(trunc(m.queryErr, m.width))
|
||||
case m.status == "":
|
||||
return ""
|
||||
case m.statusOK:
|
||||
return styleOK.Render(trunc(m.status, m.width))
|
||||
}
|
||||
return styleErr.Render(trunc(m.status, m.width))
|
||||
}
|
||||
|
||||
func (m *Model) row(r *roots.Root, cursor bool, wl, wt int, now time.Time) string {
|
||||
mark := " "
|
||||
if m.marks[r.Link] {
|
||||
mark = "*"
|
||||
} else if r.Protected {
|
||||
mark = "!"
|
||||
}
|
||||
link := r.Link
|
||||
if r.Kind == roots.KindRuntime && r.Command != "" {
|
||||
link += " " + r.Command
|
||||
}
|
||||
target := r.TargetName()
|
||||
switch {
|
||||
case r.Kind == roots.KindRuntime && r.Count > 1:
|
||||
target = fmt.Sprintf("(%d paths)", r.Count)
|
||||
case r.Kind == roots.KindDangling:
|
||||
target = "(missing)"
|
||||
case r.Reason != "" && r.Target == "":
|
||||
target = "(" + r.Reason + ")"
|
||||
}
|
||||
size := filter.HumanSize(r.ClosureSize)
|
||||
if r.Kind == roots.KindDangling {
|
||||
size = "-"
|
||||
}
|
||||
kind := fmt.Sprintf("%-*s", wKind, r.Kind)
|
||||
if !cursor {
|
||||
kind = lipgloss.NewStyle().Foreground(kindColor[r.Kind]).Render(kind)
|
||||
}
|
||||
s := fmt.Sprintf("%-*s %s %-*s %*s %*s %-*s %-*s",
|
||||
wMark, mark, kind, wOwner, trunc(r.Owner, wOwner), wAge, filter.HumanAge(r.ModTime, now),
|
||||
wSize, size, wl, midTrunc(link, wl), wt, trunc(target, wt))
|
||||
switch {
|
||||
case cursor:
|
||||
return styleCursor.Render(s)
|
||||
case m.marks[r.Link]:
|
||||
return styleMarked.Render(s)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (m *Model) markedSize() int64 {
|
||||
var n int64
|
||||
for _, r := range m.all {
|
||||
if m.marks[r.Link] && r.ClosureSize > 0 {
|
||||
n += r.ClosureSize
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (m *Model) details() string {
|
||||
r := m.view[m.cursor]
|
||||
var b strings.Builder
|
||||
field := func(k, v string) {
|
||||
if v != "" {
|
||||
fmt.Fprintf(&b, "%s %s\n", styleHeader.Render(fmt.Sprintf("%-12s", k)), v)
|
||||
}
|
||||
}
|
||||
field("kind", string(r.Kind))
|
||||
field("link", r.Link)
|
||||
field("via", r.Via)
|
||||
field("target", r.Target)
|
||||
field("owner", fmt.Sprintf("%s (uid %d)", r.Owner, r.UID))
|
||||
if !r.ModTime.IsZero() {
|
||||
field("modified", r.ModTime.Format(time.RFC3339)+" ("+filter.HumanAge(r.ModTime, time.Now())+" ago)")
|
||||
}
|
||||
if r.Kind == roots.KindProfile {
|
||||
field("profile", r.Profile)
|
||||
cur := ""
|
||||
if r.Current {
|
||||
cur = " (current)"
|
||||
}
|
||||
field("generation", fmt.Sprintf("%d%s", r.Generation, cur))
|
||||
}
|
||||
if r.Count > 0 {
|
||||
field("store paths", fmt.Sprint(r.Count))
|
||||
}
|
||||
field("command", r.Command)
|
||||
if r.Target != "" {
|
||||
field("closure", filter.HumanSize(r.ClosureSize))
|
||||
field("nar size", filter.HumanSize(r.NarSize))
|
||||
}
|
||||
if r.Protected {
|
||||
field("protected", r.Reason)
|
||||
} else {
|
||||
field("note", r.Reason)
|
||||
}
|
||||
b.WriteString("\n" + styleDim.Render("esc to close"))
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *Model) confirmView() string {
|
||||
var b strings.Builder
|
||||
var size int64
|
||||
protected := 0
|
||||
for _, r := range m.pending {
|
||||
if r.ClosureSize > 0 {
|
||||
size += r.ClosureSize
|
||||
}
|
||||
if r.Protected {
|
||||
protected++
|
||||
}
|
||||
}
|
||||
verb := "Delete"
|
||||
if m.collect {
|
||||
verb = "Delete and garbage collect"
|
||||
}
|
||||
fmt.Fprintf(&b, "%s %d root(s)? Closures total up to %s (shared paths may stay alive).\n", verb, len(m.pending), filter.HumanSize(size))
|
||||
if m.collect {
|
||||
b.WriteString(styleDim.Render("Store paths only reachable through these roots are deleted right away.") + "\n")
|
||||
}
|
||||
b.WriteString("\n")
|
||||
show := min(len(m.pending), max(3, m.height-12))
|
||||
for _, r := range m.pending[:show] {
|
||||
fmt.Fprintf(&b, " %-9s %s\n", r.Kind, r.Link)
|
||||
}
|
||||
if len(m.pending) > show {
|
||||
fmt.Fprintf(&b, " ... and %d more\n", len(m.pending)-show)
|
||||
}
|
||||
if protected > 0 && !m.opts.Force {
|
||||
b.WriteString(styleErr.Render(fmt.Sprintf("\n%d protected root(s) will be skipped (use --force to override).\n", protected)))
|
||||
}
|
||||
b.WriteString("\n[y] delete [n] cancel")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (m *Model) resultsView() string {
|
||||
var b strings.Builder
|
||||
if m.dryRun {
|
||||
b.WriteString(styleHeader.Render("Dry run: these actions would be taken") + "\n\n")
|
||||
} else {
|
||||
b.WriteString(styleHeader.Render("Delete results") + "\n\n")
|
||||
}
|
||||
if m.collected != "" {
|
||||
b.WriteString(m.collected + "\n\n")
|
||||
}
|
||||
show := min(len(m.results), max(3, m.height-10))
|
||||
for _, r := range m.results[:show] {
|
||||
if r.OK() {
|
||||
b.WriteString(styleOK.Render("ok ") + r.Action + "\n")
|
||||
} else {
|
||||
b.WriteString(styleErr.Render("fail ") + r.Link + ": " + r.Err + "\n")
|
||||
}
|
||||
}
|
||||
if len(m.results) > show {
|
||||
fmt.Fprintf(&b, "... and %d more\n", len(m.results)-show)
|
||||
}
|
||||
b.WriteString("\n" + styleDim.Render("esc to close"))
|
||||
return b.String()
|
||||
}
|
||||
|
||||
const helpText = `Keys
|
||||
j/k, up/down, pgup/pgdn, home/end move
|
||||
space / x mark/unmark root under cursor
|
||||
a / A mark all shown (non-protected) / clear marks
|
||||
d delete marked roots (or the one under cursor)
|
||||
X delete, then garbage collect just the paths they held
|
||||
D dry run: show what delete would do
|
||||
g run nix-store --gc
|
||||
/ edit filter, enter/esc to leave, esc again clears it
|
||||
1-5 toggle kinds: indirect profile direct dangling runtime
|
||||
0 reset kind toggles
|
||||
h show/hide nix cache roots (~/.cache/nix/...)
|
||||
s / S cycle sort key / reverse
|
||||
enter details
|
||||
R full results of the last delete / dry run
|
||||
r rescan
|
||||
q quit
|
||||
|
||||
Filter syntax (terms are ANDed, prefix with - to negate)
|
||||
foo /regex/ kind:indirect,dangling owner:me under:/tmp
|
||||
older:30d newer:2h size>1G size<10M is:protected is:current is:hidden`
|
||||
|
||||
func trunc(s string, w int) string {
|
||||
if w <= 0 {
|
||||
return ""
|
||||
}
|
||||
r := []rune(s)
|
||||
if len(r) <= w {
|
||||
return s
|
||||
}
|
||||
if w == 1 {
|
||||
return string(r[:1])
|
||||
}
|
||||
return string(r[:w-1]) + "~"
|
||||
}
|
||||
|
||||
// midTrunc keeps both ends of a path, which carry the most information.
|
||||
func midTrunc(s string, w int) string {
|
||||
r := []rune(s)
|
||||
if len(r) <= w || w < 5 {
|
||||
return trunc(s, w)
|
||||
}
|
||||
head := (w - 3) / 3
|
||||
tail := w - 3 - head
|
||||
return string(r[:head]) + "..." + string(r[len(r)-tail:])
|
||||
}
|
||||
@@ -0,0 +1,325 @@
|
||||
// Package web serves an optional htmx based UI and a small JSON API.
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"embed"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/illustris/i-am-gcroot/internal/filter"
|
||||
"github.com/illustris/i-am-gcroot/internal/roots"
|
||||
)
|
||||
|
||||
//go:embed templates/*.html static/*
|
||||
var assets embed.FS
|
||||
|
||||
type Options struct {
|
||||
Addr string
|
||||
NoAuth bool
|
||||
Force bool
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
set *roots.Set
|
||||
opts Options
|
||||
token string
|
||||
tmpl *template.Template
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func New(ctx context.Context, set *roots.Set, opts Options) (*Server, error) {
|
||||
b := make([]byte, 16)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
funcs := template.FuncMap{
|
||||
"size": filter.HumanSize,
|
||||
"age": func(t time.Time) string { return filter.HumanAge(t, time.Now()) },
|
||||
"kinds": func() []roots.Kind { return roots.AllKinds },
|
||||
"sorts": func() []string { return filter.SortNames },
|
||||
}
|
||||
tmpl, err := template.New("").Funcs(funcs).ParseFS(assets, "templates/*.html")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Server{set: set, opts: opts, token: hex.EncodeToString(b), tmpl: tmpl, ctx: ctx}, nil
|
||||
}
|
||||
|
||||
// Listen binds the address and returns the URL to open (including the token).
|
||||
func (s *Server) Listen() (net.Listener, string, error) {
|
||||
ln, err := net.Listen("tcp", s.opts.Addr)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
url := "http://" + ln.Addr().String() + "/"
|
||||
if !s.opts.NoAuth {
|
||||
url += "?token=" + s.token
|
||||
}
|
||||
return ln, url, nil
|
||||
}
|
||||
|
||||
func (s *Server) Serve(ln net.Listener) error {
|
||||
srv := &http.Server{Handler: s.Handler(), ReadHeaderTimeout: 10 * time.Second}
|
||||
go func() {
|
||||
<-s.ctx.Done()
|
||||
srv.Close()
|
||||
}()
|
||||
err := srv.Serve(ln)
|
||||
if err == http.ErrServerClosed {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Server) Handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
static, _ := fs.Sub(assets, "static")
|
||||
mux.Handle("GET /static/", http.StripPrefix("/static/", http.FileServerFS(static)))
|
||||
mux.HandleFunc("GET /{$}", s.index)
|
||||
mux.HandleFunc("GET /rows", s.rows)
|
||||
mux.HandleFunc("POST /delete", s.deleteHTML)
|
||||
mux.HandleFunc("POST /gc", s.gcHTML)
|
||||
mux.HandleFunc("POST /refresh", s.refreshHTML)
|
||||
mux.HandleFunc("GET /api/roots", s.apiRoots)
|
||||
mux.HandleFunc("POST /api/delete", s.apiDelete)
|
||||
mux.HandleFunc("POST /api/gc", s.apiGC)
|
||||
return s.auth(mux)
|
||||
}
|
||||
|
||||
const cookieName = "i-am-gcroot-token"
|
||||
|
||||
// auth accepts the token as ?token=, a cookie, or a bearer header. A valid
|
||||
// ?token= sets the cookie so the UI keeps working without it in the URL.
|
||||
// Mutating requests from the browser must also carry X-Token (htmx sends it)
|
||||
// so a cross-site form post cannot ride the cookie.
|
||||
func (s *Server) auth(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if s.opts.NoAuth || strings.HasPrefix(r.URL.Path, "/static/") {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
ok := func(t string) bool { return subtle.ConstantTimeCompare([]byte(t), []byte(s.token)) == 1 }
|
||||
header := r.Header.Get("X-Token")
|
||||
if h := r.Header.Get("Authorization"); strings.HasPrefix(h, "Bearer ") {
|
||||
header = strings.TrimPrefix(h, "Bearer ")
|
||||
}
|
||||
if r.Method != http.MethodGet {
|
||||
if !ok(header) {
|
||||
http.Error(w, "missing or bad token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
if q := r.URL.Query().Get("token"); ok(q) {
|
||||
http.SetCookie(w, &http.Cookie{Name: cookieName, Value: s.token, Path: "/", HttpOnly: true, SameSite: http.SameSiteStrictMode})
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
if c, err := r.Cookie(cookieName); (err == nil && ok(c.Value)) || ok(header) {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
http.Error(w, "missing or bad token; use the URL printed at startup", http.StatusUnauthorized)
|
||||
})
|
||||
}
|
||||
|
||||
type viewParams struct {
|
||||
Query string
|
||||
Kinds map[roots.Kind]bool
|
||||
Sort string
|
||||
Desc bool
|
||||
Hidden bool
|
||||
Err string
|
||||
Rows []roots.Root
|
||||
Total int
|
||||
Status roots.Status
|
||||
Token string
|
||||
Message string
|
||||
Results []roots.Result
|
||||
Checked map[string]bool
|
||||
}
|
||||
|
||||
type deleteOutcome struct {
|
||||
Results []roots.Result `json:"results"`
|
||||
Collected *roots.CollectResult `json:"collected,omitempty"`
|
||||
GCError string `json:"gcError,omitempty"`
|
||||
}
|
||||
|
||||
func (s *Server) params(r *http.Request) viewParams {
|
||||
_ = r.ParseForm()
|
||||
p := viewParams{Query: r.Form.Get("q"), Sort: r.Form.Get("sort"), Desc: r.Form.Get("desc") != "", Hidden: r.Form.Get("hidden") != "", Checked: map[string]bool{}}
|
||||
for _, l := range r.Form["link"] {
|
||||
p.Checked[l] = true
|
||||
}
|
||||
if s.opts.NoAuth {
|
||||
p.Token = ""
|
||||
} else {
|
||||
p.Token = s.token
|
||||
}
|
||||
p.Kinds = map[roots.Kind]bool{}
|
||||
if ks, ok := r.Form["kind"]; ok || r.Form.Get("kinds") != "" {
|
||||
for _, k := range ks {
|
||||
p.Kinds[roots.Kind(k)] = true
|
||||
}
|
||||
} else {
|
||||
for _, k := range roots.AllKinds {
|
||||
p.Kinds[k] = k != roots.KindRuntime
|
||||
}
|
||||
}
|
||||
if p.Sort == "" {
|
||||
p.Sort = "age"
|
||||
}
|
||||
if p.Kinds[roots.KindRuntime] && !s.set.Status().Runtime {
|
||||
s.set.SetRuntime(true)
|
||||
_ = s.set.Refresh(s.ctx)
|
||||
}
|
||||
q, err := filter.Parse(p.Query)
|
||||
if err != nil {
|
||||
p.Err = err.Error()
|
||||
q, _ = filter.Parse("")
|
||||
}
|
||||
all := s.set.Snapshot()
|
||||
p.Total = len(all)
|
||||
p.Rows = filter.Apply(all, q, p.Kinds, p.Hidden, filter.ParseSort(p.Sort), p.Desc)
|
||||
p.Status = s.set.Status()
|
||||
return p
|
||||
}
|
||||
|
||||
func (s *Server) render(w http.ResponseWriter, name string, data any) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
if err := s.tmpl.ExecuteTemplate(w, name, data); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) index(w http.ResponseWriter, r *http.Request) {
|
||||
s.render(w, "index.html", s.params(r))
|
||||
}
|
||||
|
||||
func (s *Server) rows(w http.ResponseWriter, r *http.Request) {
|
||||
s.render(w, "rows", s.params(r))
|
||||
}
|
||||
|
||||
func (s *Server) doDelete(r *http.Request, links []string, dry, collect bool) deleteOutcome {
|
||||
rs := s.set.Lookup(links)
|
||||
out := deleteOutcome{Results: roots.Delete(r.Context(), s.set.Cfg, rs, roots.DeleteOpts{DryRun: dry, Force: s.opts.Force})}
|
||||
if dry {
|
||||
return out
|
||||
}
|
||||
gone := map[string]bool{}
|
||||
for _, x := range out.Results {
|
||||
if x.OK() {
|
||||
gone[x.Link] = true
|
||||
}
|
||||
}
|
||||
s.set.Remove(gone)
|
||||
defer s.set.Refresh(s.ctx)
|
||||
if collect {
|
||||
c, err := roots.Collect(r.Context(), s.set.Cfg, roots.CollectedTargets(out.Results))
|
||||
out.Collected = &c
|
||||
if err != nil {
|
||||
out.GCError = err.Error()
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *Server) deleteHTML(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
dry := r.Form.Get("dry") != ""
|
||||
out := s.doDelete(r, r.Form["link"], dry, r.Form.Get("collect") != "")
|
||||
res := out.Results
|
||||
p := s.params(r)
|
||||
p.Results = res
|
||||
ok := 0
|
||||
for _, x := range res {
|
||||
if x.OK() {
|
||||
ok++
|
||||
}
|
||||
}
|
||||
if dry {
|
||||
p.Message = fmt.Sprintf("Dry run: %d action(s)", len(res))
|
||||
} else {
|
||||
p.Message = fmt.Sprintf("Deleted %d of %d root(s)", ok, len(res))
|
||||
if out.Collected != nil {
|
||||
p.Message += "; " + out.Collected.String()
|
||||
}
|
||||
p.Err = out.GCError
|
||||
}
|
||||
s.render(w, "rows", p)
|
||||
}
|
||||
|
||||
func (s *Server) gcHTML(w http.ResponseWriter, r *http.Request) {
|
||||
summary, err := roots.GC(r.Context(), s.set.Cfg)
|
||||
_ = s.set.Refresh(s.ctx)
|
||||
p := s.params(r)
|
||||
if err != nil {
|
||||
p.Err = err.Error()
|
||||
} else {
|
||||
p.Message = "GC: " + summary
|
||||
}
|
||||
s.render(w, "rows", p)
|
||||
}
|
||||
|
||||
func (s *Server) refreshHTML(w http.ResponseWriter, r *http.Request) {
|
||||
p := viewParams{}
|
||||
if err := s.set.Refresh(s.ctx); err != nil {
|
||||
p.Err = err.Error()
|
||||
}
|
||||
p2 := s.params(r)
|
||||
p2.Err = p.Err
|
||||
p2.Message = "Rescanned"
|
||||
s.render(w, "rows", p2)
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, v any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetIndent("", " ")
|
||||
_ = enc.Encode(v)
|
||||
}
|
||||
|
||||
func (s *Server) apiRoots(w http.ResponseWriter, r *http.Request) {
|
||||
p := s.params(r)
|
||||
if p.Err != "" {
|
||||
http.Error(w, p.Err, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
writeJSON(w, p.Rows)
|
||||
}
|
||||
|
||||
type deleteReq struct {
|
||||
Links []string `json:"links"`
|
||||
DryRun bool `json:"dryRun"`
|
||||
Collect bool `json:"collect"`
|
||||
}
|
||||
|
||||
func (s *Server) apiDelete(w http.ResponseWriter, r *http.Request) {
|
||||
var req deleteReq
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
writeJSON(w, s.doDelete(r, req.Links, req.DryRun, req.Collect))
|
||||
}
|
||||
|
||||
func (s *Server) apiGC(w http.ResponseWriter, r *http.Request) {
|
||||
summary, err := roots.GC(r.Context(), s.set.Cfg)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
_ = s.set.Refresh(s.ctx)
|
||||
writeJSON(w, map[string]string{"summary": summary})
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/illustris/i-am-gcroot/internal/roots"
|
||||
)
|
||||
|
||||
func testServer(t *testing.T) (*Server, string) {
|
||||
dir := t.TempDir()
|
||||
cfg := roots.Config{
|
||||
StoreDir: filepath.Join(dir, "store"), StateDir: filepath.Join(dir, "state"),
|
||||
NixStoreBin: "false", NixBin: "false", NixEnvBin: "true",
|
||||
}
|
||||
sp := filepath.Join(cfg.StoreDir, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-hello")
|
||||
os.MkdirAll(sp, 0o755)
|
||||
link := filepath.Join(dir, "result")
|
||||
os.Symlink(sp, link)
|
||||
os.MkdirAll(filepath.Join(cfg.StateDir, "gcroots", "auto"), 0o755)
|
||||
os.Symlink(link, filepath.Join(cfg.StateDir, "gcroots", "auto", "h1"))
|
||||
|
||||
set := roots.NewSet(cfg)
|
||||
if err := set.Refresh(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
srv, err := New(context.Background(), set, Options{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return srv, link
|
||||
}
|
||||
|
||||
func TestAuthAndRender(t *testing.T) {
|
||||
srv, link := testServer(t)
|
||||
h := srv.Handler()
|
||||
|
||||
do := func(method, url string, hdr map[string]string, body string) *httptest.ResponseRecorder {
|
||||
req := httptest.NewRequest(method, url, strings.NewReader(body))
|
||||
for k, v := range hdr {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
if method == http.MethodPost && body != "" && !strings.HasPrefix(body, "{") {
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, req)
|
||||
return w
|
||||
}
|
||||
|
||||
if w := do("GET", "/", nil, ""); w.Code != http.StatusUnauthorized {
|
||||
t.Errorf("no token: %d", w.Code)
|
||||
}
|
||||
w := do("GET", "/?token="+srv.token, nil, "")
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), link) {
|
||||
t.Fatalf("index: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if len(w.Result().Cookies()) == 0 {
|
||||
t.Error("token should set a cookie")
|
||||
}
|
||||
if w := do("GET", "/static/htmx.min.js", nil, ""); w.Code != 200 {
|
||||
t.Errorf("static: %d", w.Code)
|
||||
}
|
||||
if w := do("GET", "/api/roots?q=hello", map[string]string{"Authorization": "Bearer " + srv.token}, ""); w.Code != 200 || !strings.Contains(w.Body.String(), `"kind": "indirect"`) {
|
||||
t.Errorf("api: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
// POST with only the cookie (no X-Token) must be rejected
|
||||
if w := do("POST", "/delete", map[string]string{"Cookie": cookieName + "=" + srv.token}, "link="+link); w.Code != http.StatusUnauthorized {
|
||||
t.Errorf("cookie-only post: %d", w.Code)
|
||||
}
|
||||
w = do("POST", "/delete", map[string]string{"X-Token": srv.token}, "link="+link)
|
||||
if w.Code != 200 || !strings.Contains(w.Body.String(), "Deleted 1 of 1") {
|
||||
t.Errorf("delete: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if _, err := os.Lstat(link); err == nil {
|
||||
t.Error("link still exists")
|
||||
}
|
||||
}
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
var htmx=function(){"use strict";const Q={onLoad:null,process:null,on:null,off:null,trigger:null,ajax:null,find:null,findAll:null,closest:null,values:function(e,t){const n=cn(e,t||"post");return n.values},remove:null,addClass:null,removeClass:null,toggleClass:null,takeClass:null,swap:null,defineExtension:null,removeExtension:null,logAll:null,logNone:null,logger:null,config:{historyEnabled:true,historyCacheSize:10,refreshOnHistoryMiss:false,defaultSwapStyle:"innerHTML",defaultSwapDelay:0,defaultSettleDelay:20,includeIndicatorStyles:true,indicatorClass:"htmx-indicator",requestClass:"htmx-request",addedClass:"htmx-added",settlingClass:"htmx-settling",swappingClass:"htmx-swapping",allowEval:true,allowScriptTags:true,inlineScriptNonce:"",inlineStyleNonce:"",attributesToSettle:["class","style","width","height"],withCredentials:false,timeout:0,wsReconnectDelay:"full-jitter",wsBinaryType:"blob",disableSelector:"[hx-disable], [data-hx-disable]",scrollBehavior:"instant",defaultFocusScroll:false,getCacheBusterParam:false,globalViewTransitions:false,methodsThatUseUrlParams:["get","delete"],selfRequestsOnly:true,ignoreTitle:false,scrollIntoViewOnBoost:true,triggerSpecsCache:null,disableInheritance:false,responseHandling:[{code:"204",swap:false},{code:"[23]..",swap:true},{code:"[45]..",swap:false,error:true}],allowNestedOobSwaps:true},parseInterval:null,_:null,version:"2.0.4"};Q.onLoad=j;Q.process=kt;Q.on=ye;Q.off=be;Q.trigger=he;Q.ajax=Rn;Q.find=u;Q.findAll=x;Q.closest=g;Q.remove=z;Q.addClass=K;Q.removeClass=G;Q.toggleClass=W;Q.takeClass=Z;Q.swap=$e;Q.defineExtension=Fn;Q.removeExtension=Bn;Q.logAll=V;Q.logNone=_;Q.parseInterval=d;Q._=e;const n={addTriggerHandler:St,bodyContains:le,canAccessLocalStorage:B,findThisElement:Se,filterValues:hn,swap:$e,hasAttribute:s,getAttributeValue:te,getClosestAttributeValue:re,getClosestMatch:o,getExpressionVars:En,getHeaders:fn,getInputValues:cn,getInternalData:ie,getSwapSpecification:gn,getTriggerSpecs:st,getTarget:Ee,makeFragment:P,mergeObjects:ce,makeSettleInfo:xn,oobSwap:He,querySelectorExt:ae,settleImmediately:Kt,shouldCancel:ht,triggerEvent:he,triggerErrorEvent:fe,withExtensions:Ft};const r=["get","post","put","delete","patch"];const H=r.map(function(e){return"[hx-"+e+"], [data-hx-"+e+"]"}).join(", ");function d(e){if(e==undefined){return undefined}let t=NaN;if(e.slice(-2)=="ms"){t=parseFloat(e.slice(0,-2))}else if(e.slice(-1)=="s"){t=parseFloat(e.slice(0,-1))*1e3}else if(e.slice(-1)=="m"){t=parseFloat(e.slice(0,-1))*1e3*60}else{t=parseFloat(e)}return isNaN(t)?undefined:t}function ee(e,t){return e instanceof Element&&e.getAttribute(t)}function s(e,t){return!!e.hasAttribute&&(e.hasAttribute(t)||e.hasAttribute("data-"+t))}function te(e,t){return ee(e,t)||ee(e,"data-"+t)}function c(e){const t=e.parentElement;if(!t&&e.parentNode instanceof ShadowRoot)return e.parentNode;return t}function ne(){return document}function m(e,t){return e.getRootNode?e.getRootNode({composed:t}):ne()}function o(e,t){while(e&&!t(e)){e=c(e)}return e||null}function i(e,t,n){const r=te(t,n);const o=te(t,"hx-disinherit");var i=te(t,"hx-inherit");if(e!==t){if(Q.config.disableInheritance){if(i&&(i==="*"||i.split(" ").indexOf(n)>=0)){return r}else{return null}}if(o&&(o==="*"||o.split(" ").indexOf(n)>=0)){return"unset"}}return r}function re(t,n){let r=null;o(t,function(e){return!!(r=i(t,ue(e),n))});if(r!=="unset"){return r}}function h(e,t){const n=e instanceof Element&&(e.matches||e.matchesSelector||e.msMatchesSelector||e.mozMatchesSelector||e.webkitMatchesSelector||e.oMatchesSelector);return!!n&&n.call(e,t)}function T(e){const t=/<([a-z][^\/\0>\x20\t\r\n\f]*)/i;const n=t.exec(e);if(n){return n[1].toLowerCase()}else{return""}}function q(e){const t=new DOMParser;return t.parseFromString(e,"text/html")}function L(e,t){while(t.childNodes.length>0){e.append(t.childNodes[0])}}function A(e){const t=ne().createElement("script");se(e.attributes,function(e){t.setAttribute(e.name,e.value)});t.textContent=e.textContent;t.async=false;if(Q.config.inlineScriptNonce){t.nonce=Q.config.inlineScriptNonce}return t}function N(e){return e.matches("script")&&(e.type==="text/javascript"||e.type==="module"||e.type==="")}function I(e){Array.from(e.querySelectorAll("script")).forEach(e=>{if(N(e)){const t=A(e);const n=e.parentNode;try{n.insertBefore(t,e)}catch(e){O(e)}finally{e.remove()}}})}function P(e){const t=e.replace(/<head(\s[^>]*)?>[\s\S]*?<\/head>/i,"");const n=T(t);let r;if(n==="html"){r=new DocumentFragment;const i=q(e);L(r,i.body);r.title=i.title}else if(n==="body"){r=new DocumentFragment;const i=q(t);L(r,i.body);r.title=i.title}else{const i=q('<body><template class="internal-htmx-wrapper">'+t+"</template></body>");r=i.querySelector("template").content;r.title=i.title;var o=r.querySelector("title");if(o&&o.parentNode===r){o.remove();r.title=o.innerText}}if(r){if(Q.config.allowScriptTags){I(r)}else{r.querySelectorAll("script").forEach(e=>e.remove())}}return r}function oe(e){if(e){e()}}function t(e,t){return Object.prototype.toStrinLine truncated
|
||||
@@ -0,0 +1,124 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>i-am-gcroot</title>
|
||||
<script src="/static/htmx.min.js"></script>
|
||||
<style>
|
||||
:root {
|
||||
--bg: #fbfbfa; --fg: #1d1d1b; --dim: #6b6b66; --line: #e3e2de; --row: #f2f1ee;
|
||||
--accent: #2f6fb3; --danger: #b3382f; --ok: #2f7d3a; --mark: #fff4cc;
|
||||
--indirect: #1f7a8c; --profile: #8a3fa0; --direct: #2f6fb3; --dangling: #b3382f; --runtime: #6b6b66;
|
||||
}
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--bg: #17181a; --fg: #e6e4df; --dim: #9a978f; --line: #2c2d30; --row: #1f2023;
|
||||
--accent: #6aa5e6; --danger: #e5776d; --ok: #74c47f; --mark: #3a3420;
|
||||
--indirect: #5cc0d1; --profile: #c48ad6; --direct: #6aa5e6; --dangling: #e5776d; --runtime: #9a978f;
|
||||
}
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body { margin: 0; background: var(--bg); color: var(--fg); font: 14px/1.4 system-ui, sans-serif; }
|
||||
header { padding: 12px 16px; border-bottom: 1px solid var(--line); display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; position: sticky; top: 0; background: var(--bg); z-index: 1; }
|
||||
h1 { font-size: 16px; margin: 0; font-family: ui-monospace, monospace; }
|
||||
#controls { display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; flex: 1; }
|
||||
input[name=q] { flex: 1; min-width: 240px; padding: 6px 8px; font: 13px ui-monospace, monospace; background: var(--row); color: var(--fg); border: 1px solid var(--line); border-radius: 4px; }
|
||||
label { white-space: nowrap; color: var(--dim); }
|
||||
select, button { font: inherit; padding: 4px 10px; border-radius: 4px; border: 1px solid var(--line); background: var(--row); color: var(--fg); cursor: pointer; }
|
||||
button.danger { background: var(--danger); color: #fff; border-color: var(--danger); }
|
||||
.actions { display: flex; gap: 8px; align-items: center; padding: 8px 16px; flex-wrap: wrap; }
|
||||
.msg { padding: 8px 16px; }
|
||||
.msg.err { color: var(--danger); }
|
||||
.msg.ok { color: var(--ok); }
|
||||
.dim { color: var(--dim); }
|
||||
.wrap { overflow-x: auto; }
|
||||
table { border-collapse: collapse; width: 100%; font: 12.5px ui-monospace, monospace; }
|
||||
th, td { text-align: left; padding: 3px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
|
||||
th { position: sticky; top: 0; background: var(--bg); color: var(--dim); font-weight: 600; }
|
||||
td.num { text-align: right; }
|
||||
td.path { max-width: 50vw; overflow: hidden; text-overflow: ellipsis; direction: rtl; text-align: left; }
|
||||
tr:hover td { background: var(--row); }
|
||||
tr:has(input:checked) td { background: var(--mark); }
|
||||
.kind { font-weight: 600; }
|
||||
.k-indirect { color: var(--indirect); } .k-profile { color: var(--profile); } .k-direct { color: var(--direct); }
|
||||
.k-dangling { color: var(--dangling); } .k-runtime { color: var(--runtime); }
|
||||
ul.results { margin: 4px 0; font: 12px ui-monospace, monospace; }
|
||||
details summary { cursor: pointer; color: var(--dim); }
|
||||
.htmx-request .spin { visibility: visible; } .spin { visibility: hidden; }
|
||||
</style>
|
||||
</head>
|
||||
<body hx-headers='{"X-Token": "{{.Token}}"}'>
|
||||
<header>
|
||||
<h1>i-am-gcroot</h1>
|
||||
<form id="controls" hx-get="/rows" hx-target="#rows" hx-include="#rows input[name=link]:checked"
|
||||
hx-trigger="input changed delay:300ms from:input[name=q], change, submit" onsubmit="return false">
|
||||
<input type="hidden" name="kinds" value="1">
|
||||
<input name="q" value="{{.Query}}" placeholder="text /re/ kind: owner:me under:/tmp older:30d size>1G -negate" autofocus>
|
||||
{{range kinds}}<label><input type="checkbox" name="kind" value="{{.}}" {{if index $.Kinds .}}checked{{end}}> <span class="k-{{.}}">{{.}}</span></label>{{end}}
|
||||
<label>sort <select name="sort">
|
||||
{{range $s := sorts}}<option {{if eq $s $.Sort}}selected{{end}}>{{$s}}</option>{{end}}
|
||||
</select></label>
|
||||
<label><input type="checkbox" name="desc" value="1" {{if .Desc}}checked{{end}}> desc</label>
|
||||
<label title="nix cache roots such as ~/.cache/nix/flake-registry.json"><input type="checkbox" name="hidden" value="1" {{if .Hidden}}checked{{end}}> hidden</label>
|
||||
</form>
|
||||
</header>
|
||||
<div id="rows">{{template "rows" .}}</div>
|
||||
<script>
|
||||
function selectAll(on) {
|
||||
document.querySelectorAll('#rows input[name=link]:not([data-protected])').forEach(c => c.checked = on);
|
||||
updateSel();
|
||||
}
|
||||
function updateSel() {
|
||||
let n = 0, size = 0;
|
||||
document.querySelectorAll('#rows input[name=link]:checked').forEach(c => { n++; size += +c.dataset.size || 0; });
|
||||
const el = document.getElementById('sel');
|
||||
if (el) el.textContent = n + ' selected, up to ' + human(size);
|
||||
}
|
||||
function human(n) {
|
||||
const u = ['B', 'K', 'M', 'G', 'T']; let i = 0;
|
||||
while (n >= 1024 && i < u.length - 1) { n /= 1024; i++; }
|
||||
return (i ? n.toFixed(1) : n) + u[i];
|
||||
}
|
||||
document.addEventListener('change', e => { if (e.target.name === 'link') updateSel(); });
|
||||
document.body.addEventListener('htmx:afterSwap', updateSel);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
{{define "rows"}}
|
||||
{{if .Err}}<div class="msg err">{{.Err}}</div>{{end}}
|
||||
{{if .Message}}<div class="msg ok">{{.Message}}
|
||||
{{if .Results}}<details {{if lt (len .Results) 15}}open{{end}}><summary>{{len .Results}} result(s)</summary><ul class="results">
|
||||
{{range .Results}}<li>{{if .Err}}<span class="msg err">fail</span> {{.Link}}: {{.Err}}{{else}}ok {{.Action}}{{end}}</li>{{end}}
|
||||
</ul></details>{{end}}
|
||||
</div>{{end}}
|
||||
<div class="actions">
|
||||
<span class="dim">{{.Total}} roots, {{len .Rows}} shown{{if .Status.Sizing}}, sizing {{.Status.SizesDone}}/{{.Status.SizesTodo}}{{end}}</span>
|
||||
<span id="sel" class="dim"></span>
|
||||
<button type="button" onclick="selectAll(true)">select all shown</button>
|
||||
<button type="button" onclick="selectAll(false)">clear</button>
|
||||
<button hx-post="/delete" hx-target="#rows" hx-include="#controls, #rows input[name=link]:checked" hx-vals='{"dry": "1"}'>dry run</button>
|
||||
<button class="danger" hx-post="/delete" hx-target="#rows" hx-include="#controls, #rows input[name=link]:checked"
|
||||
hx-confirm="Delete the selected GC roots?">delete selected</button>
|
||||
<button class="danger" hx-post="/delete" hx-target="#rows" hx-include="#controls, #rows input[name=link]:checked" hx-vals='{"collect": "1"}'
|
||||
hx-confirm="Delete the selected GC roots and garbage collect the store paths only they kept alive?">delete + collect <span class="spin">...</span></button>
|
||||
<button hx-post="/gc" hx-target="#rows" hx-include="#controls, #rows input[name=link]:checked"
|
||||
hx-confirm="Run nix-store --gc now?">run gc <span class="spin">...</span></button>
|
||||
<button hx-post="/refresh" hx-target="#rows" hx-include="#controls, #rows input[name=link]:checked">rescan</button>
|
||||
</div>
|
||||
{{if .Status.Sizing}}<div hx-get="/rows" hx-trigger="every 2s" hx-target="#rows" hx-include="#controls, #rows input[name=link]:checked"></div>{{end}}
|
||||
<div class="wrap"><table>
|
||||
<thead><tr><th></th><th>kind</th><th>owner</th><th>age</th><th>closure</th><th>link</th><th>target</th></tr></thead>
|
||||
<tbody>
|
||||
{{range .Rows}}<tr title="{{.Reason}}">
|
||||
<td>{{if .Deletable}}<input type="checkbox" name="link" value="{{.Link}}" data-size="{{if gt .ClosureSize 0}}{{.ClosureSize}}{{end}}" {{if .Protected}}data-protected{{end}} {{if index $.Checked .Link}}checked{{end}}>{{end}}{{if .Protected}} <span title="{{.Reason}}">!</span>{{end}}</td>
|
||||
<td class="kind k-{{.Kind}}">{{.Kind}}{{if .Current}} (current){{end}}</td>
|
||||
<td>{{.Owner}}</td>
|
||||
<td class="num">{{age .ModTime}}</td>
|
||||
<td class="num">{{if eq .Kind "dangling"}}-{{else}}{{size .ClosureSize}}{{end}}</td>
|
||||
<td class="path" title="{{.Link}}{{if .Via}} (via {{.Via}}){{end}}"><bdi>{{.Link}}{{if .Command}} <span class="dim">{{.Command}}</span>{{end}}</bdi></td>
|
||||
<td title="{{.Target}}">{{if .Target}}{{.TargetName}}{{else if gt .Count 1}}<span class="dim">({{.Count}} paths)</span>{{else if eq .Kind "dangling"}}<span class="dim">(missing)</span>{{else}}<span class="dim">{{.Reason}}</span>{{end}}</td>
|
||||
</tr>{{end}}
|
||||
</tbody></table></div>
|
||||
{{end}}
|
||||
@@ -0,0 +1,356 @@
|
||||
// i-am-gcroot lists, filters and deletes Nix garbage collector roots, from a
|
||||
// TUI, an optional web UI, or the command line.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
|
||||
"github.com/illustris/i-am-gcroot/internal/filter"
|
||||
"github.com/illustris/i-am-gcroot/internal/roots"
|
||||
"github.com/illustris/i-am-gcroot/internal/tui"
|
||||
"github.com/illustris/i-am-gcroot/internal/web"
|
||||
)
|
||||
|
||||
var version = "dev"
|
||||
|
||||
const usage = `i-am-gcroot: list, filter and delete Nix GC roots
|
||||
|
||||
Usage:
|
||||
i-am-gcroot [tui] [-q QUERY] [--web] [--addr ADDR] [--runtime] [--force]
|
||||
i-am-gcroot list [-q QUERY] [--kind K,..] [--sort KEY] [--desc] [--sizes] [--json] [--runtime] [--all]
|
||||
i-am-gcroot rm [-n] [-y] [-c] [--force] [--all] (-q QUERY | LINK...)
|
||||
i-am-gcroot gc
|
||||
i-am-gcroot web [--addr ADDR] [--no-auth] [--force]
|
||||
i-am-gcroot version
|
||||
|
||||
Query syntax (terms ANDed, prefix - to negate):
|
||||
foo /regex/ kind:indirect,dangling owner:me under:/tmp
|
||||
older:30d newer:2h size>1G size<10M is:protected is:current is:hidden
|
||||
|
||||
rm -c deletes the roots, then garbage collects only the store paths that
|
||||
nothing else keeps alive (much faster than a full gc).
|
||||
|
||||
Nix cache roots (~/.cache/nix/...) are hidden unless --all or is:hidden.
|
||||
Runtime roots (held by running processes) are hidden unless --runtime or
|
||||
kind:runtime is used, and can never be deleted.
|
||||
`
|
||||
|
||||
func main() {
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
|
||||
args := os.Args[1:]
|
||||
cmd := "tui"
|
||||
if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
|
||||
cmd, args = args[0], args[1:]
|
||||
}
|
||||
var err error
|
||||
switch cmd {
|
||||
case "tui":
|
||||
err = runTUI(ctx, args)
|
||||
case "list", "ls":
|
||||
err = runList(ctx, args)
|
||||
case "rm", "delete":
|
||||
err = runRm(ctx, args)
|
||||
case "gc":
|
||||
var s string
|
||||
s, err = roots.GC(ctx, roots.DefaultConfig())
|
||||
if err == nil {
|
||||
fmt.Println(s)
|
||||
}
|
||||
case "web":
|
||||
err = runWeb(ctx, args)
|
||||
case "version":
|
||||
fmt.Println(version)
|
||||
case "help", "-h", "--help":
|
||||
fmt.Print(usage)
|
||||
default:
|
||||
fmt.Fprint(os.Stderr, usage)
|
||||
os.Exit(2)
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func newFlags(name string) *flag.FlagSet {
|
||||
fs := flag.NewFlagSet(name, flag.ExitOnError)
|
||||
fs.Usage = func() { fmt.Fprint(os.Stderr, usage) }
|
||||
return fs
|
||||
}
|
||||
|
||||
const defaultAddr = "127.0.0.1:8377"
|
||||
|
||||
func runTUI(ctx context.Context, args []string) error {
|
||||
fs := newFlags("tui")
|
||||
q := fs.String("q", "", "initial filter query")
|
||||
withWeb := fs.Bool("web", false, "also serve the web UI")
|
||||
addr := fs.String("addr", defaultAddr, "web UI listen address")
|
||||
noAuth := fs.Bool("no-auth", false, "disable the web UI token")
|
||||
runtime := fs.Bool("runtime", false, "include runtime roots")
|
||||
force := fs.Bool("force", false, "allow deleting protected roots")
|
||||
fs.Parse(args)
|
||||
|
||||
set := roots.NewSet(roots.DefaultConfig())
|
||||
set.SetRuntime(*runtime)
|
||||
opts := tui.Options{Query: *q, Force: *force}
|
||||
if *withWeb {
|
||||
srv, err := web.New(ctx, set, web.Options{Addr: *addr, NoAuth: *noAuth, Force: *force})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ln, url, err := srv.Listen()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts.WebURL = url
|
||||
go srv.Serve(ln)
|
||||
}
|
||||
m := tui.New(ctx, set, opts)
|
||||
_, err := tea.NewProgram(m, tea.WithAltScreen(), tea.WithContext(ctx)).Run()
|
||||
if err == tea.ErrProgramKilled && ctx.Err() != nil {
|
||||
return nil
|
||||
}
|
||||
if *withWeb && opts.WebURL != "" {
|
||||
// keep the token discoverable after the alt screen is gone
|
||||
fmt.Fprintln(os.Stderr, "web UI was at", opts.WebURL)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// kindsFlag turns "--kind a,b" into a kind set; empty means the default
|
||||
// (everything except runtime, unless runtime is requested).
|
||||
func kindsFlag(s string, runtime bool) (map[roots.Kind]bool, error) {
|
||||
k := map[roots.Kind]bool{}
|
||||
if s == "" {
|
||||
for _, kind := range roots.AllKinds {
|
||||
k[kind] = kind != roots.KindRuntime || runtime
|
||||
}
|
||||
return k, nil
|
||||
}
|
||||
for _, p := range strings.Split(s, ",") {
|
||||
kind, err := filter.ParseKind(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
k[kind] = true
|
||||
}
|
||||
return k, nil
|
||||
}
|
||||
|
||||
// load scans roots and, if sizes is set, waits for closure sizes.
|
||||
func load(ctx context.Context, runtime, sizes bool) (*roots.Set, error) {
|
||||
set := roots.NewSet(roots.DefaultConfig())
|
||||
set.SetRuntime(runtime)
|
||||
if err := set.Refresh(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for sizes && set.Status().Sizing {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
return set, nil
|
||||
}
|
||||
|
||||
func runList(ctx context.Context, args []string) error {
|
||||
fs := newFlags("list")
|
||||
q := fs.String("q", "", "filter query")
|
||||
kinds := fs.String("kind", "", "comma separated kinds")
|
||||
sortKey := fs.String("sort", "link", "sort key: link, age, size, target, kind")
|
||||
desc := fs.Bool("desc", false, "reverse sort")
|
||||
sizes := fs.Bool("sizes", false, "compute closure sizes (slower)")
|
||||
asJSON := fs.Bool("json", false, "JSON output")
|
||||
runtime := fs.Bool("runtime", false, "include runtime roots")
|
||||
all := fs.Bool("all", false, "include hidden nix cache roots")
|
||||
fs.Parse(args)
|
||||
if fs.NArg() > 0 && *q == "" {
|
||||
*q = strings.Join(fs.Args(), " ")
|
||||
}
|
||||
|
||||
query, err := filter.Parse(*q)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ks, err := kindsFlag(*kinds, *runtime || strings.Contains(*q, "runtime"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
wantRuntime := ks[roots.KindRuntime]
|
||||
needSizes := *sizes || *sortKey == "size" || strings.Contains(*q, "size")
|
||||
set, err := load(ctx, wantRuntime, needSizes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows := filter.Apply(set.Snapshot(), query, ks, *all, filter.ParseSort(*sortKey), *desc)
|
||||
if *asJSON {
|
||||
enc := json.NewEncoder(os.Stdout)
|
||||
enc.SetIndent("", " ")
|
||||
if rows == nil {
|
||||
rows = []roots.Root{}
|
||||
}
|
||||
return enc.Encode(rows)
|
||||
}
|
||||
tw := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
|
||||
now := time.Now()
|
||||
fmt.Fprintln(tw, "KIND\tOWNER\tAGE\tCLOSURE\tLINK\tTARGET")
|
||||
var total int64
|
||||
for _, r := range rows {
|
||||
size := "-"
|
||||
if needSizes {
|
||||
size = filter.HumanSize(r.ClosureSize)
|
||||
if r.ClosureSize > 0 {
|
||||
total += r.ClosureSize
|
||||
}
|
||||
}
|
||||
target := r.Target
|
||||
if r.Kind == roots.KindRuntime && r.Count > 1 {
|
||||
target = fmt.Sprintf("(%d paths)", r.Count)
|
||||
}
|
||||
kind := string(r.Kind)
|
||||
if r.Protected {
|
||||
kind += "!"
|
||||
}
|
||||
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\t%s\n", kind, r.Owner, filter.HumanAge(r.ModTime, now), size, r.Link, target)
|
||||
}
|
||||
tw.Flush()
|
||||
if needSizes {
|
||||
fmt.Fprintf(os.Stderr, "%d roots, closures sum to %s (shared paths counted repeatedly)\n", len(rows), filter.HumanSize(total))
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "%d roots\n", len(rows))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func runRm(ctx context.Context, args []string) error {
|
||||
fs := newFlags("rm")
|
||||
q := fs.String("q", "", "delete every root matching this query")
|
||||
dry := fs.Bool("n", false, "dry run")
|
||||
yes := fs.Bool("y", false, "do not ask for confirmation")
|
||||
force := fs.Bool("force", false, "allow deleting protected roots")
|
||||
collect := fs.Bool("c", false, "garbage collect the paths only these roots kept alive")
|
||||
all := fs.Bool("all", false, "let -q match hidden nix cache roots")
|
||||
fs.Parse(args)
|
||||
if *q == "" && fs.NArg() == 0 {
|
||||
return fmt.Errorf("rm needs -q QUERY or link paths")
|
||||
}
|
||||
|
||||
set, err := load(ctx, false, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var targets []roots.Root
|
||||
if *q != "" {
|
||||
query, err := filter.Parse(*q)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ks, _ := kindsFlag("", false)
|
||||
targets = filter.Apply(set.Snapshot(), query, ks, *all, filter.SortLink, false)
|
||||
}
|
||||
if fs.NArg() > 0 {
|
||||
var links []string
|
||||
for _, a := range fs.Args() {
|
||||
if abs, err := filepath.Abs(a); err == nil {
|
||||
a = abs
|
||||
}
|
||||
links = append(links, a)
|
||||
}
|
||||
found := set.Lookup(links)
|
||||
if len(found) != len(links) {
|
||||
have := map[string]bool{}
|
||||
for _, r := range found {
|
||||
have[r.Link] = true
|
||||
}
|
||||
for _, l := range links {
|
||||
if !have[l] {
|
||||
return fmt.Errorf("%s is not a known GC root", l)
|
||||
}
|
||||
}
|
||||
}
|
||||
targets = append(targets, found...)
|
||||
}
|
||||
if len(targets) == 0 {
|
||||
fmt.Fprintln(os.Stderr, "nothing matched")
|
||||
return nil
|
||||
}
|
||||
|
||||
if !*dry && !*yes {
|
||||
for _, r := range targets {
|
||||
fmt.Fprintf(os.Stderr, " %-9s %s\n", r.Kind, r.Link)
|
||||
}
|
||||
fmt.Fprintf(os.Stderr, "Delete %d root(s)? [y/N] ", len(targets))
|
||||
line, _ := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||
if a := strings.TrimSpace(strings.ToLower(line)); a != "y" && a != "yes" {
|
||||
return fmt.Errorf("aborted")
|
||||
}
|
||||
}
|
||||
failed := 0
|
||||
results := roots.Delete(ctx, set.Cfg, targets, roots.DeleteOpts{DryRun: *dry, Force: *force})
|
||||
for _, res := range results {
|
||||
switch {
|
||||
case !res.OK():
|
||||
failed++
|
||||
fmt.Printf("fail %s: %s\n", res.Link, res.Err)
|
||||
case *dry:
|
||||
fmt.Printf("would %s\n", res.Action)
|
||||
default:
|
||||
fmt.Printf("ok %s\n", res.Action)
|
||||
}
|
||||
}
|
||||
if *collect && !*dry {
|
||||
c, err := roots.Collect(ctx, set.Cfg, roots.CollectedTargets(results))
|
||||
fmt.Println(c)
|
||||
for _, p := range c.Kept {
|
||||
fmt.Printf("kept %s (still alive)\n", p)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("gc: %w", err)
|
||||
}
|
||||
}
|
||||
if failed > 0 {
|
||||
return fmt.Errorf("%d of %d deletions failed", failed, len(targets))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func runWeb(ctx context.Context, args []string) error {
|
||||
fs := newFlags("web")
|
||||
addr := fs.String("addr", defaultAddr, "listen address")
|
||||
noAuth := fs.Bool("no-auth", false, "disable the access token")
|
||||
force := fs.Bool("force", false, "allow deleting protected roots")
|
||||
runtime := fs.Bool("runtime", false, "include runtime roots")
|
||||
fs.Parse(args)
|
||||
|
||||
set := roots.NewSet(roots.DefaultConfig())
|
||||
set.SetRuntime(*runtime)
|
||||
if err := set.Refresh(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
srv, err := web.New(ctx, set, web.Options{Addr: *addr, NoAuth: *noAuth, Force: *force})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ln, url, err := srv.Listen()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("serving", url)
|
||||
return srv.Serve(ln)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
{ pkgs, self }:
|
||||
|
||||
pkgs.testers.runNixOSTest {
|
||||
name = "i-am-gcroot";
|
||||
|
||||
nodes.machine = { pkgs, ... }: {
|
||||
imports = [ self.nixosModules.default ];
|
||||
programs.i-am-gcroot.enable = true;
|
||||
users.users.alice = {
|
||||
isNormalUser = true;
|
||||
uid = 1000;
|
||||
};
|
||||
environment.systemPackages = [ pkgs.curl pkgs.jq ];
|
||||
virtualisation.memorySize = 1024;
|
||||
};
|
||||
|
||||
testScript = builtins.readFile ./vm-test.py;
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
import json
|
||||
|
||||
def alice(cmd):
|
||||
return machine.succeed(f"su - alice -c '{cmd}'")
|
||||
|
||||
def roots(query=""):
|
||||
out = alice(f"i-am-gcroot list --json -q \"{query}\"")
|
||||
return {r["link"]: r for r in json.loads(out)}
|
||||
|
||||
machine.wait_for_unit("multi-user.target")
|
||||
machine.wait_for_unit("nix-daemon.socket")
|
||||
|
||||
# store paths without building anything
|
||||
paths = {}
|
||||
for n in ["one", "two", "three", "four", "five", "reg", "g1", "g2"]:
|
||||
paths[n] = alice(f"echo {n} > /tmp/{n}.txt && nix-store --add /tmp/{n}.txt").strip()
|
||||
|
||||
alice(f"nix-store --add-root ~/result1 -r {paths['one']}")
|
||||
alice(f"nix-store --add-root ~/result2 -r {paths['two']}")
|
||||
alice(f"nix-store --add-root ~/result3 -r {paths['three']} && rm ~/result3")
|
||||
alice(f"nix-store --add-root ~/result4 -r {paths['four']}")
|
||||
alice(f"nix-store --add-root ~/result5 -r {paths['five']}")
|
||||
alice(f"nix-store --add-root ~/result6 -r {paths['five']}")
|
||||
alice(f"mkdir -p ~/.cache/nix && nix-store --add-root ~/.cache/nix/flake-registry.json -r {paths['reg']}")
|
||||
alice(f"nix-env -p ~/prof --set {paths['g1']}")
|
||||
alice(f"nix-env -p ~/prof --set {paths['g2']}")
|
||||
|
||||
with subtest("list classifies roots"):
|
||||
rs = roots("under:/home/alice")
|
||||
print(json.dumps(rs, indent=1))
|
||||
assert rs["/home/alice/result1"]["kind"] == "indirect"
|
||||
assert rs["/home/alice/result1"]["target"] == paths["one"]
|
||||
assert rs["/home/alice/result1"]["owner"] == "alice"
|
||||
assert rs["/home/alice/result3"]["kind"] == "dangling"
|
||||
assert rs["/home/alice/prof-1-link"]["kind"] == "profile"
|
||||
assert not rs["/home/alice/prof-1-link"].get("current")
|
||||
assert rs["/home/alice/prof-2-link"]["current"]
|
||||
assert rs["/home/alice/prof-2-link"]["protected"]
|
||||
|
||||
with subtest("hidden cache roots"):
|
||||
assert "/home/alice/.cache/nix/flake-registry.json" not in roots()
|
||||
assert set(roots("is:hidden")) == {"/home/alice/.cache/nix/flake-registry.json"}
|
||||
out = alice("i-am-gcroot list --all --json -q flake-registry")
|
||||
assert json.loads(out)[0]["hidden"], out
|
||||
|
||||
with subtest("filters"):
|
||||
assert set(roots("result -kind:dangling")) == {"/home/alice/result1", "/home/alice/result2", "/home/alice/result4", "/home/alice/result5", "/home/alice/result6"}
|
||||
assert set(roots("owner:me kind:profile")) == {"/home/alice/prof-1-link", "/home/alice/prof-2-link"}
|
||||
|
||||
with subtest("query delete removes dangling auto entries as root (before gc prunes them)"):
|
||||
out = machine.succeed("i-am-gcroot rm -y -q 'kind:dangling under:/home/alice'")
|
||||
assert "ok" in out, out
|
||||
assert "/home/alice/result3" not in roots()
|
||||
|
||||
with subtest("delete indirect root and collect"):
|
||||
out = alice("i-am-gcroot rm -n ~/result1")
|
||||
assert "would rm /home/alice/result1" in out, out
|
||||
machine.succeed("test -L /home/alice/result1")
|
||||
alice("i-am-gcroot rm -y ~/result1")
|
||||
machine.fail("test -e /home/alice/result1")
|
||||
machine.succeed("nix-store --gc")
|
||||
machine.fail(f"test -e {paths['one']}")
|
||||
machine.succeed(f"test -e {paths['two']}")
|
||||
|
||||
with subtest("delete with targeted collect"):
|
||||
out = alice("i-am-gcroot rm -y -c ~/result4")
|
||||
print(out)
|
||||
assert "collected 1 store paths" in out, out
|
||||
machine.fail(f"test -e {paths['four']}")
|
||||
# five is still held by result6, so collecting after removing result5 frees nothing
|
||||
out = alice("i-am-gcroot rm -y -c ~/result5")
|
||||
print(out)
|
||||
assert "collected 0 store paths" in out, out
|
||||
machine.succeed(f"test -e {paths['five']}")
|
||||
machine.succeed(f"test -e {paths['two']}")
|
||||
|
||||
with subtest("profile generations"):
|
||||
machine.fail("su - alice -c 'i-am-gcroot rm -y ~/prof-2-link'")
|
||||
machine.succeed("test -L /home/alice/prof-2-link")
|
||||
alice("i-am-gcroot rm -y ~/prof-1-link")
|
||||
machine.fail("test -e /home/alice/prof-1-link")
|
||||
|
||||
with subtest("web ui"):
|
||||
machine.succeed("su - alice -c 'i-am-gcroot web --addr 127.0.0.1:8377 > /tmp/web.log 2>&1 &'")
|
||||
machine.wait_until_succeeds("grep -q serving /tmp/web.log")
|
||||
token = machine.succeed("grep -o 'token=[0-9a-f]*' /tmp/web.log").strip().split("=")[1]
|
||||
machine.succeed("test $(curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:8377/api/roots) = 401")
|
||||
out = machine.succeed(f"curl -sf -H 'Authorization: Bearer {token}' 'http://127.0.0.1:8377/api/roots?q=result2'")
|
||||
assert json.loads(out)[0]["link"] == "/home/alice/result2", out
|
||||
machine.succeed(f"curl -sf 'http://127.0.0.1:8377/?token={token}' | grep -q result2")
|
||||
out = machine.succeed(f"curl -sf -X POST -H 'X-Token: {token}' -d 'link=/home/alice/result2' http://127.0.0.1:8377/delete")
|
||||
assert "Deleted 1 of 1" in out, out
|
||||
machine.fail("test -e /home/alice/result2")
|
||||
Reference in new issue
Block a user