-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
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
usbipd: add usbipd service with autobind #348301
Open
ChristianBingman
wants to merge
13
commits into
NixOS:master
Choose a base branch
from
ChristianBingman:add-usbipd-service
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6341462
Create service for usbipd and automated mounting
ChristianBingman 1d8baaa
maintainers: add cbingman
ChristianBingman 20f077c
Apply nixfmt
ChristianBingman 73d874d
Include module in module-list
ChristianBingman 7c385d2
Nount all devices with same PID/VID and use latest kernel package
ChristianBingman 773eb64
Disable building docs in sandbox
ChristianBingman cab374a
Fix missing ;
ChristianBingman faf817b
Revert to latest linuxPackage
ChristianBingman 10da7ab
Remove example
ChristianBingman fde30fe
More docs cleanup
ChristianBingman d50daf9
Run nixfmt
ChristianBingman 0112738
Readability updates and using abstractions
ChristianBingman 49c24e7
Run nixfmt
ChristianBingman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3756,6 +3756,12 @@ | |
matrix = "@cedric:cbarrete.com"; | ||
name = "Cédric Barreteau"; | ||
}; | ||
cbingman = { | ||
name = "Christian Bingman"; | ||
email = "[email protected]"; | ||
github = "ChristianBingman"; | ||
githubId = 42191425; | ||
}; | ||
cbleslie = { | ||
email = "[email protected]"; | ||
github = "cbleslie"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
{ | ||
lib, | ||
pkgs, | ||
config, | ||
... | ||
}: | ||
let | ||
cfg = config.services.usbipd; | ||
device = lib.types.submodule { | ||
options = { | ||
productid = lib.mkOption { | ||
type = lib.types.str; | ||
description = "The product id of the device"; | ||
}; | ||
vendorid = lib.mkOption { | ||
type = lib.types.str; | ||
description = "The vendor id of the device"; | ||
}; | ||
}; | ||
}; | ||
in | ||
{ | ||
options.services.usbipd = { | ||
enable = lib.mkEnableOption "usbip server"; | ||
kernelPackage = lib.mkPackageOption pkgs.linuxPackages_latest "usbip" { }; | ||
devices = lib.mkOption { | ||
type = lib.types.listOf device; | ||
default = [ ]; | ||
description = "List of USB devices to watch and automatically export."; | ||
example = [ | ||
{ | ||
productid = "xxxx"; | ||
vendorid = "xxxx"; | ||
} | ||
]; | ||
}; | ||
openFirewall = lib.mkOption { | ||
type = lib.types.bool; | ||
default = true; | ||
description = "Open port 3240 for usbipd"; | ||
example = false; | ||
}; | ||
}; | ||
|
||
config = lib.mkIf cfg.enable { | ||
boot.extraModulePackages = [ cfg.kernelPackage ]; | ||
boot.kernelModules = [ | ||
"usbip-core" | ||
"usbip-host" | ||
]; | ||
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ 3240 ]; | ||
services.udev.extraRules = lib.strings.concatLines ( | ||
(map ( | ||
dev: | ||
"ACTION==\"add\", SUBSYSTEM==\"usb\", ATTRS{idProduct}==\"${dev.productid}\", ATTRS{idVendor}==\"${dev.vendorid}\", RUN+=\"${pkgs.systemd}/bin/systemctl restart usbip-${dev.vendorid}:${dev.productid}.service\"" | ||
) cfg.devices) | ||
); | ||
|
||
systemd.services = | ||
(builtins.listToAttrs ( | ||
map (dev: { | ||
name = "usbip-${dev.vendorid}:${dev.productid}"; | ||
value = { | ||
after = [ "usbipd.service" ]; | ||
script = '' | ||
set +e | ||
devices=$(${cfg.kernelPackage}/bin/usbip list -l | grep -E "^.*- busid.*(${dev.vendorid}:${dev.productid})" ) | ||
echo $devices | while read device; do | ||
output=$(${cfg.kernelPackage}/bin/usbip -d bind -b $(echo $device | ${pkgs.gawk}/bin/awk '{ print $3 }') 2>&1) | ||
code=$? | ||
|
||
echo $output | ||
if [[ $output =~ "already bound" ]] || [ $code -eq 0 ]; then | ||
continue | ||
else | ||
exit $code | ||
fi | ||
done | ||
''; | ||
serviceConfig.Type = "oneshot"; | ||
serviceConfig.RemainAfterExit = true; | ||
restartTriggers = [ "usbipd.service" ]; | ||
}; | ||
}) cfg.devices | ||
)) | ||
// { | ||
usbipd = { | ||
wantedBy = [ "multi-user.target" ]; | ||
script = '' | ||
${lib.getExe' pkgs.kmod "modprobe"} usbip-host usbip-core | ||
exec ${lib.getExe' cfg.kernelPackage "usbipd"} -D | ||
''; | ||
serviceConfig.Type = "forking"; | ||
}; | ||
}; | ||
}; | ||
meta.maintainers = with lib.maintainers; [ cbingman ]; | ||
meta.buildDocsInSandbox = false; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done via boot.kernelModules already, I think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not, e.g. rke2 does this and several others.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rke2 does not set boot.kernelModules. My understanding is that putting something in
boot.kernelModules
is almost equivalent to amodprobe
call at boot time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I could get confirmation, I am happy to remove it. Though when I initially tested this case the module was not loaded until after reboot, so that is why I added it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at kernel.nix it seems like it is supposed to autoload, but in my experience it didn’t and the wording seems to indicate otherwise “The set of kernel modules to be loaded in the second stage of the boot process”. In any case I will remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is correct. It will not work with a simple
nixos-rebuild switch
, only a reboot will apply a change toboot.kernelModules
.