Creating a custom udev rule

Hello all,

I’m trying to create a custom udev rule. I already have the rule itself in a text file. The canonical way to do this seems to be to define the rule as a derivation, say xilinx_udev_rules, then to add it to configuration.nix with services.udev.packages = [ xilinx_udev_rules ];. I’ve tried this, borrowing from a few working examples here and there, but without success. I have a fairly minimal derivation:

pkgs.stdenv.mkDerivation {
  name = "xilinx-udev-rules";

  src = ./rules/.;

  dontBuild = true;
  dontConfigure = true;

  installPhase = ''
    mkdir -p $out/lib/udev/rules.d
    cp 52-xilinx-digilent-usb.rules $out/lib/udev/rules.d

The relevant sections of my configuration.nix are

  udevRules = pkgs.callPackage ./udev/default.nix { inherit pkgs; };
in {
  services.udev.packages = [ udevRules ];

where ./udev/default.nix is the derivation defined above.

If I then do nixos-rebuild it seems to make some progress but then hits me with

/nix/store/qqmw740w7ra8s7yqa7lxg92pibx93mbv-udev-rules/52-xilinx-digilent-usb.rules (originally from /nix/store/yjf7lwr0yb60rf2qw5aq0sqlx91vr9nl-xilinx-udev-rules/lib/udev/rules.d/52-xilinx-
digilent-usb.rules) contains references to /sbin/udevcontrol.
builder for '/nix/store/yw3xil18qr9v5hy80nbj1kpx7yyfcbsg-udev-rules.drv' failed with exit code 1

I’m not sure how I’m getting this wrong since I’ve copied almost all of this from some working examples. Could it be that the derivation is local? It will build OK using nix-build, outside of nixos-rebuild.

Any help gratefully received.

If you just need a single udev rule set up and don’t mind using a different method, there is a attribute you can set in your configuration.nix file called services.udev.extraRules. It takes a string but you can create multiple rules there.

  services.udev.extraRules = ''
    # Your rule goes here

The issue may be that the rule has a hardcoded path to /sbin/udevcontrol in it based on the error message you’ve shown, where as you would want to refer to the derivation when writing the rule if you need to execute a particular program. That’s just a guess on my part, usually there are rewrite tools that kick in automatically but they may not extend to this context.


Actually, thank you, this helped me figure things out. The /sbin/udevcontrol reference appeared in a comment in one of the files. I’ve deleted all the comments and it now seems to work fine. I guess that the udev tool may not be respecting comments.


1 Like