Is there an easy way to set filesystem mount options?

How does one set default mount options for a given filesystem. For example, I would like all my btrfs subvolumes to be mounted with compress=zstd:1. However, even if I mount them that way and generate a new hardware-configuration.nix they will be mounted with the default options. I don’t particularly want to manually modify hardware-configuration.nix as then I wouldn’t be able to regenerate it. Further, I tried modifying it as a test and ignored the options I specified and used the default options anyway.

I am guessing there is some way of specifying mount options that I haven’t stumbled across yet.

I am not sure if switching to a config re-mounts the disks, did you try rebooting into that configuration?
I have following set up on my config for example:

  fileSystems."/" =
    { device = "/dev/disk/by-uuid/5474a12e-4fcb-44cb-9107-e7f333392836";
      fsType = "btrfs";
      options = [
        "noatime"
        "nodiratime"
        "discard"
      ];
    };

And it looks like it’s working:

~> mount | grep btrfs
/dev/mapper/vg0-root on / type btrfs (rw,noatime,nodiratime,ssd,discard,space_cache,subvolid=5,subvol=/)
3 Likes

One alternative is to have hardware-configuration.nix contain the device and fsType settings (auto-generated) and manually add the options setting in configuration.nix. NixOS fileSystems options listed in separate files merge.

I have been looking at this for the last couple of days and now I think I see what is happening at least partially.

After reading through the code, I don’t think you can change the defaults globally.

So, here is what I found about that. I have a bunch of btrfs mounts. If I specify options for some of them, the result is inconsistent. Let me demonstrate with a sample.

Here is an excerpt from hardware-configuration.nix

  fileSystems."/" =
    { device = "/dev/disk/by-uuid/d44aea1d-f740-46a0-9174-48fe9cb3888f";
      fsType = "btrfs";
      options = [ "subvol=nixos-root" "compress=zstd:1" ];
    };

  fileSystems."/nix" =
    { device = "/dev/disk/by-uuid/d44aea1d-f740-46a0-9174-48fe9cb3888f";
      fsType = "btrfs";
      options = [ "subvol=nixos-nix" ];
    };

  fileSystems."/var/log" =
    { device = "/dev/disk/by-uuid/d44aea1d-f740-46a0-9174-48fe9cb3888f";
      fsType = "btrfs";
      options = [ "subvol=nixos-varlog" ];
    };

  fileSystems."/home" =
    { device = "/dev/disk/by-uuid/d44aea1d-f740-46a0-9174-48fe9cb3888f";
      fsType = "btrfs";
      options = [ "subvol=nixos-home" "compress=lzo" ];
    };

They get correctly converted to /etc/fstab

/dev/disk/by-uuid/d44aea1d-f740-46a0-9174-48fe9cb3888f / btrfs subvol=nixos-root,compress=zstd:1 0 0
/dev/disk/by-uuid/d44aea1d-f740-46a0-9174-48fe9cb3888f /home btrfs subvol=nixos-home,compress=lzo 0 0
/dev/disk/by-uuid/d44aea1d-f740-46a0-9174-48fe9cb3888f /nix btrfs subvol=nixos-nix 0 0
/dev/disk/by-uuid/d44aea1d-f740-46a0-9174-48fe9cb3888f /var/log btrfs subvol=nixos-varlog 0 0

The generated mount files are also correct.

However, here is what mount, findmnt and /proc/mounts all show.

/dev/mapper/luks-sda2 on / type btrfs (rw,relatime,compress=lzo,space_cache,subvolid=299,subvol=/nixos-root)
/dev/mapper/luks-sda2 on /nix type btrfs (rw,relatime,compress=lzo,space_cache,subvolid=302,subvol=/nixos-nix)
/dev/mapper/luks-sda2 on /var/log type btrfs (rw,relatime,compress=lzo,space_cache,subvolid=301,subvol=/nixos-varlog)
/dev/mapper/luks-sda2 on /home type btrfs (rw,relatime,compress=lzo,space_cache,subvolid=300,subvol=/nixos-home)

Essentially, they all end up with the same options. I tested this on Fedora and I don’t see this behavior there. :confused:

That is a good idea. Thanks!

1 Like

don’t forget to add your user to the “storage” group