Database layout (PostgreSQL ≥ 14) #
Authoritative DDL: src/nixray/adapters/postgres/migrations/.
┌──────────────┐ ┌──────────────┐
│ cve │ │ package │
│ id (PK) │ │ id (PK) │
│ description │ │ pname │
│ cvss_v3 │ │ version │
│ first_seen_at│ │ UQ(pname,ver)│
│ updated_at │ └──────┬───────┘
└──┬────────┬──┘ │
│ │ │
┌───────────┴──┐ ┌──┴───────────────────┴──┐ ┌───────────────────────┐
│ suppression │ │ finding │ │ scan │
│ id (PK) │ │ id (PK) │ │ id (PK) │
│ cve_id (FK) │ │ cve_id (FK) │ │ started_at │
│ pname? │ │ package_id (FK) │ │ finished_at? │
│ version? │ │ UQ(cve_id, package_id) │ │ status (enum) │
│ reason │ └──┬───────────────────┬──┘ │ target │
│ created_by/at│ │ │ │ system_store_path? │
│ expires_at? │ │ │ │ nixos_version? │
│ revoked_*? │ │ │ │ scanner_version? │
└──────────────┘ │ │ │ finding_count? │
│ │ │ error? │
┌─────────────┴────────┐ ┌──────┴──────────┴───┐ │
│ observation │ │ exposure_period │ │
│ scan_id (PK,FK) ────┼──▶│ id (PK) │ │
│ finding_id (PK,FK) │ │ finding_id (FK) │ │
│ derivation_paths[] │ │ opened_scan_id (FK) ─┼──────────────────┘
└──────────────────────┘ │ closed_scan_id? (FK) │
│ period tstzrange │
│ EXCLUDE overlapping │
└──────────────────────┘
Tables #
host #
A machine nixray keeps history for (migration 0007). name is unique and
hostname-like (CHECK). Only the history belongs to a host (scan, and through it
observation, plus exposure_period); findings, CVEs and every decision
(suppression, confirmation, external_triage, finding_judgement) are about
a package and apply on all hosts. Migration 0007 assigns the existing history to
one host named by NIXRAY_HOST (default: the machine's hostname) at migration time.
scan #
One row per scanner run, success or failure.
| column | type | notes |
|---|---|---|
id |
bigint identity |
PK |
host_id |
bigint |
FK host; UNIQUE (id, host_id) lets periods reference both |
started_at |
timestamptz |
observation time of all findings of this scan |
finished_at |
timestamptz null |
NULL ⇔ status = 'running' (CHECK) |
status |
scan_status enum running/succeeded/failed |
|
target |
text |
e.g. --system or a path |
system_store_path |
text null |
/run/current-system resolved → correlates with deployments |
nixos_version |
text null |
|
scanner_version |
text null |
vulnix version |
finding_count |
integer null |
denormalised summary; set iff succeeded (CHECK) |
runtime_count |
integer null |
how many of them are runtime findings; null for scans before migration 0005 |
error |
text null |
required when failed (CHECK) |
Partial index on (host_id, started_at) for status = 'succeeded' (trend queries).
cve #
Reference data from NVD as delivered by vulnix. Upserted on every scan because
NVD re-scores and re-describes CVEs over time.
id is checked against ^CVE-[0-9]{4}-[0-9]{4,}$; cvss_v3 numeric(3,1) in [0,10], nullable.
package #
(pname, version) as split by vulnix. Unique pair.
finding #
Identity of "package P in version V is affected by CVE C" – the unit of triage
and exposure. Unique (cve_id, package_id).
observation – immutable facts #
"Scan S observed finding F (through derivations D, in scope X)". PK
(scan_id, finding_id), derivation_paths text[] because one name/version may
come from several .drvs, scope finding_scope (runtime/buildtime, added
by migration 0005; earlier observations were set to runtime).
Indexed on finding_id. Source of truth for the trend and for rebuilding periods.
exposure_period – derived, maintained on ingest #
period tstzrange = [opened_at, closed_at), upper bound unbounded while open.
A period lies on one host: host_id with FKs (opened_scan_id, host_id) and
(closed_scan_id, host_id) → scan (id, host_id), so only that host's scans open
and close it.
EXCLUDE USING gist (host_id WITH =, finding_id WITH =, period WITH &&)(needsbtree_gist): periods of a finding on a host never overlap.- Partial unique index
(host_id, finding_id) WHERE upper_inf(period): at most one open period per finding and host. - CHECK
closed_scan_id IS NULL ⇔ upper_inf(period).
suppression – false-positive rules (append-only) #
| column | notes |
|---|---|
cve_id |
FK cve, required |
pname |
null = every package |
version |
null = every version; CHECK version IS NULL OR pname IS NOT NULL |
reason |
required, CHECK at least 10 non-blank characters |
created_by, created_at |
audit |
expires_at |
optional review date; expired rule no longer suppresses |
revoked_at, revoked_by, revoke_reason |
all-or-nothing CHECK; rows are never deleted |
Partial unique index on (cve_id, coalesce(pname,''), coalesce(version,'')) WHERE revoked_at IS NULL – one live rule per scope; creating a new rule for the
same scope revokes ("supersedes") the previous one in the same transaction.
A finding is suppressed iff a rule exists with revoked_at IS NULL,
(expires_at IS NULL OR expires_at > now()), same cve_id, and matching or
null pname/version. This rule lives only in the domain
(Suppression.suppresses); the database stores facts, not interpretations
(see architecture ADR-4).
A trigger (suppression_append_only) rejects DELETE and any UPDATE other
than setting the revocation once, so the audit trail cannot be rewritten even
with direct SQL access by the application role.
confirmation – review decisions (append-only, revocable) #
"CVE C really affects package P (any version)", recorded from the review inbox.
| column | notes |
|---|---|
cve_id |
FK cve |
pname |
package name, required |
note |
optional, ≤ 2000 characters |
created_by, created_at |
audit |
revoked_at, revoked_by, revoke_reason |
all-or-nothing CHECK; reason ≥ 10 characters |
Partial unique index (cve_id, pname) WHERE revoked_at IS NULL: one active
confirmation per scope; confirming again revokes ("supersedes") the previous one
in the same transaction. A trigger (confirmation_append_only) rejects
DELETE and every UPDATE other than setting the revocation once.
Migration 0003 resolved duplicates from before that rule by keeping the newest.
A finding needs review iff it is open, not suppressed and has no active
confirmation for its (cve_id, pname) (rule in the domain, ADR-4).
external_triage – cache of the NixOS security tracker #
One row per CVE (cve_id primary key, no foreign key: the tracker may know CVEs
before nixray does) with outcome, basis (human/automatic), the linked
(affected) and removed (excluded) attribute names, explanation, issue,
url (CHECK: http(s) only, so a hostile value cannot become a javascript: link)
and fetched_at. Rows are replaced on refresh; this is a cache of external
data, not an audit record. See tracker.md.
finding_judgement – judgements by a language model #
One row per finding (finding_id primary key and foreign key) with
applicability (affected/not_affected/uncertain), the model's
reasoning (1–2000 characters), model and judged_at. Like
external_triage it is advice, replaced when a finding is judged again, not an
audit record. See judgement.md.
How the views are derived #
The read side loads flat rows and lets the domain compute (ADR-4):
- Trend: for every successful scan time
t, count the exposure periods withlower <= t < upper. Because a period is only closed by a successful scan that no longer observed the finding, this equals "findings observed by that scan". Implemented as a sweep (count_open_at) in O((n+m) log n). - Exposure of a CVE: union of the periods of all its findings (touching periods merge, e.g. an upgrade to a still-affected version), open periods last until now.
The same answers are available in SQL for ad-hoc analysis, e.g.:
SELECT f.cve_id,
range_agg(tstzrange(lower(p.period), coalesce(upper(p.period), now()))) AS exposed
FROM exposure_period p JOIN finding f ON f.id = p.finding_id
GROUP BY f.cve_id;
Roles & privileges (NixOS module) #
The database is owned by the nixray role, connected via Unix socket with peer
authentication – no password exists. Both services (nixray-scan,
nixray-web) run as the nixray system user. Migrations run in
nixray-migrate.service before both.
Retention #
Facts are small (≈ findings × scans). At 2 scans/day and ~600 findings this is
≈ 440k observation rows per year, comfortably within Postgres' range.
scan rows can be deleted (ON DELETE CASCADE on observations) if retention is
ever needed; periods reference scans by id with ON DELETE RESTRICT so history
of exposure cannot be destroyed accidentally.