···
<!-- This anchor is here for backwards compatibility -->
···
A file set is a (mathematical) set of local files that can be added to the Nix store for use in Nix derivations.
File sets are easy and safe to use, providing obvious and composable semantics with good error messages to prevent mistakes.
9
-
## Overview {#sec-fileset-overview}
9
+
# Overview {#sec-fileset-overview}
- [Implicit coercion from paths to file sets](#sec-fileset-path-coercion)
···
see [this issue](https://github.com/NixOS/nixpkgs/issues/266356) to request it.
61
-
## Implicit coercion from paths to file sets {#sec-fileset-path-coercion}
61
+
# Implicit coercion from paths to file sets {#sec-fileset-path-coercion}
All functions accepting file sets as arguments can also accept [paths](https://nixos.org/manual/nix/stable/language/values.html#type-path) as arguments.
Such path arguments are implicitly coerced to file sets containing all files under that path:
···
This is in contrast to using [paths in string interpolation](https://nixos.org/manual/nix/stable/language/values.html#type-path), which does add the entire referenced path to the store.
81
-
### Example {#sec-fileset-path-coercion-example}
81
+
## Example {#sec-fileset-path-coercion-example}
Assume we are in a local directory with a file hierarchy like this:
···
Create a file set from a path that may or may not exist:
- If the path does exist, the path is [coerced to a file set](#sec-fileset-path-coercion).
- If the path does not exist, a file set containing no files is returned.
166
-
maybeMissing :: Path -> FileSet
170
+
: 1\. Function argument
169
-
# All files in the current directory, but excluding main.o if it exists
170
-
difference ./. (maybeMissing ./main.o)
175
+
maybeMissing :: Path -> FileSet
180
+
## `lib.fileset.maybeMissing` usage example
183
+
# All files in the current directory, but excluding main.o if it exists
184
+
difference ./. (maybeMissing ./main.o)
···
Incrementally evaluate and trace a file set in a pretty way.
This function is only intended for debugging purposes.
The exact tracing format is unspecified and may change.
···
This variant is useful for tracing file sets in the Nix repl.
198
-
trace :: FileSet -> Any -> Any
201
-
trace (unions [ ./Makefile ./src ./tests/run.sh ]) null
203
-
trace: /home/user/src/myProject
204
-
trace: - Makefile (regular)
205
-
trace: - src (all files in directory)
207
-
trace: - run.sh (regular)
212
-
The file set to trace.
219
+
: The file set to trace.
221
+
This argument can also be a path,
222
+
which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
226
+
: The value to return.
214
-
This argument can also be a path,
215
-
which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
231
+
trace :: FileSet -> Any -> Any
236
+
## `lib.fileset.trace` usage example
239
+
trace (unions [ ./Makefile ./src ./tests/run.sh ]) null
241
+
trace: /home/user/src/myProject
242
+
trace: - Makefile (regular)
243
+
trace: - src (all files in directory)
245
+
trace: - run.sh (regular)
# "fileset" would be a better name, but that would clash with the argument name,
# and we cannot change that because of https://github.com/nix-community/nixdoc/issues/76
···
(_printFileset actualFileset)
Incrementally evaluate and trace a file set in a pretty way.
This function is only intended for debugging purposes.
The exact tracing format is unspecified and may change.
···
This variant is useful for tracing file sets passed as arguments to other functions.
238
-
traceVal :: FileSet -> FileSet
276
+
: The file set to trace and return.
278
+
This argument can also be a path,
279
+
which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
284
+
traceVal :: FileSet -> FileSet
243
-
fileset = traceVal (unions [
250
-
trace: /home/user/src/myProject
251
-
trace: - Makefile (regular)
252
-
trace: - src (all files in directory)
254
-
trace: - run.sh (regular)
255
-
"/nix/store/...-source"
289
+
## `lib.fileset.traceVal` usage example
294
+
fileset = traceVal (unions [
301
+
trace: /home/user/src/myProject
302
+
trace: - Makefile (regular)
303
+
trace: - src (all files in directory)
305
+
trace: - run.sh (regular)
306
+
"/nix/store/...-source"
259
-
The file set to trace and return.
261
-
This argument can also be a path,
262
-
which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
311
+
traceVal = fileset:
# "fileset" would be a better name, but that would clash with the argument name,
# and we cannot change that because of https://github.com/nix-community/nixdoc/issues/76
···
# but that would then duplicate work for consumers of the fileset, because then they have to coerce it again
Add the local files contained in `fileset` to the store as a single [store path](https://nixos.org/manual/nix/stable/glossary#gloss-store-path) rooted at `root`.
The result is the store path as a string-like value, making it usable e.g. as the `src` of a derivation, or in string interpolation:
···
The name of the store path is always `source`.
292
-
fileset :: FileSet,
296
-
# Import the current directory into the store
297
-
# but only include files under ./src
302
-
=> "/nix/store/...-source"
304
-
# Import the current directory into the store
305
-
# but only include ./Makefile and all files under ./src
312
-
=> "/nix/store/...-source"
314
-
# Trying to include a file outside the root will fail
338
+
Takes an attribute set with the following attributes
325
-
# The root needs to point to a directory that contains all the files
334
-
=> "/nix/store/...-source"
340
+
`root` (Path; _required_)
336
-
# The root has to be a local filesystem path
338
-
root = "/nix/store/...-source";
345
-
(required) The local directory [path](https://nixos.org/manual/nix/stable/language/values.html#type-path) that will correspond to the root of the resulting store path.
342
+
: The local directory [path](https://nixos.org/manual/nix/stable/language/values.html#type-path) that will correspond to the root of the resulting store path.
Paths in [strings](https://nixos.org/manual/nix/stable/language/values.html#type-string), including Nix store paths, cannot be passed as `root`.
`root` has to be a directory.
···
Changing `root` only affects the directory structure of the resulting store path, it does not change which files are added to the store.
The only way to change which files get added to the store is by changing the `fileset` attribute.
356
-
(required) The file set whose files to import into the store.
351
+
`fileset` (FileSet; _required_)
353
+
: The file set whose files to import into the store.
File sets can be created using other functions in this library.
This argument can also be a path,
which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
···
If a directory does not recursively contain any file, it is omitted from the store path contents.
367
+
fileset :: FileSet,
373
+
## `lib.fileset.toSource` usage example
376
+
# Import the current directory into the store
377
+
# but only include files under ./src
382
+
=> "/nix/store/...-source"
384
+
# Import the current directory into the store
385
+
# but only include ./Makefile and all files under ./src
392
+
=> "/nix/store/...-source"
394
+
# Trying to include a file outside the root will fail
405
+
# The root needs to point to a directory that contains all the files
414
+
=> "/nix/store/...-source"
416
+
# The root has to be a local filesystem path
418
+
root = "/nix/store/...-source";
···
The list of file paths contained in the given file set.
···
The resulting list of files can be turned back into a file set using [`lib.fileset.unions`](#function-library-lib.fileset.unions).
436
-
toList :: FileSet -> [ Path ]
502
+
: The file set whose file paths to return. This argument can also be a path, which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
507
+
toList :: FileSet -> [ Path ]
512
+
## `lib.fileset.toList` usage example
516
+
[ ./README.md ./Makefile ./src/main.c ./src/main.h ]
440
-
[ ./README.md ./Makefile ./src/main.c ./src/main.h ]
518
+
toList (difference ./. ./src)
519
+
[ ./README.md ./Makefile ]
442
-
toList (difference ./. ./src)
443
-
[ ./README.md ./Makefile ]
446
-
# The file set whose file paths to return.
447
-
# This argument can also be a path,
448
-
# which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
_toList (_coerce "lib.fileset.toList: Argument" fileset);
The file set containing all files that are in either of two given file sets.
This is the same as [`unions`](#function-library-lib.fileset.unions),
but takes just two file sets instead of a list.
···
The given file sets are evaluated as lazily as possible,
with the first argument being evaluated first if needed.
462
-
union :: FileSet -> FileSet -> FileSet
541
+
: The first file set. This argument can also be a path, which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
545
+
: The second file set. This argument can also be a path, which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
550
+
union :: FileSet -> FileSet -> FileSet
465
-
# Create a file set containing the file `Makefile`
466
-
# and all files recursively in the `src` directory
467
-
union ./Makefile ./src
555
+
## `lib.fileset.union` usage example
558
+
# Create a file set containing the file `Makefile`
559
+
# and all files recursively in the `src` directory
560
+
union ./Makefile ./src
562
+
# Create a file set containing the file `Makefile`
563
+
# and the LICENSE file from the parent directory
564
+
union ./Makefile ../LICENSE
469
-
# Create a file set containing the file `Makefile`
470
-
# and the LICENSE file from the parent directory
471
-
union ./Makefile ../LICENSE
474
-
# The first file set.
475
-
# This argument can also be a path,
476
-
# which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
478
-
# The second file set.
479
-
# This argument can also be a path,
480
-
# which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
(_coerceMany "lib.fileset.union" [
···
The file set containing all files that are in any of the given file sets.
This is the same as [`union`](#function-library-lib.fileset.unions),
but takes a list of file sets instead of just two.
···
The given file sets are evaluated as lazily as possible,
with earlier elements being evaluated first if needed.
504
-
unions :: [ FileSet ] -> FileSet
507
-
# Create a file set containing selected files
509
-
# Include the single file `Makefile` in the current directory
510
-
# This errors if the file doesn't exist
513
-
# Recursively include all files in the `src/code` directory
514
-
# If this directory is empty this has no effect
598
+
: A list of file sets. The elements can also be paths, which get [implicitly coerced to file sets](#sec-fileset-path-coercion).
517
-
# Include the files `run.sh` and `unit.c` from the `tests` directory
603
+
unions :: [ FileSet ] -> FileSet
521
-
# Include the `LICENSE` file from the parent directory
608
+
## `lib.fileset.unions` usage example
611
+
# Create a file set containing selected files
613
+
# Include the single file `Makefile` in the current directory
614
+
# This errors if the file doesn't exist
617
+
# Recursively include all files in the `src/code` directory
618
+
# If this directory is empty this has no effect
621
+
# Include the files `run.sh` and `unit.c` from the `tests` directory
625
+
# Include the `LICENSE` file from the parent directory
526
-
# A list of file sets.
527
-
# The elements can also be paths,
528
-
# which get [implicitly coerced to file sets](#sec-fileset-path-coercion).
if ! isList filesets then
···
The file set containing all files that are in both of two given file sets.
See also [Intersection (set theory)](https://en.wikipedia.org/wiki/Intersection_(set_theory)).
The given file sets are evaluated as lazily as possible,
with the first argument being evaluated first if needed.
552
-
intersection :: FileSet -> FileSet -> FileSet
660
+
: The first file set. This argument can also be a path, which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
664
+
: The second file set. This argument can also be a path, which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
669
+
intersection :: FileSet -> FileSet -> FileSet
674
+
## `lib.fileset.intersection` usage example
555
-
# Limit the selected files to the ones in ./., so only ./src and ./Makefile
556
-
intersection ./. (unions [ ../LICENSE ./src ./Makefile ])
677
+
# Limit the selected files to the ones in ./., so only ./src and ./Makefile
678
+
intersection ./. (unions [ ../LICENSE ./src ./Makefile ])
559
-
# The first file set.
560
-
# This argument can also be a path,
561
-
# which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
563
-
# The second file set.
564
-
# This argument can also be a path,
565
-
# which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
filesets = _coerceMany "lib.fileset.intersection" [
···
The file set containing all files from the first file set that are not in the second file set.
See also [Difference (set theory)](https://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement).
The given file sets are evaluated as lazily as possible,
with the first argument being evaluated first if needed.
591
-
union :: FileSet -> FileSet -> FileSet
714
+
: The positive file set. The result can only contain files that are also in this file set. This argument can also be a path, which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
718
+
: The negative file set. The result will never contain files that are also in this file set. This argument can also be a path, which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
723
+
union :: FileSet -> FileSet -> FileSet
728
+
## `lib.fileset.difference` usage example
731
+
# Create a file set containing all files from the current directory,
732
+
# except ones under ./tests
733
+
difference ./. ./tests
594
-
# Create a file set containing all files from the current directory,
595
-
# except ones under ./tests
596
-
difference ./. ./tests
736
+
# A set of Nix-related files
737
+
nixFiles = unions [ ./default.nix ./nix ./tests/default.nix ];
739
+
# Create a file set containing all files under ./tests, except ones in `nixFiles`,
740
+
# meaning only without ./tests/default.nix
741
+
difference ./tests nixFiles
599
-
# A set of Nix-related files
600
-
nixFiles = unions [ ./default.nix ./nix ./tests/default.nix ];
602
-
# Create a file set containing all files under ./tests, except ones in `nixFiles`,
603
-
# meaning only without ./tests/default.nix
604
-
difference ./tests nixFiles
607
-
# The positive file set.
608
-
# The result can only contain files that are also in this file set.
610
-
# This argument can also be a path,
611
-
# which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
613
-
# The negative file set.
614
-
# The result will never contain files that are also in this file set.
616
-
# This argument can also be a path,
617
-
# which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
filesets = _coerceMany "lib.fileset.difference" [
···
Filter a file set to only contain files matching some predicate.
643
-
hasExt :: String -> Bool,
650
-
# Include all regular `default.nix` files in the current directory
651
-
fileFilter (file: file.name == "default.nix") ./.
653
-
# Include all non-Nix files from the current directory
654
-
fileFilter (file: ! file.hasExt "nix") ./.
656
-
# Include all files that start with a "." in the current directory
657
-
fileFilter (file: hasPrefix "." file.name) ./.
659
-
# Include all regular files (not symlinks or others) in the current directory
660
-
fileFilter (file: file.type == "regular") ./.
664
-
The predicate function to call on all files contained in given file set.
773
+
: The predicate function to call on all files contained in given file set.
A file is included in the resulting file set if this function returns true for it.
This function is called with an attribute set containing these attributes:
···
`hasExt "gitignore"` is true.
Other attributes may be added in the future.
793
+
: The path whose files to filter
802
+
hasExt :: String -> Bool,
811
+
## `lib.fileset.fileFilter` usage example
814
+
# Include all regular `default.nix` files in the current directory
815
+
fileFilter (file: file.name == "default.nix") ./.
817
+
# Include all non-Nix files from the current directory
818
+
fileFilter (file: ! file.hasExt "nix") ./.
820
+
# Include all files that start with a "." in the current directory
821
+
fileFilter (file: hasPrefix "." file.name) ./.
823
+
# Include all regular files (not symlinks or others) in the current directory
824
+
fileFilter (file: file.type == "regular") ./.
683
-
# The path whose files to filter
if ! isFunction predicate then
···
_fileFilter predicate path;
703
-
Create a file set with the same files as a `lib.sources`-based value.
704
-
This does not import any of the files into the store.
850
+
Create a file set with the same files as a `lib.sources`-based value.
851
+
This does not import any of the files into the store.
853
+
This can be used to gradually migrate from `lib.sources`-based filtering to `lib.fileset`.
855
+
A file set can be turned back into a source using [`toSource`](#function-library-lib.fileset.toSource).
858
+
File sets cannot represent empty directories.
859
+
Turning the result of this function back into a source using `toSource` will therefore not preserve empty directories.
706
-
This can be used to gradually migrate from `lib.sources`-based filtering to `lib.fileset`.
708
-
A file set can be turned back into a source using [`toSource`](#function-library-lib.fileset.toSource).
711
-
File sets cannot represent empty directories.
712
-
Turning the result of this function back into a source using `toSource` will therefore not preserve empty directories.
867
+
: 1\. Function argument
fromSource :: SourceLike -> FileSet
877
+
## `lib.fileset.fromSource` usage example
# There's no cleanSource-like function for file sets yet,
# but we can just convert cleanSource to a file set and use it that way
···
···
# If there's no filter, no need to run the expensive conversion, all subpaths will be included
Create a file set containing all [Git-tracked files](https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository) in a repository.
This function behaves like [`gitTrackedWith { }`](#function-library-lib.fileset.gitTrackedWith) - using the defaults.
777
-
gitTracked :: Path -> FileSet
780
-
# Include all files tracked by the Git repository in the current directory
945
+
: The [path](https://nixos.org/manual/nix/stable/language/values#type-path) to the working directory of a local Git repository.
946
+
This directory must contain a `.git` file or subdirectory.
783
-
# Include only files tracked by the Git repository in the parent directory
784
-
# that are also in the current directory
785
-
intersection ./. (gitTracked ../.)
951
+
gitTracked :: Path -> FileSet
956
+
## `lib.fileset.gitTracked` usage example
959
+
# Include all files tracked by the Git repository in the current directory
962
+
# Include only files tracked by the Git repository in the parent directory
963
+
# that are also in the current directory
964
+
intersection ./. (gitTracked ../.)
789
-
The [path](https://nixos.org/manual/nix/stable/language/values#type-path) to the working directory of a local Git repository.
790
-
This directory must contain a `.git` file or subdirectory.
···
Create a file set containing all [Git-tracked files](https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository) in a repository.
The first argument allows configuration with an attribute set,
while the second argument is the path to the Git working tree.
···
This may change in the future.
824
-
gitTrackedWith :: { recurseSubmodules :: Bool ? false } -> Path -> FileSet
1004
+
`options` (attribute set)
1005
+
: `recurseSubmodules` (optional, default: `false`)
1006
+
: Whether to recurse into [Git submodules](https://git-scm.com/book/en/v2/Git-Tools-Submodules) to also include their tracked files.
1007
+
If `true`, this is equivalent to passing the [--recurse-submodules](https://git-scm.com/docs/git-ls-files#Documentation/git-ls-files.txt---recurse-submodules) flag to `git ls-files`.
1010
+
: The [path](https://nixos.org/manual/nix/stable/language/values#type-path) to the working directory of a local Git repository.
1011
+
This directory must contain a `.git` file or subdirectory.
1016
+
gitTrackedWith :: { recurseSubmodules :: Bool ? false } -> Path -> FileSet
1021
+
## `lib.fileset.gitTrackedWith` usage example
827
-
# Include all files tracked by the Git repository in the current directory
828
-
# and any submodules under it
829
-
gitTracked { recurseSubmodules = true; } ./.
1024
+
# Include all files tracked by the Git repository in the current directory
1025
+
# and any submodules under it
1026
+
gitTracked { recurseSubmodules = true; } ./.
834
-
(optional, default: `false`) Whether to recurse into [Git submodules](https://git-scm.com/book/en/v2/Git-Tools-Submodules) to also include their tracked files.
836
-
If `true`, this is equivalent to passing the [--recurse-submodules](https://git-scm.com/docs/git-ls-files#Documentation/git-ls-files.txt---recurse-submodules) flag to `git ls-files`.
recurseSubmodules ? false,
841
-
The [path](https://nixos.org/manual/nix/stable/language/values#type-path) to the working directory of a local Git repository.
842
-
This directory must contain a `.git` file or subdirectory.
if ! isBool recurseSubmodules then
throw "lib.fileset.gitTrackedWith: Expected the attribute `recurseSubmodules` of the first argument to be a boolean, but it's a ${typeOf recurseSubmodules} instead."