From c701a4dd299aaa82589e4e4d4ce49ae330729a25 Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Wed, 5 Apr 2023 16:10:09 +0200 Subject: [PATCH] lib.sources.pathType and co.: Move to lib.filesystem These functions only work with the filesystem, they don't import anything as sources --- lib/default.nix | 5 +++-- lib/filesystem.nix | 26 ++++++++++++++++++++++++++ lib/sources.nix | 37 ++++++++++++++++++------------------- 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/lib/default.nix b/lib/default.nix index 0424db36b2e9..8fea4b8ad637 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -117,10 +117,11 @@ let inherit (self.meta) addMetaAttrs dontDistribute setName updateName appendToName mapDerivationAttrset setPrio lowPrio lowPrioSet hiPrio hiPrioSet getLicenseFromSpdxId getExe; - inherit (self.sources) pathType pathIsDirectory cleanSourceFilter + inherit (self.filesystem) pathType pathIsDirectory pathIsRegularFile; + inherit (self.sources) cleanSourceFilter cleanSource sourceByRegex sourceFilesBySuffices commitIdFromGitRepo cleanSourceWith pathHasContext - canCleanSource pathIsRegularFile pathIsGitRepo; + canCleanSource pathIsGitRepo; inherit (self.modules) evalModules setDefaultModuleLocation unifyModuleSyntax applyModuleArgsIfFunction mergeModules mergeModules' mergeOptionDecls evalOptionValue mergeDefinitions diff --git a/lib/filesystem.nix b/lib/filesystem.nix index 948196055203..2d8a62639ec9 100644 --- a/lib/filesystem.nix +++ b/lib/filesystem.nix @@ -2,12 +2,38 @@ { lib }: let + inherit (builtins) + getAttr + readDir + pathExists + ; + inherit (lib.strings) hasPrefix ; + + inherit (lib.filesystem) + pathType + ; in { + + /* + Returns the type of a path: regular (for file), symlink, or directory. + */ + pathType = path: getAttr (baseNameOf path) (readDir (dirOf path)); + + /* + Returns true if the path exists and is a directory, false otherwise. + */ + pathIsDirectory = path: if pathExists path then (pathType path) == "directory" else false; + + /* + Returns true if the path exists and is a regular file, false otherwise. + */ + pathIsRegularFile = path: if pathExists path then (pathType path) == "regular" else false; + /* A map of all haskell packages defined in the given path, identified by having a cabal file with the same name as the diff --git a/lib/sources.nix b/lib/sources.nix index 3ad7dc633554..d990777c6fcc 100644 --- a/lib/sources.nix +++ b/lib/sources.nix @@ -18,21 +18,11 @@ let pathExists readFile ; - - /* - Returns the type of a path: regular (for file), symlink, or directory. - */ - pathType = path: getAttr (baseNameOf path) (readDir (dirOf path)); - - /* - Returns true if the path exists and is a directory, false otherwise. - */ - pathIsDirectory = path: if pathExists path then (pathType path) == "directory" else false; - - /* - Returns true if the path exists and is a regular file, false otherwise. - */ - pathIsRegularFile = path: if pathExists path then (pathType path) == "regular" else false; + inherit (lib.filesystem) + pathType + pathIsDirectory + pathIsRegularFile + ; /* A basic filter for `cleanSourceWith` that removes @@ -271,11 +261,20 @@ let }; in { - inherit - pathType - pathIsDirectory - pathIsRegularFile + pathType = lib.warnIf (lib.isInOldestRelease 2305) + "lib.sources.pathType has been moved to lib.filesystem.pathType." + lib.filesystem.pathType; + + pathIsDirectory = lib.warnIf (lib.isInOldestRelease 2305) + "lib.sources.pathIsDirectory has been moved to lib.filesystem.pathIsDirectory." + lib.filesystem.pathIsDirectory; + + pathIsRegularFile = lib.warnIf (lib.isInOldestRelease 2305) + "lib.sources.pathIsRegularFile has been moved to lib.filesystem.pathIsRegularFile." + lib.filesystem.pathIsRegularFile; + + inherit pathIsGitRepo commitIdFromGitRepo