From 625becd03e7de4bb6f0f36d0fc74cfe64dcbf235 Mon Sep 17 00:00:00 2001 From: illustris Date: Wed, 28 Oct 2026 08:00:16 +0530 Subject: [PATCH] init --- .gitignore | 2 + CLAUDE.md | 26 ++ README.md | 103 ++++++ flake.lock | 27 ++ flake.nix | 63 ++++ go.mod | 30 ++ go.sum | 47 +++ internal/filter/query.go | 307 ++++++++++++++++++ internal/filter/query_test.go | 105 ++++++ internal/roots/collect.go | 198 ++++++++++++ internal/roots/delete.go | 166 ++++++++++ internal/roots/root.go | 78 +++++ internal/roots/roots_test.go | 209 ++++++++++++ internal/roots/scan.go | 519 ++++++++++++++++++++++++++++++ internal/roots/size.go | 82 +++++ internal/tui/model.go | 517 +++++++++++++++++++++++++++++ internal/tui/view.go | 343 ++++++++++++++++++++ internal/web/server.go | 325 +++++++++++++++++++ internal/web/server_test.go | 83 +++++ internal/web/static/htmx.min.js | 1 + internal/web/templates/index.html | 124 +++++++ main.go | 356 ++++++++++++++++++++ nix/vm-test.nix | 18 ++ nix/vm-test.py | 93 ++++++ 24 files changed, 3822 insertions(+) create mode 100644 .gitignore create mode 100644 CLAUDE.md create mode 100644 README.md create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/filter/query.go create mode 100644 internal/filter/query_test.go create mode 100644 internal/roots/collect.go create mode 100644 internal/roots/delete.go create mode 100644 internal/roots/root.go create mode 100644 internal/roots/roots_test.go create mode 100644 internal/roots/scan.go create mode 100644 internal/roots/size.go create mode 100644 internal/tui/model.go create mode 100644 internal/tui/view.go create mode 100644 internal/web/server.go create mode 100644 internal/web/server_test.go create mode 100644 internal/web/static/htmx.min.js create mode 100644 internal/web/templates/index.html create mode 100644 main.go create mode 100644 nix/vm-test.nix create mode 100644 nix/vm-test.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..750baeb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +result +result-* diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..f1d7302 --- /dev/null +++ b/CLAUDE.md @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..c231c0a --- /dev/null +++ b/README.md @@ -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, `--link`, system or user | `nix-env --profile

--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 `): + +- `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; +} +``` diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..3ddc189 --- /dev/null +++ b/flake.lock @@ -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 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..dc5fefd --- /dev/null +++ b/flake.nix @@ -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; }; + }); + }; +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..93268bb --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ee425d3 --- /dev/null +++ b/go.sum @@ -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= diff --git a/internal/filter/query.go b/internal/filter/query.go new file mode 100644 index 0000000..6944e3b --- /dev/null +++ b/internal/filter/query.go @@ -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) +} diff --git a/internal/filter/query_test.go b/internal/filter/query_test.go new file mode 100644 index 0000000..7afd4e4 --- /dev/null +++ b/internal/filter/query_test.go @@ -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) + } + } +} diff --git a/internal/roots/collect.go b/internal/roots/collect.go new file mode 100644 index 0000000..04c87a6 --- /dev/null +++ b/internal/roots/collect.go @@ -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 +} diff --git a/internal/roots/delete.go b/internal/roots/delete.go new file mode 100644 index 0000000..b1400ac --- /dev/null +++ b/internal/roots/delete.go @@ -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 +} diff --git a/internal/roots/root.go b/internal/roots/root.go new file mode 100644 index 0000000..cbcd87c --- /dev/null +++ b/internal/roots/root.go @@ -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 (--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/. + Link string `json:"link"` + // Via is the gcroots entry that registers Link (auto/ 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 +} diff --git a/internal/roots/roots_test.go b/internal/roots/roots_test.go new file mode 100644 index 0000000..93a864d --- /dev/null +++ b/internal/roots/roots_test.go @@ -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) + } +} diff --git a/internal/roots/scan.go b/internal/roots/scan.go new file mode 100644 index 0000000..450f302 --- /dev/null +++ b/internal/roots/scan.go @@ -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 +} diff --git a/internal/roots/size.go b/internal/roots/size.go new file mode 100644 index 0000000..ed31635 --- /dev/null +++ b/internal/roots/size.go @@ -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 +} diff --git a/internal/tui/model.go b/internal/tui/model.go new file mode 100644 index 0000000..eb8e5c5 --- /dev/null +++ b/internal/tui/model.go @@ -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]" +} diff --git a/internal/tui/view.go b/internal/tui/view.go new file mode 100644 index 0000000..f548c25 --- /dev/null +++ b/internal/tui/view.go @@ -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:]) +} diff --git a/internal/web/server.go b/internal/web/server.go new file mode 100644 index 0000000..42e638d --- /dev/null +++ b/internal/web/server.go @@ -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}) +} diff --git a/internal/web/server_test.go b/internal/web/server_test.go new file mode 100644 index 0000000..eddc2c7 --- /dev/null +++ b/internal/web/server_test.go @@ -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") + } +} diff --git a/internal/web/static/htmx.min.js b/internal/web/static/htmx.min.js new file mode 100644 index 0000000..59937d7 --- /dev/null +++ b/internal/web/static/htmx.min.js @@ -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(/]*)?>[\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('");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.toString.call(e)==="[object "+t+"]"}function k(e){return typeof e==="function"}function D(e){return t(e,"Object")}function ie(e){const t="htmx-internal-data";let n=e[t];if(!n){n=e[t]={}}return n}function M(t){const n=[];if(t){for(let e=0;e=0}function le(e){return e.getRootNode({composed:true})===document}function F(e){return e.trim().split(/\s+/)}function ce(e,t){for(const n in t){if(t.hasOwnProperty(n)){e[n]=t[n]}}return e}function S(e){try{return JSON.parse(e)}catch(e){O(e);return null}}function B(){const e="htmx:localStorageTest";try{localStorage.setItem(e,e);localStorage.removeItem(e);return true}catch(e){return false}}function U(t){try{const e=new URL(t);if(e){t=e.pathname+e.search}if(!/^\/$/.test(t)){t=t.replace(/\/+$/,"")}return t}catch(e){return t}}function e(e){return vn(ne().body,function(){return eval(e)})}function j(t){const e=Q.on("htmx:load",function(e){t(e.detail.elt)});return e}function V(){Q.logger=function(e,t,n){if(console){console.log(t,e,n)}}}function _(){Q.logger=null}function u(e,t){if(typeof e!=="string"){return e.querySelector(t)}else{return u(ne(),e)}}function x(e,t){if(typeof e!=="string"){return e.querySelectorAll(t)}else{return x(ne(),e)}}function E(){return window}function z(e,t){e=y(e);if(t){E().setTimeout(function(){z(e);e=null},t)}else{c(e).removeChild(e)}}function ue(e){return e instanceof Element?e:null}function $(e){return e instanceof HTMLElement?e:null}function J(e){return typeof e==="string"?e:null}function f(e){return e instanceof Element||e instanceof Document||e instanceof DocumentFragment?e:null}function K(e,t,n){e=ue(y(e));if(!e){return}if(n){E().setTimeout(function(){K(e,t);e=null},n)}else{e.classList&&e.classList.add(t)}}function G(e,t,n){let r=ue(y(e));if(!r){return}if(n){E().setTimeout(function(){G(r,t);r=null},n)}else{if(r.classList){r.classList.remove(t);if(r.classList.length===0){r.removeAttribute("class")}}}}function W(e,t){e=y(e);e.classList.toggle(t)}function Z(e,t){e=y(e);se(e.parentElement.children,function(e){G(e,t)});K(ue(e),t)}function g(e,t){e=ue(y(e));if(e&&e.closest){return e.closest(t)}else{do{if(e==null||h(e,t)){return e}}while(e=e&&ue(c(e)));return null}}function l(e,t){return e.substring(0,t.length)===t}function Y(e,t){return e.substring(e.length-t.length)===t}function ge(e){const t=e.trim();if(l(t,"<")&&Y(t,"/>")){return t.substring(1,t.length-2)}else{return t}}function p(t,r,n){if(r.indexOf("global ")===0){return p(t,r.slice(7),true)}t=y(t);const o=[];{let t=0;let n=0;for(let e=0;e"){t--}}if(n0){const r=ge(o.shift());let e;if(r.indexOf("closest ")===0){e=g(ue(t),ge(r.substr(8)))}else if(r.indexOf("find ")===0){e=u(f(t),ge(r.substr(5)))}else if(r==="next"||r==="nextElementSibling"){e=ue(t).nextElementSibling}else if(r.indexOf("next ")===0){e=pe(t,ge(r.substr(5)),!!n)}else if(r==="previous"||r==="previousElementSibling"){e=ue(t).previousElementSibling}else if(r.indexOf("previous ")===0){e=me(t,ge(r.substr(9)),!!n)}else if(r==="document"){e=document}else if(r==="window"){e=window}else if(r==="body"){e=document.body}else if(r==="root"){e=m(t,!!n)}else if(r==="host"){e=t.getRootNode().host}else{s.push(r)}if(e){i.push(e)}}if(s.length>0){const e=s.join(",");const c=f(m(t,!!n));i.push(...M(c.querySelectorAll(e)))}return i}var pe=function(t,e,n){const r=f(m(t,n)).querySelectorAll(e);for(let e=0;e=0;e--){const o=r[e];if(o.compareDocumentPosition(t)===Node.DOCUMENT_POSITION_FOLLOWING){return o}}};function ae(e,t){if(typeof e!=="string"){return p(e,t)[0]}else{return p(ne().body,e)[0]}}function y(e,t){if(typeof e==="string"){return u(f(t)||document,e)}else{return e}}function xe(e,t,n,r){if(k(t)){return{target:ne().body,event:J(e),listener:t,options:n}}else{return{target:y(e),event:J(t),listener:n,options:r}}}function ye(t,n,r,o){Vn(function(){const e=xe(t,n,r,o);e.target.addEventListener(e.event,e.listener,e.options)});const e=k(n);return e?n:r}function be(t,n,r){Vn(function(){const e=xe(t,n,r);e.target.removeEventListener(e.event,e.listener)});return k(n)?n:r}const ve=ne().createElement("output");function we(e,t){const n=re(e,t);if(n){if(n==="this"){return[Se(e,t)]}else{const r=p(e,n);if(r.length===0){O('The selector "'+n+'" on '+t+" returned no matches!");return[ve]}else{return r}}}}function Se(e,t){return ue(o(e,function(e){return te(ue(e),t)!=null}))}function Ee(e){const t=re(e,"hx-target");if(t){if(t==="this"){return Se(e,"hx-target")}else{return ae(e,t)}}else{const n=ie(e);if(n.boosted){return ne().body}else{return e}}}function Ce(t){const n=Q.config.attributesToSettle;for(let e=0;e0){s=e.substring(0,e.indexOf(":"));n=e.substring(e.indexOf(":")+1)}else{s=e}o.removeAttribute("hx-swap-oob");o.removeAttribute("data-hx-swap-oob");const r=p(t,n,false);if(r){se(r,function(e){let t;const n=o.cloneNode(true);t=ne().createDocumentFragment();t.appendChild(n);if(!Re(s,e)){t=f(n)}const r={shouldSwap:true,target:e,fragment:t};if(!he(e,"htmx:oobBeforeSwap",r))return;e=r.target;if(r.shouldSwap){qe(t);_e(s,e,e,t,i);Te()}se(i.elts,function(e){he(e,"htmx:oobAfterSwap",r)})});o.parentNode.removeChild(o)}else{o.parentNode.removeChild(o);fe(ne().body,"htmx:oobErrorNoTarget",{content:o})}return e}function Te(){const e=u("#--htmx-preserve-pantry--");if(e){for(const t of[...e.children]){const n=u("#"+t.id);n.parentNode.moveBefore(t,n);n.remove()}e.remove()}}function qe(e){se(x(e,"[hx-preserve], [data-hx-preserve]"),function(e){const t=te(e,"id");const n=ne().getElementById(t);if(n!=null){if(e.moveBefore){let e=u("#--htmx-preserve-pantry--");if(e==null){ne().body.insertAdjacentHTML("afterend","

");e=u("#--htmx-preserve-pantry--")}e.moveBefore(n,null)}else{e.parentNode.replaceChild(n,e)}}})}function Le(l,e,c){se(e.querySelectorAll("[id]"),function(t){const n=ee(t,"id");if(n&&n.length>0){const r=n.replace("'","\\'");const o=t.tagName.replace(":","\\:");const e=f(l);const i=e&&e.querySelector(o+"[id='"+r+"']");if(i&&i!==e){const s=t.cloneNode();Oe(t,i);c.tasks.push(function(){Oe(t,s)})}}})}function Ae(e){return function(){G(e,Q.config.addedClass);kt(ue(e));Ne(f(e));he(e,"htmx:load")}}function Ne(e){const t="[autofocus]";const n=$(h(e,t)?e:e.querySelector(t));if(n!=null){n.focus()}}function a(e,t,n,r){Le(e,n,r);while(n.childNodes.length>0){const o=n.firstChild;K(ue(o),Q.config.addedClass);e.insertBefore(o,t);if(o.nodeType!==Node.TEXT_NODE&&o.nodeType!==Node.COMMENT_NODE){r.tasks.push(Ae(o))}}}function Ie(e,t){let n=0;while(n0}function $e(e,t,r,o){if(!o){o={}}e=y(e);const i=o.contextElement?m(o.contextElement,false):ne();const n=document.activeElement;let s={};try{s={elt:n,start:n?n.selectionStart:null,end:n?n.selectionEnd:null}}catch(e){}const l=xn(e);if(r.swapStyle==="textContent"){e.textContent=t}else{let n=P(t);l.title=n.title;if(o.selectOOB){const u=o.selectOOB.split(",");for(let t=0;t0){E().setTimeout(c,r.settleDelay)}else{c()}}function Je(e,t,n){const r=e.getResponseHeader(t);if(r.indexOf("{")===0){const o=S(r);for(const i in o){if(o.hasOwnProperty(i)){let e=o[i];if(D(e)){n=e.target!==undefined?e.target:n}else{e={value:e}}he(n,i,e)}}}else{const s=r.split(",");for(let e=0;e0){const s=o[0];if(s==="]"){e--;if(e===0){if(n===null){t=t+"true"}o.shift();t+=")})";try{const l=vn(r,function(){return Function(t)()},function(){return true});l.source=t;return l}catch(e){fe(ne().body,"htmx:syntax:error",{error:e,source:t});return null}}}else if(s==="["){e++}if(tt(s,n,i)){t+="(("+i+"."+s+") ? ("+i+"."+s+") : (window."+s+"))"}else{t=t+s}n=o.shift()}}}function C(e,t){let n="";while(e.length>0&&!t.test(e[0])){n+=e.shift()}return n}function rt(e){let t;if(e.length>0&&Ye.test(e[0])){e.shift();t=C(e,Qe).trim();e.shift()}else{t=C(e,v)}return t}const ot="input, textarea, select";function it(e,t,n){const r=[];const o=et(t);do{C(o,w);const l=o.length;const c=C(o,/[,\[\s]/);if(c!==""){if(c==="every"){const u={trigger:"every"};C(o,w);u.pollInterval=d(C(o,/[,\[\s]/));C(o,w);var i=nt(e,o,"event");if(i){u.eventFilter=i}r.push(u)}else{const a={trigger:c};var i=nt(e,o,"event");if(i){a.eventFilter=i}C(o,w);while(o.length>0&&o[0]!==","){const f=o.shift();if(f==="changed"){a.changed=true}else if(f==="once"){a.once=true}else if(f==="consume"){a.consume=true}else if(f==="delay"&&o[0]===":"){o.shift();a.delay=d(C(o,v))}else if(f==="from"&&o[0]===":"){o.shift();if(Ye.test(o[0])){var s=rt(o)}else{var s=C(o,v);if(s==="closest"||s==="find"||s==="next"||s==="previous"){o.shift();const h=rt(o);if(h.length>0){s+=" "+h}}}a.from=s}else if(f==="target"&&o[0]===":"){o.shift();a.target=rt(o)}else if(f==="throttle"&&o[0]===":"){o.shift();a.throttle=d(C(o,v))}else if(f==="queue"&&o[0]===":"){o.shift();a.queue=C(o,v)}else if(f==="root"&&o[0]===":"){o.shift();a[f]=rt(o)}else if(f==="threshold"&&o[0]===":"){o.shift();a[f]=C(o,v)}else{fe(e,"htmx:syntax:error",{token:o.shift()})}C(o,w)}r.push(a)}}if(o.length===l){fe(e,"htmx:syntax:error",{token:o.shift()})}C(o,w)}while(o[0]===","&&o.shift());if(n){n[t]=r}return r}function st(e){const t=te(e,"hx-trigger");let n=[];if(t){const r=Q.config.triggerSpecsCache;n=r&&r[t]||it(e,t,r)}if(n.length>0){return n}else if(h(e,"form")){return[{trigger:"submit"}]}else if(h(e,'input[type="button"], input[type="submit"]')){return[{trigger:"click"}]}else if(h(e,ot)){return[{trigger:"change"}]}else{return[{trigger:"click"}]}}function lt(e){ie(e).cancelled=true}function ct(e,t,n){const r=ie(e);r.timeout=E().setTimeout(function(){if(le(e)&&r.cancelled!==true){if(!gt(n,e,Mt("hx:poll:trigger",{triggerSpec:n,target:e}))){t(e)}ct(e,t,n)}},n.pollInterval)}function ut(e){return location.hostname===e.hostname&&ee(e,"href")&&ee(e,"href").indexOf("#")!==0}function at(e){return g(e,Q.config.disableSelector)}function ft(t,n,e){if(t instanceof HTMLAnchorElement&&ut(t)&&(t.target===""||t.target==="_self")||t.tagName==="FORM"&&String(ee(t,"method")).toLowerCase()!=="dialog"){n.boosted=true;let r,o;if(t.tagName==="A"){r="get";o=ee(t,"href")}else{const i=ee(t,"method");r=i?i.toLowerCase():"get";o=ee(t,"action");if(o==null||o===""){o=ne().location.href}if(r==="get"&&o.includes("?")){o=o.replace(/\?[^#]+/,"")}}e.forEach(function(e){pt(t,function(e,t){const n=ue(e);if(at(n)){b(n);return}de(r,o,n,t)},n,e,true)})}}function ht(e,t){const n=ue(t);if(!n){return false}if(e.type==="submit"||e.type==="click"){if(n.tagName==="FORM"){return true}if(h(n,'input[type="submit"], button')&&(h(n,"[form]")||g(n,"form")!==null)){return true}if(n instanceof HTMLAnchorElement&&n.href&&(n.getAttribute("href")==="#"||n.getAttribute("href").indexOf("#")!==0)){return true}}return false}function dt(e,t){return ie(e).boosted&&e instanceof HTMLAnchorElement&&t.type==="click"&&(t.ctrlKey||t.metaKey)}function gt(e,t,n){const r=e.eventFilter;if(r){try{return r.call(t,n)!==true}catch(e){const o=r.source;fe(ne().body,"htmx:eventFilter:error",{error:e,source:o});return true}}return false}function pt(l,c,e,u,a){const f=ie(l);let t;if(u.from){t=p(l,u.from)}else{t=[l]}if(u.changed){if(!("lastValue"in f)){f.lastValue=new WeakMap}t.forEach(function(e){if(!f.lastValue.has(u)){f.lastValue.set(u,new WeakMap)}f.lastValue.get(u).set(e,e.value)})}se(t,function(i){const s=function(e){if(!le(l)){i.removeEventListener(u.trigger,s);return}if(dt(l,e)){return}if(a||ht(e,l)){e.preventDefault()}if(gt(u,l,e)){return}const t=ie(e);t.triggerSpec=u;if(t.handledFor==null){t.handledFor=[]}if(t.handledFor.indexOf(l)<0){t.handledFor.push(l);if(u.consume){e.stopPropagation()}if(u.target&&e.target){if(!h(ue(e.target),u.target)){return}}if(u.once){if(f.triggeredOnce){return}else{f.triggeredOnce=true}}if(u.changed){const n=event.target;const r=n.value;const o=f.lastValue.get(u);if(o.has(n)&&o.get(n)===r){return}o.set(n,r)}if(f.delayed){clearTimeout(f.delayed)}if(f.throttle){return}if(u.throttle>0){if(!f.throttle){he(l,"htmx:trigger");c(l,e);f.throttle=E().setTimeout(function(){f.throttle=null},u.throttle)}}else if(u.delay>0){f.delayed=E().setTimeout(function(){he(l,"htmx:trigger");c(l,e)},u.delay)}else{he(l,"htmx:trigger");c(l,e)}}};if(e.listenerInfos==null){e.listenerInfos=[]}e.listenerInfos.push({trigger:u.trigger,listener:s,on:i});i.addEventListener(u.trigger,s)})}let mt=false;let xt=null;function yt(){if(!xt){xt=function(){mt=true};window.addEventListener("scroll",xt);window.addEventListener("resize",xt);setInterval(function(){if(mt){mt=false;se(ne().querySelectorAll("[hx-trigger*='revealed'],[data-hx-trigger*='revealed']"),function(e){bt(e)})}},200)}}function bt(e){if(!s(e,"data-hx-revealed")&&X(e)){e.setAttribute("data-hx-revealed","true");const t=ie(e);if(t.initHash){he(e,"revealed")}else{e.addEventListener("htmx:afterProcessNode",function(){he(e,"revealed")},{once:true})}}}function vt(e,t,n,r){const o=function(){if(!n.loaded){n.loaded=true;he(e,"htmx:trigger");t(e)}};if(r>0){E().setTimeout(o,r)}else{o()}}function wt(t,n,e){let i=false;se(r,function(r){if(s(t,"hx-"+r)){const o=te(t,"hx-"+r);i=true;n.path=o;n.verb=r;e.forEach(function(e){St(t,e,n,function(e,t){const n=ue(e);if(g(n,Q.config.disableSelector)){b(n);return}de(r,o,n,t)})})}});return i}function St(r,e,t,n){if(e.trigger==="revealed"){yt();pt(r,n,t,e);bt(ue(r))}else if(e.trigger==="intersect"){const o={};if(e.root){o.root=ae(r,e.root)}if(e.threshold){o.threshold=parseFloat(e.threshold)}const i=new IntersectionObserver(function(t){for(let e=0;e0){t.polling=true;ct(ue(r),n,e)}else{pt(r,n,t,e)}}function Et(e){const t=ue(e);if(!t){return false}const n=t.attributes;for(let e=0;e", "+e).join(""));return o}else{return[]}}function Tt(e){const t=g(ue(e.target),"button, input[type='submit']");const n=Lt(e);if(n){n.lastButtonClicked=t}}function qt(e){const t=Lt(e);if(t){t.lastButtonClicked=null}}function Lt(e){const t=g(ue(e.target),"button, input[type='submit']");if(!t){return}const n=y("#"+ee(t,"form"),t.getRootNode())||g(t,"form");if(!n){return}return ie(n)}function At(e){e.addEventListener("click",Tt);e.addEventListener("focusin",Tt);e.addEventListener("focusout",qt)}function Nt(t,e,n){const r=ie(t);if(!Array.isArray(r.onHandlers)){r.onHandlers=[]}let o;const i=function(e){vn(t,function(){if(at(t)){return}if(!o){o=new Function("event",n)}o.call(t,e)})};t.addEventListener(e,i);r.onHandlers.push({event:e,listener:i})}function It(t){ke(t);for(let e=0;eQ.config.historyCacheSize){i.shift()}while(i.length>0){try{localStorage.setItem("htmx-history-cache",JSON.stringify(i));break}catch(e){fe(ne().body,"htmx:historyCacheError",{cause:e,cache:i});i.shift()}}}function Vt(t){if(!B()){return null}t=U(t);const n=S(localStorage.getItem("htmx-history-cache"))||[];for(let e=0;e=200&&this.status<400){he(ne().body,"htmx:historyCacheMissLoad",i);const e=P(this.response);const t=e.querySelector("[hx-history-elt],[data-hx-history-elt]")||e;const n=Ut();const r=xn(n);kn(e.title);qe(e);Ve(n,t,r);Te();Kt(r.tasks);Bt=o;he(ne().body,"htmx:historyRestore",{path:o,cacheMiss:true,serverResponse:this.response})}else{fe(ne().body,"htmx:historyCacheMissLoadError",i)}};e.send()}function Wt(e){zt();e=e||location.pathname+location.search;const t=Vt(e);if(t){const n=P(t.content);const r=Ut();const o=xn(r);kn(t.title);qe(n);Ve(r,n,o);Te();Kt(o.tasks);E().setTimeout(function(){window.scrollTo(0,t.scroll)},0);Bt=e;he(ne().body,"htmx:historyRestore",{path:e,item:t})}else{if(Q.config.refreshOnHistoryMiss){window.location.reload(true)}else{Gt(e)}}}function Zt(e){let t=we(e,"hx-indicator");if(t==null){t=[e]}se(t,function(e){const t=ie(e);t.requestCount=(t.requestCount||0)+1;e.classList.add.call(e.classList,Q.config.requestClass)});return t}function Yt(e){let t=we(e,"hx-disabled-elt");if(t==null){t=[]}se(t,function(e){const t=ie(e);t.requestCount=(t.requestCount||0)+1;e.setAttribute("disabled","");e.setAttribute("data-disabled-by-htmx","")});return t}function Qt(e,t){se(e.concat(t),function(e){const t=ie(e);t.requestCount=(t.requestCount||1)-1});se(e,function(e){const t=ie(e);if(t.requestCount===0){e.classList.remove.call(e.classList,Q.config.requestClass)}});se(t,function(e){const t=ie(e);if(t.requestCount===0){e.removeAttribute("disabled");e.removeAttribute("data-disabled-by-htmx")}})}function en(t,n){for(let e=0;en.indexOf(e)<0)}else{e=e.filter(e=>e!==n)}r.delete(t);se(e,e=>r.append(t,e))}}function on(t,n,r,o,i){if(o==null||en(t,o)){return}else{t.push(o)}if(tn(o)){const s=ee(o,"name");let e=o.value;if(o instanceof HTMLSelectElement&&o.multiple){e=M(o.querySelectorAll("option:checked")).map(function(e){return e.value})}if(o instanceof HTMLInputElement&&o.files){e=M(o.files)}nn(s,e,n);if(i){sn(o,r)}}if(o instanceof HTMLFormElement){se(o.elements,function(e){if(t.indexOf(e)>=0){rn(e.name,e.value,n)}else{t.push(e)}if(i){sn(e,r)}});new FormData(o).forEach(function(e,t){if(e instanceof File&&e.name===""){return}nn(t,e,n)})}}function sn(e,t){const n=e;if(n.willValidate){he(n,"htmx:validation:validate");if(!n.checkValidity()){t.push({elt:n,message:n.validationMessage,validity:n.validity});he(n,"htmx:validation:failed",{message:n.validationMessage,validity:n.validity})}}}function ln(n,e){for(const t of e.keys()){n.delete(t)}e.forEach(function(e,t){n.append(t,e)});return n}function cn(e,t){const n=[];const r=new FormData;const o=new FormData;const i=[];const s=ie(e);if(s.lastButtonClicked&&!le(s.lastButtonClicked)){s.lastButtonClicked=null}let l=e instanceof HTMLFormElement&&e.noValidate!==true||te(e,"hx-validate")==="true";if(s.lastButtonClicked){l=l&&s.lastButtonClicked.formNoValidate!==true}if(t!=="get"){on(n,o,i,g(e,"form"),l)}on(n,r,i,e,l);if(s.lastButtonClicked||e.tagName==="BUTTON"||e.tagName==="INPUT"&&ee(e,"type")==="submit"){const u=s.lastButtonClicked||e;const a=ee(u,"name");nn(a,u.value,o)}const c=we(e,"hx-include");se(c,function(e){on(n,r,i,ue(e),l);if(!h(e,"form")){se(f(e).querySelectorAll(ot),function(e){on(n,r,i,e,l)})}});ln(r,o);return{errors:i,formData:r,values:An(r)}}function un(e,t,n){if(e!==""){e+="&"}if(String(n)==="[object Object]"){n=JSON.stringify(n)}const r=encodeURIComponent(n);e+=encodeURIComponent(t)+"="+r;return e}function an(e){e=qn(e);let n="";e.forEach(function(e,t){n=un(n,t,e)});return n}function fn(e,t,n){const r={"HX-Request":"true","HX-Trigger":ee(e,"id"),"HX-Trigger-Name":ee(e,"name"),"HX-Target":te(t,"id"),"HX-Current-URL":ne().location.href};bn(e,"hx-headers",false,r);if(n!==undefined){r["HX-Prompt"]=n}if(ie(e).boosted){r["HX-Boosted"]="true"}return r}function hn(n,e){const t=re(e,"hx-params");if(t){if(t==="none"){return new FormData}else if(t==="*"){return n}else if(t.indexOf("not ")===0){se(t.slice(4).split(","),function(e){e=e.trim();n.delete(e)});return n}else{const r=new FormData;se(t.split(","),function(t){t=t.trim();if(n.has(t)){n.getAll(t).forEach(function(e){r.append(t,e)})}});return r}}else{return n}}function dn(e){return!!ee(e,"href")&&ee(e,"href").indexOf("#")>=0}function gn(e,t){const n=t||re(e,"hx-swap");const r={swapStyle:ie(e).boosted?"innerHTML":Q.config.defaultSwapStyle,swapDelay:Q.config.defaultSwapDelay,settleDelay:Q.config.defaultSettleDelay};if(Q.config.scrollIntoViewOnBoost&&ie(e).boosted&&!dn(e)){r.show="top"}if(n){const s=F(n);if(s.length>0){for(let e=0;e0?o.join(":"):null;r.scroll=u;r.scrollTarget=i}else if(l.indexOf("show:")===0){const a=l.slice(5);var o=a.split(":");const f=o.pop();var i=o.length>0?o.join(":"):null;r.show=f;r.showTarget=i}else if(l.indexOf("focus-scroll:")===0){const h=l.slice("focus-scroll:".length);r.focusScroll=h=="true"}else if(e==0){r.swapStyle=l}else{O("Unknown modifier in hx-swap: "+l)}}}}return r}function pn(e){return re(e,"hx-encoding")==="multipart/form-data"||h(e,"form")&&ee(e,"enctype")==="multipart/form-data"}function mn(t,n,r){let o=null;Ft(n,function(e){if(o==null){o=e.encodeParameters(t,r,n)}});if(o!=null){return o}else{if(pn(n)){return ln(new FormData,qn(r))}else{return an(r)}}}function xn(e){return{tasks:[],elts:[e]}}function yn(e,t){const n=e[0];const r=e[e.length-1];if(t.scroll){var o=null;if(t.scrollTarget){o=ue(ae(n,t.scrollTarget))}if(t.scroll==="top"&&(n||o)){o=o||n;o.scrollTop=0}if(t.scroll==="bottom"&&(r||o)){o=o||r;o.scrollTop=o.scrollHeight}}if(t.show){var o=null;if(t.showTarget){let e=t.showTarget;if(t.showTarget==="window"){e="body"}o=ue(ae(n,e))}if(t.show==="top"&&(n||o)){o=o||n;o.scrollIntoView({block:"start",behavior:Q.config.scrollBehavior})}if(t.show==="bottom"&&(r||o)){o=o||r;o.scrollIntoView({block:"end",behavior:Q.config.scrollBehavior})}}}function bn(r,e,o,i){if(i==null){i={}}if(r==null){return i}const s=te(r,e);if(s){let e=s.trim();let t=o;if(e==="unset"){return null}if(e.indexOf("javascript:")===0){e=e.slice(11);t=true}else if(e.indexOf("js:")===0){e=e.slice(3);t=true}if(e.indexOf("{")!==0){e="{"+e+"}"}let n;if(t){n=vn(r,function(){return Function("return ("+e+")")()},{})}else{n=S(e)}for(const l in n){if(n.hasOwnProperty(l)){if(i[l]==null){i[l]=n[l]}}}}return bn(ue(c(r)),e,o,i)}function vn(e,t,n){if(Q.config.allowEval){return t()}else{fe(e,"htmx:evalDisallowedError");return n}}function wn(e,t){return bn(e,"hx-vars",true,t)}function Sn(e,t){return bn(e,"hx-vals",false,t)}function En(e){return ce(wn(e),Sn(e))}function Cn(t,n,r){if(r!==null){try{t.setRequestHeader(n,r)}catch(e){t.setRequestHeader(n,encodeURIComponent(r));t.setRequestHeader(n+"-URI-AutoEncoded","true")}}}function On(t){if(t.responseURL&&typeof URL!=="undefined"){try{const e=new URL(t.responseURL);return e.pathname+e.search}catch(e){fe(ne().body,"htmx:badResponseUrl",{url:t.responseURL})}}}function R(e,t){return t.test(e.getAllResponseHeaders())}function Rn(t,n,r){t=t.toLowerCase();if(r){if(r instanceof Element||typeof r==="string"){return de(t,n,null,null,{targetOverride:y(r)||ve,returnPromise:true})}else{let e=y(r.target);if(r.target&&!e||r.source&&!e&&!y(r.source)){e=ve}return de(t,n,y(r.source),r.event,{handler:r.handler,headers:r.headers,values:r.values,targetOverride:e,swapOverride:r.swap,select:r.select,returnPromise:true})}}else{return de(t,n,null,null,{returnPromise:true})}}function Hn(e){const t=[];while(e){t.push(e);e=e.parentElement}return t}function Tn(e,t,n){let r;let o;if(typeof URL==="function"){o=new URL(t,document.location.href);const i=document.location.origin;r=i===o.origin}else{o=t;r=l(t,document.location.origin)}if(Q.config.selfRequestsOnly){if(!r){return false}}return he(e,"htmx:validateUrl",ce({url:o,sameHost:r},n))}function qn(e){if(e instanceof FormData)return e;const t=new FormData;for(const n in e){if(e.hasOwnProperty(n)){if(e[n]&&typeof e[n].forEach==="function"){e[n].forEach(function(e){t.append(n,e)})}else if(typeof e[n]==="object"&&!(e[n]instanceof Blob)){t.append(n,JSON.stringify(e[n]))}else{t.append(n,e[n])}}}return t}function Ln(r,o,e){return new Proxy(e,{get:function(t,e){if(typeof e==="number")return t[e];if(e==="length")return t.length;if(e==="push"){return function(e){t.push(e);r.append(o,e)}}if(typeof t[e]==="function"){return function(){t[e].apply(t,arguments);r.delete(o);t.forEach(function(e){r.append(o,e)})}}if(t[e]&&t[e].length===1){return t[e][0]}else{return t[e]}},set:function(e,t,n){e[t]=n;r.delete(o);e.forEach(function(e){r.append(o,e)});return true}})}function An(o){return new Proxy(o,{get:function(e,t){if(typeof t==="symbol"){const r=Reflect.get(e,t);if(typeof r==="function"){return function(){return r.apply(o,arguments)}}else{return r}}if(t==="toJSON"){return()=>Object.fromEntries(o)}if(t in e){if(typeof e[t]==="function"){return function(){return o[t].apply(o,arguments)}}else{return e[t]}}const n=o.getAll(t);if(n.length===0){return undefined}else if(n.length===1){return n[0]}else{return Ln(e,t,n)}},set:function(t,n,e){if(typeof n!=="string"){return false}t.delete(n);if(e&&typeof e.forEach==="function"){e.forEach(function(e){t.append(n,e)})}else if(typeof e==="object"&&!(e instanceof Blob)){t.append(n,JSON.stringify(e))}else{t.append(n,e)}return true},deleteProperty:function(e,t){if(typeof t==="string"){e.delete(t)}return true},ownKeys:function(e){return Reflect.ownKeys(Object.fromEntries(e))},getOwnPropertyDescriptor:function(e,t){return Reflect.getOwnPropertyDescriptor(Object.fromEntries(e),t)}})}function de(t,n,r,o,i,D){let s=null;let l=null;i=i!=null?i:{};if(i.returnPromise&&typeof Promise!=="undefined"){var e=new Promise(function(e,t){s=e;l=t})}if(r==null){r=ne().body}const M=i.handler||Dn;const X=i.select||null;if(!le(r)){oe(s);return e}const c=i.targetOverride||ue(Ee(r));if(c==null||c==ve){fe(r,"htmx:targetError",{target:te(r,"hx-target")});oe(l);return e}let u=ie(r);const a=u.lastButtonClicked;if(a){const L=ee(a,"formaction");if(L!=null){n=L}const A=ee(a,"formmethod");if(A!=null){if(A.toLowerCase()!=="dialog"){t=A}}}const f=re(r,"hx-confirm");if(D===undefined){const K=function(e){return de(t,n,r,o,i,!!e)};const G={target:c,elt:r,path:n,verb:t,triggeringEvent:o,etc:i,issueRequest:K,question:f};if(he(r,"htmx:confirm",G)===false){oe(s);return e}}let h=r;let d=re(r,"hx-sync");let g=null;let F=false;if(d){const N=d.split(":");const I=N[0].trim();if(I==="this"){h=Se(r,"hx-sync")}else{h=ue(ae(r,I))}d=(N[1]||"drop").trim();u=ie(h);if(d==="drop"&&u.xhr&&u.abortable!==true){oe(s);return e}else if(d==="abort"){if(u.xhr){oe(s);return e}else{F=true}}else if(d==="replace"){he(h,"htmx:abort")}else if(d.indexOf("queue")===0){const W=d.split(" ");g=(W[1]||"last").trim()}}if(u.xhr){if(u.abortable){he(h,"htmx:abort")}else{if(g==null){if(o){const P=ie(o);if(P&&P.triggerSpec&&P.triggerSpec.queue){g=P.triggerSpec.queue}}if(g==null){g="last"}}if(u.queuedRequests==null){u.queuedRequests=[]}if(g==="first"&&u.queuedRequests.length===0){u.queuedRequests.push(function(){de(t,n,r,o,i)})}else if(g==="all"){u.queuedRequests.push(function(){de(t,n,r,o,i)})}else if(g==="last"){u.queuedRequests=[];u.queuedRequests.push(function(){de(t,n,r,o,i)})}oe(s);return e}}const p=new XMLHttpRequest;u.xhr=p;u.abortable=F;const m=function(){u.xhr=null;u.abortable=false;if(u.queuedRequests!=null&&u.queuedRequests.length>0){const e=u.queuedRequests.shift();e()}};const B=re(r,"hx-prompt");if(B){var x=prompt(B);if(x===null||!he(r,"htmx:prompt",{prompt:x,target:c})){oe(s);m();return e}}if(f&&!D){if(!confirm(f)){oe(s);m();return e}}let y=fn(r,c,x);if(t!=="get"&&!pn(r)){y["Content-Type"]="application/x-www-form-urlencoded"}if(i.headers){y=ce(y,i.headers)}const U=cn(r,t);let b=U.errors;const j=U.formData;if(i.values){ln(j,qn(i.values))}const V=qn(En(r));const v=ln(j,V);let w=hn(v,r);if(Q.config.getCacheBusterParam&&t==="get"){w.set("org.htmx.cache-buster",ee(c,"id")||"true")}if(n==null||n===""){n=ne().location.href}const S=bn(r,"hx-request");const _=ie(r).boosted;let E=Q.config.methodsThatUseUrlParams.indexOf(t)>=0;const C={boosted:_,useUrlParams:E,formData:w,parameters:An(w),unfilteredFormData:v,unfilteredParameters:An(v),headers:y,target:c,verb:t,errors:b,withCredentials:i.credentials||S.credentials||Q.config.withCredentials,timeout:i.timeout||S.timeout||Q.config.timeout,path:n,triggeringEvent:o};if(!he(r,"htmx:configRequest",C)){oe(s);m();return e}n=C.path;t=C.verb;y=C.headers;w=qn(C.parameters);b=C.errors;E=C.useUrlParams;if(b&&b.length>0){he(r,"htmx:validation:halted",C);oe(s);m();return e}const z=n.split("#");const $=z[0];const O=z[1];let R=n;if(E){R=$;const Z=!w.keys().next().done;if(Z){if(R.indexOf("?")<0){R+="?"}else{R+="&"}R+=an(w);if(O){R+="#"+O}}}if(!Tn(r,R,C)){fe(r,"htmx:invalidPath",C);oe(l);return e}p.open(t.toUpperCase(),R,true);p.overrideMimeType("text/html");p.withCredentials=C.withCredentials;p.timeout=C.timeout;if(S.noHeaders){}else{for(const k in y){if(y.hasOwnProperty(k)){const Y=y[k];Cn(p,k,Y)}}}const H={xhr:p,target:c,requestConfig:C,etc:i,boosted:_,select:X,pathInfo:{requestPath:n,finalRequestPath:R,responsePath:null,anchor:O}};p.onload=function(){try{const t=Hn(r);H.pathInfo.responsePath=On(p);M(r,H);if(H.keepIndicators!==true){Qt(T,q)}he(r,"htmx:afterRequest",H);he(r,"htmx:afterOnLoad",H);if(!le(r)){let e=null;while(t.length>0&&e==null){const n=t.shift();if(le(n)){e=n}}if(e){he(e,"htmx:afterRequest",H);he(e,"htmx:afterOnLoad",H)}}oe(s);m()}catch(e){fe(r,"htmx:onLoadError",ce({error:e},H));throw e}};p.onerror=function(){Qt(T,q);fe(r,"htmx:afterRequest",H);fe(r,"htmx:sendError",H);oe(l);m()};p.onabort=function(){Qt(T,q);fe(r,"htmx:afterRequest",H);fe(r,"htmx:sendAbort",H);oe(l);m()};p.ontimeout=function(){Qt(T,q);fe(r,"htmx:afterRequest",H);fe(r,"htmx:timeout",H);oe(l);m()};if(!he(r,"htmx:beforeRequest",H)){oe(s);m();return e}var T=Zt(r);var q=Yt(r);se(["loadstart","loadend","progress","abort"],function(t){se([p,p.upload],function(e){e.addEventListener(t,function(e){he(r,"htmx:xhr:"+t,{lengthComputable:e.lengthComputable,loaded:e.loaded,total:e.total})})})});he(r,"htmx:beforeSend",H);const J=E?null:mn(p,r,w);p.send(J);return e}function Nn(e,t){const n=t.xhr;let r=null;let o=null;if(R(n,/HX-Push:/i)){r=n.getResponseHeader("HX-Push");o="push"}else if(R(n,/HX-Push-Url:/i)){r=n.getResponseHeader("HX-Push-Url");o="push"}else if(R(n,/HX-Replace-Url:/i)){r=n.getResponseHeader("HX-Replace-Url");o="replace"}if(r){if(r==="false"){return{}}else{return{type:o,path:r}}}const i=t.pathInfo.finalRequestPath;const s=t.pathInfo.responsePath;const l=re(e,"hx-push-url");const c=re(e,"hx-replace-url");const u=ie(e).boosted;let a=null;let f=null;if(l){a="push";f=l}else if(c){a="replace";f=c}else if(u){a="push";f=s||i}if(f){if(f==="false"){return{}}if(f==="true"){f=s||i}if(t.pathInfo.anchor&&f.indexOf("#")===-1){f=f+"#"+t.pathInfo.anchor}return{type:a,path:f}}else{return{}}}function In(e,t){var n=new RegExp(e.code);return n.test(t.toString(10))}function Pn(e){for(var t=0;t0){E().setTimeout(e,x.swapDelay)}else{e()}}if(f){fe(o,"htmx:responseError",ce({error:"Response Status Error Code "+s.status+" from "+i.pathInfo.requestPath},i))}}const Mn={};function Xn(){return{init:function(e){return null},getSelectors:function(){return null},onEvent:function(e,t){return true},transformResponse:function(e,t,n){return e},isInlineSwap:function(e){return false},handleSwap:function(e,t,n,r){return false},encodeParameters:function(e,t,n){return null}}}function Fn(e,t){if(t.init){t.init(n)}Mn[e]=ce(Xn(),t)}function Bn(e){delete Mn[e]}function Un(e,n,r){if(n==undefined){n=[]}if(e==undefined){return n}if(r==undefined){r=[]}const t=te(e,"hx-ext");if(t){se(t.split(","),function(e){e=e.replace(/ /g,"");if(e.slice(0,7)=="ignore:"){r.push(e.slice(7));return}if(r.indexOf(e)<0){const t=Mn[e];if(t&&n.indexOf(t)<0){n.push(t)}}})}return Un(ue(c(e)),n,r)}var jn=false;ne().addEventListener("DOMContentLoaded",function(){jn=true});function Vn(e){if(jn||ne().readyState==="complete"){e()}else{ne().addEventListener("DOMContentLoaded",e)}}function _n(){if(Q.config.includeIndicatorStyles!==false){const e=Q.config.inlineStyleNonce?` nonce="${Q.config.inlineStyleNonce}"`:"";ne().head.insertAdjacentHTML("beforeend"," ."+Q.config.indicatorClass+"{opacity:0} ."+Q.config.requestClass+" ."+Q.config.indicatorClass+"{opacity:1; transition: opacity 200ms ease-in;} ."+Q.config.requestClass+"."+Q.config.indicatorClass+"{opacity:1; transition: opacity 200ms ease-in;} ")}}function zn(){const e=ne().querySelector('meta[name="htmx-config"]');if(e){return S(e.content)}else{return null}}function $n(){const e=zn();if(e){Q.config=ce(Q.config,e)}}Vn(function(){$n();_n();let e=ne().body;kt(e);const t=ne().querySelectorAll("[hx-trigger='restored'],[data-hx-trigger='restored']");e.addEventListener("htmx:abort",function(e){const t=e.target;const n=ie(t);if(n&&n.xhr){n.xhr.abort()}});const n=window.onpopstate?window.onpopstate.bind(window):null;window.onpopstate=function(e){if(e.state&&e.state.htmx){Wt();se(t,function(e){he(e,"htmx:restored",{document:ne(),triggerEvent:he})})}else{if(n){n(e)}}};E().setTimeout(function(){he(e,"htmx:load",{});e=null},0)});return Q}(); \ No newline at end of file diff --git a/internal/web/templates/index.html b/internal/web/templates/index.html new file mode 100644 index 0000000..4266b3f --- /dev/null +++ b/internal/web/templates/index.html @@ -0,0 +1,124 @@ + + + + + +i-am-gcroot + + + + +
+

i-am-gcroot

+
+ + + {{range kinds}}{{end}} + + + +
+
+
{{template "rows" .}}
+ + + + +{{define "rows"}} +{{if .Err}}
{{.Err}}
{{end}} +{{if .Message}}
{{.Message}} + {{if .Results}}
{{len .Results}} result(s)
    + {{range .Results}}
  • {{if .Err}}fail {{.Link}}: {{.Err}}{{else}}ok {{.Action}}{{end}}
  • {{end}} +
{{end}} +
{{end}} +
+ {{.Total}} roots, {{len .Rows}} shown{{if .Status.Sizing}}, sizing {{.Status.SizesDone}}/{{.Status.SizesTodo}}{{end}} + + + + + + + + +
+{{if .Status.Sizing}}
{{end}} +
+ + +{{range .Rows}} + + + + + + + +{{end}} +
kindownerageclosurelinktarget
{{if .Deletable}}{{end}}{{if .Protected}} !{{end}}{{.Kind}}{{if .Current}} (current){{end}}{{.Owner}}{{age .ModTime}}{{if eq .Kind "dangling"}}-{{else}}{{size .ClosureSize}}{{end}}{{.Link}}{{if .Command}} {{.Command}}{{end}}{{if .Target}}{{.TargetName}}{{else if gt .Count 1}}({{.Count}} paths){{else if eq .Kind "dangling"}}(missing){{else}}{{.Reason}}{{end}}
+{{end}} diff --git a/main.go b/main.go new file mode 100644 index 0000000..1eb9660 --- /dev/null +++ b/main.go @@ -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) +} diff --git a/nix/vm-test.nix b/nix/vm-test.nix new file mode 100644 index 0000000..956884a --- /dev/null +++ b/nix/vm-test.nix @@ -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; +} diff --git a/nix/vm-test.py b/nix/vm-test.py new file mode 100644 index 0000000..809b243 --- /dev/null +++ b/nix/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")