diff --git a/CHANGELOG.md b/CHANGELOG.md index 24361f1..761aa1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ - Fixes: - Fix unprefixed versions being generated as `version = "version";` @blaggacao + - Fixed directories being passed as `[PATH]` not becoming `dir/default.nix` ## v0.2.0 diff --git a/src/file_path.rs b/src/file_path.rs index 2540efc..2e2a040 100644 --- a/src/file_path.rs +++ b/src/file_path.rs @@ -59,10 +59,11 @@ pub fn nix_file_paths( return (file_path, module); } + // assume that they want a directory for the individual package if !radix.ends_with(&pname) && radix.extension() != Some(std::ffi::OsStr::new("nix")) { radix.push(&pname); } - + // nix_path is the path used in pkgs/top-level/*.nix or nixos/tests/all-tests.nix let mut nix_path = PathBuf::from(".."); nix_path.push(&radix); @@ -80,7 +81,16 @@ pub fn nix_file_paths( return (file_path, nix_path); } - (path.to_path_buf(), PathBuf::from("")) + let mut path_buf = path.to_path_buf(); + + // if it's a directory, we need to default to using the default.nix which `import` expects + // Path.is_dir() appears to return false if the directory doesn't exist, so stringify and assert if path ends in '/' + if path.to_str().unwrap().ends_with("/") { + path_buf.push("default.nix"); + eprintln!("Directory was passed as [PATH], defaulting to {:?}", path_buf.display()); + } + + (path_buf, PathBuf::from("")) } #[cfg(test)] @@ -145,6 +155,18 @@ mod tests { assert_paths(m, expected); } + #[test] + #[serial] + fn test_dir_to_default_nix() { + let m = + build_cli().get_matches_from(vec!["nix-template", "python", "-p", "requests", "local/"]); + let expected = ( + PathBuf::from("local/default.nix"), + PathBuf::from(""), + ); + assert_paths(m, expected); + } + #[test] #[serial] fn test_python() {