nixy-rs 0.3.3

Homebrew-style wrapper for Nix using flake.nix
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! Package state management for nixy.
//!
//! This module handles the persistent state of installed packages, stored in `packages.json`.
//! It tracks both standard nixpkgs packages and custom packages from external flakes.
//!
//! The state file uses atomic writes (write to temp file, then rename) to prevent
//! corruption if a write is interrupted.

use std::fs;
use std::path::Path;

use serde::{Deserialize, Serialize};

use crate::error::{Error, Result};

/// Package resolved via Nixhub API with specific nixpkgs commit
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ResolvedNixpkgPackage {
    /// Package name (e.g., "nodejs")
    pub name: String,
    /// Version spec as specified by user (e.g., "20" for semver range)
    /// None means latest, used for upgrade behavior
    #[serde(default)]
    pub version_spec: Option<String>,
    /// Resolved version (e.g., "20.11.0")
    pub resolved_version: String,
    /// Nix attribute path (e.g., "nodejs_20")
    pub attribute_path: String,
    /// nixpkgs commit hash
    pub commit_hash: String,
    /// Platform restrictions (e.g., ["x86_64-darwin", "aarch64-darwin"])
    /// None means all platforms
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub platforms: Option<Vec<String>>,
}

/// Custom package installed from a flake registry
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CustomPackage {
    pub name: String,
    pub input_name: String,
    pub input_url: String,
    pub package_output: String, // e.g., "packages" or "legacyPackages"
    #[serde(default)]
    pub source_name: Option<String>, // The actual package name in the source flake (for aliases)
    /// Platform restrictions (e.g., ["x86_64-darwin", "aarch64-darwin"])
    /// None means all platforms
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub platforms: Option<Vec<String>>,
}

impl CustomPackage {
    /// Get the source package name (falls back to name if not set)
    pub fn source_package_name(&self) -> &str {
        self.source_name.as_deref().unwrap_or(&self.name)
    }
}

/// State file for tracking installed packages.
///
/// The state tracks three categories of packages:
/// - `packages`: Legacy format (v1) - simple package names like "hello", kept for backwards
///   compatibility. These are resolved via the default nixpkgs input.
/// - `resolved_packages`: Nixhub-resolved packages with pinned versions and nixpkgs commits.
///   Supports version syntax like "nodejs@20" with reproducible builds.
/// - `custom_packages`: Packages from custom flake URLs (e.g., github:owner/repo).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackageState {
    pub version: u32,
    /// Legacy: simple package names (version 1 format, kept for backwards compatibility)
    #[serde(default)]
    pub packages: Vec<String>,
    /// Packages resolved via Nixhub with specific versions and commits
    #[serde(default)]
    pub resolved_packages: Vec<ResolvedNixpkgPackage>,
    /// Packages from custom flake URLs
    #[serde(default)]
    pub custom_packages: Vec<CustomPackage>,
}

impl Default for PackageState {
    fn default() -> Self {
        Self {
            version: 2,
            packages: Vec::new(),
            resolved_packages: Vec::new(),
            custom_packages: Vec::new(),
        }
    }
}

impl PackageState {
    /// Load state from a file, or return default if file doesn't exist
    /// Automatically migrates from version 1 to version 2 format
    pub fn load(path: &Path) -> Result<Self> {
        if !path.exists() {
            return Ok(Self::default());
        }

        let content = fs::read_to_string(path)?;
        let mut state: Self =
            serde_json::from_str(&content).map_err(|e| Error::StateFile(e.to_string()))?;

        // Migrate from version 1 to version 2 if needed
        if state.version < 2 {
            state.version = 2;
            // Note: Old packages in state.packages remain as-is for backwards compatibility
            // They will be migrated to resolved_packages when upgraded or re-added
        }

        Ok(state)
    }

    /// Save state to a file atomically
    pub fn save(&self, path: &Path) -> Result<()> {
        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }

        let content =
            serde_json::to_string_pretty(self).map_err(|e| Error::StateFile(e.to_string()))?;

        // Write to a temporary file first, then atomically rename it into place
        let tmp_path = path.with_extension("json.tmp");
        fs::write(&tmp_path, &content)?;
        fs::rename(&tmp_path, path)?;
        Ok(())
    }

    /// Add a standard nixpkgs package (legacy method for backwards compatibility)
    #[allow(dead_code)]
    pub fn add_package(&mut self, name: &str) {
        if !self.packages.contains(&name.to_string()) {
            self.packages.push(name.to_string());
            self.packages.sort();
        }
    }

    /// Add a resolved nixpkgs package (new method with version info)
    pub fn add_resolved_package(&mut self, pkg: ResolvedNixpkgPackage) {
        // Remove from legacy packages if present
        self.packages.retain(|p| p != &pkg.name);
        // Remove any existing resolved package with the same name
        self.resolved_packages.retain(|p| p.name != pkg.name);
        self.resolved_packages.push(pkg);
        self.resolved_packages.sort_by(|a, b| a.name.cmp(&b.name));
    }

    /// Get a resolved package by name
    #[allow(dead_code)]
    pub fn get_resolved_package(&self, name: &str) -> Option<&ResolvedNixpkgPackage> {
        self.resolved_packages.iter().find(|p| p.name == name)
    }

    /// Add a custom package from a registry
    pub fn add_custom_package(&mut self, pkg: CustomPackage) {
        // Remove any existing package with the same name
        self.custom_packages.retain(|p| p.name != pkg.name);
        self.custom_packages.push(pkg);
        self.custom_packages.sort_by(|a, b| a.name.cmp(&b.name));
    }

    /// Remove a package by name (checks legacy, resolved, and custom)
    pub fn remove_package(&mut self, name: &str) -> bool {
        let removed_legacy = self.packages.iter().position(|p| p == name).map(|i| {
            self.packages.remove(i);
            true
        });

        let removed_resolved = self
            .resolved_packages
            .iter()
            .position(|p| p.name == name)
            .map(|i| {
                self.resolved_packages.remove(i);
                true
            });

        let removed_custom = self
            .custom_packages
            .iter()
            .position(|p| p.name == name)
            .map(|i| {
                self.custom_packages.remove(i);
                true
            });

        removed_legacy.unwrap_or(false)
            || removed_resolved.unwrap_or(false)
            || removed_custom.unwrap_or(false)
    }

    /// Check if a package is installed (legacy, resolved, or custom)
    pub fn has_package(&self, name: &str) -> bool {
        self.packages.contains(&name.to_string())
            || self.resolved_packages.iter().any(|p| p.name == name)
            || self.custom_packages.iter().any(|p| p.name == name)
    }

    /// Check if a package is a legacy (non-resolved) package
    #[allow(dead_code)]
    pub fn is_legacy_package(&self, name: &str) -> bool {
        self.packages.contains(&name.to_string())
    }
}

#[cfg(test)]
impl PackageState {
    /// Get all package names (legacy + resolved + custom) - test helper
    pub fn all_package_names(&self) -> Vec<String> {
        let mut names: Vec<String> = self.packages.clone();
        names.extend(self.resolved_packages.iter().map(|p| p.name.clone()));
        names.extend(self.custom_packages.iter().map(|p| p.name.clone()));
        names.sort();
        names.dedup();
        names
    }
}

/// Get the state file path for a profile
pub fn get_state_path(profile_dir: &Path) -> std::path::PathBuf {
    profile_dir.join("packages.json")
}

/// All valid Nix system platforms
pub const VALID_PLATFORMS: &[&str] = &[
    "x86_64-darwin",
    "aarch64-darwin",
    "x86_64-linux",
    "aarch64-linux",
];

/// Platform aliases that expand to multiple platforms
const PLATFORM_ALIASES: &[(&str, &[&str])] = &[
    ("darwin", &["x86_64-darwin", "aarch64-darwin"]),
    ("macos", &["x86_64-darwin", "aarch64-darwin"]),
    ("linux", &["x86_64-linux", "aarch64-linux"]),
];

/// Normalize platform names, expanding aliases like "darwin" to full platform names.
/// Returns an error message if any platform is invalid.
pub fn normalize_platforms(platforms: &[String]) -> std::result::Result<Vec<String>, String> {
    let mut result = Vec::new();
    for p in platforms {
        let p_lower = p.to_lowercase();
        // Check if it's an alias
        if let Some((_, expanded)) = PLATFORM_ALIASES.iter().find(|(alias, _)| *alias == p_lower) {
            for exp in *expanded {
                if !result.contains(&exp.to_string()) {
                    result.push(exp.to_string());
                }
            }
        } else if VALID_PLATFORMS.contains(&p_lower.as_str()) {
            if !result.contains(&p_lower) {
                result.push(p_lower);
            }
        } else {
            return Err(format!(
                "Invalid platform '{}'. Valid platforms: darwin, macos, linux, {}",
                p,
                VALID_PLATFORMS.join(", ")
            ));
        }
    }
    result.sort();
    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_default_state() {
        let state = PackageState::default();
        assert_eq!(state.version, 2);
        assert!(state.packages.is_empty());
        assert!(state.resolved_packages.is_empty());
        assert!(state.custom_packages.is_empty());
    }

    #[test]
    fn test_add_package() {
        let mut state = PackageState::default();
        state.add_package("ripgrep");
        state.add_package("fzf");

        assert_eq!(state.packages.len(), 2);
        assert!(state.has_package("ripgrep"));
        assert!(state.has_package("fzf"));
    }

    #[test]
    fn test_add_package_deduplication() {
        let mut state = PackageState::default();
        state.add_package("ripgrep");
        state.add_package("ripgrep");

        assert_eq!(state.packages.len(), 1);
    }

    #[test]
    fn test_add_package_sorted() {
        let mut state = PackageState::default();
        state.add_package("zsh");
        state.add_package("bat");
        state.add_package("fzf");

        assert_eq!(state.packages, vec!["bat", "fzf", "zsh"]);
    }

    #[test]
    fn test_add_custom_package() {
        let mut state = PackageState::default();
        let pkg = CustomPackage {
            name: "neovim".to_string(),
            input_name: "neovim-nightly".to_string(),
            input_url: "github:nix-community/neovim-nightly-overlay".to_string(),
            package_output: "packages".to_string(),
            source_name: None,
            platforms: None,
        };
        state.add_custom_package(pkg.clone());

        assert_eq!(state.custom_packages.len(), 1);
        assert!(state.has_package("neovim"));
    }

    #[test]
    fn test_add_custom_package_replaces_existing() {
        let mut state = PackageState::default();
        let pkg1 = CustomPackage {
            name: "neovim".to_string(),
            input_name: "neovim-old".to_string(),
            input_url: "github:old/overlay".to_string(),
            package_output: "packages".to_string(),
            source_name: None,
            platforms: None,
        };
        state.add_custom_package(pkg1);

        let pkg2 = CustomPackage {
            name: "neovim".to_string(),
            input_name: "neovim-new".to_string(),
            input_url: "github:new/overlay".to_string(),
            package_output: "packages".to_string(),
            source_name: None,
            platforms: None,
        };
        state.add_custom_package(pkg2);

        assert_eq!(state.custom_packages.len(), 1);
        assert_eq!(state.custom_packages[0].input_name, "neovim-new");
    }

    #[test]
    fn test_remove_package() {
        let mut state = PackageState::default();
        state.add_package("ripgrep");
        state.add_package("fzf");

        assert!(state.remove_package("ripgrep"));
        assert!(!state.has_package("ripgrep"));
        assert!(state.has_package("fzf"));
    }

    #[test]
    fn test_remove_custom_package() {
        let mut state = PackageState::default();
        let pkg = CustomPackage {
            name: "neovim".to_string(),
            input_name: "neovim-nightly".to_string(),
            input_url: "github:nix-community/neovim-nightly-overlay".to_string(),
            package_output: "packages".to_string(),
            source_name: None,
            platforms: None,
        };
        state.add_custom_package(pkg);

        assert!(state.remove_package("neovim"));
        assert!(!state.has_package("neovim"));
    }

    #[test]
    fn test_remove_nonexistent_package() {
        let mut state = PackageState::default();
        assert!(!state.remove_package("nonexistent"));
    }

    #[test]
    fn test_all_package_names() {
        let mut state = PackageState::default();
        state.add_package("ripgrep");
        state.add_custom_package(CustomPackage {
            name: "neovim".to_string(),
            input_name: "neovim-nightly".to_string(),
            input_url: "github:nix-community/neovim-nightly-overlay".to_string(),
            package_output: "packages".to_string(),
            source_name: None,
            platforms: None,
        });

        let names = state.all_package_names();
        assert_eq!(names, vec!["neovim", "ripgrep"]);
    }

    #[test]
    fn test_save_and_load() {
        let temp = TempDir::new().unwrap();
        let path = temp.path().join("packages.json");

        let mut state = PackageState::default();
        state.add_package("ripgrep");
        state.add_custom_package(CustomPackage {
            name: "neovim".to_string(),
            input_name: "neovim-nightly".to_string(),
            input_url: "github:nix-community/neovim-nightly-overlay".to_string(),
            package_output: "packages".to_string(),
            source_name: None,
            platforms: None,
        });

        state.save(&path).unwrap();

        let loaded = PackageState::load(&path).unwrap();
        assert_eq!(loaded.packages, state.packages);
        assert_eq!(loaded.custom_packages.len(), state.custom_packages.len());
        assert_eq!(
            loaded.custom_packages[0].name,
            state.custom_packages[0].name
        );
    }

    #[test]
    fn test_load_nonexistent_returns_default() {
        let temp = TempDir::new().unwrap();
        let path = temp.path().join("nonexistent.json");

        let state = PackageState::load(&path).unwrap();
        assert!(state.packages.is_empty());
        assert!(state.resolved_packages.is_empty());
        assert!(state.custom_packages.is_empty());
    }

    #[test]
    fn test_add_resolved_package() {
        let mut state = PackageState::default();
        let pkg = ResolvedNixpkgPackage {
            name: "nodejs".to_string(),
            version_spec: Some("20".to_string()),
            resolved_version: "20.11.0".to_string(),
            attribute_path: "nodejs_20".to_string(),
            commit_hash: "abc123".to_string(),
            platforms: None,
        };
        state.add_resolved_package(pkg.clone());

        assert_eq!(state.resolved_packages.len(), 1);
        assert!(state.has_package("nodejs"));
        assert_eq!(
            state
                .get_resolved_package("nodejs")
                .unwrap()
                .resolved_version,
            "20.11.0"
        );
    }

    #[test]
    fn test_add_resolved_package_removes_legacy() {
        let mut state = PackageState::default();
        state.add_package("nodejs");
        assert!(state.packages.contains(&"nodejs".to_string()));

        let pkg = ResolvedNixpkgPackage {
            name: "nodejs".to_string(),
            version_spec: Some("20".to_string()),
            resolved_version: "20.11.0".to_string(),
            attribute_path: "nodejs_20".to_string(),
            commit_hash: "abc123".to_string(),
            platforms: None,
        };
        state.add_resolved_package(pkg);

        // Legacy should be removed
        assert!(!state.packages.contains(&"nodejs".to_string()));
        // But resolved should exist
        assert_eq!(state.resolved_packages.len(), 1);
        assert!(state.has_package("nodejs"));
    }

    #[test]
    fn test_remove_resolved_package() {
        let mut state = PackageState::default();
        let pkg = ResolvedNixpkgPackage {
            name: "nodejs".to_string(),
            version_spec: Some("20".to_string()),
            resolved_version: "20.11.0".to_string(),
            attribute_path: "nodejs_20".to_string(),
            commit_hash: "abc123".to_string(),
            platforms: None,
        };
        state.add_resolved_package(pkg);

        assert!(state.remove_package("nodejs"));
        assert!(!state.has_package("nodejs"));
        assert!(state.resolved_packages.is_empty());
    }

    #[test]
    fn test_is_legacy_package() {
        let mut state = PackageState::default();
        state.add_package("legacy-pkg");
        state.add_resolved_package(ResolvedNixpkgPackage {
            name: "resolved-pkg".to_string(),
            version_spec: None,
            resolved_version: "1.0.0".to_string(),
            attribute_path: "resolved-pkg".to_string(),
            commit_hash: "abc123".to_string(),
            platforms: None,
        });

        assert!(state.is_legacy_package("legacy-pkg"));
        assert!(!state.is_legacy_package("resolved-pkg"));
    }

    #[test]
    fn test_migration_from_v1() {
        let temp = TempDir::new().unwrap();
        let path = temp.path().join("packages.json");

        // Write a v1 state file
        let v1_content = r#"{"version":1,"packages":["ripgrep","fzf"],"custom_packages":[]}"#;
        fs::write(&path, v1_content).unwrap();

        // Load should migrate to v2
        let state = PackageState::load(&path).unwrap();
        assert_eq!(state.version, 2);
        // Legacy packages should be preserved
        assert_eq!(state.packages, vec!["ripgrep", "fzf"]);
        assert!(state.resolved_packages.is_empty());
    }

    #[test]
    fn test_normalize_platforms_darwin_alias() {
        let result = normalize_platforms(&["darwin".to_string()]).unwrap();
        assert_eq!(result, vec!["aarch64-darwin", "x86_64-darwin"]);
    }

    #[test]
    fn test_normalize_platforms_macos_alias() {
        let result = normalize_platforms(&["macos".to_string()]).unwrap();
        assert_eq!(result, vec!["aarch64-darwin", "x86_64-darwin"]);
    }

    #[test]
    fn test_normalize_platforms_linux_alias() {
        let result = normalize_platforms(&["linux".to_string()]).unwrap();
        assert_eq!(result, vec!["aarch64-linux", "x86_64-linux"]);
    }

    #[test]
    fn test_normalize_platforms_full_name() {
        let result = normalize_platforms(&["x86_64-darwin".to_string()]).unwrap();
        assert_eq!(result, vec!["x86_64-darwin"]);
    }

    #[test]
    fn test_normalize_platforms_case_insensitive() {
        let result = normalize_platforms(&["Darwin".to_string()]).unwrap();
        assert_eq!(result, vec!["aarch64-darwin", "x86_64-darwin"]);

        let result = normalize_platforms(&["X86_64-LINUX".to_string()]).unwrap();
        assert_eq!(result, vec!["x86_64-linux"]);
    }

    #[test]
    fn test_normalize_platforms_dedup() {
        let result =
            normalize_platforms(&["darwin".to_string(), "x86_64-darwin".to_string()]).unwrap();
        assert_eq!(result, vec!["aarch64-darwin", "x86_64-darwin"]);
    }

    #[test]
    fn test_normalize_platforms_invalid() {
        let result = normalize_platforms(&["windows".to_string()]);
        assert!(result.is_err());
        let err_msg = result.unwrap_err();
        assert!(err_msg.contains("Invalid platform"));
    }

    #[test]
    fn test_normalize_platforms_mixed() {
        let result =
            normalize_platforms(&["darwin".to_string(), "x86_64-linux".to_string()]).unwrap();
        assert_eq!(
            result,
            vec!["aarch64-darwin", "x86_64-darwin", "x86_64-linux"]
        );
    }
}