Skip to content

Commit

Permalink
Merge pull request #444 from nix-community/usbip-ns
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSandro2000 committed Apr 17, 2024
2 parents 3ab8873 + f5a6c03 commit fc58f5c
Showing 1 changed file with 21 additions and 9 deletions.
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

0 comments on commit fc58f5c

Please sign in to comment.