Abstract fileSystems NFS mount

Hey everyone,

I wanted to try to abstract NFS mounting in my configuration files, but – as I am new to nix – I have troubles with the mount point and passing strings to it.

Basically, I want to have something like this (with several more folders):

{
  fileSystems = {
    "/mnt/backup" = {
      device = "192.168.0.1:/volume1/backup";
      fsType = "nfs";
    };
    "/mnt/media" = {
      device = "192.168.0.1:/volume1/media";
      fsType = "nfs";
    };
}

abstracted to something following this idea:

{
  fileSystems =
    let
      mountNFS = {name, serverIP}:
        {
          "/mnt/$name" = {
            device = "$serverIP:/volume1/$name";
            fsType = "nfs";
          };
        };
    in map mountNFS
      [ { name = "backup"; serverIP = "192.168.0.1"; }
        { name = "media"; serverIP = "192.168.0.1"; }
      ];
}

I tried to concat strings but that was far worse… Of course, I get an error with the version above, which is:

error: The option `fileSystems.unnamed-1.1."/mnt/\$name"' defined in `/etc/nixos/home-thomas.nix' does not exist.

Any idea how to improve this with a correct nix syntax?
Thanks, and all the best,
Thomas

use “${var}” to do string interpolation in Nix

    let
      mountNFS = {name, serverIP}:
        {
          "/mnt/${name}" = {
            device = "${serverIP}:/volume1/${name}";
            fsType = "nfs";
          };
        };
    in map mountNFS
      [ { name = "backup"; serverIP = "192.168.0.1"; }
        { name = "media"; serverIP = "192.168.0.1"; }
      ];
1 Like

What you have won’t quite work for the fileSystems option. It expects either an attrset whose keys are mount points and values are the various FS settings, or a list of FS settings that include a mountPoint = "..."; setting. So rather than:

{
  "/mnt/${name}" = {
    device = "${serverIP}:/volume1/${name}";
    fsType = "nfs";
  };
}

Because map won’t merge all these resulting attrsets in the list for you, you need something more like:

{
  mountPoint = "/mnt/${name}";
  device = "${serverIP}:/volume1/${name}";
  fsType = "nfs";
}

@jonringer: thanks a lot for your answer, and sorry for that basic syntax error.
@ElvishJerricco: that was exactly what I needed, thanks – that solved my problem. I thought that the mountPoint had to be parent to submodules device and fsType – at least that’s what I guessed from the fileSystems.<name?>.* syntax in the NixOS-options. That is way more convenient for me like that.

However, I don’t know if I should open another topic, but my system is booting more slowly automounting my NFS directories, all the more if I am not connected to my network (and that I know it will fail…). I tried to put this option:

{
  # ...
  options = ["x-systemd.automount" "noauto"];
}

as presented in this wiki, but I got mounting errors when trying to access those folders. Any idea of options to add to avoid that minor problem?

I don’t know if this is related, but I also have a long start-up (after user login), blamed on DHCP:

[tcip@nixos:~]$ systemd-analyze blame 
14.540s dhcpcd.service

and I can’t enable my Wifi card, as explained in my previous topic.
For your information, I’m running:

[tcip@nixos:~]$ hostnamectl 
   Static hostname: nixos
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 8d63d12262424a9da8e939aa57049018
           Boot ID: a46f09658e61403c890f1e16b9053be4
  Operating System: NixOS 20.03pre202088.e89b21504f3 (Markhor)
            Kernel: Linux 4.19.84
      Architecture: x86-64
[tcip@nixos:~]$ nix-channel --list | grep nixos
nixos https://nixos.org/channels/nixos-unstable

Thanks again, and all the best,