about summary refs log tree commit diff
path: root/colmena/cobalt/services/gitolite.nix
blob: af4c88a505776637bee252150758d3781f2d649b (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
{ pkgs, ... }:

let
  # https://groups.google.com/g/gitolite/c/NwZ1-hq9-9E/m/mDbiKyAvDwAJ
  fixRefsTrigger = pkgs.writeText "fix-refs" ''
    [[ $4 == W ]] || exit 0

    cd $GL_REPO_BASE/$2.git

    head=`git symbolic-ref HEAD`
    [[ -f $head ]] || {
      set -- refs/heads/*
      git symbolic-ref HEAD $1
    }
  '';
in
{
  services.gitolite = {
    enable = true;
    user = "git";
    group = "git";
    adminPubkey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDi7GGOGVj1Y5Sc1EW6zEdrp78dS6hvmS348pqu9dUsB openpgp:0x6BE7BD6F";
    extraGitoliteRc = ''
      $RC{UMASK} = 0027;
      $RC{GIT_CONFIG_KEYS} = '.*';
      $RC{ROLES}{OWNERS} = 1;
      $RC{OWNER_ROLENAME} = 'OWNERS';
      # For some unknown reason, $ENV{HOME} doesn't get resolved to the correct
      # directory.
      # $RC{LOCAL_CODE} = '$ENV{HOME}/local';
      $RC{LOCAL_CODE} = '/var/lib/gitolite/local';
      push(@{$RC{ENABLE}}, 'D');
      push(@{$RC{POST_GIT}}, 'fix-refs');
      # push(@{$RC{ENABLE}}, 'set-default-roles');
      # push(@{$RC{ENABLE}}, 'create');
      # push(@{$RC{ENABLE}}, 'fork');

    '';
  };

  environment.persistence."/persist".directories = [
    "/var/lib/gitolite"
  ];

  systemd.tmpfiles.rules = [
    "C /var/lib/gitolite/local/triggers/fix-refs 755 git git - ${fixRefsTrigger}"
  ];


  systemd.timers."gitolite-trash-cleanup" = {
    wantedBy = [ "timers.target" ];
    timerConfig = {
      OnCalendar = "*-*-* 00:00:00";
      Unit = "gitolite-trash-cleanup.service";
    };
  };

  systemd.services."gitolite-trash-cleanup" = {
    script = ''
      set -euo pipefail
      if [ ! -d "Trash" ] ; then
        echo Trash directory is nonexistent!
        echo No operations to perform. Exiting.
        exit 0
      fi

      match=$(find Trash -type d -regextype posix-extended -regex ".*/[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}:[0-9]{2}:[0-9]{2}$")
      processed_entry=0
      removed_entry=0

      for dir in $match
      do
        system_timestamp=$(date +%s)
        trash_timestamp=$(basename $dir | sed -e "s/_/ /g" | date -f - +%s)
        age=$(( $system_timestamp - $trash_timestamp ))
        # Wipe trashes older than 2w
        if [[ age -gt 1209600 ]] ; then
          echo "Removing '$dir' (age $age)"
          rm -rf $dir
          ((removed_entry+=1))
        fi
        ((processed_entry+=1))
      done

      echo "Directories that needs cleanup:"
      find Trash -type d -empty -print -delete
      echo "Cleaned empty directories."

      echo "Done! Removed $removed_entry/$processed_entry"
    '';

    path = with pkgs; [ bash util-linux coreutils ];

    serviceConfig = {
      Type = "oneshot";
      User = "git";
      WorkingDirectory = "/var/lib/gitolite/repositories";
    };
  };
}