From 9226d4e9ee551ff7db365937949711ccd96611b4 Mon Sep 17 00:00:00 2001 From: r-vdp Date: Tue, 3 Dec 2024 11:35:02 +0100 Subject: [PATCH] nixos/filesystems: don't silently ignore label when device is set Before this change, when both device and label were set, the label would be silently ignored. This is especially problematic when the device is set in another module, and it's not immediately obvious why the label is not having any effect. Additionally, some other modules use the device option to get the device of a filesystem, but this option is not populated when only the label is set, causing those modules to conclude that the filesystem does not have a backing device. With this change, we populate the device option using the label when the label is set. This means that we get a clear error message when both the device and label are set, and that the device is properly populated whenever the label is used to specify the backing device. --- nixos/modules/tasks/filesystems.nix | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/nixos/modules/tasks/filesystems.nix b/nixos/modules/tasks/filesystems.nix index 5c95cd3d451e..b7d18dfe4173 100644 --- a/nixos/modules/tasks/filesystems.nix +++ b/nixos/modules/tasks/filesystems.nix @@ -4,6 +4,8 @@ with lib; with utils; let + # https://wiki.archlinux.org/index.php/fstab#Filepath_spaces + escape = string: builtins.replaceStrings [ " " "\t" ] [ "\\040" "\\011" ] string; addCheckDesc = desc: elemType: check: types.addCheck elemType check // { description = "${elemType.description} (with check: ${desc})"; }; @@ -136,6 +138,8 @@ let }; + config.device = lib.mkIf (config.label != null) "/dev/disk/by-label/${escape config.label}"; + config.options = let inInitrd = utils.fsNeededForBoot config; in mkMerge [ @@ -196,11 +200,8 @@ let ]; isBindMount = fs: builtins.elem "bind" fs.options; skipCheck = fs: fs.noCheck || fs.device == "none" || builtins.elem fs.fsType fsToSkipCheck || isBindMount fs; - # https://wiki.archlinux.org/index.php/fstab#Filepath_spaces - escape = string: builtins.replaceStrings [ " " "\t" ] [ "\\040" "\\011" ] string; in fstabFileSystems: { }: concatMapStrings (fs: (if fs.device != null then escape fs.device - else if fs.label != null then "/dev/disk/by-label/${escape fs.label}" else throw "No device specified for mount point ‘${fs.mountPoint}’.") + " " + escape fs.mountPoint + " " + fs.fsType