Search code examples
virtualboxshared-directorynixos

Combine two nix expressions in "add shared folder in virtualbox guest NixOS"


I have installed an NixOS in my virtualbox. I would like to add a shared folder in my NixOS. I have set up a shared folder called Code in Device->SharedFolder->Setting on my virtualbox side. I tried to insert Guest Addition image. It says:

Could not mount the media/drive 'C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso' (VERR_PDM_MEDIA_LOCKED).

I force mount it in Device->CDRom

ls -l /dev/cdrom and mount -t iso9660 -o ro /dev/cdrom /cdrom. Files of Guest Additions can be seen in /cdrom

virtualization.virtualbox.guest.enable = true in /etc/nixos/configuration.nix (I have read about somewhere that this line alone is sufficient to install Guest Addition in nixos.)

Then I set my configuration as:

...
fileSystems = [
  {
    mountPoint = "/";
    label = "nixos";
  }
];
...

fileSystems."/virtualboxshare" = {
  fsType = "vboxsf";
  device = "Code";
  option = [ "rw" ];
};

nixos-rebuild switch, it says:

error: attribute fileSystems."/virtualboxshare" at ... already defined at ... (fileSystems = ...)

So how can I combine these two expressions together and make my shard folder visable in nixOS?


Solution

  • You are defining fileSystems twice in the attrset that makes up your nixos configuration or module. In the first definition it's a list, whereas in the second, you're defining fileSystems as an attrset, by using the nested attribute set shorthand notation. So actually, this error is produced by the Nix language rather than the NixOS module system.

    This error can be solved by defining all your filesystems with the same notation you have used for /virtualboxshare.


    To get a better feeling for nested attrsets, it may be useful to play around with nix repl:

    $ nix repl
    Welcome to Nix version 2.0. Type :? for help.
    
    nix-repl> :p { a = {c = 2; }; a.b = 1; }
    { a = { b = 1; c = 2; }; }
    

    (Note the use of :p to evaluate everything instead of just the outermost attrset)