Udev rules for persistent sda/sdb?


I’ve bought an Intel NUC515RYH. It’s now having an internal m.2 sata ssd (500GB) and a 2.5 Zoll sata hdd (2TB).

When I first installed Nixos, the ssd was on /dev/sda and the hdd on /dev/sdb
A few reboots later it’s reversed (ssd = /dev/sdb, hdd = /dev/sda)

Yeah, the system works (I’m using fileSystems entries with /dev/disk/by-label/…) but I do want to get back the old behavior^^

So I’ve added this to configuration.nix:

  services.udev = {
    extraRules = ''
      # Seagate SpinPoint M9T 2TB SATA HDD
      KERNEL=="sd*", SUBSYSTEMS=="scsi", ATTRS{model}=="ST2000LM003 HN-M", SYMLINK+="sdb%n"
      # Crucial MX500 500GB m.2 SATA SSD
      KERNEL=="sd*", SUBSYSTEMS=="scsi", ATTRS{model}=="CT500MX500SSD4", SYMLINK+="sda%n"

When I use

nixos-rebuild switch

it adds these entries to the 99-local.rules file but when I reboot both drives still use the reversed /dev/sda|b names.

Are my rules just executed too late by the udev service and if this should be the case, how can I let them be executed earlier in the process?

/dev/sda and /dev/sdb naming are done by the kernel itself which obviously runs way earlier than udev. That behaviour is not configurable unfortunately. Hence making a symlink named /dev/sda through udev won’t work; as the device will already exist way before udev runs (and the order is indeed undefined).

Instead you should symlink to a path that does not exist yet, e.g SYMLINK+="mydisk%n"

However note that udev already ships with built-in rules that puts stable names under /dev/disk/by-{uuid,id,path} Perhaps you can use those instead


I see,

thanks @arianvp!