forked from nix-community/impermanence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.nix
133 lines (117 loc) · 3.27 KB
/
lib.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
{ lib }:
let
inherit (lib)
concatMap
concatStringsSep
elem
filter
foldl'
hasPrefix
head
last
length
optionalString
removePrefix
replaceStrings
take
toList
;
inherit (lib.strings)
sanitizeDerivationName
;
# ["/home/user/" "/.screenrc"] -> ["home" "user" ".screenrc"]
# "/home/user/.screenrc" -> ["home" "user" ".screenrc"]
splitPath = paths:
(filter
(s: builtins.typeOf s == "string" && s != "")
(concatMap (builtins.split "/") (toList 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;
isParentOf = parent: child:
child != parent && lib.lists.hasPrefix (splitPath parent) (splitPath child);
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;
getHomeDirCfg = { pkgs, homeDirectory, storage }: dir:
let
dirCfg.storage = storage;
dirCfg.mountDir =
if dirCfg.storage.removePrefixDirectory then
dirListToPath (builtins.tail (splitPath [ dir ]))
else
dir;
dirCfg.mountPoint = concatPaths [ homeDirectory dirCfg.mountDir ];
dirCfg.targetDir = concatPaths [ dirCfg.storage.persistentStoragePath dir ];
dirCfg.escaped.mountPoint = lib.escapeShellArg dirCfg.mountPoint;
dirCfg.escaped.targetDir = lib.escapeShellArg dirCfg.targetDir;
dirCfg.sanitized.targetDir = sanitizeName dirCfg.targetDir;
dirCfg.sanitized.mountPoint = sanitizeName dirCfg.mountPoint;
dirCfg.sanitized.mountDir = sanitizeName dirCfg.mountDir;
dirCfg.unitName = "bindMount--${dirCfg.sanitized.mountDir}";
dirCfg.runBindfsArgs =
let
bindfsOptions =
lib.lists.optional (!dirCfg.storage.allowOther) "no-allow-other"
++ lib.lists.optional (lib.versionAtLeast pkgs.bindfs.version "1.14.9") "fsname=${dirCfg.targetDir}"
;
in
lib.lists.optionals (bindfsOptions != [ ]) [ "-o" (concatStringsSep "," bindfsOptions) ]
;
in
dirCfg;
in
{
inherit
concatPaths
dirListToPath
duplicates
getHomeDirCfg
isParentOf
parentsOf
sanitizeName
splitPath
;
}