This commit is contained in:
Valentin Rieß 2024-04-29 00:27:24 +02:00 committed by GitHub
commit c905d867c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 205 additions and 0 deletions

View file

@ -171,4 +171,5 @@ features or generally made searx better:
- Paolo Basso `<https://github.com/paolobasso99>`
- Bernie Huang `<https://github.com/BernieHuang2008>`
- Austin Olacsi `<https://github.com/Austin-Olacsi>`
- Valentin Rieß `<https://github.com/v411e>`
- @micsthepick

156
searx/engines/nixpkgs.py Normal file
View file

@ -0,0 +1,156 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
""".. sidebar:: info
- :origin:`nixpkgs.py <searx/engines/nixpkgs.py>`
- `NixOS Search <https://search.nixos.org>`_
NixOS-Search_ makes it easy to search the huge nixpkgs package index.
The search backend is an Elasticsearch instance.
Example
=======
The following is an example configuration for an NixOS-Search_ engine with
authentication configured.
Credentials are available here: https://github.com/NixOS/nixos-search/blob/main/frontend/src/index.js
.. code:: yaml
- name: nixos-search
shortcut: nix
engine: nixpkgs
base_url: https://search.nixos.org/backend
username:
password:
index: latest-42-nixos-unstable
"""
from json import loads
from searx.exceptions import SearxEngineAPIException
base_url = "https://search.nixos.org/backend"
username = ""
password = ""
index = "latest-42-nixos-unstable"
search_url = base_url + "/" + index + "/_search"
show_metadata = False
categories = ["general"]
def init():
if not (username or password):
raise ValueError("username and password need to be configured.")
def request(query, params):
search_url = base_url + "/" + index + "/_search"
if username and password:
params["auth"] = (username, password)
params["url"] = search_url
params["method"] = "GET"
params["data"] = _build_query(query)
params["headers"]["Content-Type"] = "application/json"
return params
def response(resp):
results = []
resp_json = loads(resp.text)
if "error" in resp_json:
raise SearxEngineAPIException(resp_json["error"])
for result in resp_json["hits"]["hits"]:
r = {key: value if not key.startswith("_") else value for key, value in result["_source"].items()}
r["template"] = "nix-package.html"
r["title"] = result["_source"]["package_pname"]
r["content"] = result["_source"]["package_description"]
r["github_url"] = _position_to_github_url(result["_source"]["package_position"])
r["codelines"] = _get_codelines(result["_source"]["package_pname"])
if show_metadata:
r["metadata"] = {
"index": result["_index"],
"id": result["_id"],
"score": result["_score"],
}
results.append(r)
return results
def _build_query(query: str):
return f"""
{{
"from": 0,
"size": 50,
"sort": [
{{
"_score": "desc",
"package_attr_name": "desc",
"package_pversion": "desc"
}}
],
"query": {{
"bool": {{
"must": [
{{
"dis_max": {{
"tie_breaker": 0.7,
"queries": [
{{
"multi_match": {{
"type": "cross_fields",
"query": "{query}",
"analyzer": "whitespace",
"auto_generate_synonyms_phrase_query": false,
"operator": "and",
"_name": "multi_match_firefox",
"fields": [
"package_attr_name^9",
"package_attr_name.*^5.3999999999999995",
"package_programs^9",
"package_programs.*^5.3999999999999995",
"package_pname^6",
"package_pname.*^3.5999999999999996",
"package_description^1.3",
"package_description.*^0.78",
"package_longDescription^1",
"package_longDescription.*^0.6",
"flake_name^0.5",
"flake_name.*^0.3"
]
}}
}},
{{
"wildcard": {{
"package_attr_name": {{
"value": "*{query}*",
"case_insensitive": true
}}
}}
}}
]
}}
}}
]
}}
}}
}}
"""
def _position_to_github_url(package_position: str):
path, line = package_position.split(":")
return f"https://github.com/NixOS/nixpkgs/blob/master/{path}#L{line}"
def _get_codelines(package_name: str):
code = f"nix-shell -p {package_name}"
return [(0, code)]

View file

@ -2276,6 +2276,15 @@ engines:
shortcut: pgo
disabled: true
# NixOS Package Search
# - name: nixos-search
# shortcut: nix
# engine: nixpkgs
# base_url: https://search.nixos.org/backend
# username:
# password:
# index: latest-42-nixos-unstable
# Doku engine lets you access to any Doku wiki instance:
# A public one or a privete/corporate one.
# - name: ubuntuwiki

View file

@ -0,0 +1,39 @@
{% from 'simple/macros.html' import result_header, result_sub_header, result_sub_footer, result_footer with context %}
<h3>{{ result.package_pname }}</h3>
{{- result_sub_header(result) -}}
{%- if result.content %}
<p class="content">
{{ result.content|safe }}
</p>
{%- else %}
<p class="content empty_element">
{{ _('No description available.')|safe }}
</p>
{% endif -%}
<ul>
<li>Name: <code>{{ result.package_pname }}</code></li>
<li>Version: <strong>{{ result.package_pversion }}</strong></li>
<li>Outputs:
<code>{{ result.package_outputs|join(", ") }}</code>
</li>
{% if result.package_homepage %}
<li><a href="{{ result.package_homepage|first() }}" target="_blank">🌐 Homepage</a></li>
{% endif %}
{% if result.package_position %}
<li><a href="{{ result.github_url }}" target="_blank">📦 Source</a></li>
{% endif %}
{% if result.package_license.url and result.package_license.fullName %}
<li>License: <a href="{{ result.package_license.url }}" target="_blank">{{ result.package_license.fullName }}</a></li>
{% endif %}
</ul>
<div dir="ltr" class="codelines">
{{- result.codelines|code_highlighter('sh')|safe -}}
</div>
<div class="engines">
{% for engine in result.engines %}<span>{{ engine }}</span>{% endfor %}
</div>{{- '' -}}
<div class="break"></div>{{- '' -}}
{{- result_footer(result) -}}