How to combine common settings among Samba mount?

Currently, I’m mounting 3 Samba directories with the following config

fileSystems."/mnt/data-raid" = {
    device = "//192.168.179.69/data-raid";
    fsType = "cifs";
    options = let
      automount_opts = "x-systemd.automount,noauto,x-systemd.idle-timeout=60,x-systemd.device-timeout=5s,x-systemd.mount-timeout=5s";
    in [ "${automount_opts},credentials=/home/${user}/smb-secrets" ];
  };
  fileSystems."/mnt/data-wd" = {
    device = "//192.168.179.69/data-wd";
    fsType = "cifs";
    options = let
      automount_opts = "x-systemd.automount,noauto,x-systemd.idle-timeout=60,x-systemd.device-timeout=5s,x-systemd.mount-timeout=5s";
    in [ "${automount_opts},credentials=/home/${user}/smb-secrets" ];
  };
  fileSystems."/mnt/data-buffalo" = {
    device = "//192.168.179.69/data-buffalo";
    fsType = "cifs";
    options = let
      automount_opts = "x-systemd.automount,noauto,x-systemd.idle-timeout=60,x-systemd.device-timeout=5s,x-systemd.mount-timeout=5s";
    in [ "${automount_opts},credentials=/home/${user}/smb-secrets" ];
  };

As you can see, the following part is repeated 3 times

    fsType = "cifs";
    options = let
      automount_opts = "x-systemd.automount,noauto,x-systemd.idle-timeout=60,x-systemd.device-timeout=5s,x-systemd.mount-timeout=5s";
    in [ "${automount_opts},credentials=/home/${user}/smb-secrets" ];

Is there any way I can make this common setting into one place?

Just bring the declaration up higher in the file. To reuse the fsType declaration, you’d probably want to make an attribute set containing the shared options and merge them with the later options using lib.recursiveUpdate

let automount_opts = "x-systemd.automount,noauto,x-systemd.idle-timeout=60,x-systemd.device-timeout=5s,x-systemd.mount-timeout=5s,credentials=/home/${user}/smb-secrets";
in {
  fileSystems."/mnt/data-raid" = {
    device = "//192.168.179.69/data-raid";
    fsType = "cifs";
    options = [ automount_opts ];
  };
  fileSystems."/mnt/data-wd" = {
    device = "//192.168.179.69/data-wd";
    fsType = "cifs";
    options = [ automount_opts ];
  };
  fileSystems."/mnt/data-buffalo" = {
    device = "//192.168.179.69/data-buffalo";
    fsType = "cifs";
    options = [ automount_opts ];
  };
}
1 Like