From 6341462af8e0cf7cb2e6ceab0c8c6e1f0520e658 Mon Sep 17 00:00:00 2001 From: ChristianBingman Date: Sat, 12 Oct 2024 14:49:55 -0500 Subject: [PATCH 01/13] Create service for usbipd and automated mounting Creates a service for usbipd to run via a unit and add systemd services for devices and automatically bind them via udev. --- nixos/modules/services/hardware/usbipd.nix | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 nixos/modules/services/hardware/usbipd.nix diff --git a/nixos/modules/services/hardware/usbipd.nix b/nixos/modules/services/hardware/usbipd.nix new file mode 100755 index 0000000000000..64511058ad7b9 --- /dev/null +++ b/nixos/modules/services/hardware/usbipd.nix @@ -0,0 +1,75 @@ +{ lib, pkgs, config, ... }: +with lib; +let + cfg = config.services.usbipd; + device = types.submodule { + options = { + productid = mkOption { + type = types.str; + }; + vendorid = mkOption { + type = types.str; + }; + }; + }; +in { + options.services.usbipd = { + enable = mkEnableOption "usbip server"; + kernelPackage = mkOption { + type = types.package; + default = config.boot.kernelPackages.usbip; + description = "The kernel module package to install."; + }; + devices = mkOption { + type = types.listOf device; + default = []; + description = "List of USB devices to watch and automatically export."; + example = { + productid = "xxxx"; + vendorid = "xxxx"; + }; + }; + openFirewall = mkOption { + type = types.bool; + default = true; + description = "Open port 3240 for usbipd"; + example = false; + }; + }; + + config = mkIf cfg.enable { + boot.extraModulePackages = [ cfg.kernelPackage ]; + boot.kernelModules = [ "usbip-core" "usbip-host" ]; + networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ 3240 ]; + services.udev.extraRules = 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})" ) + output=$(${cfg.kernelPackage}/bin/usbip -d bind -b $(echo $devices | ${pkgs.gawk}/bin/awk '{ print $3 }') 2>&1) + code=$? + + echo $output + if [[ $output =~ "already bound" ]]; then + exit 0 + else + exit $code + fi + ''; + serviceConfig.Type = "oneshot"; + serviceConfig.RemainAfterExit = true; + restartTriggers = [ "usbipd.service" ]; + }; + }) cfg.devices)) // { + usbipd = { + wantedBy = [ "multi-user.target" ]; + serviceConfig.ExecStart = "${cfg.kernelPackage}/bin/usbipd -D"; + serviceConfig.Type = "forking"; + }; + }; + }; +} From 1d8baaa7491bab503bff6cdb5fa10ed6c1d67796 Mon Sep 17 00:00:00 2001 From: ChristianBingman Date: Sun, 13 Oct 2024 11:07:59 -0500 Subject: [PATCH 02/13] maintainers: add cbingman --- maintainers/maintainer-list.nix | 6 ++++++ nixos/modules/services/hardware/usbipd.nix | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/maintainers/maintainer-list.nix b/maintainers/maintainer-list.nix index b1246893c90b6..b142a90bf2a1d 100644 --- a/maintainers/maintainer-list.nix +++ b/maintainers/maintainer-list.nix @@ -3756,6 +3756,12 @@ matrix = "@cedric:cbarrete.com"; name = "Cédric Barreteau"; }; + cbingman = { + name = "Christian Bingman"; + email = "maintainer@christianbingman.com"; + github = "ChristianBingman"; + githubId = 42191425; + }; cbleslie = { email = "cameronleslie@gmail.com"; github = "cbleslie"; diff --git a/nixos/modules/services/hardware/usbipd.nix b/nixos/modules/services/hardware/usbipd.nix index 64511058ad7b9..97f8534671969 100755 --- a/nixos/modules/services/hardware/usbipd.nix +++ b/nixos/modules/services/hardware/usbipd.nix @@ -41,8 +41,8 @@ in { boot.extraModulePackages = [ cfg.kernelPackage ]; boot.kernelModules = [ "usbip-core" "usbip-host" ]; networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ 3240 ]; - services.udev.extraRules = strings.concatLines - ((map (dev: + services.udev.extraRules = 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 = { @@ -72,4 +72,5 @@ in { }; }; }; + meta.maintainers = with maintainers; [ cbingman ]; } From 20f077c9d7eab66158b7d0454f49518b909a4c30 Mon Sep 17 00:00:00 2001 From: ChristianBingman Date: Sun, 13 Oct 2024 12:32:31 -0500 Subject: [PATCH 03/13] Apply nixfmt --- nixos/modules/services/hardware/usbipd.nix | 70 ++++++++++++++-------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/nixos/modules/services/hardware/usbipd.nix b/nixos/modules/services/hardware/usbipd.nix index 97f8534671969..6de8989198c42 100755 --- a/nixos/modules/services/hardware/usbipd.nix +++ b/nixos/modules/services/hardware/usbipd.nix @@ -1,4 +1,9 @@ -{ lib, pkgs, config, ... }: +{ + lib, + pkgs, + config, + ... +}: with lib; let cfg = config.services.usbipd; @@ -12,7 +17,8 @@ let }; }; }; -in { +in +{ options.services.usbipd = { enable = mkEnableOption "usbip server"; kernelPackage = mkOption { @@ -22,7 +28,7 @@ in { }; devices = mkOption { type = types.listOf device; - default = []; + default = [ ]; description = "List of USB devices to watch and automatically export."; example = { productid = "xxxx"; @@ -39,32 +45,44 @@ in { config = mkIf cfg.enable { boot.extraModulePackages = [ cfg.kernelPackage ]; - boot.kernelModules = [ "usbip-core" "usbip-host" ]; + boot.kernelModules = [ + "usbip-core" + "usbip-host" + ]; networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ 3240 ]; - services.udev.extraRules = 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)); + services.udev.extraRules = 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})" ) - output=$(${cfg.kernelPackage}/bin/usbip -d bind -b $(echo $devices | ${pkgs.gawk}/bin/awk '{ print $3 }') 2>&1) - code=$? + 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})" ) + output=$(${cfg.kernelPackage}/bin/usbip -d bind -b $(echo $devices | ${pkgs.gawk}/bin/awk '{ print $3 }') 2>&1) + code=$? - echo $output - if [[ $output =~ "already bound" ]]; then - exit 0 - else - exit $code - fi - ''; - serviceConfig.Type = "oneshot"; - serviceConfig.RemainAfterExit = true; - restartTriggers = [ "usbipd.service" ]; - }; - }) cfg.devices)) // { + echo $output + if [[ $output =~ "already bound" ]]; then + exit 0 + else + exit $code + fi + ''; + serviceConfig.Type = "oneshot"; + serviceConfig.RemainAfterExit = true; + restartTriggers = [ "usbipd.service" ]; + }; + }) cfg.devices + )) + // { usbipd = { wantedBy = [ "multi-user.target" ]; serviceConfig.ExecStart = "${cfg.kernelPackage}/bin/usbipd -D"; From 73d874d3a911f95ea81507fc2849aac4a445b539 Mon Sep 17 00:00:00 2001 From: ChristianBingman Date: Sun, 13 Oct 2024 13:55:59 -0500 Subject: [PATCH 04/13] Include module in module-list --- nixos/modules/module-list.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index 2bfb963c19dfc..4da5052b7f9e3 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -643,6 +643,7 @@ ./services/hardware/udisks2.nix ./services/hardware/undervolt.nix ./services/hardware/upower.nix + ./services/hardware/usbipd.nix ./services/hardware/usbmuxd.nix ./services/hardware/usbrelayd.nix ./services/hardware/vdr.nix From 7c385d245abbc5d3c8f190d35417fd9293147f47 Mon Sep 17 00:00:00 2001 From: ChristianBingman Date: Sat, 26 Oct 2024 18:38:57 -0500 Subject: [PATCH 05/13] Nount all devices with same PID/VID and use latest kernel package --- nixos/modules/services/hardware/usbipd.nix | 26 +++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/nixos/modules/services/hardware/usbipd.nix b/nixos/modules/services/hardware/usbipd.nix index 6de8989198c42..533b98bdf24d2 100755 --- a/nixos/modules/services/hardware/usbipd.nix +++ b/nixos/modules/services/hardware/usbipd.nix @@ -23,8 +23,9 @@ in enable = mkEnableOption "usbip server"; kernelPackage = mkOption { type = types.package; - default = config.boot.kernelPackages.usbip; + default = pkgs.linuxPackages_latest.usbip; description = "The kernel module package to install."; + example = config.boot.kernelPackages.usbip; }; devices = mkOption { type = types.listOf device; @@ -66,15 +67,17 @@ in script = '' set +e devices=$(${cfg.kernelPackage}/bin/usbip list -l | grep -E "^.*- busid.*(${dev.vendorid}:${dev.productid})" ) - output=$(${cfg.kernelPackage}/bin/usbip -d bind -b $(echo $devices | ${pkgs.gawk}/bin/awk '{ print $3 }') 2>&1) - code=$? + 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" ]]; then - exit 0 - else - exit $code - fi + echo $output + if [[ $output =~ "already bound" ]] || [ $code -eq 0 ]; then + continue + else + exit $code + fi + done ''; serviceConfig.Type = "oneshot"; serviceConfig.RemainAfterExit = true; @@ -85,7 +88,10 @@ in // { usbipd = { wantedBy = [ "multi-user.target" ]; - serviceConfig.ExecStart = "${cfg.kernelPackage}/bin/usbipd -D"; + script = '' + ${pkgs.kmod}/bin/modprobe usbip-host usbip-core + exec ${cfg.kernelPackage}/bin/usbipd -D + ''; serviceConfig.Type = "forking"; }; }; From 773eb641018ea95dfd36e2e98ea2de32cd4293e2 Mon Sep 17 00:00:00 2001 From: ChristianBingman Date: Sat, 26 Oct 2024 18:45:54 -0500 Subject: [PATCH 06/13] Disable building docs in sandbox --- nixos/modules/services/hardware/usbipd.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nixos/modules/services/hardware/usbipd.nix b/nixos/modules/services/hardware/usbipd.nix index 533b98bdf24d2..5244a43bb4572 100755 --- a/nixos/modules/services/hardware/usbipd.nix +++ b/nixos/modules/services/hardware/usbipd.nix @@ -23,9 +23,8 @@ in enable = mkEnableOption "usbip server"; kernelPackage = mkOption { type = types.package; - default = pkgs.linuxPackages_latest.usbip; + default = config.boot.kernelPackages.usbip; description = "The kernel module package to install."; - example = config.boot.kernelPackages.usbip; }; devices = mkOption { type = types.listOf device; @@ -97,4 +96,5 @@ in }; }; meta.maintainers = with maintainers; [ cbingman ]; + meta.buildDocsInSandbox = false } From cab374afe27b9c5beb5fceac74ee88d3b3577cb9 Mon Sep 17 00:00:00 2001 From: ChristianBingman Date: Sat, 26 Oct 2024 18:47:11 -0500 Subject: [PATCH 07/13] Fix missing ; --- nixos/modules/services/hardware/usbipd.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/modules/services/hardware/usbipd.nix b/nixos/modules/services/hardware/usbipd.nix index 5244a43bb4572..3a727b5875d3f 100755 --- a/nixos/modules/services/hardware/usbipd.nix +++ b/nixos/modules/services/hardware/usbipd.nix @@ -96,5 +96,5 @@ in }; }; meta.maintainers = with maintainers; [ cbingman ]; - meta.buildDocsInSandbox = false + meta.buildDocsInSandbox = false; } From faf817b82d8665d9a0b24f8c1bc2cf42799e4b9f Mon Sep 17 00:00:00 2001 From: ChristianBingman Date: Sat, 26 Oct 2024 18:49:09 -0500 Subject: [PATCH 08/13] Revert to latest linuxPackage --- nixos/modules/services/hardware/usbipd.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nixos/modules/services/hardware/usbipd.nix b/nixos/modules/services/hardware/usbipd.nix index 3a727b5875d3f..c0a7c4c4fa90d 100755 --- a/nixos/modules/services/hardware/usbipd.nix +++ b/nixos/modules/services/hardware/usbipd.nix @@ -23,8 +23,9 @@ in enable = mkEnableOption "usbip server"; kernelPackage = mkOption { type = types.package; - default = config.boot.kernelPackages.usbip; + default = pkgs.linuxPackages_latest.usbip; description = "The kernel module package to install."; + example = config.boot.kernelPackages.usbip; }; devices = mkOption { type = types.listOf device; From 10da7ab7919e8951dddfc66d354c672db1c410ca Mon Sep 17 00:00:00 2001 From: ChristianBingman Date: Sat, 26 Oct 2024 18:50:25 -0500 Subject: [PATCH 09/13] Remove example --- nixos/modules/services/hardware/usbipd.nix | 1 - 1 file changed, 1 deletion(-) diff --git a/nixos/modules/services/hardware/usbipd.nix b/nixos/modules/services/hardware/usbipd.nix index c0a7c4c4fa90d..8d5fbeb4e31f6 100755 --- a/nixos/modules/services/hardware/usbipd.nix +++ b/nixos/modules/services/hardware/usbipd.nix @@ -25,7 +25,6 @@ in type = types.package; default = pkgs.linuxPackages_latest.usbip; description = "The kernel module package to install."; - example = config.boot.kernelPackages.usbip; }; devices = mkOption { type = types.listOf device; From fde30feb13c32be9542a7ab48bce1f6e64d919db Mon Sep 17 00:00:00 2001 From: ChristianBingman Date: Sat, 26 Oct 2024 18:53:04 -0500 Subject: [PATCH 10/13] More docs cleanup --- nixos/modules/services/hardware/usbipd.nix | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/nixos/modules/services/hardware/usbipd.nix b/nixos/modules/services/hardware/usbipd.nix index 8d5fbeb4e31f6..fb8ba05dcb7f9 100755 --- a/nixos/modules/services/hardware/usbipd.nix +++ b/nixos/modules/services/hardware/usbipd.nix @@ -11,9 +11,11 @@ let options = { productid = mkOption { type = types.str; + description = "The product id of the device"; }; vendorid = mkOption { type = types.str; + description = "The vendor id of the device"; }; }; }; @@ -30,10 +32,10 @@ in type = types.listOf device; default = [ ]; description = "List of USB devices to watch and automatically export."; - example = { + example = [{ productid = "xxxx"; vendorid = "xxxx"; - }; + }]; }; openFirewall = mkOption { type = types.bool; From d50daf9b33c7ea0eb1c32074e0c2aa5e9556ec70 Mon Sep 17 00:00:00 2001 From: ChristianBingman Date: Sat, 26 Oct 2024 18:57:15 -0500 Subject: [PATCH 11/13] Run nixfmt --- nixos/modules/services/hardware/usbipd.nix | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/nixos/modules/services/hardware/usbipd.nix b/nixos/modules/services/hardware/usbipd.nix index fb8ba05dcb7f9..9abede2c737b6 100755 --- a/nixos/modules/services/hardware/usbipd.nix +++ b/nixos/modules/services/hardware/usbipd.nix @@ -32,10 +32,12 @@ in type = types.listOf device; default = [ ]; description = "List of USB devices to watch and automatically export."; - example = [{ - productid = "xxxx"; - vendorid = "xxxx"; - }]; + example = [ + { + productid = "xxxx"; + vendorid = "xxxx"; + } + ]; }; openFirewall = mkOption { type = types.bool; From 011273818bf7873d34fa049f386cb4cb56754e69 Mon Sep 17 00:00:00 2001 From: ChristianBingman Date: Sun, 22 Dec 2024 10:10:59 -0600 Subject: [PATCH 12/13] Readability updates and using abstractions --- nixos/modules/services/hardware/usbipd.nix | 39 ++++++++++------------ 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/nixos/modules/services/hardware/usbipd.nix b/nixos/modules/services/hardware/usbipd.nix index 9abede2c737b6..75a61803f5a57 100755 --- a/nixos/modules/services/hardware/usbipd.nix +++ b/nixos/modules/services/hardware/usbipd.nix @@ -4,17 +4,16 @@ config, ... }: -with lib; let cfg = config.services.usbipd; - device = types.submodule { + device = lib.types.submodule { options = { - productid = mkOption { - type = types.str; + productid = lib.mkOption { + type = lib.types.str; description = "The product id of the device"; }; - vendorid = mkOption { - type = types.str; + vendorid = lib.mkOption { + type = lib.types.str; description = "The vendor id of the device"; }; }; @@ -22,14 +21,10 @@ let in { options.services.usbipd = { - enable = mkEnableOption "usbip server"; - kernelPackage = mkOption { - type = types.package; - default = pkgs.linuxPackages_latest.usbip; - description = "The kernel module package to install."; - }; - devices = mkOption { - type = types.listOf device; + 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 = [ @@ -39,22 +34,22 @@ in } ]; }; - openFirewall = mkOption { - type = types.bool; + openFirewall = lib.mkOption { + type = lib.types.bool; default = true; description = "Open port 3240 for usbipd"; example = false; }; }; - config = mkIf cfg.enable { + config = lib.mkIf cfg.enable { boot.extraModulePackages = [ cfg.kernelPackage ]; boot.kernelModules = [ "usbip-core" "usbip-host" ]; - networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ 3240 ]; - services.udev.extraRules = strings.concatLines ( + 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\"" @@ -92,13 +87,13 @@ in usbipd = { wantedBy = [ "multi-user.target" ]; script = '' - ${pkgs.kmod}/bin/modprobe usbip-host usbip-core - exec ${cfg.kernelPackage}/bin/usbipd -D + ${lib.getExe' pkgs.kmod "modprobe"} usbip-host usbip-core + exec ${lib.getExe' cfg.kernelPackage "usbipd"} -D ''; serviceConfig.Type = "forking"; }; }; }; - meta.maintainers = with maintainers; [ cbingman ]; + meta.maintainers = with lib.maintainers; [ cbingman ]; meta.buildDocsInSandbox = false; } From 49c24e71ee6fb71ad8d974d6682e49beaf0e8fc4 Mon Sep 17 00:00:00 2001 From: ChristianBingman Date: Sun, 22 Dec 2024 10:14:03 -0600 Subject: [PATCH 13/13] Run nixfmt --- nixos/modules/services/hardware/usbipd.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nixos/modules/services/hardware/usbipd.nix b/nixos/modules/services/hardware/usbipd.nix index 75a61803f5a57..843d8c3c4aace 100755 --- a/nixos/modules/services/hardware/usbipd.nix +++ b/nixos/modules/services/hardware/usbipd.nix @@ -22,7 +22,7 @@ in { options.services.usbipd = { enable = lib.mkEnableOption "usbip server"; - kernelPackage = lib.mkPackageOption pkgs.linuxPackages_latest "usbip" {}; + kernelPackage = lib.mkPackageOption pkgs.linuxPackages_latest "usbip" { }; devices = lib.mkOption { type = lib.types.listOf device; default = [ ];