impermanence/lib.nix

92 lines
1.8 KiB
Nix
Raw Normal View History

2020-06-04 21:42:16 +00:00
{ lib }:
let
2022-11-13 17:16:57 +00:00
inherit (lib)
filter
concatMap
concatStringsSep
hasPrefix
head
replaceStrings
optionalString
removePrefix
foldl'
elem
take
length
last
2022-11-13 17:16:57 +00:00
;
inherit (lib.strings)
sanitizeDerivationName
;
2020-06-04 21:42:16 +00:00
# ["/home/user/" "/.screenrc"] -> ["home" "user" ".screenrc"]
splitPath = paths:
(filter
(s: builtins.typeOf s == "string" && s != "")
(concatMap (builtins.split "/") paths)
);
# ["home" "user" ".screenrc"] -> "home/user/.screenrc"
dirListToPath = dirList: (concatStringsSep "/" dirList);
# ["/home/user/" "/.screenrc"] -> "/home/user/.screenrc"
concatPaths = paths:
let
prefix = optionalString (hasPrefix "/" (head paths)) "/";
path = dirListToPath (splitPath paths);
in
prefix + path;
parentsOf = path:
let
prefix = optionalString (hasPrefix "/" path) "/";
split = splitPath [ path ];
parents = take ((length split) - 1) split;
in
foldl'
(state: item:
state ++ [
(concatPaths [
(if state != [ ] then last state else prefix)
item
])
])
[ ]
parents;
sanitizeName = name:
replaceStrings
[ "." ] [ "" ]
(sanitizeDerivationName (removePrefix "/" name));
duplicates = list:
let
result =
foldl'
(state: item:
if elem item state.items then
{
items = state.items ++ [ item ];
duplicates = state.duplicates ++ [ item ];
}
else
state // {
items = state.items ++ [ item ];
})
{ items = [ ]; duplicates = [ ]; }
list;
in
result.duplicates;
2020-06-04 21:42:16 +00:00
in
{
2022-11-13 17:16:57 +00:00
inherit
splitPath
dirListToPath
concatPaths
parentsOf
2022-11-13 17:16:57 +00:00
sanitizeName
duplicates
;
}