Match pull requests and issues by nixpkgs files

Status: design for review. scan --files is an experimental prototype. It writes the file list into the local report only. The upload and the server do not change yet.

The problem

A person has flakes with packages and nixosConfigurations outputs. They want to see only the pull requests and issues that affect those outputs.

Today the scan reads the derivation closure. Then it matches each derivation back to an attribute by name: the pname, the lower-case name and python3Packages.<name> (PublicList::candidates in src/report.rs). This is fragile. A store path does not say which attribute or which file made it.

There is a better key. An evaluation reads nixpkgs files. A pull request changes nixpkgs files. If the scan records the files that the evaluation read, the server can match pull requests by file. It needs no name.

What iets gives

iets is a Nix evaluator in Rust. Its evaluation cache records every observation of the outside world that an evaluation makes. We use that record. The facts below are from iets 0.1.0 at commit 0fba531c064e.

What iets does not give, and what the prototype does instead:

MissingWorkaround in the prototypeAsk
No flag on eval that names its cache entry. --entry-to FILE exists on print-dev-env only.A new, empty --cache-dir per scan; it holds one entry.eval --entry-to FILE, or --read-set-to FILE.
cache dump is text for debugging, not a contract. A path with a space is ambiguous.Parse from the right: the last two fields of a file line are size and hash.cache dump --json.
eval --json fails on a list that map made: cannot convert a thunk to JSON. Reproduce with a flake output x = builtins.map (a: a + 1) [ 1 ];.builtins.toJSON in the expression and --raw.Fix the bug.
cache paths leaves out store paths, so it leaves out a flake input.Use cache dump.None.

What the scan records

scan --files evaluates the chosen outputs once with iets. It keeps:

It drops stat and dir observations. They are probes. pkgs/by-name makes a dir observation for every shard on every evaluation.

Each path is relative to the nixpkgs root, such as pkgs/servers/http/nginx/generic.nix. The store prefix of nixpkgs comes from flake.inputs.<nixpkgs>.outPath in the same evaluation. A path must be plain: no leading /, no . or .. part, no control character, at most 512 bytes.

The local report gets one new optional field, files:

"files": {
  "files": ["lib/modules.nix", "pkgs/servers/http/nginx/generic.nix", "…"],
  "modules": ["nixos/modules/services/web-servers/nginx/default.nix", "…"],
  "modules_dropped": 2415
}

modules is the result of the module signal (below). files already has the module filter applied.

The NixOS module problem

A NixOS evaluation imports every file of module-list.nix. So the trace of an nginx host has the Apache module too. In the test below, the trace has 2,465 paths under nixos/modules/, and a host without nginx has the same 2,465.

The module signal

Keep a module file only when the configuration defines an option that the file declares.

The module system records both sides:

options.<path>.files alone is not enough. To read it for every option, Nix must evaluate every mkIf condition. Some conditions fail when the module is off, with an error that tryEval does not catch. In the test, hardware.nvidia.open stopped the walk with expected a set but found null.

So the signal works in two steps (src/trace.nix):

  1. Find candidates without the conditions. Load each module of the configuration again with the real config. Walk its config attribute names down to an option. Do not go into the content of an mkIf whose condition is not true.
  2. Confirm with the module system. Keep a candidate only when options.<path>.files names the file. This applies the real conditions, but only to options that the configuration uses.

Then it grows in hops:

The client then keeps a path under nixos/modules/ only when it is in the area of a kept module. The area is the directory for a default.nix, as in area_key (src/area.rs). So nginx/vhost-options.nix stays with nginx/default.nix.

Test result

The test host is a scratch flake on nixpkgs 7fd4004d0511. It sets boot.loader.grub.device, fileSystems."/", system.stateVersion and services.nginx.enable = true. A second host is the same without nginx.

nginx hosthost without nginx
Hop 08 files7 files
Hop 131 files24 files
Fixed point (5 hops)45 files43 files
Module paths in the trace2,4652,465

Known gaps:

Files that nearly every pull request touches

A trace has lib/, pkgs/top-level/all-packages.nix, pkgs/stdenv/ and pkgs/build-support/. Every evaluation reads them. A lens that matches on them shows most of nixpkgs.

Measured on the nginx host, after the module signal (1,683 paths):

AreaPaths
pkgs/by-name/627
pkgs/development/494
pkgs/build-support/109
nixos/modules/50
lib/43
nixos/lib/27
pkgs/top-level/18
pkgs/stdenv/15
*.patch285

Compare the raw traces, before the module filter: the nginx host reads 4,101 paths, and the host without nginx reads all but 14 of them. The 14 are the nginx package files, its two patches, and gixy, nginx-doc and nginx-config-formatter, which the nginx module uses. hello reads 381 paths, and the host without nginx reads 378 of them.

Proposal: the server sorts each matched path into one of three tiers. It uses public data only. It never uses the sets of other people.

  1. Core. The server traces a reference evaluation at each index: a minimal NixOS host and hello. Every path in that trace is core. A pull request that matches only core paths goes into a folded "Core" group. The lens shows its count, not its rows.
  2. Shared. A path outside core that more than 25 packages share, by the same rule as SHARED_AREA in src/links.rs. Examples: a generic builder, python-packages.nix. The lens shows these rows below the specific ones.
  3. Specific. All other paths. The lens shows these rows first.

The tier of a pull request is the best tier of its matched paths. Inside a tier, the lens sorts by the number of specific paths matched, then by date. all-packages.nix and aliases.nix are core, so a one-line alias change does not reach the lens.

The server can learn more later. It can count how many pull requests touched each path in 90 days. A path with a high count is a hub and goes to core.

The privacy rule

A path leaves the machine only when it exists in public nixpkgs at the revision that the scan read. This is the same strictness as the attribute upload.

On the client:

On the server:

The upload format

The upload stays schema: 1. It gets one optional field, files:

{
  "schema": 1,
  "source": { "system": "x86_64-linux", "nixpkgs_rev": "7fd4…", "…": "…" },
  "packages": ["…"],
  "omitted": { "unknown": 12 },
  "files": {
    "version": 1,
    "paths": ["lib/modules.nix", "pkgs/servers/http/nginx/generic.nix"],
    "omitted": 0
  }
}

How the server matches

src/links.rs already links a pull request by the files it changes (Pr.files), and an issue by the paths in its body (Issue.mentions with subject::is_path). File matching adds a second lookup with the same input:

  1. Keep the paths of the set in memory as a set of strings.
  2. For a pull request, match each path of Pr.files. Match each parent directory too, so a new file in pkgs/by-name/ng/nginx/ matches the directory of a file that the set read.
  3. For an issue, match each path of mentions in the same way.
  4. Give each match its tier (above).

Two limits apply:

The cost is small. One set has about 2,000 paths. The open pull requests have fewer than 50 paths each. One pass over all open pull requests is a few hundred thousand set lookups.

Named sets

Another change adds named package sets, keyed by (login, name), and public flake sets on profiles. File matching follows the same keys:

Cost and memory

Measured on the nginx host with iets 0.1.0, cold IR cache:

StepWall timePeak memory
iets eval of toplevel.drvPath, traced3.9–4.5 s0.86 GB
The same, with the module signal (src/trace.nix)3.3–9.2 s0.87 GB
The module signal alone with Nix 2.366.5–8.6 s0.63 GB
hello.drvPath, traced1.7 s0.22 GB

The prototype

Result on the nginx host:

$ maintainer-mode scan --flake . --files \
    --output nixosConfigurations.web.config.system.build.toplevel
1683 nixpkgs files, 45 modules kept, 2415 module files dropped
1481 packages -> maintainer-mode-scan.json

Rollout

  1. Now. scan --files, local only. Owners try it on their hosts. We collect the counts, the time and the memory.
  2. Server, read side. Build the path set of each index and the core trace. Add "accepts": ["files/1"]. Accept files in uploads, check the paths, store the blob. Show nothing yet.
  3. The lens. A "By files" filter on the set view, beside the name match. Compare both on our own sets for a few weeks.
  4. Default. If the file match is better, submit sends files by default when iets is on PATH. The name match stays as the fallback for a scan without iets.
  5. Server rescans. Use iets in the rescan sandbox for public flakes.

Open questions