switch-to-configuration-ng: /etc/os-release should not be required

The comment

    // This is a NixOS installation if it has /etc/NIXOS or a proper /etc/os-release.

said OR but the code implemented AND because it errored out
if `/etc/os-release` did not exist.

This was a regression from the Perl-to-Rust rewrite.

See https://github.com/NixOS/nixpkgs/pull/308801#pullrequestreview-4718561904

Assisted-by: Claude Opus 4.8 in Zoo Code, human comment cleanup
This commit is contained in:
Niklas Hambüchen
2026-07-18 20:20:42 +00:00
parent 61b7c44c40
commit e022b86ee8
@@ -211,17 +211,22 @@ fn die() -> ! {
std::process::exit(code);
}
/// Parses the key-value pairs of `/etc/os-release`.
/// Absence or emptiness of the file become the empty map.
/// Failure to read returns an error Result.
fn parse_os_release() -> Result<HashMap<String, String>> {
Ok(std::fs::read_to_string("/etc/os-release")
.context("Failed to read /etc/os-release")?
.lines()
.fold(HashMap::new(), |mut acc, line| {
if let Some((k, v)) = line.split_once('=') {
acc.insert(k.to_string(), v.to_string());
}
let contents = match std::fs::read_to_string("/etc/os-release") {
Ok(contents) => contents,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => String::new(),
Err(err) => return Err(err).context("Failed to read /etc/os-release"),
};
Ok(contents.lines().fold(HashMap::new(), |mut acc, line| {
if let Some((k, v)) = line.split_once('=') {
acc.insert(k.to_string(), v.to_string());
}
acc
}))
acc
}))
}
fn do_pre_switch_check(command: &str, toplevel: &Path, action: &Action) -> Result<()> {