/
gitignore.nix
50 lines (50 loc) · 1.65 KB
/
gitignore.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{pkgs, config, lib, ...}:
let
cfg = config.files.gitignore;
enabledAttrs = lib.filterAttrs (name: value: value) cfg.pattern;
patterns = builtins.attrNames enabledAttrs;
toList = names: lib.ca;
githubIgnore = builtins.fetchGit {
url = "https://github.com/github/gitignore";
ref = "main";
rev = "cdd9e946da421758c6f42c427c7bc65c8326155d";
};
enabledTemplates = builtins.attrNames (lib.filterAttrs (name: value: value) cfg.template);
templatePatterns = map (name: builtins.readFile "${githubIgnore}/${name}.gitignore") enabledTemplates;
in {
options.files.gitignore.enable = lib.mkEnableOption "Auto generated .gitignore file";
options.files.gitignore.pattern = lib.mkOption {
type = lib.types.attrsOf lib.types.bool;
description = "Gitignore pattern to ignore";
default = {};
example = {
"ignore-this-file.txt" = true;
};
};
options.files.gitignore.template = lib.mkOption {
type = lib.types.attrsOf lib.types.bool;
description = "Append gitignore template from https://github.com/github/gitignore";
default = {};
example = {
Android = true;
"community/AWS/SAM" = true;
};
};
config.commands = lib.mkIf cfg.enable [
{
help = "list available ignore templates";
package = pkgs.writeShellScriptBin "files-ignore-templates" ''
echo "Available Git Ignore Templates:"
find ${githubIgnore} -name '*.gitignore' \
| sed "s#${githubIgnore}/##" \
| sed "s#\.gitignore##" \
| sort
'';
}
];
config.files.text = lib.mkIf cfg.enable {
"/.gitignore" = builtins.concatStringsSep "\n" (
patterns ++ templatePatterns
);
};
}