2 Functions for querying information about the filesystem
3 without copying any files to the Nix store.
7 # Tested in lib/tests/filesystem.sh
15 inherit (lib.attrsets)
20 inherit (lib.filesystem)
33 The type of a path. The path needs to exist and be accessible.
34 The result is either "directory" for a directory, "regular" for a regular file, "symlink" for a symlink, or "unknown" for anything else.
45 pathType :: Path -> String
50 ## `lib.filesystem.pathType` usage example
56 pathType /some/file.nix
63 builtins.readFileType or
64 # Nix <2.14 compatibility shim
67 # Fail irrecoverably to mimic the historic behavior of this function and
68 # the new builtins.readFileType
69 then abort "lib.filesystem.pathType: Path ${toString path} does not exist."
70 # The filesystem root is the only path where `dirOf / == /` and
71 # `baseNameOf /` is not valid. We can detect this and directly return
72 # "directory", since we know the filesystem root can't be anything else.
73 else if dirOf path == path
75 else (readDir (dirOf path)).${baseNameOf path}
79 Whether a path exists and is a directory.
86 : 1\. Function argument
91 pathIsDirectory :: Path -> Bool
96 ## `lib.filesystem.pathIsDirectory` usage example
102 pathIsDirectory /this/does/not/exist
105 pathIsDirectory /some/file.nix
111 pathIsDirectory = path:
112 pathExists path && pathType path == "directory";
115 Whether a path exists and is a regular file, meaning not a symlink or any other special file type.
122 : 1\. Function argument
127 pathIsRegularFile :: Path -> Bool
132 ## `lib.filesystem.pathIsRegularFile` usage example
138 pathIsRegularFile /this/does/not/exist
141 pathIsRegularFile /some/file.nix
147 pathIsRegularFile = path:
148 pathExists path && pathType path == "regular";
151 A map of all haskell packages defined in the given path,
152 identified by having a cabal file with the same name as the
160 : The directory within to search
165 Path -> Map String Path
170 let # Files in the root
171 root-files = builtins.attrNames (builtins.readDir root);
172 # Files with their full paths
173 root-files-with-paths =
175 { name = file; value = root + "/${file}"; }
177 # Subdirectories of the root with a cabal file.
179 builtins.filter ({ name, value }:
180 builtins.pathExists (value + "/${name}.cabal")
181 ) root-files-with-paths;
182 in builtins.listToAttrs cabal-subdirs;
184 Find the first directory containing a file matching 'pattern'
185 upward from a given 'file'.
186 Returns 'null' if no directories contain a file matching 'pattern'.
193 : The pattern to search for
197 : The file to start searching upward from
202 RegExp -> Path -> Nullable { path : Path; matches : [ MatchResults ]; }
205 locateDominatingFile =
209 let files = builtins.attrNames (builtins.readDir path);
210 matches = builtins.filter (match: match != null)
211 (map (builtins.match pattern) files);
213 if builtins.length matches != 0
214 then { inherit path matches; }
217 else go (dirOf path);
220 let base = baseNameOf file;
221 type = (builtins.readDir parent).${base} or null;
222 in file == /. || type == "directory";
223 in go (if isDir then file else parent);
227 Given a directory, return a flattened list of all files within it recursively.
234 : The path to recursively list
244 lib.flatten (lib.mapAttrsToList (name: type:
245 if type == "directory" then
246 lib.filesystem.listFilesRecursive (dir + "/${name}")
249 ) (builtins.readDir dir));
252 Transform a directory tree containing package files suitable for
253 `callPackage` into a matching nested attribute set of derivations.
255 For a directory tree like this:
262 │ ├── my-extra-feature.patch
264 │ └── support-definitions.nix
272 `packagesFromDirectoryRecursive` will produce an attribute set like this:
275 # packagesFromDirectoryRecursive {
276 # callPackage = pkgs.callPackage;
277 # directory = ./my-packages;
280 a = pkgs.callPackage ./my-packages/a.nix { };
281 b = pkgs.callPackage ./my-packages/b.nix { };
282 c = pkgs.callPackage ./my-packages/c/package.nix { };
284 d = pkgs.callPackage ./my-packages/my-namespace/d.nix { };
285 e = pkgs.callPackage ./my-packages/my-namespace/e.nix { };
286 f = pkgs.callPackage ./my-packages/my-namespace/f/package.nix { };
292 - If the input directory contains a `package.nix` file, then
293 `callPackage <directory>/package.nix { }` is returned.
294 - Otherwise, the input directory's contents are listed and transformed into
296 - If a file name has the `.nix` extension, it is turned into attribute
298 - The attribute name is the file name without the `.nix` extension
299 - The attribute value is `callPackage <file path> { }`
300 - Other files are ignored.
301 - Directories are turned into an attribute where:
302 - The attribute name is the name of the directory
303 - The attribute value is the result of calling
304 `packagesFromDirectoryRecursive { ... }` on the directory.
306 As a result, directories with no `.nix` files (including empty
307 directories) will be transformed into empty attribute sets.
311 Structured function argument
313 : Attribute set containing the following attributes.
314 Additional attributes are ignored.
320 Type: `Path -> AttrSet -> a`
324 : The directory to read package files from
332 packagesFromDirectoryRecursive :: AttrSet -> AttrSet
337 ## `lib.filesystem.packagesFromDirectoryRecursive` usage example
340 packagesFromDirectoryRecursive {
341 inherit (pkgs) callPackage;
342 directory = ./my-packages;
346 lib.makeScope pkgs.newScope (
347 self: packagesFromDirectoryRecursive {
348 callPackage = self.callPackage;
349 directory = ./my-packages;
357 packagesFromDirectoryRecursive =
364 # Determine if a directory entry from `readDir` indicates a package or
365 # directory of packages.
366 directoryEntryIsPackage = basename: type:
367 type == "directory" || hasSuffix ".nix" basename;
369 # List directory entries that indicate packages in the given `path`.
370 packageDirectoryEntries = path:
371 filterAttrs directoryEntryIsPackage (readDir path);
373 # Transform a directory entry (a `basename` and `type` pair) into a
375 directoryEntryToAttrPair = subdirectory: basename: type:
377 path = subdirectory + "/${basename}";
382 name = removeSuffix ".nix" basename;
383 value = callPackage path { };
386 if type == "directory"
390 value = packagesFromDirectory path;
395 lib.filesystem.packagesFromDirectoryRecursive: Unsupported file type ${type} at path ${toString subdirectory}
398 # Transform a directory into a package (if there's a `package.nix`) or
399 # set of packages (otherwise).
400 packagesFromDirectory = path:
402 defaultPackagePath = path + "/package.nix";
404 if pathExists defaultPackagePath
405 then callPackage defaultPackagePath { }
407 (directoryEntryToAttrPair path)
408 (packageDirectoryEntries path);
410 packagesFromDirectory directory;