Repository: tauri-apps/tauri (language: rust)

Issue:
[bug] tauri resource directory is hard-coded
### Describe the bug

Tauri resource directory location is hard-coded making Tauri fail when it can't read or write from it, this is especially a problem in immutable distros like NixOS.

### Reproduction

https://github.com/NixOS/nixpkgs/pull/265771 - build this without the tauri patch in it.

### Expected behavior

For it to succeed in building.

### Full `tauri info` output

```text
N/A
```


### Stack trace

_No response_

### Additional context

There is a fix in the Github pr I linked in the files it's called [tauri-env-resource-dir.patch](https://github.com/NixOS/nixpkgs/blob/728e957b03d9194b7d277907cb1bc154081cf35a/pkgs/by-name/do/dorion/tauri-env-resource-dir.patch)


Discussion / hints:
To be honest i am super confused, probably because i have no nix(os) experience.
Are you saying that rust's `target/` folder is read-only in nixos? How can the app itself be compiled then?

Do you have any error logs from before the patch was applied?
Hi there! I actually wrote that patch. Basically the issue is that `/usr` does not exist and packages are built into and later run from an immutable directory like `/nix/store/[hash]-name` with exe installed to `/nix/store/[hash]-name/bin/exe` and resources installed to `/nix/store/[hash]-name/lib/cargo_name`

The assumptions in this code snippet do not hold for that layout because it assumes either `/usr` exists, `$APPDIR` is set in the environment, or that the path ends in `/data/usr/bin`: https://github.com/tauri-apps/tauri/blob/d70470f868511adb551afcd17acade6dd08b14da/core/tauri-utils/src/platform.rs#L253-L275

The [patch linked above](https://github.com/NixOS/nixpkgs/blob/728e957b03d9194b7d277907cb1bc154081cf35a/pkgs/by-name/do/dorion/tauri-env-resource-dir.patch) basically adds an escape hatch so that any downstream which conforms to a filesystem hierarchy that is not one of those 3 options can use to override the resource directory (which that need is not Nix-specific and would occur elsewhere as well, like with AppImages which already had to be hardcoded)

Alternatively, if the code path could try to use `exe_dir.join(format!("../lib/{}", package_info.package_name()))` regardless of the path ending in `/data/usr/bin` or the existence of `/usr` (either as a fallback or as an initial check), that would also work for our and I imagine most any other downstreams

Thank you for the prompt response!

> Are you saying that rust's target/ folder is read-only in nixos? How can the app itself be compiled then?

Also just to be clear, this doesn't have to do with it. The target directory gets written to just fine, but when it does `cargo install` it gets put not in `/usr` and that is the part that Tauri trips up on
> To be honest i am super confused, probably because i have no nix(os) experience. Are you saying that rust's `target/` folder is read-only in nixos? How can the app itself be compiled then?
> 
> Do you have any error logs from before the patch was applied?

This should be an easy fix right?
Bump :P 

Fix patch:
diff --git a/.changes/enhance-resource-dir-linux.md b/.changes/enhance-resource-dir-linux.md
new file mode 100644
index 000000000000..c528bad12467
--- /dev/null
+++ b/.changes/enhance-resource-dir-linux.md
@@ -0,0 +1,5 @@
+---
+"tauri-utils": patch:bug
+---
+
+Enhance resource directory resolution to support running on distros like NixOS.
diff --git a/core/tauri-utils/src/platform.rs b/core/tauri-utils/src/platform.rs
index 2504a8a55090..70e9ce58e844 100644
--- a/core/tauri-utils/src/platform.rs
+++ b/core/tauri-utils/src/platform.rs
@@ -170,12 +170,12 @@ pub fn resource_dir(package_info: &PackageInfo, env: &Env) -> crate::Result<Path
 
   #[cfg(target_os = "linux")]
   {
-    res = if curr_dir.ends_with("/data/usr/bin") {
-      // running from the deb bundle dir
-      exe_dir
-        .join(format!("../lib/{}", package_info.package_name()))
-        .canonicalize()
-        .map_err(Into::into)
+    // (canonicalize checks for existence, so there's no need for an extra check)
+    res = if let Ok(bundle_dir) = exe_dir
+      .join(format!("../lib/{}", package_info.package_name()))
+      .canonicalize()
+    {
+      Ok(bundle_dir)
     } else if let Some(appdir) = &env.appdir {
       let appdir: &std::path::Path = appdir.as_ref();
       Ok(PathBuf::from(format!(


Test patch: