-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib.path.splitRoot
: init
#244358
lib.path.splitRoot
: init
#244358
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -271,8 +271,57 @@ in /* No rec! Add dependencies on this file at the top. */ { | |
second argument: "${toString path2}" with root "${toString path2Deconstructed.root}"''; | ||
joinRelPath components; | ||
|
||
/* | ||
Split the filesystem root from a [path](https://nixos.org/manual/nix/stable/language/values.html#type-path). | ||
The result is an attribute set with these attributes: | ||
- `root`: The filesystem root of the path, meaning that this directory has no parent directory. | ||
- `subpath`: The [normalised subpath string](#function-library-lib.path.subpath.normalise) that when [appended](#function-library-lib.path.append) to `root` returns the original path. | ||
|
||
Laws: | ||
- [Appending](#function-library-lib.path.append) the `root` and `subpath` gives the original path: | ||
|
||
p == | ||
append | ||
(splitRoot p).root | ||
(splitRoot p).subpath | ||
|
||
- Trying to get the parent directory of `root` using [`readDir`](https://nixos.org/manual/nix/stable/language/builtins.html#builtins-readDir) returns `root` itself: | ||
|
||
dirOf (splitRoot p).root == (splitRoot p).root | ||
|
||
Type: | ||
splitRoot :: Path -> { root :: Path, subpath :: String } | ||
|
||
Example: | ||
splitRoot /foo/bar | ||
=> { root = /.; subpath = "./foo/bar"; } | ||
|
||
splitRoot /. | ||
=> { root = /.; subpath = "./."; } | ||
|
||
# Nix neutralises `..` path components for all path values automatically | ||
splitRoot /foo/../bar | ||
=> { root = /.; subpath = "./bar"; } | ||
|
||
splitRoot "/foo/bar" | ||
=> <error> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reader will wonder what's the problem with this? Surely I think it might be sensible to treat all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That feels like something that should be supported by |
||
*/ | ||
roberth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
splitRoot = path: | ||
assert assertMsg | ||
(isPath path) | ||
"lib.path.splitRoot: Argument is of type ${typeOf path}, but a path was expected"; | ||
let | ||
deconstructed = deconstructPath path; | ||
in { | ||
root = deconstructed.root; | ||
subpath = joinRelPath deconstructed.components; | ||
}; | ||
fricklerhandwerk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/* Whether a value is a valid subpath string. | ||
|
||
A subpath string points to a specific file or directory within an absolute base directory. | ||
It is a stricter form of a relative path that excludes `..` components, since those could escape the base directory. | ||
|
||
- The value is a string | ||
|
||
- The string is not empty | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's unfortunate that this concept does not have Nix documentation yet.
(no action required now)