Skip to content

Commit 8b28755

Browse files
committed
Append default.nix if path was given
closes #45
1 parent 6e0ef6a commit 8b28755

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
- Fixes:
1717
- Fix unprefixed versions being generated as `version = "version";` @blaggacao
18+
- Fixed directories being passed as `[PATH]` not becoming `dir/default.nix`
1819

1920
## v0.2.0
2021

src/file_path.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ pub fn nix_file_paths(
5959
return (file_path, module);
6060
}
6161

62+
// assume that they want a directory for the individual package
6263
if !radix.ends_with(&pname) && radix.extension() != Some(std::ffi::OsStr::new("nix")) {
6364
radix.push(&pname);
6465
}
65-
66+
6667
// nix_path is the path used in pkgs/top-level/*.nix or nixos/tests/all-tests.nix
6768
let mut nix_path = PathBuf::from("..");
6869
nix_path.push(&radix);
@@ -80,7 +81,16 @@ pub fn nix_file_paths(
8081
return (file_path, nix_path);
8182
}
8283

83-
(path.to_path_buf(), PathBuf::from(""))
84+
let mut path_buf = path.to_path_buf();
85+
86+
// if it's a directory, we need to default to using the default.nix which `import` expects
87+
// Path.is_dir() appears to return false if the directory doesn't exist, so stringify and assert if path ends in '/'
88+
if path.to_str().unwrap().ends_with("/") {
89+
path_buf.push("default.nix");
90+
eprintln!("Directory was passed as [PATH], defaulting to {:?}", path_buf.display());
91+
}
92+
93+
(path_buf, PathBuf::from(""))
8494
}
8595

8696
#[cfg(test)]
@@ -145,6 +155,18 @@ mod tests {
145155
assert_paths(m, expected);
146156
}
147157

158+
#[test]
159+
#[serial]
160+
fn test_dir_to_default_nix() {
161+
let m =
162+
build_cli().get_matches_from(vec!["nix-template", "python", "-p", "requests", "local/"]);
163+
let expected = (
164+
PathBuf::from("local/default.nix"),
165+
PathBuf::from(""),
166+
);
167+
assert_paths(m, expected);
168+
}
169+
148170
#[test]
149171
#[serial]
150172
fn test_python() {

0 commit comments

Comments
 (0)