From e022b86ee842b06284a8052be28fb99f434e201e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= Date: Fri, 17 Jul 2026 00:59:24 +0000 Subject: [PATCH] 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 --- .../sw/switch-to-configuration-ng/src/main.rs | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/pkgs/by-name/sw/switch-to-configuration-ng/src/main.rs b/pkgs/by-name/sw/switch-to-configuration-ng/src/main.rs index d74fce7b6d19..6961a7c0967b 100644 --- a/pkgs/by-name/sw/switch-to-configuration-ng/src/main.rs +++ b/pkgs/by-name/sw/switch-to-configuration-ng/src/main.rs @@ -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> { - 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<()> {