nixy-rs 0.1.0

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
use std::fs;
use std::path::Path;
use std::process::Command;

use regex::Regex;

use crate::cli::InstallArgs;
use crate::config::Config;
use crate::error::{Error, Result};
use crate::flake::editor::insert_after_marker;
use crate::flake::parser::parse_local_package_attr;
use crate::flake::template::{generate_flake, has_custom_modifications, PreservedContent};
use crate::flake::{is_flake_file, is_nixy_managed};
use crate::nix::Nix;
use crate::profile::get_flake_dir;

use super::{info, success, warn};

pub fn run(config: &Config, args: InstallArgs) -> Result<()> {
    // Handle --file option
    if let Some(file) = args.file {
        return install_from_file(config, &file, args.force);
    }

    // Handle --from option
    if let Some(from) = args.from {
        let pkg = args
            .package
            .ok_or_else(|| Error::Usage("Package name is required with --from".to_string()))?;
        return install_from_registry(config, &from, &pkg);
    }

    // Standard nixpkgs install
    let pkg = args.package.ok_or_else(|| {
        Error::Usage(
            "Usage: nixy install <package> or nixy install --file <path> or nixy install --from <registry> <package>".to_string(),
        )
    })?;

    // Validate package exists in nixpkgs
    info(&format!("Validating package {}...", pkg));
    if !Nix::validate_package(&pkg)? {
        return Err(Error::PackageNotFound(pkg));
    }

    // Check if existing flake.nix is nixy-managed
    let flake_dir = get_flake_dir(config)?;
    let flake_path = flake_dir.join("flake.nix");

    if flake_path.exists() && !is_nixy_managed(&flake_path) {
        return Err(Error::NotNixyManaged);
    }

    // Add package to flake.nix
    add_package_to_flake(config, &pkg)?;

    info(&format!("Installing {}...", pkg));
    super::sync::run(config)?;

    Ok(())
}

/// Add a package to flake.nix
fn add_package_to_flake(config: &Config, pkg: &str) -> Result<()> {
    let flake_dir = get_flake_dir(config)?;
    let flake_path = flake_dir.join("flake.nix");

    // Check if existing flake.nix is nixy-managed
    if flake_path.exists() && !is_nixy_managed(&flake_path) {
        return Err(Error::NotNixyManaged);
    }

    // If flake doesn't exist, create it
    if !flake_path.exists() {
        fs::create_dir_all(&flake_dir)?;
        let content = generate_flake(&[pkg.to_string()], Some(&flake_dir), None);
        fs::write(&flake_path, content)?;
        success(&format!("Added {} to {}", pkg, flake_path.display()));
        return Ok(());
    }

    // Check if package already exists
    let content = fs::read_to_string(&flake_path)?;
    let pattern = format!(
        r"^\s*{} = pkgs\.{};",
        regex::escape(pkg),
        regex::escape(pkg)
    );
    if Regex::new(&pattern)?.is_match(&content) {
        success(&format!(
            "Package {} already in {}",
            pkg,
            flake_path.display()
        ));
        return Ok(());
    }

    // Partial edit: insert package into existing flake.nix
    let pkg_entry = format!("          {} = pkgs.{};", pkg, pkg);
    let content = insert_after_marker(&content, "nixy:packages", &pkg_entry);

    let path_entry = format!("              {}", pkg);
    let content = insert_after_marker(&content, "nixy:env-paths", &path_entry);

    fs::write(&flake_path, content)?;
    success(&format!("Added {} to {}", pkg, flake_path.display()));

    Ok(())
}

/// Install from a flake registry or direct URL
fn install_from_registry(config: &Config, from_arg: &str, pkg: &str) -> Result<()> {
    let flake_url = if from_arg.contains(':') {
        // Direct flake URL
        info(&format!("Using flake URL: {}", from_arg));
        from_arg.to_string()
    } else {
        // Registry lookup
        info(&format!("Looking up '{}' in nix registry...", from_arg));
        let url = Nix::registry_lookup(from_arg)?
            .ok_or_else(|| Error::RegistryNotFound(from_arg.to_string()))?;
        info(&format!("Found: {}", url));
        url
    };

    // Derive input name
    let input_name = if !from_arg.contains(':') {
        // Use registry alias
        sanitize_input_name(from_arg)
    } else {
        // Derive from URL (owner-repo)
        derive_input_name_from_url(&flake_url)
    };

    // Validate the package exists
    info(&format!(
        "Validating package '{}' in {}...",
        pkg, input_name
    ));
    let pkg_output = Nix::validate_flake_package(&flake_url, pkg)?.ok_or_else(|| {
        let available = Nix::list_flake_packages(&flake_url, None)
            .unwrap_or_default()
            .into_iter()
            .take(10)
            .collect::<Vec<_>>()
            .join(" ");
        if available.is_empty() {
            Error::FlakePackageNotFound(pkg.to_string(), input_name.clone())
        } else {
            Error::Usage(format!(
                "Package '{}' not found in '{}'. Available packages: {}...",
                pkg, input_name, available
            ))
        }
    })?;

    // Get or create flake
    let flake_dir = get_flake_dir(config)?;
    let flake_path = flake_dir.join("flake.nix");

    if !flake_path.exists() {
        fs::create_dir_all(&flake_dir)?;
        let content = generate_flake(&[], Some(&flake_dir), None);
        fs::write(&flake_path, content)?;
    }

    if !is_nixy_managed(&flake_path) {
        return Err(Error::NotNixyManaged);
    }

    // Add the flake as an input and the package
    add_registry_package_to_flake(config, &input_name, &flake_url, pkg, &pkg_output)?;

    info(&format!("Installing {} from {}...", pkg, input_name));
    super::sync::run(config)?;

    Ok(())
}

/// Add a package from a registry flake to flake.nix
fn add_registry_package_to_flake(
    config: &Config,
    input_name: &str,
    flake_url: &str,
    pkg: &str,
    pkg_output: &str,
) -> Result<()> {
    let flake_dir = get_flake_dir(config)?;
    let flake_path = flake_dir.join("flake.nix");
    let mut content = fs::read_to_string(&flake_path)?;

    // Check if we should reuse existing nixpkgs input
    let (final_input_name, use_existing_nixpkgs) =
        if flake_url.contains("NixOS/nixpkgs") && content.contains("nixpkgs.url") {
            info("Using existing nixpkgs input");
            ("nixpkgs".to_string(), true)
        } else {
            (input_name.to_string(), false)
        };

    // Add input if needed
    if !use_existing_nixpkgs {
        let input_pattern = format!(r"^\s*{}\.url", regex::escape(&final_input_name));
        if !Regex::new(&input_pattern)?.is_match(&content) {
            let input_entry = format!("    {}.url = \"{}\";", final_input_name, flake_url);
            content = insert_after_marker(&content, "nixy:custom-inputs", &input_entry);
            success(&format!("Added input '{}' to flake.nix", final_input_name));
        } else {
            info(&format!(
                "Input '{}' already exists in flake.nix",
                final_input_name
            ));
        }
    }

    // Check if package already exists
    let pkg_pattern = format!(r"^\s*{} = ", regex::escape(pkg));
    if Regex::new(&pkg_pattern)?.is_match(&content) {
        success(&format!("Package '{}' already in flake.nix", pkg));
        fs::write(&flake_path, content)?;
        return Ok(());
    }

    // Add package
    let pkg_entry = format!(
        "          {} = inputs.{}.{}.${{system}}.{};",
        pkg, final_input_name, pkg_output, pkg
    );
    content = insert_after_marker(&content, "nixy:custom-packages", &pkg_entry);

    // Add to env-paths
    let path_entry = format!("              {}", pkg);
    content = insert_after_marker(&content, "nixy:custom-paths", &path_entry);

    fs::write(&flake_path, content)?;
    success(&format!(
        "Added {} from {} to flake.nix",
        pkg, final_input_name
    ));

    Ok(())
}

/// Install from a local nix file
fn install_from_file(config: &Config, file: &Path, force: bool) -> Result<()> {
    if !file.exists() {
        return Err(Error::FileNotFound(file.display().to_string()));
    }

    // Check if this is a flake file
    if is_flake_file(file) {
        return install_from_flake_file(config, file, force);
    }

    // Extract package name
    let content = fs::read_to_string(file)?;
    let pkg_name = parse_local_package_attr(&content, "pname")
        .or_else(|| parse_local_package_attr(&content, "name"))
        .ok_or_else(|| Error::NoPackageName(file.display().to_string()))?;

    info(&format!(
        "Installing local package: {} from {}",
        pkg_name,
        file.display()
    ));

    let flake_dir = get_flake_dir(config)?;
    let flake_path = flake_dir.join("flake.nix");

    // Check if existing flake.nix is nixy-managed
    if flake_path.exists() && !is_nixy_managed(&flake_path) {
        return Err(Error::NotNixyManaged);
    }

    // Check for custom modifications
    if flake_path.exists() {
        let packages = Nix::eval_packages(&flake_dir)?;
        if has_custom_modifications(&flake_path, &packages, &flake_dir) {
            if !force {
                warn("flake.nix has modifications outside nixy markers.");
                warn("Use --force to proceed (custom changes will be lost).");
                return Err(Error::CustomModifications);
            }
            warn("Proceeding with --force: custom modifications outside markers will be lost.");
        }
    }

    // Create packages directory
    let pkg_dir = flake_dir.join("packages");
    fs::create_dir_all(&pkg_dir)?;

    // Copy file to packages directory
    let dest = pkg_dir.join(format!("{}.nix", pkg_name));
    fs::copy(file, &dest)?;
    success(&format!("Copied package definition to {}", dest.display()));

    // Add to git if in a git repo
    git_add(&flake_dir, &format!("packages/{}.nix", pkg_name));

    // Regenerate flake.nix
    let packages = Nix::eval_packages(&flake_dir)?;
    let preserved = PreservedContent::from_file(&flake_path);
    let new_content = generate_flake(&packages, Some(&flake_dir), Some(&preserved));
    fs::write(&flake_path, new_content)?;

    info(&format!("Installing {}...", pkg_name));
    super::sync::run(config)?;

    Ok(())
}

/// Install from a local flake file
fn install_from_flake_file(config: &Config, file: &Path, force: bool) -> Result<()> {
    // Extract package name from filename
    let pkg_name = file
        .file_stem()
        .and_then(|s| s.to_str())
        .map(sanitize_input_name)
        .ok_or_else(|| Error::InvalidFilename(file.display().to_string()))?;

    if pkg_name.is_empty() {
        return Err(Error::InvalidFilename(file.display().to_string()));
    }

    info(&format!(
        "Installing local flake: {} from {}",
        pkg_name,
        file.display()
    ));

    let flake_dir = get_flake_dir(config)?;
    let flake_path = flake_dir.join("flake.nix");

    // Check if existing flake.nix is nixy-managed
    if flake_path.exists() && !is_nixy_managed(&flake_path) {
        return Err(Error::NotNixyManaged);
    }

    // Check for custom modifications
    if flake_path.exists() {
        let packages = Nix::eval_packages(&flake_dir)?;
        if has_custom_modifications(&flake_path, &packages, &flake_dir) {
            if !force {
                warn("flake.nix has modifications outside nixy markers.");
                warn("Use --force to proceed (custom changes will be lost).");
                return Err(Error::CustomModifications);
            }
            warn("Proceeding with --force: custom modifications outside markers will be lost.");
        }
    }

    // Create package directory
    let pkg_dir = flake_dir.join("packages").join(&pkg_name);
    fs::create_dir_all(&pkg_dir)?;

    // Copy file as flake.nix
    let dest = pkg_dir.join("flake.nix");
    fs::copy(file, &dest)?;
    success(&format!("Copied flake to {}", dest.display()));

    // Add to git if in a git repo
    git_add(&flake_dir, &format!("packages/{}/flake.nix", pkg_name));

    // Regenerate flake.nix
    let packages = Nix::eval_packages(&flake_dir)?;
    let preserved = PreservedContent::from_file(&flake_path);
    let new_content = generate_flake(&packages, Some(&flake_dir), Some(&preserved));
    fs::write(&flake_path, new_content)?;

    info(&format!("Installing {}...", pkg_name));
    super::sync::run(config)?;

    Ok(())
}

/// Sanitize a string for use as an input name
fn sanitize_input_name(s: &str) -> String {
    let sanitized: String = s
        .chars()
        .map(|c| {
            if c.is_alphanumeric() || c == '-' {
                c
            } else {
                '-'
            }
        })
        .collect();
    sanitized.trim_matches('-').to_string()
}

/// Derive an input name from a flake URL
fn derive_input_name_from_url(url: &str) -> String {
    // Try to extract owner-repo from URL
    let parts: Vec<&str> = url.split('/').collect();
    if parts.len() >= 2 {
        let owner = parts[parts.len() - 2];
        let repo = parts[parts.len() - 1].trim_end_matches(".git");
        sanitize_input_name(&format!("{}-{}", owner, repo))
    } else {
        "custom-flake".to_string()
    }
}

/// Add a file to git if in a git repo
fn git_add(dir: &Path, file: &str) {
    // Check if in a git repo
    let is_git_repo = dir.join(".git").exists()
        || Command::new("git")
            .args(["-C", &dir.to_string_lossy(), "rev-parse", "--git-dir"])
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false);

    if is_git_repo {
        let _ = Command::new("git")
            .args(["-C", &dir.to_string_lossy(), "add", file])
            .output();
    }
}