Skip to content

Commit

Permalink
Add usbip module for usb support in WSL
Browse files Browse the repository at this point in the history
  • Loading branch information
terlar committed Nov 13, 2023
1 parent 0e4c17e commit e8576ab
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
imports = [
./modules

({ ... }: {
(_: {
wsl.version.rev = mkIf (self ? rev) self.rev;
})
];
Expand Down
1 change: 1 addition & 0 deletions modules/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
./interop.nix
./recovery.nix
./systemd
./usbip.nix
./version.nix
./welcome.nix
./wsl-conf.nix
Expand Down
51 changes: 51 additions & 0 deletions modules/usbip.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{ 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";
hash = "sha256-KJ0tEuY+hDJbBQtJj8nSNk17FHqdpDWTpy9/DLqUFaM=";
};

cfg = config.wsl.usbip;
in
{
options.wsl.usbip = with types; {
enable = mkEnableOption "USB/IP integration";
autoAttach = mkOption {
type = listOf str;
default = [ ];
example = [ "4-1" ];
description = "Auto attach devices with provided Bus IDs.";
};
};

config = mkIf (config.wsl.enable && cfg.enable) {
environment.systemPackages = [
pkgs.linuxPackages.usbip
];

services.udev.enable = true;

systemd = {
services."usbip-auto-attach@" = {
description = "Auto attach device having busid %i with usbip";
after = [ "network.target" ];

scriptArgs = "%i";
path = [ pkgs.linuxPackages.usbip ];

script = ''
busid="$1"
ip="$(grep nameserver /etc/resolv.conf | cut -d' ' -f2)"
echo "Starting auto attach for busid $busid on $ip."
source ${usbipd-win-auto-attach} "$ip" "$busid"
'';
};

targets.multi-user.wants = map (busid: "usbip-auto-attach@${busid}.service") cfg.autoAttach;
};
};
}

0 comments on commit e8576ab

Please sign in to comment.