+461
-270
lib/fileset/default.nix
+461
-270
lib/fileset/default.nix
···
1
-
/*
1
+
/**
2
2
<!-- This anchor is here for backwards compatibility -->
3
3
[]{#sec-fileset}
4
4
···
6
6
A file set is a (mathematical) set of local files that can be added to the Nix store for use in Nix derivations.
7
7
File sets are easy and safe to use, providing obvious and composable semantics with good error messages to prevent mistakes.
8
8
9
-
## Overview {#sec-fileset-overview}
9
+
# Overview {#sec-fileset-overview}
10
10
11
11
Basics:
12
12
- [Implicit coercion from paths to file sets](#sec-fileset-path-coercion)
···
58
58
see [this issue](https://github.com/NixOS/nixpkgs/issues/266356) to request it.
59
59
60
60
61
-
## Implicit coercion from paths to file sets {#sec-fileset-path-coercion}
61
+
# Implicit coercion from paths to file sets {#sec-fileset-path-coercion}
62
62
63
63
All functions accepting file sets as arguments can also accept [paths](https://nixos.org/manual/nix/stable/language/values.html#type-path) as arguments.
64
64
Such path arguments are implicitly coerced to file sets containing all files under that path:
···
78
78
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.
79
79
:::
80
80
81
-
### Example {#sec-fileset-path-coercion-example}
81
+
## Example {#sec-fileset-path-coercion-example}
82
82
83
83
Assume we are in a local directory with a file hierarchy like this:
84
84
```
···
157
157
158
158
in {
159
159
160
-
/*
160
+
/**
161
161
Create a file set from a path that may or may not exist:
162
162
- If the path does exist, the path is [coerced to a file set](#sec-fileset-path-coercion).
163
163
- If the path does not exist, a file set containing no files is returned.
164
164
165
-
Type:
166
-
maybeMissing :: Path -> FileSet
165
+
166
+
# Inputs
167
+
168
+
`path`
169
+
170
+
: 1\. Function argument
171
+
172
+
# Type
167
173
168
-
Example:
169
-
# All files in the current directory, but excluding main.o if it exists
170
-
difference ./. (maybeMissing ./main.o)
174
+
```
175
+
maybeMissing :: Path -> FileSet
176
+
```
177
+
178
+
# Examples
179
+
:::{.example}
180
+
## `lib.fileset.maybeMissing` usage example
181
+
182
+
```nix
183
+
# All files in the current directory, but excluding main.o if it exists
184
+
difference ./. (maybeMissing ./main.o)
185
+
```
186
+
187
+
:::
171
188
*/
172
189
maybeMissing =
173
190
path:
···
183
200
else
184
201
_singleton path;
185
202
186
-
/*
203
+
/**
187
204
Incrementally evaluate and trace a file set in a pretty way.
188
205
This function is only intended for debugging purposes.
189
206
The exact tracing format is unspecified and may change.
···
194
211
195
212
This variant is useful for tracing file sets in the Nix repl.
196
213
197
-
Type:
198
-
trace :: FileSet -> Any -> Any
199
214
200
-
Example:
201
-
trace (unions [ ./Makefile ./src ./tests/run.sh ]) null
202
-
=>
203
-
trace: /home/user/src/myProject
204
-
trace: - Makefile (regular)
205
-
trace: - src (all files in directory)
206
-
trace: - tests
207
-
trace: - run.sh (regular)
208
-
null
209
-
*/
210
-
trace =
211
-
/*
212
-
The file set to trace.
215
+
# Inputs
216
+
217
+
`fileset`
218
+
219
+
: The file set to trace.
220
+
221
+
This argument can also be a path,
222
+
which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
223
+
224
+
`val`
225
+
226
+
: The value to return.
213
227
214
-
This argument can also be a path,
215
-
which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
216
-
*/
217
-
fileset:
228
+
# Type
229
+
230
+
```
231
+
trace :: FileSet -> Any -> Any
232
+
```
233
+
234
+
# Examples
235
+
:::{.example}
236
+
## `lib.fileset.trace` usage example
237
+
238
+
```nix
239
+
trace (unions [ ./Makefile ./src ./tests/run.sh ]) null
240
+
=>
241
+
trace: /home/user/src/myProject
242
+
trace: - Makefile (regular)
243
+
trace: - src (all files in directory)
244
+
trace: - tests
245
+
trace: - run.sh (regular)
246
+
null
247
+
```
248
+
249
+
:::
250
+
*/
251
+
trace = fileset:
218
252
let
219
253
# "fileset" would be a better name, but that would clash with the argument name,
220
254
# and we cannot change that because of https://github.com/nix-community/nixdoc/issues/76
···
224
258
(_printFileset actualFileset)
225
259
(x: x);
226
260
227
-
/*
261
+
/**
228
262
Incrementally evaluate and trace a file set in a pretty way.
229
263
This function is only intended for debugging purposes.
230
264
The exact tracing format is unspecified and may change.
···
234
268
235
269
This variant is useful for tracing file sets passed as arguments to other functions.
236
270
237
-
Type:
238
-
traceVal :: FileSet -> FileSet
271
+
272
+
# Inputs
273
+
274
+
`fileset`
275
+
276
+
: The file set to trace and return.
277
+
278
+
This argument can also be a path,
279
+
which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
280
+
281
+
# Type
282
+
283
+
```
284
+
traceVal :: FileSet -> FileSet
285
+
```
239
286
240
-
Example:
241
-
toSource {
242
-
root = ./.;
243
-
fileset = traceVal (unions [
244
-
./Makefile
245
-
./src
246
-
./tests/run.sh
247
-
]);
248
-
}
249
-
=>
250
-
trace: /home/user/src/myProject
251
-
trace: - Makefile (regular)
252
-
trace: - src (all files in directory)
253
-
trace: - tests
254
-
trace: - run.sh (regular)
255
-
"/nix/store/...-source"
287
+
# Examples
288
+
:::{.example}
289
+
## `lib.fileset.traceVal` usage example
290
+
291
+
```nix
292
+
toSource {
293
+
root = ./.;
294
+
fileset = traceVal (unions [
295
+
./Makefile
296
+
./src
297
+
./tests/run.sh
298
+
]);
299
+
}
300
+
=>
301
+
trace: /home/user/src/myProject
302
+
trace: - Makefile (regular)
303
+
trace: - src (all files in directory)
304
+
trace: - tests
305
+
trace: - run.sh (regular)
306
+
"/nix/store/...-source"
307
+
```
308
+
309
+
:::
256
310
*/
257
-
traceVal =
258
-
/*
259
-
The file set to trace and return.
260
-
261
-
This argument can also be a path,
262
-
which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
263
-
*/
264
-
fileset:
311
+
traceVal = fileset:
265
312
let
266
313
# "fileset" would be a better name, but that would clash with the argument name,
267
314
# and we cannot change that because of https://github.com/nix-community/nixdoc/issues/76
···
273
320
# but that would then duplicate work for consumers of the fileset, because then they have to coerce it again
274
321
actualFileset;
275
322
276
-
/*
323
+
/**
277
324
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`.
278
325
279
326
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:
···
286
333
287
334
The name of the store path is always `source`.
288
335
289
-
Type:
290
-
toSource :: {
291
-
root :: Path,
292
-
fileset :: FileSet,
293
-
} -> SourceLike
294
-
295
-
Example:
296
-
# Import the current directory into the store
297
-
# but only include files under ./src
298
-
toSource {
299
-
root = ./.;
300
-
fileset = ./src;
301
-
}
302
-
=> "/nix/store/...-source"
303
-
304
-
# Import the current directory into the store
305
-
# but only include ./Makefile and all files under ./src
306
-
toSource {
307
-
root = ./.;
308
-
fileset = union
309
-
./Makefile
310
-
./src;
311
-
}
312
-
=> "/nix/store/...-source"
336
+
# Inputs
313
337
314
-
# Trying to include a file outside the root will fail
315
-
toSource {
316
-
root = ./.;
317
-
fileset = unions [
318
-
./Makefile
319
-
./src
320
-
../LICENSE
321
-
];
322
-
}
323
-
=> <error>
338
+
Takes an attribute set with the following attributes
324
339
325
-
# The root needs to point to a directory that contains all the files
326
-
toSource {
327
-
root = ../.;
328
-
fileset = unions [
329
-
./Makefile
330
-
./src
331
-
../LICENSE
332
-
];
333
-
}
334
-
=> "/nix/store/...-source"
340
+
`root` (Path; _required_)
335
341
336
-
# The root has to be a local filesystem path
337
-
toSource {
338
-
root = "/nix/store/...-source";
339
-
fileset = ./.;
340
-
}
341
-
=> <error>
342
-
*/
343
-
toSource = {
344
-
/*
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.
346
343
Paths in [strings](https://nixos.org/manual/nix/stable/language/values.html#type-string), including Nix store paths, cannot be passed as `root`.
347
344
`root` has to be a directory.
348
345
···
350
347
Changing `root` only affects the directory structure of the resulting store path, it does not change which files are added to the store.
351
348
The only way to change which files get added to the store is by changing the `fileset` attribute.
352
349
:::
353
-
*/
354
-
root,
355
-
/*
356
-
(required) The file set whose files to import into the store.
350
+
351
+
`fileset` (FileSet; _required_)
352
+
353
+
: The file set whose files to import into the store.
357
354
File sets can be created using other functions in this library.
358
355
This argument can also be a path,
359
356
which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
···
362
359
If a directory does not recursively contain any file, it is omitted from the store path contents.
363
360
:::
364
361
365
-
*/
362
+
# Type
363
+
364
+
```
365
+
toSource :: {
366
+
root :: Path,
367
+
fileset :: FileSet,
368
+
} -> SourceLike
369
+
```
370
+
371
+
# Examples
372
+
:::{.example}
373
+
## `lib.fileset.toSource` usage example
374
+
375
+
```nix
376
+
# Import the current directory into the store
377
+
# but only include files under ./src
378
+
toSource {
379
+
root = ./.;
380
+
fileset = ./src;
381
+
}
382
+
=> "/nix/store/...-source"
383
+
384
+
# Import the current directory into the store
385
+
# but only include ./Makefile and all files under ./src
386
+
toSource {
387
+
root = ./.;
388
+
fileset = union
389
+
./Makefile
390
+
./src;
391
+
}
392
+
=> "/nix/store/...-source"
393
+
394
+
# Trying to include a file outside the root will fail
395
+
toSource {
396
+
root = ./.;
397
+
fileset = unions [
398
+
./Makefile
399
+
./src
400
+
../LICENSE
401
+
];
402
+
}
403
+
=> <error>
404
+
405
+
# The root needs to point to a directory that contains all the files
406
+
toSource {
407
+
root = ../.;
408
+
fileset = unions [
409
+
./Makefile
410
+
./src
411
+
../LICENSE
412
+
];
413
+
}
414
+
=> "/nix/store/...-source"
415
+
416
+
# The root has to be a local filesystem path
417
+
toSource {
418
+
root = "/nix/store/...-source";
419
+
fileset = ./.;
420
+
}
421
+
=> <error>
422
+
```
423
+
424
+
:::
425
+
*/
426
+
toSource = {
427
+
root,
366
428
fileset,
367
429
}:
368
430
let
···
418
480
};
419
481
420
482
421
-
/*
483
+
/**
422
484
The list of file paths contained in the given file set.
423
485
424
486
:::{.note}
···
432
494
433
495
The resulting list of files can be turned back into a file set using [`lib.fileset.unions`](#function-library-lib.fileset.unions).
434
496
435
-
Type:
436
-
toList :: FileSet -> [ Path ]
497
+
498
+
# Inputs
499
+
500
+
`fileset`
501
+
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).
503
+
504
+
# Type
505
+
506
+
```
507
+
toList :: FileSet -> [ Path ]
508
+
```
509
+
510
+
# Examples
511
+
:::{.example}
512
+
## `lib.fileset.toList` usage example
513
+
514
+
```nix
515
+
toList ./.
516
+
[ ./README.md ./Makefile ./src/main.c ./src/main.h ]
437
517
438
-
Example:
439
-
toList ./.
440
-
[ ./README.md ./Makefile ./src/main.c ./src/main.h ]
518
+
toList (difference ./. ./src)
519
+
[ ./README.md ./Makefile ]
520
+
```
441
521
442
-
toList (difference ./. ./src)
443
-
[ ./README.md ./Makefile ]
522
+
:::
444
523
*/
445
-
toList =
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).
449
-
fileset:
524
+
toList = fileset:
450
525
_toList (_coerce "lib.fileset.toList: Argument" fileset);
451
526
452
-
/*
527
+
/**
453
528
The file set containing all files that are in either of two given file sets.
454
529
This is the same as [`unions`](#function-library-lib.fileset.unions),
455
530
but takes just two file sets instead of a list.
···
458
533
The given file sets are evaluated as lazily as possible,
459
534
with the first argument being evaluated first if needed.
460
535
461
-
Type:
462
-
union :: FileSet -> FileSet -> FileSet
536
+
537
+
# Inputs
538
+
539
+
`fileset1`
540
+
541
+
: The first file set. This argument can also be a path, which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
542
+
543
+
`fileset2`
544
+
545
+
: The second file set. This argument can also be a path, which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
546
+
547
+
# Type
548
+
549
+
```
550
+
union :: FileSet -> FileSet -> FileSet
551
+
```
463
552
464
-
Example:
465
-
# Create a file set containing the file `Makefile`
466
-
# and all files recursively in the `src` directory
467
-
union ./Makefile ./src
553
+
# Examples
554
+
:::{.example}
555
+
## `lib.fileset.union` usage example
556
+
557
+
```nix
558
+
# Create a file set containing the file `Makefile`
559
+
# and all files recursively in the `src` directory
560
+
union ./Makefile ./src
561
+
562
+
# Create a file set containing the file `Makefile`
563
+
# and the LICENSE file from the parent directory
564
+
union ./Makefile ../LICENSE
565
+
```
468
566
469
-
# Create a file set containing the file `Makefile`
470
-
# and the LICENSE file from the parent directory
471
-
union ./Makefile ../LICENSE
567
+
:::
472
568
*/
473
569
union =
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).
477
570
fileset1:
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).
481
571
fileset2:
482
572
_unionMany
483
573
(_coerceMany "lib.fileset.union" [
···
491
581
}
492
582
]);
493
583
494
-
/*
584
+
/**
495
585
The file set containing all files that are in any of the given file sets.
496
586
This is the same as [`union`](#function-library-lib.fileset.unions),
497
587
but takes a list of file sets instead of just two.
···
500
590
The given file sets are evaluated as lazily as possible,
501
591
with earlier elements being evaluated first if needed.
502
592
503
-
Type:
504
-
unions :: [ FileSet ] -> FileSet
505
593
506
-
Example:
507
-
# Create a file set containing selected files
508
-
unions [
509
-
# Include the single file `Makefile` in the current directory
510
-
# This errors if the file doesn't exist
511
-
./Makefile
594
+
# Inputs
512
595
513
-
# Recursively include all files in the `src/code` directory
514
-
# If this directory is empty this has no effect
515
-
./src/code
596
+
`filesets`
597
+
598
+
: A list of file sets. The elements can also be paths, which get [implicitly coerced to file sets](#sec-fileset-path-coercion).
599
+
600
+
# Type
516
601
517
-
# Include the files `run.sh` and `unit.c` from the `tests` directory
518
-
./tests/run.sh
519
-
./tests/unit.c
602
+
```
603
+
unions :: [ FileSet ] -> FileSet
604
+
```
520
605
521
-
# Include the `LICENSE` file from the parent directory
522
-
../LICENSE
523
-
]
606
+
# Examples
607
+
:::{.example}
608
+
## `lib.fileset.unions` usage example
609
+
610
+
```nix
611
+
# Create a file set containing selected files
612
+
unions [
613
+
# Include the single file `Makefile` in the current directory
614
+
# This errors if the file doesn't exist
615
+
./Makefile
616
+
617
+
# Recursively include all files in the `src/code` directory
618
+
# If this directory is empty this has no effect
619
+
./src/code
620
+
621
+
# Include the files `run.sh` and `unit.c` from the `tests` directory
622
+
./tests/run.sh
623
+
./tests/unit.c
624
+
625
+
# Include the `LICENSE` file from the parent directory
626
+
../LICENSE
627
+
]
628
+
```
629
+
630
+
:::
524
631
*/
525
632
unions =
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).
529
633
filesets:
530
634
if ! isList filesets then
531
635
throw ''
···
541
645
_unionMany
542
646
];
543
647
544
-
/*
648
+
/**
545
649
The file set containing all files that are in both of two given file sets.
546
650
See also [Intersection (set theory)](https://en.wikipedia.org/wiki/Intersection_(set_theory)).
547
651
548
652
The given file sets are evaluated as lazily as possible,
549
653
with the first argument being evaluated first if needed.
550
654
551
-
Type:
552
-
intersection :: FileSet -> FileSet -> FileSet
655
+
656
+
# Inputs
657
+
658
+
`fileset1`
659
+
660
+
: The first file set. This argument can also be a path, which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
661
+
662
+
`fileset2`
663
+
664
+
: The second file set. This argument can also be a path, which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
665
+
666
+
# Type
667
+
668
+
```
669
+
intersection :: FileSet -> FileSet -> FileSet
670
+
```
671
+
672
+
# Examples
673
+
:::{.example}
674
+
## `lib.fileset.intersection` usage example
553
675
554
-
Example:
555
-
# Limit the selected files to the ones in ./., so only ./src and ./Makefile
556
-
intersection ./. (unions [ ../LICENSE ./src ./Makefile ])
676
+
```nix
677
+
# Limit the selected files to the ones in ./., so only ./src and ./Makefile
678
+
intersection ./. (unions [ ../LICENSE ./src ./Makefile ])
679
+
```
680
+
681
+
:::
557
682
*/
558
683
intersection =
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).
562
684
fileset1:
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).
566
685
fileset2:
567
686
let
568
687
filesets = _coerceMany "lib.fileset.intersection" [
···
580
699
(elemAt filesets 0)
581
700
(elemAt filesets 1);
582
701
583
-
/*
702
+
/**
584
703
The file set containing all files from the first file set that are not in the second file set.
585
704
See also [Difference (set theory)](https://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement).
586
705
587
706
The given file sets are evaluated as lazily as possible,
588
707
with the first argument being evaluated first if needed.
589
708
590
-
Type:
591
-
union :: FileSet -> FileSet -> FileSet
709
+
710
+
# Inputs
711
+
712
+
`positive`
713
+
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).
715
+
716
+
`negative`
717
+
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).
719
+
720
+
# Type
721
+
722
+
```
723
+
union :: FileSet -> FileSet -> FileSet
724
+
```
725
+
726
+
# Examples
727
+
:::{.example}
728
+
## `lib.fileset.difference` usage example
729
+
730
+
```nix
731
+
# Create a file set containing all files from the current directory,
732
+
# except ones under ./tests
733
+
difference ./. ./tests
592
734
593
-
Example:
594
-
# Create a file set containing all files from the current directory,
595
-
# except ones under ./tests
596
-
difference ./. ./tests
735
+
let
736
+
# A set of Nix-related files
737
+
nixFiles = unions [ ./default.nix ./nix ./tests/default.nix ];
738
+
in
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
742
+
```
597
743
598
-
let
599
-
# A set of Nix-related files
600
-
nixFiles = unions [ ./default.nix ./nix ./tests/default.nix ];
601
-
in
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
744
+
:::
605
745
*/
606
746
difference =
607
-
# The positive file set.
608
-
# The result can only contain files that are also in this file set.
609
-
#
610
-
# This argument can also be a path,
611
-
# which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
612
747
positive:
613
-
# The negative file set.
614
-
# The result will never contain files that are also in this file set.
615
-
#
616
-
# This argument can also be a path,
617
-
# which gets [implicitly coerced to a file set](#sec-fileset-path-coercion).
618
748
negative:
619
749
let
620
750
filesets = _coerceMany "lib.fileset.difference" [
···
632
762
(elemAt filesets 0)
633
763
(elemAt filesets 1);
634
764
635
-
/*
765
+
/**
636
766
Filter a file set to only contain files matching some predicate.
637
767
638
-
Type:
639
-
fileFilter ::
640
-
({
641
-
name :: String,
642
-
type :: String,
643
-
hasExt :: String -> Bool,
644
-
...
645
-
} -> Bool)
646
-
-> Path
647
-
-> FileSet
648
768
649
-
Example:
650
-
# Include all regular `default.nix` files in the current directory
651
-
fileFilter (file: file.name == "default.nix") ./.
769
+
# Inputs
652
770
653
-
# Include all non-Nix files from the current directory
654
-
fileFilter (file: ! file.hasExt "nix") ./.
655
-
656
-
# Include all files that start with a "." in the current directory
657
-
fileFilter (file: hasPrefix "." file.name) ./.
771
+
`predicate`
658
772
659
-
# Include all regular files (not symlinks or others) in the current directory
660
-
fileFilter (file: file.type == "regular") ./.
661
-
*/
662
-
fileFilter =
663
-
/*
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.
665
774
A file is included in the resulting file set if this function returns true for it.
666
775
667
776
This function is called with an attribute set containing these attributes:
···
678
787
`hasExt "gitignore"` is true.
679
788
680
789
Other attributes may be added in the future.
681
-
*/
790
+
791
+
`path`
792
+
793
+
: The path whose files to filter
794
+
795
+
# Type
796
+
797
+
```
798
+
fileFilter ::
799
+
({
800
+
name :: String,
801
+
type :: String,
802
+
hasExt :: String -> Bool,
803
+
...
804
+
} -> Bool)
805
+
-> Path
806
+
-> FileSet
807
+
```
808
+
809
+
# Examples
810
+
:::{.example}
811
+
## `lib.fileset.fileFilter` usage example
812
+
813
+
```nix
814
+
# Include all regular `default.nix` files in the current directory
815
+
fileFilter (file: file.name == "default.nix") ./.
816
+
817
+
# Include all non-Nix files from the current directory
818
+
fileFilter (file: ! file.hasExt "nix") ./.
819
+
820
+
# Include all files that start with a "." in the current directory
821
+
fileFilter (file: hasPrefix "." file.name) ./.
822
+
823
+
# Include all regular files (not symlinks or others) in the current directory
824
+
fileFilter (file: file.type == "regular") ./.
825
+
```
826
+
827
+
:::
828
+
*/
829
+
fileFilter =
682
830
predicate:
683
-
# The path whose files to filter
684
831
path:
685
832
if ! isFunction predicate then
686
833
throw ''
···
699
846
else
700
847
_fileFilter predicate path;
701
848
702
-
/*
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.
849
+
/**
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.
852
+
853
+
This can be used to gradually migrate from `lib.sources`-based filtering to `lib.fileset`.
854
+
855
+
A file set can be turned back into a source using [`toSource`](#function-library-lib.fileset.toSource).
856
+
857
+
:::{.note}
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.
860
+
:::
705
861
706
-
This can be used to gradually migrate from `lib.sources`-based filtering to `lib.fileset`.
707
862
708
-
A file set can be turned back into a source using [`toSource`](#function-library-lib.fileset.toSource).
863
+
# Inputs
709
864
710
-
:::{.note}
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.
713
-
:::
865
+
`source`
714
866
715
-
Type:
867
+
: 1\. Function argument
868
+
869
+
# Type
870
+
871
+
```
716
872
fromSource :: SourceLike -> FileSet
873
+
```
717
874
718
-
Example:
875
+
# Examples
876
+
:::{.example}
877
+
## `lib.fileset.fromSource` usage example
878
+
879
+
```nix
719
880
# There's no cleanSource-like function for file sets yet,
720
881
# but we can just convert cleanSource to a file set and use it that way
721
882
toSource {
···
740
901
./Makefile
741
902
./src
742
903
]);
904
+
```
905
+
906
+
:::
743
907
*/
744
908
fromSource = source:
745
909
let
···
768
932
# If there's no filter, no need to run the expensive conversion, all subpaths will be included
769
933
_singleton path;
770
934
771
-
/*
935
+
/**
772
936
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.
773
937
774
938
This function behaves like [`gitTrackedWith { }`](#function-library-lib.fileset.gitTrackedWith) - using the defaults.
775
939
776
-
Type:
777
-
gitTracked :: Path -> FileSet
940
+
941
+
# Inputs
942
+
943
+
`path`
778
944
779
-
Example:
780
-
# Include all files tracked by the Git repository in the current directory
781
-
gitTracked ./.
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.
782
947
783
-
# Include only files tracked by the Git repository in the parent directory
784
-
# that are also in the current directory
785
-
intersection ./. (gitTracked ../.)
948
+
# Type
949
+
950
+
```
951
+
gitTracked :: Path -> FileSet
952
+
```
953
+
954
+
# Examples
955
+
:::{.example}
956
+
## `lib.fileset.gitTracked` usage example
957
+
958
+
```nix
959
+
# Include all files tracked by the Git repository in the current directory
960
+
gitTracked ./.
961
+
962
+
# Include only files tracked by the Git repository in the parent directory
963
+
# that are also in the current directory
964
+
intersection ./. (gitTracked ../.)
965
+
```
966
+
967
+
:::
786
968
*/
787
969
gitTracked =
788
-
/*
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.
791
-
*/
792
970
path:
793
971
_fromFetchGit
794
972
"gitTracked"
···
796
974
path
797
975
{};
798
976
799
-
/*
977
+
/**
800
978
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.
801
979
The first argument allows configuration with an attribute set,
802
980
while the second argument is the path to the Git working tree.
···
820
998
This may change in the future.
821
999
:::
822
1000
823
-
Type:
824
-
gitTrackedWith :: { recurseSubmodules :: Bool ? false } -> Path -> FileSet
1001
+
1002
+
# Inputs
1003
+
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`.
1008
+
1009
+
`path`
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.
1012
+
1013
+
# Type
1014
+
1015
+
```
1016
+
gitTrackedWith :: { recurseSubmodules :: Bool ? false } -> Path -> FileSet
1017
+
```
1018
+
1019
+
# Examples
1020
+
:::{.example}
1021
+
## `lib.fileset.gitTrackedWith` usage example
825
1022
826
-
Example:
827
-
# Include all files tracked by the Git repository in the current directory
828
-
# and any submodules under it
829
-
gitTracked { recurseSubmodules = true; } ./.
1023
+
```nix
1024
+
# Include all files tracked by the Git repository in the current directory
1025
+
# and any submodules under it
1026
+
gitTracked { recurseSubmodules = true; } ./.
1027
+
```
1028
+
1029
+
:::
830
1030
*/
831
1031
gitTrackedWith =
832
1032
{
833
-
/*
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.
835
-
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`.
837
-
*/
838
1033
recurseSubmodules ? false,
839
1034
}:
840
-
/*
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.
843
-
*/
844
1035
path:
845
1036
if ! isBool recurseSubmodules then
846
1037
throw "lib.fileset.gitTrackedWith: Expected the attribute `recurseSubmodules` of the first argument to be a boolean, but it's a ${typeOf recurseSubmodules} instead."