nix-community / NixOS-WSL

NixOS on WSL(2) [maintainer=@nzbr]
Apache License 2.0
1.92k stars 121 forks source link

usbip-auto-attach not working as intended #594

Open Rasmus-Bertell opened 4 days ago

Rasmus-Bertell commented 4 days ago

Bug description

Tried to set up usbip on WSL with auto attach but I get an error in my logs which I believe is the result of the following change in the script which came with PR #493. Should probably revert back to 3.x version of the script until it's fixed upstream. Attaching works as intended if done manually.

Nov 25 10:41:42 nixos usbip-auto-attach_-start[450]: Starting auto attach for busid x-x on xxx.xx.xxx.x.
Nov 25 10:41:44 nixos usbip-auto-attach_-start[450]: /nix/store/nll5ayjp1474ax1pyq3qf52arn438dn8-auto-attach.sh: line 40: ./usbip: No such file or directory
-    LAST_ERROR=$(usbip attach --remote="$HOST" --busid="$BUSID" 2>&1) || return 1
+    LAST_ERROR=$(./usbip attach --remote="$HOST" --busid="$BUSID" 2>&1) || return 1

WSL version

WSL version: 2.3.26.0
Kernel version: 5.15.167.4-1
WSLg version: 1.0.65
MSRDC version: 1.2.5620
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.26100.1-240331-1435.ge-release
Windows version: 10.0.26100.2314
terlar commented 4 days ago

Not sure if this is the cleanest solution, but it is possible to solve it like this:

diff --git a/modules/usbip.nix b/modules/usbip.nix
index 8ce755f..8f1cdf9 100644
--- a/modules/usbip.nix
+++ b/modules/usbip.nix
@@ -58,6 +58,7 @@ in
           ip="${cfg.snippetIpAddress}"

           echo "Starting auto attach for busid $busid on $ip."
+          cd "${pkgs.linuxPackages.usbip}/bin"
           source ${usbipd-win-auto-attach} "$ip" "$busid"
         '';
       };

Or something more intricate like this:

diff --git a/modules/usbip.nix b/modules/usbip.nix
index 8ce755f..49845f2 100644
--- a/modules/usbip.nix
+++ b/modules/usbip.nix
@@ -1,10 +1,19 @@
 { config, lib, pkgs, ... }:

 let
-  usbipd-win-auto-attach = pkgs.fetchurl {
-    url = "https://raw.githubusercontent.com/dorssel/usbipd-win/v4.2.0/Usbipd/WSL/auto-attach.sh";
-    hash = "sha256-AiXbRWwOy48mxQxxpWPtog7AAwL3mU3ZSHxrVuVk8/s=";
-  };
+  usbipd-win-wsl = pkgs.linkFarm "usbipd-win-wsl" [
+    {
+      name = "usbip";
+      path = "${pkgs.linuxPackages.usbip}/bin/usbip";
+    }
+    {
+      name = "auto-attach.sh";
+      path = pkgs.fetchurl {
+        url = "https://raw.githubusercontent.com/dorssel/usbipd-win/v4.2.0/Usbipd/WSL/auto-attach.sh";
+        hash = "sha256-AiXbRWwOy48mxQxxpWPtog7AAwL3mU3ZSHxrVuVk8/s=";
+      };
+    }
+  ];

   cfg = config.wsl.usbip;
 in
@@ -58,7 +67,8 @@ in
           ip="${cfg.snippetIpAddress}"

           echo "Starting auto attach for busid $busid on $ip."
-          source ${usbipd-win-auto-attach} "$ip" "$busid"
+          cd "${usbipd-win-wsl}"
+          source ./auto-attach.sh "$ip" "$busid"
         '';
       };

A third option (and perhaps best one) is to patch the script:

diff --git a/modules/usbip.nix b/modules/usbip.nix
index 8ce755f..3ca8436 100644
--- a/modules/usbip.nix
+++ b/modules/usbip.nix
@@ -1,9 +1,16 @@
 { config, lib, pkgs, ... }:

 let
-  usbipd-win-auto-attach = pkgs.fetchurl {
-    url = "https://raw.githubusercontent.com/dorssel/usbipd-win/v4.2.0/Usbipd/WSL/auto-attach.sh";
-    hash = "sha256-AiXbRWwOy48mxQxxpWPtog7AAwL3mU3ZSHxrVuVk8/s=";
+  usbipd-win-auto-attach = pkgs.substitute {
+    src = pkgs.fetchurl {
+      url = "https://raw.githubusercontent.com/dorssel/usbipd-win/v4.2.0/Usbipd/WSL/auto-attach.sh";
+      hash = "sha256-AiXbRWwOy48mxQxxpWPtog7AAwL3mU3ZSHxrVuVk8/s=";
+    };
+    substitutions = [
+      "--replace"
+      "./usbip"
+      "usbip"
+    ];
   };

We can also do more advanced options of the patching, like using resholve.