From 0fed0563f60415d8df6ad093aa70324f009e9d77 Mon Sep 17 00:00:00 2001 From: Eman Resu Date: Mon, 20 Jul 2026 09:22:32 -0400 Subject: [PATCH] nixos/lib/utils: partially apply hasSuffix and removeSuffix Also move helpers to higher scope. --- nixos/lib/utils.nix | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/nixos/lib/utils.nix b/nixos/lib/utils.nix index d26d8b36acb5..692f0aac40a4 100644 --- a/nixos/lib/utils.nix +++ b/nixos/lib/utils.nix @@ -42,6 +42,26 @@ let in let + hasSlashSuffix = hasSuffix "/"; + # normalisePath adds a slash at the end of the path if it didn't already + # have one. + # + # The reason slashes are added at the end of each path is to prevent `b` + # from accidentally depending on `a` in cases like + # a = { mountPoint = "/aaa"; ... } + # b = { device = "/aaaa"; ... } + # Here a.mountPoint *is* a prefix of b.device even though a.mountPoint is + # *not* a parent of b.device. If we add a slash at the end of each string, + # though, this is not a problem: "/aaa/" is not a prefix of "/aaaa/". + normalisePath = path: "${path}${optionalString (!hasSlashSuffix path) "/"}"; + normalise = + mount: + mount + // { + device = normalisePath (toString mount.device); + mountPoint = normalisePath mount.mountPoint; + depends = map normalisePath mount.depends; + }; utils = rec { # Copy configuration files to avoid having the entire sources in the system closure @@ -71,29 +91,8 @@ let fsBefore = a: b: let - # normalisePath adds a slash at the end of the path if it didn't already - # have one. - # - # The reason slashes are added at the end of each path is to prevent `b` - # from accidentally depending on `a` in cases like - # a = { mountPoint = "/aaa"; ... } - # b = { device = "/aaaa"; ... } - # Here a.mountPoint *is* a prefix of b.device even though a.mountPoint is - # *not* a parent of b.device. If we add a slash at the end of each string, - # though, this is not a problem: "/aaa/" is not a prefix of "/aaaa/". - normalisePath = path: "${path}${optionalString (!(hasSuffix "/" path)) "/"}"; - normalise = - mount: - mount - // { - device = normalisePath (toString mount.device); - mountPoint = normalisePath mount.mountPoint; - depends = map normalisePath mount.depends; - }; - a' = normalise a; b' = normalise b; - in hasPrefix a'.mountPoint b'.device || hasPrefix a'.mountPoint b'.mountPoint