1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! GNU Guix package index fetcher.
//!
//! Fetches package metadata from guix.gnu.org/packages.json.
//! The server returns gzip-compressed JSON which ureq decompresses automatically.
//!
//! ## API Strategy
//! - **fetch**: Searches cached `guix.gnu.org/packages.json`
//! - **fetch_versions**: Same, single version per package
//! - **search**: Filters cached packages.json
//! - **fetch_all**: `guix.gnu.org/packages.json` (cached 6 hours, ~25MB)
//!
//! ## Multi-channel Support
//! ```rust,ignore
//! use normalize_packages::index::guix::{Guix, GuixChannel};
//!
//! // All channels (default)
//! let all = Guix::all();
//!
//! // Official Guix channel only
//! let official = Guix::official();
//!
//! // With nonguix (community nonfree)
//! let with_nonguix = Guix::with_nonguix();
//! ```
use super::{IndexError, PackageIndex, PackageMeta, VersionMeta};
use crate::cache;
use rayon::prelude::*;
use std::collections::HashMap;
use std::time::Duration;
/// Cache TTL for the Guix package list (6 hours - it's a 25MB download).
const INDEX_CACHE_TTL: Duration = Duration::from_secs(6 * 60 * 60);
/// Available Guix channels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GuixChannel {
/// Official GNU Guix channel
Guix,
/// Nonguix - community channel for non-free software
Nonguix,
}
impl GuixChannel {
/// Get the packages.json URL for this channel.
fn packages_url(&self) -> Option<&'static str> {
match self {
Self::Guix => Some("https://guix.gnu.org/packages.json"),
// Nonguix doesn't have a public packages.json API
Self::Nonguix => None,
}
}
/// Get the channel name for tagging.
pub fn name(&self) -> &'static str {
match self {
Self::Guix => "guix",
Self::Nonguix => "nonguix",
}
}
/// All available channels.
pub fn all() -> &'static [GuixChannel] {
&[Self::Guix, Self::Nonguix]
}
/// Official channel only.
pub fn official() -> &'static [GuixChannel] {
&[Self::Guix]
}
}
/// GNU Guix package index fetcher with configurable channels.
pub struct Guix {
channels: Vec<GuixChannel>,
}
impl Guix {
/// Create a fetcher with all channels.
pub fn all() -> Self {
Self {
channels: GuixChannel::all().to_vec(),
}
}
/// Create a fetcher with official Guix channel only.
pub fn official() -> Self {
Self {
channels: GuixChannel::official().to_vec(),
}
}
/// Create a fetcher with custom channel selection.
pub fn with_channels(channels: &[GuixChannel]) -> Self {
Self {
channels: channels.to_vec(),
}
}
/// Alias for `all()` including nonguix.
pub fn with_nonguix() -> Self {
Self::all()
}
/// Fetch the full package list from a channel with caching.
fn fetch_package_list(channel: GuixChannel) -> Result<Vec<serde_json::Value>, IndexError> {
let url = channel.packages_url().ok_or_else(|| {
IndexError::NotImplemented(format!("{} has no public API", channel.name()))
})?;
let (data, _was_cached) = cache::fetch_with_cache(
"guix",
&format!("{}-packages", channel.name()),
url,
INDEX_CACHE_TTL,
)
.map_err(IndexError::Network)?;
let packages: Vec<serde_json::Value> = serde_json::from_slice(&data)?;
Ok(packages)
}
/// Load packages from all configured channels.
fn load_packages(&self) -> Result<Vec<PackageMeta>, IndexError> {
let results: Vec<_> = self
.channels
.par_iter()
.filter_map(|&channel| {
match Self::fetch_package_list(channel) {
Ok(packages) => Some(
packages
.iter()
.map(|p| package_to_meta(p, "", channel))
.collect::<Vec<_>>(),
),
Err(IndexError::NotImplemented(_)) => None, // Skip unsupported channels
Err(e) => {
tracing::warn!("failed to load Guix channel {}: {}", channel.name(), e);
None
}
}
})
.flatten()
.collect();
Ok(results)
}
}
impl PackageIndex for Guix {
fn ecosystem(&self) -> &'static str {
"guix"
}
fn display_name(&self) -> &'static str {
"GNU Guix"
}
fn fetch(&self, name: &str) -> Result<PackageMeta, IndexError> {
// Try each channel until we find the package
for &channel in &self.channels {
if let Ok(packages) = Self::fetch_package_list(channel)
&& let Some(pkg) = packages.iter().find(|p| p["name"].as_str() == Some(name))
{
return Ok(package_to_meta(pkg, name, channel));
}
}
Err(IndexError::NotFound(name.to_string()))
}
fn fetch_versions(&self, name: &str) -> Result<Vec<VersionMeta>, IndexError> {
let mut all_versions = Vec::new();
for &channel in &self.channels {
if let Ok(packages) = Self::fetch_package_list(channel) {
let versions: Vec<VersionMeta> = packages
.iter()
.filter(|p| p["name"].as_str() == Some(name))
.filter_map(|p| {
Some(VersionMeta {
version: p["version"].as_str()?.to_string(),
released: None,
yanked: false,
})
})
.collect();
all_versions.extend(versions);
}
}
if all_versions.is_empty() {
return Err(IndexError::NotFound(name.to_string()));
}
Ok(all_versions)
}
fn supports_fetch_all(&self) -> bool {
true
}
fn fetch_all(&self) -> Result<Vec<PackageMeta>, IndexError> {
self.load_packages()
}
fn search(&self, query: &str) -> Result<Vec<PackageMeta>, IndexError> {
let packages = self.load_packages()?;
let query_lower = query.to_lowercase();
Ok(packages
.into_iter()
.filter(|pkg| {
pkg.name.to_lowercase().contains(&query_lower)
|| pkg
.description
.as_ref()
.map(|d| d.to_lowercase().contains(&query_lower))
.unwrap_or(false)
})
.take(50)
.collect())
}
}
/// Convert a Guix package JSON object to PackageMeta.
fn package_to_meta(
pkg: &serde_json::Value,
fallback_name: &str,
channel: GuixChannel,
) -> PackageMeta {
let mut extra = HashMap::new();
extra.insert(
"source_repo".to_string(),
serde_json::Value::String(channel.name().to_string()),
);
PackageMeta {
name: pkg["name"].as_str().unwrap_or(fallback_name).to_string(),
version: pkg["version"].as_str().unwrap_or("unknown").to_string(),
description: pkg["synopsis"].as_str().map(String::from),
homepage: pkg["homepage"].as_str().map(String::from),
repository: extract_repo(pkg),
license: None, // Guix packages.json doesn't include license info
binaries: Vec::new(),
keywords: Vec::new(),
maintainers: Vec::new(),
published: None,
downloads: None,
archive_url: None,
checksum: None,
extra,
}
}
/// Extract repository URL from homepage if it's a known forge.
fn extract_repo(pkg: &serde_json::Value) -> Option<String> {
let homepage = pkg["homepage"].as_str()?;
if homepage.contains("github.com")
|| homepage.contains("gitlab.com")
|| homepage.contains("sr.ht")
|| homepage.contains("codeberg.org")
{
Some(homepage.to_string())
} else {
None
}
}