Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usbip: make more reliable by using default route #444

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions modules/usbip.nix
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -11,17 +9,28 @@ 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.";
};

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 = mkIf (config.wsl.enable && cfg.enable) {
config = lib.mkIf (config.wsl.enable && cfg.enable) {
environment.systemPackages = [
pkgs.linuxPackages.usbip
];
Expand All @@ -34,11 +43,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="${cfg.snippetIpAddress}"

echo "Starting auto attach for busid $busid on $ip."
source ${usbipd-win-auto-attach} "$ip" "$busid"
Expand Down
Loading