summaryrefslogtreecommitdiff
path: root/modules/nixos/server/github-mirror/default.nix
blob: 9d0d8706a775c53cdec475d9c3f6bbc798aa626c (plain)
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
{
  config,
  lib,
  pkgs,
  ...
}:
let
  cfg = config.services.github-mirror;
  cgitInstance = config.services.cgit.${cfg.hostname};

  update-mirror =
    pkgs.runCommand "update-mirror"
      {
        nativeBuildInputs = [ pkgs.patsh ];

        buildInputs = [
          config.programs.git.package
          pkgs.curl
          pkgs.jq
        ];
      }
      ''
        patsh -s ${builtins.storeDir} ${./update-mirror.sh} $out
        chmod 755 $out
        patchShebangs $out
      '';
in
{
  options.services.github-mirror = {
    enable = lib.mkEnableOption "the github-mirror service";

    hostname = lib.mkOption {
      type = lib.types.str;
      description = "Hostname of the cgit service to create";
      example = lib.literalExpression "git.example.com";
    };

    mirroredUsers = lib.mkOption {
      type = lib.types.listOf lib.types.str;
      description = "List of GitHub users to mirror repositories for";
      example = lib.literalExpression ''[ "edolstra" ]'';
    };
  };

  config = lib.mkIf cfg.enable {
    assertions = [
      {
        assertion = cfg.mirroredUsers != [ ];
        message = "`services.git-mirror.mirroredUsers` must have at least one user";
      }
    ];

    services.cgit.${cfg.hostname} = {
      enable = true;

      scanPath = "/var/lib/cgit/${cfg.hostname}";
      settings = {
        robots = "none"; # noindex, nofollow
      };

      user = "cgit";
      group = "cgit";
    };

    systemd = {
      services.github-mirror = {
        description = "Mirror a GitHub repository";

        after = [ "network-online.target" ];
        wants = [ "network-online.target" ];

        script = toString (
          [
            "exec"
            (toString update-mirror)
            "--directory"
            cgitInstance.scanPath
          ]
          ++ cfg.mirroredUsers
        );

        serviceConfig = {
          Type = "oneshot";
          User = cgitInstance.user;
          Group = cgitInstance.group;
        };
      };

      timers.github-mirror = {
        description = "Hourly timer for %N";
        timerConfig.OnCalendar = "hourly";
      };

      tmpfiles.settings."10-github-mirror" = {
        ${cgitInstance.scanPath}.d = {
          inherit (cgitInstance) user group;
        };
      };
    };
  };
}