on bc4400d help sign in
haskell.nix/docs/tutorials/clean-git.md
90 lines 3.3 kB history raw
1# cleanGit
3To filter out just the files in your git index use
4`haskell-nix.haskellLib.cleanGit { src = ./.; }` where `./.` is the
5root of your git repo (or a git work tree).
7First it filters just the files needed to run `git index`, then
8it uses the results of that to filter your directory. It does not
9need to parse the `.gitignore` files at all, but we do need to
10`git add` our files before they will be included.
11[cleanGit source](https://github.com/input-output-hk/haskell.nix/blob/master/lib/clean-git.nix).
13In addition haskell.nix (including `cleanGit`) uses a version
14of [`cleanSourceWith`](https://github.com/input-output-hk/haskell.nix/blob/master/lib/clean-source-with.nix)
15with a `subdir` argument to filter out just the package it is
16building. Then it uses the info from the `cabal` file to filter
17just the source dirs for the component it is building. That way
18if we modify a test in a package nix will not rebuild the library
19in that package (or anything that depends on that package's library).
21There is a downside to this though. If we have a test that depends
22on something outside the scope of what is described in its entry in
23the in the `.cabal` file it will not see it. For instance perhaps
24it needs to run `hlint` or `doctest` on the library source. There
25are ways to fix this with a module:
27Use `extraSrcFiles` to add dirs the test needs (this will not result
28in a change to the `.cabal` file the test will still be built the same).
29```
30components.tests.test.extraSrcFiles = [ "subdir-needed-by-test" ];
31```
32Or alternatively, override the source with a suitable filter function.
33```
34components.tests.test.src = haskell-nix.haskellLib.cleanSourceWith {
35 inherit src;
36 subdir = "path-to-package";
37 filter = ...
38};
39```
41## Multiple Git Repositories with cleanGits
43Some times it is handy to temporarily use a relative path between git
44repos. If the repos are individually cleaned this is not possible
45(since the cleaned version of one repo will never include the files
46of the other).
48There are 3 options:
50* We could `symlinkJoin` the cleaned directories together, but the
51 result could not be cleaned and any change would to either
52 repo would result in a rebuild of everything.
54* We could add one repo to the other as a submodule,
55 but adding and then removing a submodule is a pain and it does not
56 work well if you have more than one repo that needs to share the
57 submodule.
59* We could add a `source-repository-package` but then we would have
60 to commit each change before testing.
62`cleanGits` allows us to specify a root directory and any number of
63sub directories containing git repos.
65For example if `repoA` and `repoB` are two git repos with
66cabal packages and want to use the `repoB` package when building
67`repoA`. First we can add `../repoB` to `repoA/cabal.project`:
69```
70packages:
71 ./.
72 ../repoB
73```
75Then in `repoA/default.nix` we can use:
77```
78haskell-nix.project {
79 src = haskell-nix.haskellLib.cleanSourceWith {
80 src = haskell-nix.haskellLib.cleanGits {
81 name = "root";
82 src = ../.; # Parent dir that contains repoA and repoB
83 gitDirs = [ "repoA" "repoB" ];
84 };
85 subDir = "repoA"; # Where to look for the `cabal.project`
86 includeSiblings = true; # Tells it not to exclude `repoB` dir
87 };
89```