How to automatically mount external hard drive?

Hi,

I use an external hard drive which stores media file pretty frequently. Normally I would use Dolphin (on KDE) to automatically mount the drive and open up the drive with no issues. One day I found out that somehow this drive was mounted as a read-only file system. A classic “it used to work fine but it doesn’t work now”.

So I’m wondering, how do I automatically mount the drive when I plug in it, with the read/write permissions for normal user, but without opening any file manager or typing any commands?

I’ve tried the following, based on the file system section on the manual:

  # File systems
  # for mounting external hard drive for now
  fileSystems."/run/media/my-username/MyPassport" = {
    device = "/dev/sda2";
    fsType = "exfat";
    options = ["nofail" "uid=1000" "gid=100" "dmask=007" "fmask=117" "user" "u+rwx" "g+rwx" "o+rwx"];
  };

But:

  1. when I plugged in the hard drive, it didn’t seem to be mounted automatically.
  2. the mount point is correct but after mounted, its permissions were still:
drwxr-xr-x 2 root root 40 Oct 18 19:38 MyPassport

Can someone please help me on this?

1 Like

Have you tried any of:

services.devmon.enable = true;
services.gvfs.enable = true;
services.udisks2.enable = true;

You might need one or more depending on whether you’re using a Desktop Environment.

1 Like

Hi, I had not tried the three lines of configs you mentioned. But I did add those three. Looks like the hard drive was auto mounted. But its permissions is still

drwxr-xr-x 2 root root 40

I’m curious how do I mount the drive for $USER instead of root?

EDIT:

I was wrong. The hard drive was not mounted automatically. It was just the mount point successfully created. I need to manually do

sudo mount /dev/sda2 /run/media/username/MyPassport -t exfat

to mount the drive.

EDIT #2:

I’m using Sway.

1 Like

Sorry, not sure about Sway. FWIW, I often use bashmount bound to a hotkey, which works even without a DE and for me provides a nice balance between speed and control. (As an aside, more security-conscious devs advise against automounting in general, but this ofc depends on your threat model.)

2 Likes

did you add your user to the “storage” group?

1 Like