about summary refs log tree commit diff
path: root/modules/git/default.nix
blob: eb0021dec118317297b70f098176969b605591ee (plain) (blame)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
{
  config,
  inputs,
  lib,
  libNginx,
  libPlausible,
  pkgs,
  this,
  ...
}:
with lib;
let
  cfg = config.nixfiles.modules.git;
in
{
  options.nixfiles.modules.git = {
    client.enable = mkEnableOption "Git client";

    server = {
      enable = mkEnableOption "Git server";

      domain = mkOption {
        description = "Domain name sans protocol scheme.";
        type = with types; nullOr str;
        default = "git.${config.networking.domain}";
      };

      package = mkOption {
        description = "Package.";
        type = types.package;
        default = pkgs.cgit;
      };
    };
  };

  config = mkMerge [
    (mkIf cfg.client.enable {
      secrets = {
        glab-cli-config = {
          file = "${inputs.self}/secrets/glab-cli-config";
          path = "${config.dirs.config}/glab-cli/config.yml";
          owner = my.username;
        };
        # NOTE SSO requires relogin every day, so keeping persistent auth tokens
        # doesn't work.
        # gh-hosts = {
        #   file = "${inputs.self}/secrets/gh-hosts";
        #   path = "${config.dirs.config}/gh/hosts.yml";
        #   owner = my.username;
        # };
        hut = {
          file = "${inputs.self}/secrets/hut";
          path = "${config.dirs.config}/hut/config";
          owner = my.username;
        };
      };

      nixfiles.modules.common.shell.aliases = {
        gl = "glab";
        ht = "hut";
      };

      hm = {
        home.packages = with pkgs; [
          git-extras
          glab
          hut
        ];

        programs = {
          git = {
            enable = true;

            package = if this.isHeadful then pkgs.gitFull else pkgs.gitMinimal;

            userName = my.fullname;
            userEmail = my.email;
            signing = {
              inherit (my.pgp) key;
              signByDefault = true;
            };

            extraConfig =
              {
                color.ui = true;
                core.whitespace = "trailing-space";
                init.defaultBranch = "master";
                status.submoduleSummary = true;
                commit.verbose = true;
                push.autoSetupRemote = true;
                pull.rebase = true;
                rebase = {
                  autoStash = true;
                  autoSquash = true;
                };
                rerere.enabled = true;
                branch.sort = "-committerdate";
                diff = {
                  mnemonicPrefix = true;
                  renames = "copies";
                  submodule = "log";
                };
                submodule.recurse = true;
                sendemail = rec {
                  smtpServer = my.domain.shire;
                  smtpUser = "${my.username}@${smtpServer}";
                  smtpEncryption = "ssl";
                  smtpServerPort = 465;
                  annotate = true;
                  confirm = "always";
                };
                column.ui = "auto";
                github.user = my.username;
                gitlab.user = my.username;
              }
              // mapAttrs' (name: value: nameValuePair ''url "git@${value}:"'' { insteadOf = "${name}:"; }) {
                "bitbucket" = "bitbucket.com";
                "codeberg" = "codeberg.org";
                "github" = "github.com";
                "gitlab" = "gitlab.com";
                "sourcehut" = "git.sr.ht";
              }
              //
                mapAttrs' (name: values: nameValuePair ''url "https://${values}/"'' { insteadOf = "${name}:"; })
                  {
                    "alpine" = "gitlab.alpinelinux.org";
                    "debian" = "salsa.debian.org";
                    "freedesktop" = "gitlab.freedesktop.org";
                    "gnome" = "gitlab.gnome.org";
                    "haskell" = "gitlab.haskell.org";
                    "homotopic" = "gitlab.homotopic.tech";
                    "horizon" = "gitlab.horizon-haskell.net";
                    "kde" = "invent.kde.org";
                    "nixca" = "gitlab.nixca.dev";
                    "notabug" = "notabug.org";
                    "opencode" = "opencode.net";
                    "torproject" = "gitlab.torproject.org";
                    "videolan" = "code.videolan.org";
                  };

            aliases =
              let
                git = getExe config.hm.programs.git.package;
                curl = getExe pkgs.curl;
              in
              {
                amend = "commit --amend";
                cat = "cat-file -p";
                fast = "clone --depth=1";
                fixup = "commit --fixup";
                fuck = "!${git} reset --hard && ${git} clean --force -dx";
                get = "pull --all --recurse-submodules --autostash";
                gud = ''commit -m "git gud"'';
                refresh = "clean --force -dx";
                tree = "log --graph --date=relative --pretty=tformat:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%an %ad)%Creset'";
                uncommit = "reset --soft HEAD~1";
                untrack = "rm --cache --";
                wtc = "!${curl} -sq whatthecommit.com/index.txt | ${git} commit -F -";
              };

            # All helper tools/editor generated files should go here. This must be
            # kept void of any project-specific or residual files.
            ignores = [
              "*~"
              ".DS_Store"
              ".cache/clangd/"
              ".ccls-cache/"
              ".gdb_history"
              ".netrwhist"
              ".projectile"
              "[._]*.s[a-v][a-z]"
              "[._]*.sw[a-p]"
              "[._]s[a-rt-v][a-z]"
              "[._]ss[a-gi-z]"
              "[._]sw[a-p]"
              "\#*\#"
              "compile_commands*.json"
              "cscope.*"
              "vgcore.*"
            ];
          };

          gh = {
            enable = true;
            settings.git_protocol = "ssh";
          };
        };
      };
    })
    (mkIf cfg.server.enable (
      with cfg.server;
      {
        ark.directories = [ config.services.gitolite.dataDir ];

        # FIXME Plausible, go-import, custom favicon, etc.
        nixfiles.modules.nginx = {
          enable = true;
          virtualHosts.${domain}.locations = { };
        };

        services = {
          cgit.${domain} = {
            enable = true;

            # We make gitolite repos readable by the common group.
            group = "git";

            scanPath = "${config.services.gitolite.dataDir}/repositories";

            settings = {
              root-title = "cgit";
              root-desc = "https://github.com/azahi";

              clone-url = "https://${domain}/$CGIT_REPO_URL";

              about-filter = "${package}/lib/cgit/filters/about-formatting.sh";
              source-filter = "${package}/lib/cgit/filters/syntax-highlighting.py";
              commit-filter = "${package}/lib/cgit/filters/commit-links.sh";

              enable-git-config = true;
              enable-gitweb-owner = true;
              remove-suffix = true;

              logo = "/logo.gif";
            };
          };

          gitolite = {
            enable = true;
            user = "git";
            group = "git";
            adminPubkey = my.ssh.key;
            extraGitoliteRc = ''
              # This allows cgit to scan repositories while running under a
              # different user.
              $RC{UMASK} = 0027;

              # This allows hiding repositories via "cgit.ignore"[1].
              #
              # [1]: https://www.omarpolo.com/post/cgit-gitolite.html
              $RC{GIT_CONFIG_KEYS} = '.*';
            '';
          };

          nginx.virtualHosts.${domain}.locations =
            let
              extraHead = ''
                ${libNginx.config.appendHead [
                  ''<meta name="go-import" content="$host$uri git https://$host$uri">''
                  (libPlausible.htmlPlausibleScript { inherit (cfg.server) domain; })
                ]}
              '';
            in
            {
              "/" = {
                extraConfig = mkBefore extraHead;
                fastcgiParams.HTTP_ACCEPT_ENCODING = "";
              };
              "~ /.+/(info/refs|git-upload-pack)" = {
                extraConfig = mkBefore extraHead;
                fastcgiParams.HTTP_ACCEPT_ENCODING = "";
              };
              "= /logo.gif".alias = "${./logo.gif}";
              "= /favicon.ico" = {
                alias = "${./favicon.ico}";
                extraConfig = mkForce "";
              };
              "= /cgit.css" = {
                alias = pkgs.writeText "cgit.css" ''
                  ${readFile "${package}/cgit/cgit.css"}
                  div#cgit {
                    font-family: ${
                      concatMapStringsSep ", " (f: ''"${f}"'') config.fonts.fontconfig.defaultFonts.monospace
                    }, monospace !important;
                    -moz-tab-size: 2 !important;
                    tab-size: 2 !important;
                  }
                '';
                extraConfig = mkForce "";
              };
            };
        };
      }
    ))
  ];
}

Consider giving Nix/NixOS a try! <3