From 676f5c4b3117be7dbe24cc0beacc070b982decfb Mon Sep 17 00:00:00 2001 From: Cornelius Mika Date: Mon, 6 Nov 2017 10:28:42 +0100 Subject: [PATCH] files: allow arbitrary paths as home file names By sanitizing the home file name in the derivation name, the home file name is no longer exposed to the naming restrictions for nix store paths. For example, it is now possible to define home files with spaces in their names without providing a target or source attribute. --- modules/files.nix | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/modules/files.nix b/modules/files.nix index e294f97f0..71b940d20 100644 --- a/modules/files.nix +++ b/modules/files.nix @@ -6,8 +6,31 @@ with import ./lib/dag.nix { inherit lib; }; let cfg = config.home.file; + homeDirectory = config.home.homeDirectory; + # Figures out a valid Nix store name for the given path. + storeFileName = path: + let + # All characters that are considered safe. Note "-" is not + # included to avoid "-" followed by digit being interpreted as a + # version. + safeChars = + [ "+" "." "_" "?" "=" ] + ++ lowerChars + ++ upperChars + ++ stringToCharacters "0123456789"; + + empties = l: genList (x: "") (length l); + + unsafeInName = stringToCharacters ( + replaceStrings safeChars (empties safeChars) path + ); + + safeName = replaceStrings unsafeInName (empties unsafeInName) path; + in + "home_file_" + safeName; + in { @@ -53,8 +76,7 @@ in config = { target = mkDefault name; source = mkIf (config.text != null) ( - let name' = "user-etc-" + baseNameOf name; - in mkDefault (pkgs.writeText name' config.text) + mkDefault (pkgs.writeText (storeFileName name) config.text) ); }; })