From 2e2898b31fabcb3f3b561e6c4cea2aca9de9b284 Mon Sep 17 00:00:00 2001 From: Pratham Patel Date: Thu, 10 Apr 2025 22:53:37 +0530 Subject: [PATCH] feat: change `default_folder` path of system backgrounds based on distribution NixOS is not a Linux distribution that is compliant with the "File Hierarchy System." The top-level directory `/usr` does not exist on NixOS. So on NixOS, we won't find any backgrounds in the `/usr/share/backgrounds/` directory. Instead, what one usually finds in `/usr/share` on FHS-compliant distributions, on NixOS, it is found in the `/run/current-system/sw/share` directory. So, check if we are on NixOS or a NixOS-like distribution by checking the `/etc/os-release` file. If we are on such distribution, use the `/run/current-system/sw/share/backgrounds/` directory. Otherwise, use the `/usr/share/backgrounds/` directory for getting system backgrounds. --- .../src/pages/desktop/wallpaper/config.rs | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/cosmic-settings/src/pages/desktop/wallpaper/config.rs b/cosmic-settings/src/pages/desktop/wallpaper/config.rs index c36148e..2dca3f4 100644 --- a/cosmic-settings/src/pages/desktop/wallpaper/config.rs +++ b/cosmic-settings/src/pages/desktop/wallpaper/config.rs @@ -5,6 +5,7 @@ use cosmic::cosmic_config::{self, ConfigGet, ConfigSet}; use cosmic_bg_config::Source; use cosmic_settings_wallpaper as wallpaper; use std::collections::VecDeque; +use std::io::Read; use std::path::{Path, PathBuf}; const NAME: &str = "com.system76.CosmicSettings.Wallpaper"; @@ -107,7 +108,29 @@ impl Config { #[must_use] pub fn default_folder() -> &'static Path { - Path::new("/usr/share/backgrounds/") + let is_nixos_like: bool = match std::fs::File::open("/etc/os-release") { + Ok(mut file) => { + let mut os_release_contents = String::new(); + let _ = file.read_to_string(&mut os_release_contents); + + // While only want to match for distributions that are either + // NixOS or are based on NixOS. + if os_release_contents.contains("ID=nixos") + || os_release_contents.contains("ID_LIKE=nixos") + { + true + } else { + false + } + } + Err(_) => false, + }; + + if is_nixos_like { + Path::new("/run/current-system/sw/share/backgrounds/") + } else { + Path::new("/usr/share/backgrounds/") + } } /// Sets the current background folder