summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/konsens.nix
blob: d27a702fb0f9c4df3d70ad0014fa706b9a1c57c8 (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
{ config, lib, pkgs, ... }:

with import <stockholm/lib>;

let
  cfg = config.krebs.konsens;

  out = {
    options.krebs.konsens = api;
    config = lib.mkIf cfg.enable imp;
  };

  api = {
    enable = mkEnableOption "git konsens finder";
    repos = mkOption {
      type = types.attrsOf (types.submodule ({ config, ...}: {
        options = {
          url = mkOption {
            type = types.str;
            default = "git@localhost:${config._module.args.name}";
          };
          branchesToCheck = mkOption {
            type = types.listOf types.str;
            default = [ "lassulus" "makefu" "tv" ];
          };
          target = mkOption {
            type = types.str;
            default = "master";
          };
          timerConfig = mkOption {
            type = types.attrsOf types.str;
            default = {
              OnCalendar = "*:00,15,30,45";
            };
          };
        };
      }));
    };
  };

  imp = {
    users.groups.konsens.gid = genid "konsens";
    users.users.konsens = rec {
      name = "konsens";
      group = "konsens";
      uid = genid name;
      home = "/var/lib/konsens";
      isSystemUser = true;
      createHome = true;
    };

    systemd.timers = mapAttrs' (name: repo:
      nameValuePair "konsens-${name}" {
        description = "konsens timer";
        wantedBy = [ "timers.target" ];
        timerConfig = repo.timerConfig;
      }
    ) cfg.repos;

    systemd.services = mapAttrs' (name: repo:
      nameValuePair "konsens-${name}" {
        after = [ "network.target" ];
        path = [ pkgs.git ];
        restartIfChanged = false;
        serviceConfig = {
          Type = "simple";
          PermissionsStartOnly = true;
          ExecStart = pkgs.writeDash "konsens-${name}" ''
            set -efu
            git config --global --replace-all safe.directory *
            if ! test -e ${name}; then
              git clone ${repo.url} ${name}
            fi
            cd ${name}
            git fetch origin
            git push origin $(git merge-base --octopus ${concatMapStringsSep " " (branch: "origin/${branch}") repo.branchesToCheck}):refs/heads/master
          '';
          WorkingDirectory = /var/lib/konsens;
          User = "konsens";
        };
      }
    ) cfg.repos;
  };

in out