From 2a4b0a33aa286769df196e130b8f61c8ee4aff1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20J=C3=A4ckel?= Date: Tue, 2 Apr 2024 18:12:14 +0200 Subject: [PATCH 1/3] usbip: make more reliable by using default route The nameserver does not necessarily need to be the windows host. --- modules/usbip.nix | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/modules/usbip.nix b/modules/usbip.nix index 60a09732..9213e0b0 100644 --- a/modules/usbip.nix +++ b/modules/usbip.nix @@ -34,11 +34,14 @@ in after = [ "network.target" ]; scriptArgs = "%i"; - path = [ pkgs.linuxPackages.usbip ]; + path = with pkgs; [ + iproute2 + linuxPackages.usbip + ]; script = '' busid="$1" - ip="$(grep nameserver /etc/resolv.conf | cut -d' ' -f2)" + ip="$(ip route list | sed -nE 's/(default)? via (.*) dev eth0 proto kernel/\2/p')" echo "Starting auto attach for busid $busid on $ip." source ${usbipd-win-auto-attach} "$ip" "$busid" From d5589755c0bf63dc951e193fe014b9cef53b123a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20J=C3=A4ckel?= Date: Wed, 3 Apr 2024 13:44:45 +0200 Subject: [PATCH 2/3] usbip: drop with lib --- modules/usbip.nix | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/modules/usbip.nix b/modules/usbip.nix index 9213e0b0..72a5d796 100644 --- a/modules/usbip.nix +++ b/modules/usbip.nix @@ -1,7 +1,5 @@ { config, lib, pkgs, ... }: -with lib; - let usbipd-win-auto-attach = pkgs.fetchurl { url = "https://raw.githubusercontent.com/dorssel/usbipd-win/v3.1.0/Usbipd/wsl-scripts/auto-attach.sh"; @@ -11,17 +9,17 @@ let cfg = config.wsl.usbip; in { - options.wsl.usbip = with types; { - enable = mkEnableOption "USB/IP integration"; - autoAttach = mkOption { - type = listOf str; + options.wsl.usbip = { + enable = lib.mkEnableOption "USB/IP integration"; + autoAttach = lib.mkOption { + type = with lib.types; listOf str; default = [ ]; example = [ "4-1" ]; description = "Auto attach devices with provided Bus IDs."; }; }; - config = mkIf (config.wsl.enable && cfg.enable) { + config = lib.mkIf (config.wsl.enable && cfg.enable) { environment.systemPackages = [ pkgs.linuxPackages.usbip ]; From f5a6c03518b839113a9e61888e0adbd489c3118f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20J=C3=A4ckel?= Date: Wed, 3 Apr 2024 13:44:55 +0200 Subject: [PATCH 3/3] usbip: allow configuring script snippet to get ip address --- modules/usbip.nix | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/modules/usbip.nix b/modules/usbip.nix index 72a5d796..411bce28 100644 --- a/modules/usbip.nix +++ b/modules/usbip.nix @@ -11,12 +11,23 @@ in { options.wsl.usbip = { enable = lib.mkEnableOption "USB/IP integration"; + autoAttach = lib.mkOption { type = with lib.types; listOf str; default = [ ]; example = [ "4-1" ]; description = "Auto attach devices with provided Bus IDs."; }; + + snippetIpAddress = lib.mkOption { + type = lib.types.str; + default = "$(ip route list | sed -nE 's/(default)? via (.*) dev eth0 proto kernel/\2/p')"; + example = "127.0.0.1"; + description = '' + This snippet is used to obtain the address of the Windows host where Usbipd is running. + It can also be a plain IP address in case networkingMode=mirrored or wsl-vpnkit is used. + ''; + }; }; config = lib.mkIf (config.wsl.enable && cfg.enable) { @@ -39,7 +50,7 @@ in script = '' busid="$1" - ip="$(ip route list | sed -nE 's/(default)? via (.*) dev eth0 proto kernel/\2/p')" + ip="${cfg.snippetIpAddress}" echo "Starting auto attach for busid $busid on $ip." source ${usbipd-win-auto-attach} "$ip" "$busid"