21

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> Commented Aug 31, 2019 at 19:40

4 Answers 4

21

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!
6
  • 1
    So regarding the omission of a /mnt directory, the answer seems to be "make your own or mount it wherever you like". Commented May 1, 2016 at 18:40
  • 1
    What is the purpose of step 3? Does this make the mount permanent? Commented 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). Commented 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? Commented 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/… Commented Oct 29, 2021 at 12:54
10

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
2

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.

0

I'd mount it using ntfs-3g in /mnt

by default, udisks mounts filesystems at /run/media/$USER/$filesystem_name
and /run/media/$USER has mode 0700, so its readable only by $USER

to make the mountpoints readable by all users:

# /etc/nixos/configuration.nix # https://discourse.nixos.org/t/how-to-create-files-in-the-etc-udev-rules-d-directory/11929/11 services.udev.packages = [ # mount drives at /media/$filesystem_name with access for all users # /run/media/$USER has mode 0700 # https://unix.stackexchange.com/questions/473174/configure-udisks-permission (pkgs.writeTextFile { name = "udisks2-rules-share-mounts"; text = '' # UDISKS_FILESYSTEM_SHARED # ==1: mount filesystem to a shared directory (/media/$filesystem_name) # ==0: mount filesystem to a private directory (/run/media/$USER/$filesystem_name) # see also: man udisks ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{UDISKS_FILESYSTEM_SHARED}="1" ''; destination = "/etc/udev/rules.d/99-udisks2.rules"; }) ]; # https://unix.stackexchange.com/questions/710842/nixos-ensure-root-level-folder-exists systemd.tmpfiles.rules = [ # create /media directory for udisks2-rules-share-mounts "d /media 0755 root root 99999y" ]; 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.