udev.extraRules fails at udevadm verify

I have a services.udev.extraRules:

services.udev.extraRules = ''
  ACTION=="change", SUBSYSTEM=="power_supply", ATTR{status}=="Discharging", ATTR{capacity}=="[0-9]", RUN+="${pkgs.systemd}/bin/systemd-run --machine=cube@.host --user ${pkgs.libnotify}/bin/notify-send -u critical "Battery Low" "Current level: $attr{capacity}%" -h string:x-canonical-private-synchronous:lowbat"
'';

the error gives:

Verifying udev rules using udevadm verify...
/nix/store/ggz7j3nb9bpfpn27bqd6xmgw3xjn6n85-udev-rules/99-local.rules:1 Invalid value "/nix/store/baxgs06659w4i6ggsfr56srjrb52v1h8-systemd-261.2/bin/systemd-run --machine=cube@.host --user /nix/store/ks0rn72qnj8q7lb9m6pngk86llpw3khl-libnotify-0.8.8/bin/notify-send -u critical "Battery Low" "Current level: $attr{capacity}%" -h string:x-canonical-private-synchronous:lowbat" for RUN (char 236: invalid substitution type), ignoring.
/nix/store/ggz7j3nb9bpfpn27bqd6xmgw3xjn6n85-udev-rules/99-local.rules: udev rules check failed.

I have also tried with backslash before double quotes around the text Battery Low, and Current level: … lowbat, which was in the original rules file copied from another Linux machine and confirmed working. May I know what I did wrong in porting it to NixOS?

Use %% instead of %.

1 Like

I cannot believe the fix was this simple. ToT Thank you!