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.
37 pathType :: Path -> String
43 pathType /some/file.nix
47 builtins.readFileType or
48 # Nix <2.14 compatibility shim
51 # Fail irrecoverably to mimic the historic behavior of this function and
52 # the new builtins.readFileType
53 then abort "lib.filesystem.pathType: Path ${toString path} does not exist."
54 # The filesystem root is the only path where `dirOf / == /` and
55 # `baseNameOf /` is not valid. We can detect this and directly return
56 # "directory", since we know the filesystem root can't be anything else.
57 else if dirOf path == path
59 else (readDir (dirOf path)).${baseNameOf path}
63 Whether a path exists and is a directory.
66 pathIsDirectory :: Path -> Bool
72 pathIsDirectory /this/does/not/exist
75 pathIsDirectory /some/file.nix
78 pathIsDirectory = path:
79 pathExists path && pathType path == "directory";
82 Whether a path exists and is a regular file, meaning not a symlink or any other special file type.
85 pathIsRegularFile :: Path -> Bool
91 pathIsRegularFile /this/does/not/exist
94 pathIsRegularFile /some/file.nix
97 pathIsRegularFile = path:
98 pathExists path && pathType path == "regular";
101 A map of all haskell packages defined in the given path,
102 identified by having a cabal file with the same name as the
105 Type: Path -> Map String Path
108 # The directory within to search
110 let # Files in the root
111 root-files = builtins.attrNames (builtins.readDir root);
112 # Files with their full paths
113 root-files-with-paths =
115 { name = file; value = root + "/${file}"; }
117 # Subdirectories of the root with a cabal file.
119 builtins.filter ({ name, value }:
120 builtins.pathExists (value + "/${name}.cabal")
121 ) root-files-with-paths;
122 in builtins.listToAttrs cabal-subdirs;
124 Find the first directory containing a file matching 'pattern'
125 upward from a given 'file'.
126 Returns 'null' if no directories contain a file matching 'pattern'.
128 Type: RegExp -> Path -> Nullable { path : Path; matches : [ MatchResults ]; }
130 locateDominatingFile =
131 # The pattern to search for
133 # The file to start searching upward from
136 let files = builtins.attrNames (builtins.readDir path);
137 matches = builtins.filter (match: match != null)
138 (map (builtins.match pattern) files);
140 if builtins.length matches != 0
141 then { inherit path matches; }
144 else go (dirOf path);
147 let base = baseNameOf file;
148 type = (builtins.readDir parent).${base} or null;
149 in file == /. || type == "directory";
150 in go (if isDir then file else parent);
154 Given a directory, return a flattened list of all files within it recursively.
156 Type: Path -> [ Path ]
159 # The path to recursively list
161 lib.flatten (lib.mapAttrsToList (name: type:
162 if type == "directory" then
163 lib.filesystem.listFilesRecursive (dir + "/${name}")
166 ) (builtins.readDir dir));
169 Transform a directory tree containing package files suitable for
170 `callPackage` into a matching nested attribute set of derivations.
172 For a directory tree like this:
179 │ ├── my-extra-feature.patch
181 │ └── support-definitions.nix
189 `packagesFromDirectoryRecursive` will produce an attribute set like this:
192 # packagesFromDirectoryRecursive {
193 # callPackage = pkgs.callPackage;
194 # directory = ./my-packages;
197 a = pkgs.callPackage ./my-packages/a.nix { };
198 b = pkgs.callPackage ./my-packages/b.nix { };
199 c = pkgs.callPackage ./my-packages/c/package.nix { };
201 d = pkgs.callPackage ./my-packages/my-namespace/d.nix { };
202 e = pkgs.callPackage ./my-packages/my-namespace/e.nix { };
203 f = pkgs.callPackage ./my-packages/my-namespace/f/package.nix { };
209 - If the input directory contains a `package.nix` file, then
210 `callPackage <directory>/package.nix { }` is returned.
211 - Otherwise, the input directory's contents are listed and transformed into
213 - If a file name has the `.nix` extension, it is turned into attribute
215 - The attribute name is the file name without the `.nix` extension
216 - The attribute value is `callPackage <file path> { }`
217 - Other files are ignored.
218 - Directories are turned into an attribute where:
219 - The attribute name is the name of the directory
220 - The attribute value is the result of calling
221 `packagesFromDirectoryRecursive { ... }` on the directory.
223 As a result, directories with no `.nix` files (including empty
224 directories) will be transformed into empty attribute sets.
227 packagesFromDirectoryRecursive {
228 inherit (pkgs) callPackage;
229 directory = ./my-packages;
233 lib.makeScope pkgs.newScope (
234 self: packagesFromDirectoryRecursive {
235 callPackage = self.callPackage;
236 directory = ./my-packages;
242 packagesFromDirectoryRecursive :: AttrSet -> AttrSet
244 packagesFromDirectoryRecursive =
255 The directory to read package files from
264 # Determine if a directory entry from `readDir` indicates a package or
265 # directory of packages.
266 directoryEntryIsPackage = basename: type:
267 type == "directory" || hasSuffix ".nix" basename;
269 # List directory entries that indicate packages in the given `path`.
270 packageDirectoryEntries = path:
271 filterAttrs directoryEntryIsPackage (readDir path);
273 # Transform a directory entry (a `basename` and `type` pair) into a
275 directoryEntryToAttrPair = subdirectory: basename: type:
277 path = subdirectory + "/${basename}";
282 name = removeSuffix ".nix" basename;
283 value = callPackage path { };
286 if type == "directory"
290 value = packagesFromDirectory path;
295 lib.filesystem.packagesFromDirectoryRecursive: Unsupported file type ${type} at path ${toString subdirectory}
298 # Transform a directory into a package (if there's a `package.nix`) or
299 # set of packages (otherwise).
300 packagesFromDirectory = path:
302 defaultPackagePath = path + "/package.nix";
304 if pathExists defaultPackagePath
305 then callPackage defaultPackagePath { }
307 (directoryEntryToAttrPair path)
308 (packageDirectoryEntries path);
310 packagesFromDirectory directory;