Skip to content

Commit 849af1e

Browse files
cole-hjfroche
andauthored
fix: prevent duplicate /nix entries in fstab (#1746)
* fix: prevent duplicate /nix entries in fstab Filter out all existing /nix mount point entries and prelude comments, then always append exactly one new fstab entry to ensure at most one /nix entry in the output. * fixup: handle consecutive whitespace --------- Co-authored-by: Jean-François Roche <[email protected]>
1 parent 3f166cd commit 849af1e

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

src/action/macos/create_fstab_entry.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,31 +80,24 @@ impl Action for CreateFstabEntry {
8080
})
8181
.map_err(|e| Self::error(ActionErrorKind::Read(fstab_path.to_owned(), e)))?;
8282

83-
let mut line_present = false;
8483
let mut current_fstab_lines = fstab_buf
8584
.lines()
86-
.filter_map(|line| {
87-
// Delete nix-installer entries with a "prelude" comment
85+
.filter(|line| {
86+
// Remove nix-installer entries with a "prelude" comment
8887
if line.starts_with("# nix-installer created volume labelled") {
89-
None
90-
} else {
91-
Some(line)
88+
return false;
9289
}
93-
})
94-
.map(|line| {
90+
// Remove any existing /nix mount point entries
9591
if line.split(&[' ', '\t']).nth(1) == Some("/nix") {
96-
// Replace the existing line with an updated version
97-
line_present = true;
98-
fstab_entry(&uuid)
99-
} else {
100-
line.to_owned()
92+
return false;
10193
}
94+
true
10295
})
96+
.map(|line| line.to_owned())
10397
.collect::<Vec<String>>();
10498

105-
if !line_present {
106-
current_fstab_lines.push(fstab_entry(&uuid))
107-
}
99+
// Always append exactly one new /nix entry
100+
current_fstab_lines.push(fstab_entry(&uuid));
108101

109102
if current_fstab_lines.last().map(|s| s.as_ref()) != Some("") {
110103
// Don't leave the file without a trailing newline
@@ -153,7 +146,7 @@ impl Action for CreateFstabEntry {
153146
}
154147
})
155148
.filter_map(|line| {
156-
if line.split(&[' ', '\t']).nth(1) == Some("/nix") {
149+
if line.split_ascii_whitespace().nth(1) == Some("/nix") {
157150
// Delete the mount line for /nix
158151
None
159152
} else {

0 commit comments

Comments
 (0)