-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
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
Improvements to pathType, pathIsDirectory and pathIsRegularFile #224834
Conversation
These functions only work with the filesystem, they don't import anything as sources
Something I don't want to go into for this PR, but maybe in the future: The semantics of
We should consider having alternate functions with cleaner semantics in the future:
These laws would then hold:
Unfortunately the implementation of these functions would have to be a bit hacky (throwing for non-existent paths not implemented here): {
isDirectory = path: builtins.pathExists "${toString path}/";
isFile = path: ! builtins.pathExists "${toString path}/";
} This uses the fact that:
To implement these I'd propose adding more builtins, either:
|
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/tweag-nix-dev-update-47/27387/1 |
That's good. How about this?
|
lib/filesystem.nix
Outdated
=> "regular" | ||
*/ | ||
pathType = path: | ||
# TODO: Once Nix 2.14 is released, switch to `builtins.readFileType`: |
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.
2.14 was not a success, but 2.15 is out now.
This can be upgraded to a polyfill now.
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.
Done. Also take a look at NixOS/nix#7447 (comment)
85f7a5c
to
7c57a47
Compare
@roberth I'd probably go for |
7c57a47
to
e9cc2dc
Compare
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.
Some minor suggestions and a less minor idea to improve the developer experience. I'd add the comments myself, but it's better to share the idea.
Co-Authored-By: Robert Hensing <robert@roberthensing.nl>
Co-Authored-By: Robert Hensing <robert@roberthensing.nl>
Previously this function couldn't handle / being passed, it would throw an error: error: attribute '' missing at nixpkgs/lib/filesystem.nix:24:20: 23| */ 24| pathType = path: (readDir (dirOf path)).${baseNameOf path}; | ^ 25| Consequently this also fixes the lib.filesystem.{pathIsDirectory,pathIsRegularFile} functions.
Previously it would fail with error: attribute 'nonexistent' missing at nixpkgs/lib/filesystem.nix:29:10: 28| if dirOf path == path then "directory" 29| else (readDir (dirOf path)).${baseNameOf path}; | ^ 30|
Co-Authored-By: Robert Hensing <robert@roberthensing.nl>
Co-Authored-By: Robert Hensing <robert@roberthensing.nl>
e9cc2dc
to
378bf1a
Compare
Successfully created backport PR for |
Git push to origin failed for release-23.05 with exitcode 1 |
Description of changes
This is related to the path library effort.
Improves the
lib.sources.pathType
,lib.sources.pathIsDirectory
andlib.sources.pathIsRegularFile
functions, each of which is a single commit:lib.filesystem
, since they only work with the filesystem, they don't import any sources. The functions are still available inlib.sources
, but will be deprecated after the next release./
is passed. Previously they threw an unintuitive error:pathType
error message for invalid paths. Previously this was the error:pathType
can get replaced with the newbuiltins.readFileType
once Nix 2.14 is releasedThis work is sponsored by Antithesis ✨
Things done
nix-build doc
)lib/tests/filesystem.sh
on its ownnix-build lib/tests/release.nix