18

I have a USB stick and an NTFS hard drive partition that I'd want to use in NixOS. On some other distribution, I'd mount it using ntfs-3g in /mnt. But on NixOS, the directory doesn't exist; I suppose NixOS has some other canonical way and/or place of doing that.

In NixOS, how should one set up automounting of external partitions, preferably using configuration.nix?

1
  • open nautilus if you have services.xserver.desktopManager.gnome3.enable = true; in your configuration.nix and you'll see the drive and it'll get mounted at /run/media/$USER/<drive name>
    – user114651
    Aug 31, 2019 at 19:40

3 Answers 3

18

Well, I costumarily use bashmount or udisksctl to mount USB sticks. They will be mounted in /run/media/$(user name)/$(drive label or UUID).

But if you are talking about an internal harddisk or partition in a local harddrive, the simplest way is:

  1. Create a directory of your preference, as /mnt/windows-partition
  2. Mount the desired partition, say /dev/sdn5, in that directory:

$ mount /dev/sdn5 /mnt/windows-partition

  1. Run nixos-generate-config. It will update /etc/nixos/hardware-configuration.nix to match the new partition configuration (and configuration.nix stays untouched, unless you use the --force option).
  2. And, finally, a nixos-rebuild switch!
5
  • 1
    So regarding the omission of a /mnt directory, the answer seems to be "make your own or mount it wherever you like". May 1, 2016 at 18:40
  • 1
    What is the purpose of step 3? Does this make the mount permanent? Nov 21, 2018 at 21:27
  • Yep! That step is to register /mnt/windows-partition in the (machine-generated) file hardware-configuration.nix. That way, it will be mounted next boot (after a nixos-rebuild, of course). Nov 23, 2018 at 13:19
  • @AndersonTorres I followed these steps to permanently add a USB drive, but now that drive is mounted as /dev/sda whenever I boot the system. This is messing with NixOS, because it isn't the boot disk. The generated hardware config file refers to the disk as device = "/dev/disk/by-uuid/<UUID>";. How do I change the configuration file to make sure my boot disk is /dev/sda?
    – HiDefender
    Oct 21, 2021 at 10:47
  • 1
    @HiDefender I am afraid I am not understanding you here. As far as I remember, by default Nixpkgs uses UUIDs in its hardware configuration. It merely ignores /dev/sd*. If I am understanding it correctly, you need to work with udev rules in order to make those symlinks. I can't help here, however. discourse.nixos.org/t/… Oct 29, 2021 at 12:54
7

I personally use udisks for that. That means, if i want to mount an USB stick, i just have to plug it in and run:

$ udisksctl mount -b /dev/sdc

I aliased udisksctl mount -b to udm to make it even shorter. The device is then mounted in /run/media/$USER/$DEVICE_LABEL and accessible for you.

I think, some tools (e.g. nautilus and ldm) can do that automatically for you.

EDIT: ah, i just found out that udisks can also automount devices.

1

Directly modifying hardware-configuration.nix file


To achieve the same results as mentioned in other answers, you can as well add another fileSystems config entry in hardware-configuration.nix:

{
  ...

  fileSystems."${mount_location}" = {
    device = "/dev/${device}";
    fsType = "${filesystem_extension}";
  };

  ...
}
NOTES:

To get filesystem extension information about a particular partition run:

df -T | grep /dev/${device}

Example configuration for /dev/sda1 device, having ext4 file system extension, that is to be mounted at /mnt/sda1 location:

{
  ...

  fileSystems."/mnt/sda1" = {
    device = "/dev/sda1";
    fsType = "ext4";
  };

  ...
}

I think that such a solution is more unique, as the device name would not be overwritten by NixOS specific namespaces as in nixos-generate-config's solution.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .