cuenv-tools-nix 0.26.2

Nix flake tool provider for cuenv
Documentation
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
//! Nix flake tool provider for cuenv.
//!
//! Provides Nix-based tool resolution using flakes. Tools are installed into
//! per-project Nix profiles (managed by the `profile` module) rather than
//! copying individual binaries, ensuring full Nix closures are available.

pub mod commands;
pub mod profile;

use async_trait::async_trait;
use cuenv_core::Result;
use cuenv_core::tools::{
    FetchedTool, Platform, ResolvedTool, ToolOptions, ToolProvider, ToolSource,
};
use std::path::PathBuf;
use std::process::Stdio;
use tokio::process::Command;
use tracing::{debug, info};

/// Tool provider for Nix flakes.
///
/// Resolves tools from Nix flakes. Actual installation into per-project
/// Nix profiles is handled by the `profile` module during sync.
pub struct NixToolProvider {
    /// Named flake references (e.g., "stable" -> "github:NixOS/nixpkgs/nixos-24.11").
    flakes: std::collections::HashMap<String, String>,
}

impl Default for NixToolProvider {
    fn default() -> Self {
        Self::new()
    }
}

impl NixToolProvider {
    /// Create a new Nix tool provider.
    #[must_use]
    pub fn new() -> Self {
        Self {
            flakes: std::collections::HashMap::new(),
        }
    }

    /// Create a Nix tool provider with named flake references.
    #[must_use]
    pub fn with_flakes(flakes: std::collections::HashMap<String, String>) -> Self {
        Self { flakes }
    }

    /// Resolve a flake name to its full reference.
    fn resolve_flake(&self, name: &str) -> Option<&str> {
        self.flakes.get(name).map(String::as_str)
    }

    /// Check if nix is available.
    async fn check_nix_available() -> bool {
        Command::new("nix")
            .arg("--version")
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .await
            .map(|s| s.success())
            .unwrap_or(false)
    }
}

#[async_trait]
impl ToolProvider for NixToolProvider {
    fn name(&self) -> &'static str {
        "nix"
    }

    fn description(&self) -> &'static str {
        "Build tools from Nix flakes"
    }

    fn can_handle(&self, source: &ToolSource) -> bool {
        matches!(source, ToolSource::Nix { .. })
    }

    async fn resolve(
        &self,
        tool_name: &str,
        version: &str,
        platform: &Platform,
        config: &serde_json::Value,
    ) -> Result<ResolvedTool> {
        let flake_name = config
            .get("flake")
            .and_then(|v| v.as_str())
            .ok_or_else(|| cuenv_core::Error::tool_resolution("Missing 'flake' in config"))?;

        let package = config
            .get("package")
            .and_then(|v| v.as_str())
            .ok_or_else(|| cuenv_core::Error::tool_resolution("Missing 'package' in config"))?;

        let output = config.get("output").and_then(|v| v.as_str());

        info!(%tool_name, %flake_name, %package, %version, %platform, "Resolving Nix package");

        // Check if nix is available
        if !Self::check_nix_available().await {
            return Err(cuenv_core::Error::tool_resolution(
                "Nix is not installed or not in PATH".to_string(),
            ));
        }

        // Resolve flake name to full reference
        let flake_ref = self.resolve_flake(flake_name).ok_or_else(|| {
            cuenv_core::Error::tool_resolution(format!(
                "Unknown flake '{}'. Add it to runtime.flakes in your config.",
                flake_name
            ))
        })?;

        debug!(%flake_name, %flake_ref, %package, "Resolved flake reference");

        Ok(ResolvedTool {
            name: tool_name.to_string(),
            version: version.to_string(),
            platform: platform.clone(),
            source: ToolSource::Nix {
                flake: flake_ref.to_string(),
                package: package.to_string(),
                output: output.map(String::from),
            },
        })
    }

    async fn fetch(&self, resolved: &ResolvedTool, _options: &ToolOptions) -> Result<FetchedTool> {
        let ToolSource::Nix { flake, package, .. } = &resolved.source else {
            return Err(cuenv_core::Error::tool_resolution(
                "NixToolProvider received non-Nix source".to_string(),
            ));
        };

        // For Nix, we don't fetch/cache binaries - profiles handle this.
        // Just validate the package exists by checking if it can be evaluated.
        let flake_ref = format!("{flake}#{package}");
        debug!(%flake_ref, "Validating Nix package exists");

        // Use nix eval to quickly check if the package exists (no build required)
        let output = Command::new("nix")
            .args(["eval", "--raw", &format!("{flake_ref}.name")])
            .output()
            .await
            .map_err(|e| cuenv_core::Error::tool_resolution(format!("Failed to run nix: {e}")))?;

        if !output.status.success() {
            // Package might not have a .name attribute, try just evaluating the derivation
            let output2 = Command::new("nix")
                .args(["eval", "--raw", &format!("{flake_ref}.type")])
                .output()
                .await
                .map_err(|e| {
                    cuenv_core::Error::tool_resolution(format!("Failed to run nix: {e}"))
                })?;

            if !output2.status.success() {
                return Err(cuenv_core::Error::tool_resolution(format!(
                    "Nix package {flake_ref} not found or cannot be evaluated"
                )));
            }
        }

        info!(tool = %resolved.name, %flake_ref, "Validated Nix package");

        // Return placeholder - actual binary comes from profile
        Ok(FetchedTool {
            name: resolved.name.clone(),
            binary_path: PathBuf::new(), // Profile manages the actual path
            sha256: "nix-profile-managed".to_string(),
        })
    }

    fn is_cached(&self, _resolved: &ResolvedTool, _options: &ToolOptions) -> bool {
        // Nix tools are always "cached" via the Nix store.
        // Profile installation is handled by profile::ensure_profile() during sync.
        true
    }

    async fn check_prerequisites(&self) -> Result<()> {
        let output = Command::new("nix")
            .arg("--version")
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .await;

        match output {
            Ok(status) if status.success() => Ok(()),
            Ok(_) => Err(cuenv_core::Error::tool_resolution_with_help(
                "Nix command failed",
                "Install Nix: https://nixos.org/download.html",
            )),
            Err(_) => Err(cuenv_core::Error::tool_resolution_with_help(
                "Nix is not installed",
                "Install Nix: https://nixos.org/download.html",
            )),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use cuenv_core::tools::{Arch, Os};

    #[test]
    fn test_provider_name() {
        let provider = NixToolProvider::new();
        assert_eq!(provider.name(), "nix");
    }

    #[test]
    fn test_provider_description() {
        let provider = NixToolProvider::new();
        assert_eq!(provider.description(), "Build tools from Nix flakes");
    }

    #[test]
    fn test_provider_default() {
        let provider = NixToolProvider::default();
        assert_eq!(provider.name(), "nix");
        // Default provider has no flakes registered
        assert!(provider.resolve_flake("stable").is_none());
    }

    #[test]
    fn test_provider_new_empty_flakes() {
        let provider = NixToolProvider::new();
        assert!(provider.resolve_flake("anything").is_none());
    }

    #[test]
    fn test_resolve_flake() {
        let mut flakes = std::collections::HashMap::new();
        flakes.insert(
            "stable".to_string(),
            "github:NixOS/nixpkgs/nixos-24.11".to_string(),
        );
        flakes.insert(
            "unstable".to_string(),
            "github:NixOS/nixpkgs/nixos-unstable".to_string(),
        );

        let provider = NixToolProvider::with_flakes(flakes);

        assert_eq!(
            provider.resolve_flake("stable"),
            Some("github:NixOS/nixpkgs/nixos-24.11")
        );
        assert_eq!(
            provider.resolve_flake("unstable"),
            Some("github:NixOS/nixpkgs/nixos-unstable")
        );
        assert_eq!(provider.resolve_flake("unknown"), None);
    }

    #[test]
    fn test_resolve_flake_with_custom_flakes() {
        let mut flakes = std::collections::HashMap::new();
        flakes.insert(
            "my-tools".to_string(),
            "github:owner/my-tools-flake".to_string(),
        );
        flakes.insert("local".to_string(), "path:./flake".to_string());

        let provider = NixToolProvider::with_flakes(flakes);

        assert_eq!(
            provider.resolve_flake("my-tools"),
            Some("github:owner/my-tools-flake")
        );
        assert_eq!(provider.resolve_flake("local"), Some("path:./flake"));
    }

    #[test]
    fn test_can_handle() {
        let provider = NixToolProvider::new();

        let nix_source = ToolSource::Nix {
            flake: "github:NixOS/nixpkgs".into(),
            package: "jq".into(),
            output: None,
        };
        assert!(provider.can_handle(&nix_source));

        let github_source = ToolSource::GitHub {
            repo: "jqlang/jq".into(),
            tag: "v1.7.1".into(),
            asset: "jq-linux-amd64".into(),
            path: None,
        };
        assert!(!provider.can_handle(&github_source));
    }

    #[test]
    fn test_can_handle_nix_with_output() {
        let provider = NixToolProvider::new();

        let nix_source = ToolSource::Nix {
            flake: "github:NixOS/nixpkgs".into(),
            package: "hello".into(),
            output: Some("out".to_string()),
        };
        assert!(provider.can_handle(&nix_source));
    }

    #[test]
    fn test_can_handle_rustup_source() {
        let provider = NixToolProvider::new();

        let rustup_source = ToolSource::Rustup {
            toolchain: "stable".into(),
            profile: None,
            components: vec![],
            targets: vec![],
        };
        assert!(!provider.can_handle(&rustup_source));
    }

    #[test]
    fn test_can_handle_oci_source() {
        let provider = NixToolProvider::new();

        let oci_source = ToolSource::Oci {
            image: "alpine:latest".into(),
            path: "alpine".into(),
        };
        assert!(!provider.can_handle(&oci_source));
    }

    #[test]
    fn test_is_cached_always_true() {
        let provider = NixToolProvider::new();
        let options = ToolOptions::new();

        // Create a resolved tool with Nix source
        let resolved = ResolvedTool {
            name: "jq".to_string(),
            version: "1.7.1".to_string(),
            platform: Platform::new(Os::Darwin, Arch::Arm64),
            source: ToolSource::Nix {
                flake: "github:NixOS/nixpkgs".to_string(),
                package: "jq".to_string(),
                output: None,
            },
        };

        // Nix tools are always considered cached
        assert!(provider.is_cached(&resolved, &options));
    }

    #[test]
    fn test_is_cached_non_nix_source() {
        let provider = NixToolProvider::new();
        let options = ToolOptions::new();

        // Create a resolved tool with GitHub source
        let resolved = ResolvedTool {
            name: "mytool".to_string(),
            version: "1.0.0".to_string(),
            platform: Platform::new(Os::Linux, Arch::X86_64),
            source: ToolSource::GitHub {
                repo: "owner/repo".to_string(),
                tag: "v1.0.0".to_string(),
                asset: "file.zip".to_string(),
                path: None,
            },
        };

        // Even non-Nix sources return true since we just return true unconditionally
        assert!(provider.is_cached(&resolved, &options));
    }

    #[test]
    fn test_with_flakes_empty() {
        let provider = NixToolProvider::with_flakes(std::collections::HashMap::new());
        assert!(provider.resolve_flake("any").is_none());
    }

    #[test]
    fn test_with_flakes_single() {
        let mut flakes = std::collections::HashMap::new();
        flakes.insert("nixpkgs".to_string(), "github:NixOS/nixpkgs".to_string());

        let provider = NixToolProvider::with_flakes(flakes);
        assert_eq!(
            provider.resolve_flake("nixpkgs"),
            Some("github:NixOS/nixpkgs")
        );
    }

    #[test]
    fn test_with_flakes_overwrite() {
        let mut flakes = std::collections::HashMap::new();
        flakes.insert("stable".to_string(), "first-value".to_string());
        flakes.insert("stable".to_string(), "second-value".to_string());

        let provider = NixToolProvider::with_flakes(flakes);
        // HashMap uses last inserted value
        assert_eq!(provider.resolve_flake("stable"), Some("second-value"));
    }
}