summaryrefslogtreecommitdiff
path: root/microvms/cgit/cgit.nix
blob: 09b10483edc85d091fd426a120d8a4a41c66093a (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
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
{ pkgs, lib, ... }:
let
  host = "git.oxapentane.com";
  gitHome = "/var/lib/git";
  cgitPort = 8080;

    defaultBranch = "main";

  mkCreateRepoCommand =
    {
      command,
      visibility,
    }:
    pkgs.writeShellApplication {
      name = command;

      runtimeInputs = [
        pkgs.coreutils
        pkgs.git
        pkgs.gnugrep
      ];

      text = ''
        set -eu

        if [ "$#" -ne 1 ]; then
          printf 'usage: ${command} NAME\n' >&2
          exit 2
        fi

        name=$1

        # Accept either "foo" or "foo.git".
        case "$name" in
          *.git)
            name="''${name%.git}"
            ;;
        esac

        # Deliberately only allow a single path component.
        # This excludes traversal, slashes, spaces and shell metacharacters.
        if ! printf '%s\n' "$name" \
          | grep -Eq '^[A-Za-z0-9][A-Za-z0-9._-]*$'
        then
          printf '%s\n' \
            'invalid repository name: use letters, digits, ".", "_" and "-"' \
            >&2
          exit 2
        fi

        base=${lib.escapeShellArg "${gitHome}/${visibility}"}
        repo="$base/$name.git"

        umask 0027

        # mkdir is the existence check: never silently reinitialize a repo.
        if ! mkdir -- "$repo" 2>/dev/null; then
          printf 'repository already exists or cannot be created: %s\n' \
            "$name.git" >&2
          exit 1
        fi

        if ! git init \
          --bare \
          --initial-branch=${lib.escapeShellArg defaultBranch} \
          "$repo" \
          >/dev/null
        then
          printf 'failed to initialize repository: %s\n' "$name.git" >&2
          exit 1
        fi

        # Avoid Git's default "Unnamed repository..." text appearing in cgit.
        : > "$repo/description"

        # This is the only successful stdout output, making $() safe.
        printf '%s\n' "git@${host}:${visibility}/$name.git"
      '';
    };

  createPrivate = mkCreateRepoCommand {
    command = "create";
    visibility = "private";
  };

  createPublic = mkCreateRepoCommand {
    command = "create-pub";
    visibility = "public";
  };

  noInteractiveLogin = pkgs.writeShellScript "no-interactive-login" ''
    printf '%s\n' "Interactive shell access is disabled." >&2
    exit 128
  '';

  restrictedGitShell =
    (pkgs.writeShellApplication {
      name = "restricted-git-shell";

      text = ''
      umask 0027
      exec ${pkgs.git}/bin/git-shell "$@"
    '';
    }).overrideAttrs (old: {
      passthru = (old.passthru or { }) // {
        shellPath = "/bin/restricted-git-shell";
      };
    });
in
{
  # public repos in cgit
  services.cgit."${host}" = {
    enable = true;
    user = "cgit";
    group = "cgit";
    scanPath = "${gitHome}/public";
    gitHttpBackend = {
      enable = true;
      checkExportOkFiles = false; # /public is unconditionally public anyway...
    };

    settings = {
      root-title = "0xa's compilable shitposts";
      root-desc = "Public directory of my finest spaghetti";

      remove-suffix = true;
      clone-url =
        "https://${host}/$CGIT_REPO_URL.git "
        + "git@${host}:public/$CGIT_REPO_URL.git";

      enable-http-clone = false;
      enable-filter-overrides = false;
      enable-git-config = false;
      enable-http-serving = false;

      max-blob-size = 2048;
      noplainemail = true;

      snapshots = "";
    };

    nginx = {
      virtualHost = host;
      location = "/";
    };
  };

  # just bind to proxy interface, everything's handeled by caddy
  services.nginx.virtualHosts.${host}.listen = [
    {
      addr = "10.89.88.19";
      port = cgitPort;
    }
  ];

  # private - bare repos behind separate user
  users.groups.git = {};
  users.users.git = {
    isSystemUser = true;
    group = "git";
    home = gitHome;
    createHome = false;
    shell = restrictedGitShell;
    hashedPassword = "!";
    openssh.authorizedKeys.keys = [
      "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAXrwZsBChUhVuF5gFEfj8GwQlnrqEttAytS5jVCfuE4 0xa@frituurpan"
    ];
  };

  # enable and cofigure ssh
  services.openssh = {
    enable = true;
    extraConfig = ''
      Match User git
      AuthenticationMethods publickey
      PasswordAuthentication no
      KbdInteractiveAuthentication no
      DisableForwarding yes
      PermitTTY no
      PermitUserRC no
    '';
  };

  # ensure directories and rights
  systemd.tmpfiles.rules = [
    "d ${gitHome}         0711 git git  -"
    "d ${gitHome}/public  2750 git cgit -"
    "d ${gitHome}/private 0711 git git  -"
    "d ${gitHome}/git-shell-commands 0700 git git -"


    "L+ ${gitHome}/git-shell-commands/create               - - - - ${createPrivate}/bin/create"
    "L+ ${gitHome}/git-shell-commands/create-pub           - - - - ${createPublic}/bin/create-pub"
    "L+ ${gitHome}/git-shell-commands/no-interactive-login - - - - ${noInteractiveLogin}"
  ];

  # dubious ownership error
  systemd.services."fcgiwrap-cgit-${host}".environment = {
    GIT_CONFIG_COUNT = "1";
    GIT_CONFIG_KEY_0 = "safe.directory";
    GIT_CONFIG_VALUE_0 = "${gitHome}/public/*";
  };
}