XRLinuxDriver icon indicating copy to clipboard operation
XRLinuxDriver copied to clipboard

nix: Add Nix Flake support

Open shymega opened this issue 1 year ago • 50 comments

This commit consists of the following changes:

  • default.nix/shell.nix This allows for legacy Nix tooling like nix-build/nix-shell to work.
  • flake.nix This is for the (new) Nix Flake support.
  • flake.lock As above, but for tracking Flake inputs.
  • .envrc This allows for a fully self-contained developer environment to be setup with Nix.
  • nix/default.nix This is the main package derivation. Both this repo & Nixpkgs are derived from the same codebase.

Closes: #38

shymega avatar Dec 01 '24 21:12 shymega

I finally had delivery of a VITURE Pro today, after being lost, but I've tested my Flake with the glasses, and it does seem to work - about to test mouse control shortly.

shymega avatar Dec 01 '24 21:12 shymega

The long-term steps would be to upstream the derivation (+ Breezy) to Nixpkgs, as well as a Nix module, and a CI workflow for this repo (+ Breezy).

shymega avatar Dec 01 '24 21:12 shymega

@shymega can you let me know if this is ready? Also can you have another Nix user test and sign off on it?

wheaney avatar Apr 27 '25 05:04 wheaney

As for another Nix user I'm here. I actually already put together a Nix package for the driver but this one looks further along than mine so will test. Will post my findings here in the next few days.

deephack1982 avatar Apr 28 '25 17:04 deephack1982

Okay so it builds and I can install the driver but I seem to be having some issues connecting to my Viture Pro glasses. I get the following in the xrDriver log.

2025-04-28 22:02:40.853 Project version: 1.1.0 2025-04-28 22:02:40.854 Driver is disabled 2025-04-28 22:02:40.855 Using hardware id b50796e372423c376420886322d8fee0f49f3f28fb6c8a90a08dda81f1a0599f 2025-04-28 22:02:40.856 Feature sbs granted. 2025-04-28 22:02:40.856 Feature smooth_follow granted. 2025-04-28 22:02:40.856 Feature productivity_basic granted. 2025-04-28 22:02:40.856 Starting up XR driver 2025-04-28 22:02:40.857 Found device with vendor ID 0x35ca and product ID 0x101d 2025-04-28 22:02:40.857 Found device with vendor ID 0x35ca and product ID 0x101d 2025-04-28 22:03:07.988 Driver has been re-enabled 2025-04-28 22:03:09.991 Device driver connection attempt failed 2025-04-28 22:03:09.991 Retrying driver connection in 1 second 2025-04-28 22:03:12.992 Device driver connection attempt failed 2025-04-28 22:03:12.992 Retrying driver connection in 1 second 2025-04-28 22:03:15.994 Device driver connection attempt failed 2025-04-28 22:03:15.994 Retrying driver connection in 1 second 2025-04-28 22:03:16.395 Driver has been disabled

In kernel logs I have this when I plug in the glasses.

[ 187.344812] usb 2-1: new full-speed USB device number 7 using xhci_hcd [ 187.470047] usb 2-1: New USB device found, idVendor=35ca, idProduct=101d, bcdDevice= 2.00 [ 187.470064] usb 2-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [ 187.470070] usb 2-1: Product: VITURE Pro XR Glasses [ 187.470076] usb 2-1: Manufacturer: VITURE Pro [ 187.470080] usb 2-1: SerialNumber: 205A308E4742 [ 187.474054] hid-generic 0003:35CA:101D.0003: hiddev96,hidraw0: USB HID v1.11 Device [VITURE Pro VITURE Pro XR Glasses] on usb-0000:00:14.0-1/input0 [ 187.475272] hid-generic 0003:35CA:101D.0004: hiddev97,hidraw1: USB HID v1.11 Device [VITURE Pro VITURE Pro XR Glasses] on usb-0000:00:14.0-1/input1 [ 187.475951] cdc_acm 2-1:1.2: ttyACM0: USB ACM device

So the glasses are being recognised by the kernel. For refernce this is NixOS unstable running kernel 6.12.24.

deephack1982 avatar Apr 28 '25 20:04 deephack1982

You might want to try rebasing this branch, as main is on 2.x and may include a fixing change for that. But first you could try to see if this persists after a restart. And try not having the glasses plugged in on boot.

wheaney avatar Apr 28 '25 20:04 wheaney

Yes, this isn't a problem with my package. I did raise it with @wheaney over Discord, but the solution isn't clear yet.

Have you put the udev rules in your services.udev.extraRules? I could add that as an output to the package...

shymega avatar Apr 28 '25 20:04 shymega

I'll rebase this now.

shymega avatar Apr 28 '25 20:04 shymega

Tried a restart and adding the udev rules but still getting the same output for now.

deephack1982 avatar Apr 28 '25 20:04 deephack1982

@deephack1982 Try now :-)

I'm also working on a Guix package, if that's of interest to anyone following this thread.

shymega avatar Apr 28 '25 20:04 shymega

New one gives me a build failure with the following make output.

[ 58%] Building C object CMakeFiles/xrDriver.dir/src/ipc.c.o [ 60%] Building C object CMakeFiles/xrDriver.dir/src/multitap.c.o [ 62%] Building C object CMakeFiles/xrDriver.dir/src/outputs.c.o /build/xrlinuxdriver-src/src/ipc.c: In function 'setup_ipc_value': /build/xrlinuxdriver-src/src/ipc.c:78:5: error: implicit declaration of function 'close'; did you mean 'pclose'? [] 78 | close(fd); | ^~~~~ | pclose make[2]: *** [CMakeFiles/xrDriver.dir/build.make:261: CMakeFiles/xrDriver.dir/src/ipc.c.o] Error 1 make[2]: *** Waiting for unfinished jobs.... make[1]: *** [CMakeFiles/Makefile2:253: CMakeFiles/xrDriver.dir/all] Error 2 make: *** [Makefile:91: all] Error 2

Tested and this is present on wheaneys main branch if I do a basic cmake and make so nothing up with the flake.

2.0.5 tag builds cleanly however.

deephack1982 avatar Apr 28 '25 20:04 deephack1982

Yeah, the Flake builds from the Git repo, but I didn't have any issues building.

shymega avatar Apr 28 '25 20:04 shymega

How did you invoke the Flake?

shymega avatar Apr 28 '25 20:04 shymega

I allow the direnv to load and then issue the nix-build command in the root.

deephack1982 avatar Apr 28 '25 20:04 deephack1982

Ah, I can reproduce it now. It was cached.

Try building from shymega/nix-flake-support-v2.0.5 on my fork, and then we can establish if the Nix package can fix the issues with the glasses - I experienced this too.

shymega avatar Apr 28 '25 20:04 shymega

That branch builds fine but the driver output is still the same. Connection attempt fails and then it retries in a loop.

deephack1982 avatar Apr 28 '25 21:04 deephack1982

Quick note by the way as I was looking into the udev rules and how to package them. As per this thread it seems that any package containing $out/lib/udev/rules.d/ will be picked up by the system automatically. Actually not quite automatically, you need to specify sevices.udev.package = [ mypackage ]; but anyway.

https://discourse.nixos.org/t/how-to-create-files-in-the-etc-udev-rules-d-directory/11929/13

deephack1982 avatar Apr 28 '25 21:04 deephack1982

That branch builds fine but the driver output is still the same. Connection attempt fails and then it retries in a loop.

I found this happened for me too, but not on the first connection cycle of my VITURE Pros.

shymega avatar Apr 28 '25 21:04 shymega

Quick note by the way as I was looking into the udev rules and how to package them. As per this thread it seems that any package containing $out/lib/udev/rules.d/ will be picked up by the system automatically. Actually not quite automatically, you need to specify sevices.udev.package = [ mypackage ]; but anyway.

discourse.nixos.org/t/how-to-create-files-in-the-etc-udev-rules-d-directory/11929/13

Ah, that's good to hear. So the way I've packaged it makes this seamless.

shymega avatar Apr 28 '25 21:04 shymega

Actually I think line 86 in your package (default.nix) needs to become

cp ../udev/* $out/lib/udev/rules.d/

I'm testing it now though to see if it will work with sevices.udev.package

deephack1982 avatar Apr 28 '25 21:04 deephack1982

Actually I think line 86 in your package (default.nix) needs to become

cp ../udev/* $out/lib/udev/rules.d/

I'm testing it now though to see if it will work with sevices.udev.package

It shouldn't matter. usr/lib translates to lib in Nix, and indeed FHS-compliant distros.

As it works for usr/lib/systemd and usr/lib/${arch}, not sure it's worth changing.

I also had the udev rules set manually on my NixOS config, and aside from the amdgpu DRM crashes when plugging in the VITURE's, I don't think the udev rules are the problem - maybe?

shymega avatar Apr 28 '25 21:04 shymega

Yeah I have no idea if it's the udev rules or not. Something interesting though, if you used the extraRules option then based on this error report they may not be getting applied because we are applying uaccess.

https://github.com/NixOS/nixpkgs/issues/308681

deephack1982 avatar Apr 28 '25 21:04 deephack1982

This is a good point. I wonder if this gets affected by the rules in $out/usr/lib/udev/rules.d. Maybe @wheaney needs to rename the rules to be in line with the udev implementation?

shymega avatar Apr 28 '25 22:04 shymega

@deephack1982 Try now :-)

I'm also working on a Guix package, if that's of interest to anyone following this thread.

Awesome. Happy to play the role of @deephack1982 to test it on Guix system when you need there.

Kabouik avatar Apr 29 '25 01:04 Kabouik

I may have some fundamental misunderstanding when it's coming to the systemd unit and udev rules here. I'm not convinced that for me either are being applied. I manually run xrDriver you see to do my tests. This is my workflow, let me know if I'm doing something stupid.

I cd into the repo, give the direnv time to load and then run nix-build to make the package and have it put into /nix/store. I then run nix-env --install <nix store location> to install it into my system and at least the bin and libs seem to get added. However the systemd unit is not present if I run sudo systemctl list-units and sudo systemctl status xr-driver returns could not be found.

deephack1982 avatar Apr 29 '25 07:04 deephack1982

Okay took a bit of a different approach to this but I now have it working perfectly. I think the issue was indeed the udev rules. See attached files strip the txt, below is my file structure.

/etc/nixos

  • overlays
    • xrlinuxdriver-overlay.nix
  • pkgs
    • xrlinuxdriver/ # Of course in once PR is merged we will just fetchgit this in xrlinuxdriver.nix
    • xrlinuxdriver.nix

I then added the following to my configuration.nix and ran rebuild.

nixpkgs.overlays = [
    (import ./overlays/xrlinuxdriver-overlay.nix)
  ];

environment.systemPackages = with pkgs; [
    xrlinuxdriver
  ];

  services.udev.packages = with pkgs; [
    xrlinuxdriver
  ];

The systemd unit still needs to be manually installed but now all the udev stuff just works.

xrlinuxdriver.nix.txt xrlinuxdriver-overlay.nix.txt

deephack1982 avatar Apr 29 '25 10:04 deephack1982

Somewhat related, I'm working on a Breezy-Desktop package also. I have the following attached which builds but shows an error on run.

image breezy-desktop.nix.txt

deephack1982 avatar Apr 29 '25 14:04 deephack1982

For that you'll just need to disable the verification that the UI app does. See how the AUR setup does it by injecting --skip-verification into the desktop file: https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=breezy-desktop-gnome-git

wheaney avatar Apr 29 '25 14:04 wheaney

That worked immediately. Fantastic, now I'll spend some time tidying all of this up. Eventually it would be good to have the overlay load the needed files directly from Github. Then it could be just a couple of lines of config in configuration.nix to get everything working. For now though it can be manually installed and run.

deephack1982 avatar Apr 29 '25 14:04 deephack1982

I may have some fundamental misunderstanding when it's coming to the systemd unit and udev rules here. I'm not convinced that for me either are being applied. I manually run xrDriver you see to do my tests. This is my workflow, let me know if I'm doing something stupid.

I cd into the repo, give the direnv time to load and then run nix-build to make the package and have it put into /nix/store. I then run nix-env --install <nix store location> to install it into my system and at least the bin and libs seem to get added. However the systemd unit is not present if I run sudo systemctl list-units and sudo systemctl status xr-driver returns could not be found.

The derivation explains it - the systemd service is a user service, and you're executing the system systemctl manager.

Try systemctl --user $cmd $args.

shymega avatar Apr 29 '25 21:04 shymega