2021-06-04 18:22:19 +02:00
# pkgs.nix-gitignore {#sec-pkgs-nix-gitignore}
`pkgs.nix-gitignore` is a function that acts similarly to `builtins.filterSource` but also allows filtering with the help of the gitignore format.
## Usage {#sec-pkgs-nix-gitignore-usage}
2022-12-21 21:24:48 +01:00
`pkgs.nix-gitignore` exports a number of functions, but you'll most likely need either `gitignoreSource` or `gitignoreSourcePure` . As their first argument, they both accept either 1. a file with gitignore lines or 2. a string with gitignore lines, or 3. a list of either of the two. They will be concatenated into a single big string.
2021-06-04 18:22:19 +02:00
```nix
2024-03-27 19:10:27 +01:00
{ pkgs ? import < nixpkgs > {} }: {
2021-06-04 18:22:19 +02:00
2024-03-27 19:10:27 +01:00
src = nix-gitignore.gitignoreSource [] ./source;
2021-06-04 18:22:19 +02:00
# Simplest version
2024-03-27 19:10:27 +01:00
src = nix-gitignore.gitignoreSource "supplemental-ignores\n" ./source;
2021-06-04 18:22:19 +02:00
# This one reads the ./source/.gitignore and concats the auxiliary ignores
2024-03-27 19:10:27 +01:00
src = nix-gitignore.gitignoreSourcePure "ignore-this\nignore-that\n" ./source;
2021-06-04 18:22:19 +02:00
# Use this string as gitignore, don't read ./source/.gitignore.
2024-03-27 19:10:27 +01:00
src = nix-gitignore.gitignoreSourcePure ["ignore-this\nignore-that\n" ~/.gitignore] ./source;
2021-06-04 18:22:19 +02:00
# It also accepts a list (of strings and paths) that will be concatenated
# once the paths are turned to strings via readFile.
2024-03-27 19:10:27 +01:00
}
2021-06-04 18:22:19 +02:00
```
These functions are derived from the `Filter` functions by setting the first filter argument to `(_: _: true)` :
```nix
2024-03-27 19:10:27 +01:00
{
gitignoreSourcePure = gitignoreFilterSourcePure (_: _: true);
gitignoreSource = gitignoreFilterSource (_: _: true);
}
2021-06-04 18:22:19 +02:00
```
2022-12-21 21:24:48 +01:00
Those filter functions accept the same arguments the `builtins.filterSource` function would pass to its filters, thus `fn: gitignoreFilterSourcePure fn ""` should be extensionally equivalent to `filterSource` . The file is blacklisted if it's blacklisted by either your filter or the gitignoreFilter.
2021-06-04 18:22:19 +02:00
If you want to make your own filter from scratch, you may use
```nix
2024-03-27 19:10:27 +01:00
{
gitignoreFilter = ign: root: filterPattern (gitignoreToPatterns ign) root;
}
2021-06-04 18:22:19 +02:00
```
## gitignore files in subdirectories {#sec-pkgs-nix-gitignore-usage-recursive}
If you wish to use a filter that would search for .gitignore files in subdirectories, just like git does by default, use this function:
```nix
2024-03-27 19:10:27 +01:00
{
# gitignoreFilterRecursiveSource = filter: patterns: root:
# OR
gitignoreRecursiveSource = gitignoreFilterSourcePure (_: _: true);
}
2021-06-04 18:22:19 +02:00
```