Change directory permissions declaratively

Hello,
I have an external drive /mnt/gamedrive, it is defined in my hardware.nix like this:

fileSystems."/mnt/gamedrive" =
    { device = "/dev/disk/by-uuid/ecd62282-f6ad-43b8-94e0-ee06c90646ad";
      fsType = "ext4";
    };

Is there a way I can declaratively set the permissions of this directory to belong to my user?

Thanks!

File permissions are part of the filesystem itself, and can’t be changed by a simple mount. You should have set this when you partitioned the disk.

To declaratively partition your disks, you can use disko, but at this point it’s too late and you just have to manually set permissions.

1 Like

I think systemd tmpfiles may be what you are looking for?

https://search.nixos.org/options?channel=unstable&from=0&size=50&sort=relevance&type=packages&query=systemd.tmpfiles

https://www.freedesktop.org/software/systemd/man/latest/tmpfiles.d.html#

You can use it to set permissions for files, make/delete them, etc.

edit: ignore, I mixed sth. up, see TLATER’s comment below

Have you tried

fileSystems."/mnt/gamedrive" =
    { # ...
      options = [ "uid=1234" ];  # adjust for user
    };```

That’s not a generic mount option and ext4 does not have it; it will be ignored. It’d work on a FAT filesystem, but for all the wrong reasons.

Just change the file permissions when the disk is created, that is how ext4 - and in fact all UNIXy filesystems - is intended to be used. Permissions are metadata stored alonside the files, and therefore part of the data, and inherently imperative.

If it’s removable media, use udisks2 to mount it instead.

2 Likes

Ok thanks for the info