nix-community / NixOS-WSL

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

Are there steps missing integrating usbip support in NixOS-WSL ? #111

Open 573 opened 2 years ago

573 commented 2 years ago

Discussed in https://github.com/nix-community/NixOS-WSL/discussions/110

Originally posted by **573** June 23, 2022 It would be awesome to have usbip support in NixOS-WSL (see https://github.com/dorssel/usbipd-win/wiki/WSL-support). I've only gotten so far to try the usbip command inside the VM and I'm still not clear how to get that. Loading kernel package in configuration.nix didn't seem to as we use the kernel provided by Microsoft. Is it just a case of building our own version of the MS kernel (https://nixos.wiki/wiki/Linux_kernel) ? When from Powershell (regular user, Windows side) running ```console ➜ & 'C:\Program Files\usbipd-win\usbipd.exe' wsl attach --busid 3-1 ``` I'm just stuck in the ```console usbipd: error: WSL 'usbip' client not correctly installed. See https://github.com/dorssel/usbipd-win/wiki/WSL-support for the latest instructions. ``` What do you say ? Can we figure this out as a Step-By-Step list maybe just to complete the instructions (saying just "on other distros") over at https://github.com/dorssel/usbipd-win/wiki/WSL-support ?
SuperSandro2000 commented 2 years ago

Loading kernel package in configuration.nix didn't seem to as we use the kernel provided by Microsoft.

yes, that will not work.

Maybe https://nixos.wiki/wiki/Linux_kernel#Custom_configuration is a help but I am not sure if the kernel can be exported from NixOS and will work for Windows/WSL.

KoviRobi commented 1 year ago

With the new WSL version from Windows store, I'm able to use usbip with the default NixOS system (because as far as I understand, NixOS isn't providing the kernel, that's provided by Microsoft?)

I do have to use

Admin PowerShell> usbipd.exe list
Admin PowerShell> usbipd.exe bind -b 7-2
root nixos shell# usbip list -r 10.0.0.1
root nixos shell# usbip attach -r 10.0.0.1 -b 7-2

but this might be because of my unusual networking (to work around Windows having the WSL2 interface as public, and my work laptop having firewalled all public interfaces without me being able to make an exception, I have a wireguard tunnel between Windows and WSL2, which I can change the Windows net compartment on. So 10.0.0.1 is the Windows IP for me.)

SuperSandro2000 commented 1 year ago

because as far as I understand, NixOS isn't providing the kernel, that's provided by Microsoft?

We are using the kernel from Microsoft and explicitly not building one ourselves.

alexvorobiev commented 1 year ago

I have services.udev.enable = true and I am getting this error:

PS C:\Users\alexa> usbipd wsl attach --busid 1-2
usbipd: info: Using default WSL distribution 'NixOS-1'; specify the '--distribution' option to select a different one.
usbipd: error: WSL kernel is not USBIP capable; update with 'wsl --update'.
PS C:\Users\alexa> wsl --update
Checking for updates.
The most recent version of Windows Subsystem for Linux is already installed.
KoviRobi commented 1 year ago

I think from memory you might have to install WSL from the Microsoft store to get the most up to date version? It's complaining about the Microsoft kernel, not the NixOS kernel I believe

alexvorobiev commented 1 year ago

I have the WSL from the store. I did some digging to see where the message is generated: https://github.com/dorssel/usbipd-win/blob/c86397770de86a1493e1ce1a7aef497e99ba8fc2/Usbipd/CommandHandlers.cs#L657. So the test it runs is here https://github.com/dorssel/usbipd-win/blob/c86397770de86a1493e1ce1a7aef497e99ba8fc2/Usbipd/CommandHandlers.cs#L647. When I tried to run this command from the admin powershell I got an error:

> wsl --distribution NixOS-1 --user root -- cat /sys/devices/platform/vhci_hcd.0/status
/run/current-system/sw/bin/bash: line 1: cat: command not found

But it runs fine if I specify the full path to cat:

 > wsl --distribution NixOS-1 --user root -- /run/current-system/sw/bin/cat /sys/devices/platform/vhci_hcd.0/status
hub port sta spd dev      sockfd local_busid
hs  0000 004 000 00000000 000000 0-0
hs  0001 004 000 00000000 000000 0-0
hs  0002 004 000 00000000 000000 0-0
hs  0003 004 000 00000000 000000 0-0
hs  0004 004 000 00000000 000000 0-0
hs  0005 004 000 00000000 000000 0-0
hs  0006 004 000 00000000 000000 0-0
hs  0007 004 000 00000000 000000 0-0
ss  0008 004 000 00000000 000000 0-0
ss  0009 004 000 00000000 000000 0-0
ss  0010 004 000 00000000 000000 0-0
ss  0011 004 000 00000000 000000 0-0
ss  0012 004 000 00000000 000000 0-0
ss  0013 004 000 00000000 000000 0-0
ss  0014 004 000 00000000 000000 0-0
ss  0015 004 000 00000000 000000 0-0

As a matter of fact only the commands under standard Linux paths (/bin, /usr/bin) work without specifying their paths with wsl -- all the other commands fail with the same error and need the full path. My default shell is zsh for all users including root. Could that be the reason?

alexvorobiev commented 1 year ago

So after I symlinked cat and usbip to /usr/bin everything seems to be working:

sudo ln -s /run/current-system/sw/bin/cat /usr/bin
sudo ln -s /run/current-system/sw/bin/usbip /usr/bin

But I am puzzled why busid is 1-1 in Linux:

PS C:\Users\alexa> usbipd wsl attach --busid 1-2 --distribution NixOS-1
PS C:\Users\alexa> wsl --distribution NixOS-1 --user root -- cat /sys/devices/platform/vhci_hcd.0/status
hub port sta spd dev      sockfd local_busid
hs  0000 006 003 00010002 000003 1-1
hs  0001 004 000 00000000 000000 0-0
hs  0002 004 000 00000000 000000 0-0
hs  0003 004 000 00000000 000000 0-0
hs  0004 004 000 00000000 000000 0-0
hs  0005 004 000 00000000 000000 0-0
hs  0006 004 000 00000000 000000 0-0
hs  0007 004 000 00000000 000000 0-0
ss  0008 004 000 00000000 000000 0-0
ss  0009 004 000 00000000 000000 0-0
ss  0010 004 000 00000000 000000 0-0
ss  0011 004 000 00000000 000000 0-0
ss  0012 004 000 00000000 000000 0-0
ss  0013 004 000 00000000 000000 0-0
ss  0014 004 000 00000000 000000 0-0
ss  0015 004 000 00000000 000000 0-0
PS C:\Users\alexa>

Should those symlinks be part of the usbip install?

stfnx commented 10 months ago

I have the latest WSL version:

WSL-Version: 2.0.7.0
Kernelversion: 5.15.133.1-1
WSLg-Version: 1.0.59
MSRDC-Version: 1.2.4677
Direct3D-Version: 1.611.1-81528511
DXCore-Version: 10.0.25880.1000-230602-1350.main
Windows-Version: 10.0.19045.3570

And I've installed the latest usbipd 3.2.0 on the Windows host.

I've tried to attach a device from Windows using the following command:

> usbipd wsl attach --distribution NixOS --hardware-id 20a0:4109
usbipd: info: Device with hardware-id '20a0:4109' found at busid '4-4'.
usbipd: error: WSL 'usbip' client not correctly installed. See https://github.com/dorssel/usbipd-win/wiki/WSL-support for the latest instructions.

And running usbip to attach from within WSL doesn't work either, because usbip command isn't found on my NixOS WSL installation.

Which NixOS package am I supposed to install to get the usbip command?

edit: here I found the answer. its pkgs.linuxPackages.usbip

nzbr commented 9 months ago

You should now (on the latest commit on main at least) be able to use usbip, by setting wsl.usbip.enable = true

PixelyIon commented 5 months ago

Just setting wsl.usbip.enable = true results in usbipd: error: WSL kernel is not USBIP capable; update with 'wsl --update'. when trying to attach a device. The symlinks from a prior comment need to be done too:

sudo ln -s /run/current-system/sw/bin/cat /usr/bin
sudo ln -s /run/current-system/sw/bin/usbip /usr/bin