Jump to content

NTFS: Difference between revisions

From Official NixOS Wiki
m Fix grammar and spelling
34j (talk | contribs)
m Add link to man page for mount, add -o uid=$UID to mount command for consistency
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
New Technology File System (NTFS) is a proprietary journaling [[Filesystems|file system]] developed by Microsoft. It is still in use by modern Windows systems, although NTFS has not evolved since the release of version 3.1 in 2001.
New Technology File System (NTFS) is a proprietary journaling [[Filesystems|file system]] developed by Microsoft. It is still in use by modern Windows systems, although NTFS has not evolved since the release of version 3.1 in 2001.
== Enable NTFS support at boot ==
<syntaxHighlight lang=nix>
boot.supportedFilesystems = [ "ntfs" ];
</syntaxHighlight>
NixOS uses NTFS-3G for NTFS support.
=== Enable write mode ===
<syntaxhighlight lang="nixos">
  fileSystems."/path/to/mount/to" =
    { device = "/path/to/the/device";
      fsType = "ntfs-3g";
      options = [ "rw" "uid=theUidOfYourUser"];
    };
</syntaxhighlight>
== Mount NTFS filesystem ==
<syntaxhighlight lang="sh">
# mount /dev/sdeX /mnt -t ntfs-3g
</syntaxhighlight>


== Mount NTFS filesystem on boot ==
== Mount NTFS filesystem on boot ==


# Add NTFS support to ''/etc/nixos/configuration.nix'' (see [[#Enable NTFS support at boot]] above)
Using [[nixos-generate-config]] to automatically generate Nix config is the recommended way to setup filesystems.
#Run the following commands<syntaxhighlight lang="bash">
# Apply new config on next boot
sudo nixos-rebuild boot
# Reboot your system
reboot
 
# Mount the hard drive. Replace X & /mnt/folder as needed
mount /dev/sdX /mnt/folder -t ntfs3


# Generate /etc/nixos/hardware-configuration.nix
1. Run {{ic|lsblk}} to list device names.
nixos-generate-config
{{code|
</syntaxhighlight>
sd...
# Edit ''/etc/nixos/hardware-configuration.nix''<syntaxhighlight lang="diff">
└─sdX
  # /etc/nixos/hardware-configuration.nix
}}
2. Mount the device using [https://man7.org/linux/man-pages/man8/mount.8.html {{ic|mount}}], where {{ic|/dev/sdX}} replaced with your device name and {{ic|/mnt/sdX}} replaced with an existing folder path to mount your drive.
{{code|<nowiki>mount /dev/sdX /mnt/sdX -t ntfs3 -o uid=$UID</nowiki>}}
3. Run {{ic|nixos-generate-config --dir .}} to generate hardware configuration. This will <strong>automatically</strong> add all currently mounted devices to {{ic|hardware-configuration.nix}}.
{{file|/etc/nixos/hardware-configuration.nix|diff|3=
+  boot.supportedFilesystems = [ "ntfs" ];
+  fileSystems."/mnt/sdX" = {
+    device = "/dev/disk/by-uuid/F258FB9E58FB5FB1";
+    fsType = "ntfs3";
+  };
}}
4. Add {{ic|<nowiki>"uid=$UID"</nowiki>}} to {{nixos:option|fileSystems.*.options|fileSystems.<name>.options}} to get write access, where {{ic|<nowiki>$UID</nowiki>}} <strong>replaced with your UID</strong>:
{{file|/etc/nixos/configuration.nix|nix|3=
fileSystems =
  let
    ntfs-drives = [
      "/mnt/sdX"
    ];
  in
  lib.genAttrs ntfs-drives (path: {
    options = [
      "uid=$UID" # REPLACE "$UID" WITH YOUR ACTUAL UID!
      # "nofail"
    ];
  });
}}


  # Your /dev/disk/by/uuid/... and /mnt/folder will have different names!
{{note|You may find your UID by running {{ic|echo $UID}}.}}
  fileSystems."/mnt/folder" =
{{aside|It is not recommended to manually edit {{ic|hardware-configuration.nix}}.}}
      { device = "/dev/disk/by-uuid/7997ef90-6333-4c60-b137-d5cf2423e91b";
5. {{evaluate}}
-      fsType = "ntfs3";
+      fsType = "ntfs-3g";
+      options = [ "rw" "uid=UID"];
      };
</syntaxhighlight>''Note: you can quickly find your UID by running '''echo $UID'''''
#Run '''nixos-rebuild switch'''
#Done!


== Troubleshooting ==
== Troubleshooting ==

Latest revision as of 04:36, 16 November 2025

New Technology File System (NTFS) is a proprietary journaling file system developed by Microsoft. It is still in use by modern Windows systems, although NTFS has not evolved since the release of version 3.1 in 2001.

Mount NTFS filesystem on boot

Using nixos-generate-config to automatically generate Nix config is the recommended way to setup filesystems.

1. Run lsblk to list device names.

sd...
└─sdX

2. Mount the device using mount, where /dev/sdX replaced with your device name and /mnt/sdX replaced with an existing folder path to mount your drive.

mount /dev/sdX /mnt/sdX -t ntfs3 -o uid=$UID

3. Run nixos-generate-config --dir . to generate hardware configuration. This will automatically add all currently mounted devices to hardware-configuration.nix.

≡︎ /etc/nixos/hardware-configuration.nix
+  boot.supportedFilesystems = [ "ntfs" ];
+  fileSystems."/mnt/sdX" = {
+    device = "/dev/disk/by-uuid/F258FB9E58FB5FB1";
+    fsType = "ntfs3";
+  };

4. Add "uid=$UID" to fileSystems.<name>.options to get write access, where $UID replaced with your UID:

❄︎ /etc/nixos/configuration.nix
fileSystems =
  let
    ntfs-drives = [
      "/mnt/sdX"
    ];
  in
  lib.genAttrs ntfs-drives (path: {
    options = [
      "uid=$UID" # REPLACE "$UID" WITH YOUR ACTUAL UID!
      # "nofail"
    ];
  });
Note: You may find your UID by running echo $UID.
Sidenote: It is not recommended to manually edit hardware-configuration.nix.

5.

🟆︎
Tip: In order to affect your NixOS system by your nix-language-specific changes you must first evaluate it:
$ nixos-rebuild switch --sudo


Troubleshooting

Read-only file system

This is most likely caused by Windows not marking the disk as "clean" and unmounted.

To verify:

journalctl -b0 | grep -i "The disk contains an unclean file system"

It should return a similar message to what follows:

The disk contains an unclean file system (0,0). Metadata
kept in Windows cache, refused to mount. Falling back to
read-only mount because the NTFS partition is in an unsafe
state. Please resume and shutdown Windows fully (no
hibernation or fast restarting.)

If you have shutdown Windows fully, and not used hibernation, it may be caused by the fast startup or fast boot feature of Windows. It has been reported that major Windows updates may reset this setting to on.

This TechNet entry explains how to disable fast startup. Additionally, this blog post on howtogeek.com explains how the fast startup mode works, and how to disable it.