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.
This commit is contained in:
Cornelius Mika 2017-11-06 10:28:42 +01:00
parent 14083a0857
commit 676f5c4b31

View file

@ -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)
);
};
})