Skip to content

Commit e3194fb

Browse files
authored
feat: display src file location next to the version for each derivation (#452)
1 parent 27ba8a6 commit e3194fb

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

src/website/shared/listeners/cache_suggestions.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import re
3+
import urllib.parse
34
from itertools import chain
45
from typing import Any
56

@@ -179,6 +180,28 @@ def is_version_affected(version_statuses: list[str]) -> Version.Status:
179180
return result
180181

181182

183+
def get_src_position(derivation: NixDerivation) -> str | None:
184+
"""
185+
Get the GitHub URL pointing to the exact source file used for the evaluation of this derivation.
186+
E.g. https://github.com/NixOS/nixpkgs/blob/0e8be3827d0298743ba71b91eea652d43d7dc03d/pkgs/by-name/he/hello/package.nix#L47
187+
"""
188+
if derivation.metadata and derivation.metadata.position:
189+
rev = urllib.parse.quote(derivation.parent_evaluation.commit_sha1)
190+
# position is something like `/tmp/tmpfh7ff2xs/pkgs/development/python-modules/qemu/default.nix:67`
191+
position_match = re.match(
192+
# FIXME the location of the eval store is going to be configurable in the future.
193+
# https://github.com/Nix-Security-WG/nix-security-tracker/pull/451
194+
# Ideally the position field is already relative to the location.
195+
r"/tmp/[^/]+/(.+):(\d+)",
196+
derivation.metadata.position,
197+
)
198+
if position_match:
199+
path = urllib.parse.quote(position_match.group(1))
200+
linenumber = urllib.parse.quote(position_match.group(2))
201+
return f"https://github.com/NixOS/nixpkgs/blob/{rev}/{path}#L{linenumber}"
202+
return None
203+
204+
182205
def channel_structure(
183206
version_constraints: list[Version], derivations: list[NixDerivation]
184207
) -> dict:
@@ -211,21 +234,26 @@ def channel_structure(
211234
"major_version": None,
212235
"status": None,
213236
"uniform_versions": None,
237+
"src_position": None,
214238
"sub_branches": dict(),
215239
}
216-
if not branch_name == major_channel:
240+
if branch_name == major_channel:
241+
packages[attribute]["versions"][major_channel]["major_version"] = (
242+
version
243+
)
244+
packages[attribute]["versions"][major_channel]["src_position"] = (
245+
get_src_position(derivation)
246+
)
247+
else:
217248
packages[attribute]["versions"][major_channel]["sub_branches"][
218249
branch_name
219250
] = {
220251
"version": version,
221252
"status": is_version_affected(
222253
[v.is_affected(version) for v in version_constraints]
223254
),
255+
"src_position": get_src_position(derivation),
224256
}
225-
else:
226-
packages[attribute]["versions"][major_channel]["major_version"] = (
227-
version
228-
)
229257
for package_name in packages:
230258
for mc in packages[package_name]["versions"].keys():
231259
uniform_versions = True

src/website/webview/static/style.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,10 @@ article .nixpkgs-packages {
748748
color: var(--dark-grey);
749749
}
750750

751+
.nixpkgs-package .branch-minor .version {
752+
color: var(--dark-grey);
753+
}
754+
751755
.nixpkgs-package .branch-major {
752756
display: inline-flex;
753757
justify-content: flex-end;
@@ -762,6 +766,12 @@ article .nixpkgs-packages {
762766
min-width: 5em;
763767
display: inline-block;
764768
text-align: end;
769+
text-decoration: none;
770+
color: black;
771+
}
772+
773+
.nixpkgs-package .version:hover {
774+
text-decoration: underline;
765775
}
766776

767777
.nixpkgs-package .version.affected {

src/website/webview/templates/components/nixpkgs_package.html

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,31 @@ <h3><span class="pkgs">pkgs.</span>{{ attribute_name }}</h3>
77
{{ pdata.description }}
88
</div>
99
<ul class="channel-list">
10-
{% for major_channel, val in pdata.versions %}
10+
{% for major_channel, major_version in pdata.versions %}
1111
<li>
12-
<details {% if val.uniform_versions %}open{% endif %}>
12+
<details {% if major_version.uniform_versions %}open{% endif %}>
1313
<summary>
1414
<span class="branch-major">
1515
<span class="branch-name">{{ major_channel }}</span>
16-
<span class="version {{ val.status }}">
17-
{% if val.major_version %}
18-
{{ val.major_version }}
16+
<a class="version {{ major_version.status }}" target="_blank" href="{{ major_version.src_position }}">
17+
{% if major_version.major_version %}
18+
{{ major_version.major_version }}
1919
{% else %}
2020
???
2121
{% endif %}
22-
</span>
22+
</a>
2323
</span>
2424
</summary>
2525
<ul class="channel-list">
26-
{% for branch_name, vdata in val.sub_branches %}
26+
{% for branch_name, minor_version in major_version.sub_branches %}
2727
<li class="branch-minor">
2828
<span class="branch-name">{{ branch_name }}</span>
29-
<span class="version {{ vdata.status }}">{{ vdata.version }}</span>
29+
<a
30+
class="version {{ minor_version.status }}"
31+
target="_blank"
32+
href="{{ minor_version.src_position }}">
33+
{{ minor_version.version }}
34+
</a>
3035
</li>
3136
{% endfor %}
3237
</ul>

0 commit comments

Comments
 (0)