summaryrefslogtreecommitdiffstats
path: root/krebs/2configs/wiki.nix
blob: 4b0bf9768bc69ceb52982dbfd0bf025140b151bc (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
102
103
104
105
{ config, lib, pkgs, ... }:
with import ../../lib/pure.nix { inherit lib; };
let

  setupGit = ''
    export PATH=${makeBinPath [ pkgs.git ]}
    export GIT_SSH_COMMAND='${pkgs.openssh}/bin/ssh -i ${config.services.gollum.stateDir}/.ssh/id_ed25519'
    repo='git@localhost:wiki'
    cd ${config.services.gollum.stateDir}
    if ! url=$(git config remote.origin.url); then
      git remote add origin "$repo"
    elif test "$url" != "$repo"; then
      git remote set-url origin "$repo"
    fi
  '';

  pushGollum = pkgs.writers.writeDash "push_gollum" ''
    ${setupGit}
    git fetch origin
    git merge --ff-only origin/master
  '';

  pushCgit = pkgs.writers.writeDash "push_cgit" ''
    ${setupGit}
    git push origin master
  '';

in
{
  services.gollum = {
    enable = true;
    address = "::1";
    extraConfig = ''
      Gollum::Hook.register(:post_commit, :hook_id) do |committer, sha1|
        system('${pushCgit}')
      end
    '';
  };

  systemd.services.gollum.environment.LC_ALL = "en_US.UTF-8";

  networking.firewall.allowedTCPPorts = [ 80 443 ];
  security.acme.certs."wiki.r".server = config.krebs.ssl.acmeURL;
  services.nginx = {
    enable = true;
    virtualHosts."wiki.r" = {
      enableACME = true;
      addSSL = true;
      locations."/" = {
        proxyPass = "http://[::1]:${toString config.services.gollum.port}";
        proxyWebsockets = true;
        extraConfig = ''
          proxy_set_header Host $host;
        '';
      };
    };
  };

  krebs.git = {
    enable = true;
    cgit.settings = {
      root-title = "krebs repos";
    };
    rules = with git; [
      {
        user = [
          {
            name = "gollum";
            pubkey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMXbjDnQWg8EECsNRZZWezocMIiuENhCSQFcFUXcsOQ6";
          }
        ] ++ (attrValues config.krebs.users);
        repo = [ config.krebs.git.repos.wiki ];
        perm = push ''refs/heads/master'' [ create merge ];
      }
    ];
    repos.wiki = {
      public = true;
      name = "wiki";
      hooks = {
        post-receive = ''
          ${pkgs.git-hooks.irc-announce {
            channel = "#xxx";
            refs = [
              "refs/heads/master"
            ];
            nick = config.networking.hostName;
            server = "irc.r";
            verbose = true;
          }}
          /run/wrappers/bin/sudo -S -u gollum ${pushGollum}
        '';
      };
    };
  };

  krebs.secret.files.gollum = {
    path = "${config.services.gollum.stateDir}/.ssh/id_ed25519";
    owner = { name = "gollum"; };
    source-path = "${config.krebs.secret.directory}/gollum.id_ed25519";
  };

  security.sudo.extraConfig = ''
    git ALL=(gollum) NOPASSWD: ${pushGollum}
  '';
}