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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Nix/NixOS package index fetcher.
//!
//! Fetches package metadata from nixpkgs via the NixOS search API.
//! Uses the Bonsai Elasticsearch cluster that powers search.nixos.org.
//!
//! ## API Strategy
//! - **fetch**: `search.nixos.org` Elasticsearch API - Official NixOS search
//! - **fetch_versions**: Queries all configured channels
//! - **search**: `search.nixos.org` Elasticsearch query
//! - **fetch_all**: Not supported (too large, use search instead)
//!
//! ## Multi-repo Support
//! ```rust,ignore
//! use normalize_packages::index::nix::{Nix, NixChannel};
//!
//! // All channels (default)
//! let all = Nix::all();
//!
//! // Unstable only
//! let unstable = Nix::unstable();
//!
//! // Stable channels only
//! let stable = Nix::stable();
//!
//! // Custom selection
//! let custom = Nix::with_channels(&[NixChannel::NixosUnstable, NixChannel::Nixos2411]);
//! ```
use super::{IndexError, PackageIndex, PackageMeta, VersionMeta};
use rayon::prelude::*;
use std::collections::HashMap;
/// Simple base64 encoding for Basic Auth.
fn base64_encode(input: &str) -> String {
const ALPHABET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let bytes = input.as_bytes();
let mut result = String::with_capacity(bytes.len().div_ceil(3) * 4);
for chunk in bytes.chunks(3) {
let mut buf = [0u8; 3];
buf[..chunk.len()].copy_from_slice(chunk);
let indices = [
(buf[0] >> 2) as usize,
(((buf[0] & 0x03) << 4) | (buf[1] >> 4)) as usize,
(((buf[1] & 0x0f) << 2) | (buf[2] >> 6)) as usize,
(buf[2] & 0x3f) as usize,
];
result.push(ALPHABET[indices[0]] as char);
result.push(ALPHABET[indices[1]] as char);
result.push(if chunk.len() > 1 {
ALPHABET[indices[2]] as char
} else {
'='
});
result.push(if chunk.len() > 2 {
ALPHABET[indices[3]] as char
} else {
'='
});
}
result
}
/// Available Nix channels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NixChannel {
// === Unstable ===
/// NixOS unstable (rolling release)
NixosUnstable,
/// nixpkgs unstable (for non-NixOS systems)
NixpkgsUnstable,
// === Stable ===
/// NixOS 24.11 (latest stable)
Nixos2411,
/// NixOS 24.05
Nixos2405,
/// NixOS 23.11
Nixos2311,
// === Darwin ===
/// nixpkgs-unstable for macOS
NixpkgsDarwinUnstable,
}
impl NixChannel {
/// Get the Elasticsearch index pattern for this channel.
fn index_pattern(&self) -> &'static str {
match self {
Self::NixosUnstable => "latest-*-nixos-unstable",
Self::NixpkgsUnstable => "latest-*-nixpkgs-unstable",
Self::Nixos2411 => "latest-*-nixos-24.11",
Self::Nixos2405 => "latest-*-nixos-24.05",
Self::Nixos2311 => "latest-*-nixos-23.11",
Self::NixpkgsDarwinUnstable => "latest-*-nixpkgs-unstable",
}
}
/// Get the channel name for tagging.
pub fn name(&self) -> &'static str {
match self {
Self::NixosUnstable => "nixos-unstable",
Self::NixpkgsUnstable => "nixpkgs-unstable",
Self::Nixos2411 => "nixos-24.11",
Self::Nixos2405 => "nixos-24.05",
Self::Nixos2311 => "nixos-23.11",
Self::NixpkgsDarwinUnstable => "nixpkgs-darwin-unstable",
}
}
/// All available channels.
pub fn all() -> &'static [NixChannel] {
&[
Self::NixosUnstable,
Self::NixpkgsUnstable,
Self::Nixos2411,
Self::Nixos2405,
Self::Nixos2311,
]
}
/// Unstable channels only.
pub fn unstable() -> &'static [NixChannel] {
&[Self::NixosUnstable, Self::NixpkgsUnstable]
}
/// Stable channels only.
pub fn stable() -> &'static [NixChannel] {
&[Self::Nixos2411, Self::Nixos2405, Self::Nixos2311]
}
/// NixOS channels only (not plain nixpkgs).
pub fn nixos() -> &'static [NixChannel] {
&[
Self::NixosUnstable,
Self::Nixos2411,
Self::Nixos2405,
Self::Nixos2311,
]
}
/// Latest stable only.
pub fn latest_stable() -> &'static [NixChannel] {
&[Self::Nixos2411]
}
}
/// Nix package index fetcher with configurable channels.
pub struct Nix {
channels: Vec<NixChannel>,
}
impl Nix {
/// NixOS Elasticsearch-based search API (Bonsai cluster).
/// Public credentials from nixos-search repository.
const NIXOS_SEARCH: &'static str =
"https://nixos-search-7-1733963800.us-east-1.bonsaisearch.net";
const AUTH_USER: &'static str = "aWVSALXpZv";
const AUTH_PASS: &'static str = "X8gPHnzL52wFEekuxsfQ9cSh";
/// Create a fetcher with all channels.
pub fn all() -> Self {
Self {
channels: NixChannel::all().to_vec(),
}
}
/// Create a fetcher with unstable channels only.
pub fn unstable() -> Self {
Self {
channels: NixChannel::unstable().to_vec(),
}
}
/// Create a fetcher with stable channels only.
pub fn stable() -> Self {
Self {
channels: NixChannel::stable().to_vec(),
}
}
/// Create a fetcher with NixOS channels only.
pub fn nixos() -> Self {
Self {
channels: NixChannel::nixos().to_vec(),
}
}
/// Create a fetcher with latest stable only.
pub fn latest_stable() -> Self {
Self {
channels: NixChannel::latest_stable().to_vec(),
}
}
/// Create a fetcher with custom channel selection.
pub fn with_channels(channels: &[NixChannel]) -> Self {
Self {
channels: channels.to_vec(),
}
}
/// Get authorization header value.
fn auth_header() -> String {
format!(
"Basic {}",
base64_encode(&format!("{}:{}", Self::AUTH_USER, Self::AUTH_PASS))
)
}
/// Fetch a package from a specific channel.
fn fetch_from_channel(name: &str, channel: NixChannel) -> Result<PackageMeta, IndexError> {
let query = serde_json::json!({
"query": {
"bool": {
"must": [
{ "term": { "type": "package" } },
{ "term": { "package_attr_name": name } }
]
}
},
"size": 1
});
let response: serde_json::Value = ureq::post(&format!(
"{}/{}/_search",
Self::NIXOS_SEARCH,
channel.index_pattern()
))
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set("Authorization", &Self::auth_header())
.send_json(&query)?
.into_json()?;
let hits = response["hits"]["hits"]
.as_array()
.ok_or_else(|| IndexError::Parse("missing hits".into()))?;
let hit = hits
.first()
.ok_or_else(|| IndexError::NotFound(name.to_string()))?;
let source = &hit["_source"];
let mut extra = HashMap::new();
// Tag with source channel
extra.insert(
"source_repo".to_string(),
serde_json::Value::String(channel.name().to_string()),
);
Ok(PackageMeta {
name: source["package_attr_name"]
.as_str()
.unwrap_or(name)
.to_string(),
version: source["package_version"]
.as_str()
.unwrap_or("unknown")
.to_string(),
description: source["package_description"].as_str().map(String::from),
homepage: source["package_homepage"]
.as_array()
.and_then(|a| a.first())
.and_then(|u| u.as_str())
.map(String::from),
repository: extract_repo(&source["package_homepage"]),
license: source["package_license"]
.as_array()
.and_then(|a| a.first())
.and_then(|l| l["fullName"].as_str().or_else(|| l.as_str()))
.map(String::from),
binaries: source["package_programs"]
.as_array()
.map(|progs| {
progs
.iter()
.filter_map(|p| p.as_str().map(String::from))
.collect()
})
.unwrap_or_default(),
maintainers: source["package_maintainers"]
.as_array()
.map(|m| {
m.iter()
.filter_map(|p| {
p["name"]
.as_str()
.or_else(|| p["github"].as_str())
.map(String::from)
})
.collect()
})
.unwrap_or_default(),
keywords: Vec::new(),
published: None,
downloads: None,
archive_url: None,
checksum: None,
extra,
})
}
/// Search in a specific channel.
fn search_channel(query: &str, channel: NixChannel) -> Result<Vec<PackageMeta>, IndexError> {
let es_query = serde_json::json!({
"query": {
"bool": {
"must": [
{ "term": { "type": "package" } },
{
"multi_match": {
"query": query,
"fields": [
"package_attr_name^3",
"package_pname^2",
"package_description"
]
}
}
]
}
},
"size": 50
});
let response: serde_json::Value = ureq::post(&format!(
"{}/{}/_search",
Self::NIXOS_SEARCH,
channel.index_pattern()
))
.set("Content-Type", "application/json")
.set("Accept", "application/json")
.set("Authorization", &Self::auth_header())
.send_json(&es_query)?
.into_json()?;
let hits = response["hits"]["hits"]
.as_array()
.ok_or_else(|| IndexError::Parse("missing hits".into()))?;
Ok(hits
.iter()
.filter_map(|hit| {
let source = &hit["_source"];
let mut extra = HashMap::new();
extra.insert(
"source_repo".to_string(),
serde_json::Value::String(channel.name().to_string()),
);
Some(PackageMeta {
name: source["package_attr_name"].as_str()?.to_string(),
version: source["package_version"]
.as_str()
.unwrap_or("unknown")
.to_string(),
description: source["package_description"].as_str().map(String::from),
homepage: source["package_homepage"]
.as_array()
.and_then(|a| a.first())
.and_then(|u| u.as_str())
.map(String::from),
repository: extract_repo(&source["package_homepage"]),
license: source["package_license"]
.as_array()
.and_then(|a| a.first())
.and_then(|l| l["fullName"].as_str().or_else(|| l.as_str()))
.map(String::from),
binaries: source["package_programs"]
.as_array()
.map(|progs| {
progs
.iter()
.filter_map(|p| p.as_str().map(String::from))
.collect()
})
.unwrap_or_default(),
maintainers: source["package_maintainers"]
.as_array()
.map(|m| {
m.iter()
.filter_map(|p| {
p["name"]
.as_str()
.or_else(|| p["github"].as_str())
.map(String::from)
})
.collect()
})
.unwrap_or_default(),
keywords: Vec::new(),
published: None,
downloads: None,
archive_url: None,
checksum: None,
extra,
})
})
.collect())
}
}
impl PackageIndex for Nix {
fn ecosystem(&self) -> &'static str {
"nix"
}
fn display_name(&self) -> &'static str {
"Nixpkgs (Nix/NixOS)"
}
fn fetch(&self, name: &str) -> Result<PackageMeta, IndexError> {
// Try each configured channel until we find the package
for channel in &self.channels {
match Self::fetch_from_channel(name, *channel) {
Ok(pkg) => return Ok(pkg),
Err(IndexError::NotFound(_)) => continue,
Err(e) => return Err(e),
}
}
Err(IndexError::NotFound(name.to_string()))
}
fn fetch_versions(&self, name: &str) -> Result<Vec<VersionMeta>, IndexError> {
// Query all configured channels in parallel
let results: Vec<_> = self
.channels
.par_iter()
.filter_map(|channel| Self::fetch_from_channel(name, *channel).ok())
.collect();
if results.is_empty() {
return Err(IndexError::NotFound(name.to_string()));
}
Ok(results
.into_iter()
.map(|pkg| VersionMeta {
version: pkg.version,
released: None,
yanked: false,
})
.collect())
}
fn search(&self, query: &str) -> Result<Vec<PackageMeta>, IndexError> {
// Search all configured channels in parallel
let results: Vec<_> = self
.channels
.par_iter()
.filter_map(|channel| Self::search_channel(query, *channel).ok())
.flatten()
.collect();
Ok(results)
}
}
fn extract_repo(homepage: &serde_json::Value) -> Option<String> {
homepage.as_array().and_then(|urls| {
urls.iter()
.filter_map(|u| u.as_str())
.find(|u| u.contains("github.com") || u.contains("gitlab.com"))
.map(String::from)
})
}