about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/misc/sourcehut/builds.nix
blob: e446f08284f7cfc020f9ef837f8e5bd31f21cf3c (plain) (blame)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
{ config, lib, pkgs, ... }:

with lib;
let
  cfg = config.services.sourcehut;
  scfg = cfg.builds;
  rcfg = config.services.redis;
  iniKey = "builds.sr.ht";

  drv = pkgs.sourcehut.buildsrht;
in
{
  options.services.sourcehut.builds = {
    user = mkOption {
      type = types.str;
      default = "buildsrht";
      description = ''
        User for builds.sr.ht.
      '';
    };

    port = mkOption {
      type = types.port;
      default = 5002;
      description = ''
        Port on which the "builds" module should listen.
      '';
    };

    database = mkOption {
      type = types.str;
      default = "builds.sr.ht";
      description = ''
        PostgreSQL database name for builds.sr.ht.
      '';
    };

    statePath = mkOption {
      type = types.path;
      default = "${cfg.statePath}/buildsrht";
      description = ''
        State path for builds.sr.ht.
      '';
    };

    enableWorker = mkOption {
      type = types.bool;
      default = false;
      description = ''
        Run workers for builds.sr.ht.
      '';
    };

    images = mkOption {
      type = types.attrsOf (types.attrsOf (types.attrsOf types.package));
      default = { };
      example = lib.literalExample ''(let
          # Pinning unstable to allow usage with flakes and limit rebuilds.
          pkgs_unstable = builtins.fetchGit {
              url = "https://github.com/NixOS/nixpkgs";
              rev = "ff96a0fa5635770390b184ae74debea75c3fd534";
              ref = "nixos-unstable";
          };
          image_from_nixpkgs = pkgs_unstable: (import ("${pkgs.sourcehut.buildsrht}/lib/images/nixos/image.nix") {
            pkgs = (import pkgs_unstable {});
          });
        in
        {
          nixos.unstable.x86_64 = image_from_nixpkgs pkgs_unstable;
        }
      )'';
      description = ''
        Images for builds.sr.ht. Each package should be distro.release.arch and point to a /nix/store/package/root.img.qcow2.
      '';
    };

  };

  config = with scfg; let
    image_dirs = lib.lists.flatten (
      lib.attrsets.mapAttrsToList
        (distro: revs:
          lib.attrsets.mapAttrsToList
            (rev: archs:
              lib.attrsets.mapAttrsToList
                (arch: image:
                  pkgs.runCommand "buildsrht-images" { } ''
                    mkdir -p $out/${distro}/${rev}/${arch}
                    ln -s ${image}/*.qcow2 $out/${distro}/${rev}/${arch}/root.img.qcow2
                  '')
                archs)
            revs)
        scfg.images);
    image_dir_pre = pkgs.symlinkJoin {
      name = "builds.sr.ht-worker-images-pre";
      paths = image_dirs ++ [
        "${pkgs.sourcehut.buildsrht}/lib/images"
      ];
    };
    image_dir = pkgs.runCommand "builds.sr.ht-worker-images" { } ''
      mkdir -p $out/images
      cp -Lr ${image_dir_pre}/* $out/images
    '';
  in
  lib.mkIf (cfg.enable && elem "builds" cfg.services) {
    users = {
      users = {
        "${user}" = {
          isSystemUser = true;
          group = user;
          extraGroups = lib.optionals cfg.builds.enableWorker [ "docker" ];
          description = "builds.sr.ht user";
        };
      };

      groups = {
        "${user}" = { };
      };
    };

    services.postgresql = {
      authentication = ''
        local ${database} ${user} trust
      '';
      ensureDatabases = [ database ];
      ensureUsers = [
        {
          name = user;
          ensurePermissions = { "DATABASE \"${database}\"" = "ALL PRIVILEGES"; };
        }
      ];
    };

    systemd = {
      tmpfiles.rules = [
        "d ${statePath} 0755 ${user} ${user} -"
      ] ++ (lib.optionals cfg.builds.enableWorker
        [ "d ${statePath}/logs 0775 ${user} ${user} - -" ]
      );

      services = {
        buildsrht = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey
          {
            after = [ "postgresql.service" "network.target" ];
            requires = [ "postgresql.service" ];
            wantedBy = [ "multi-user.target" ];

            description = "builds.sr.ht website service";

            serviceConfig.ExecStart = "${cfg.python}/bin/gunicorn ${drv.pname}.app:app -b ${cfg.address}:${toString port}";

            # Hack to bypass this hack: https://git.sr.ht/~sircmpwn/core.sr.ht/tree/master/item/srht-update-profiles#L6
          } // { preStart = " "; };

        buildsrht-worker = {
          enable = scfg.enableWorker;
          after = [ "postgresql.service" "network.target" ];
          requires = [ "postgresql.service" ];
          wantedBy = [ "multi-user.target" ];
          partOf = [ "buildsrht.service" ];
          description = "builds.sr.ht worker service";
          path = [ pkgs.openssh pkgs.docker ];
          preStart = let qemuPackage = pkgs.qemu_kvm;
          in ''
            if [[ "$(docker images -q qemu:latest 2> /dev/null)" == "" || "$(cat ${statePath}/docker-image-qemu 2> /dev/null || true)" != "${qemuPackage.version}" ]]; then
              # Create and import qemu:latest image for docker
              ${
                pkgs.dockerTools.streamLayeredImage {
                  name = "qemu";
                  tag = "latest";
                  contents = [ qemuPackage ];
                }
              } | docker load
              # Mark down current package version
              printf "%s" "${qemuPackage.version}" > ${statePath}/docker-image-qemu
            fi
          '';
          serviceConfig = {
            Type = "simple";
            User = user;
            Group = "nginx";
            Restart = "always";
          };
          serviceConfig.ExecStart = "${pkgs.sourcehut.buildsrht}/bin/builds.sr.ht-worker";
        };
      };
    };

    services.sourcehut.settings = {
      # URL builds.sr.ht is being served at (protocol://domain)
      "builds.sr.ht".origin = mkDefault "http://builds.${cfg.originBase}";
      # Address and port to bind the debug server to
      "builds.sr.ht".debug-host = mkDefault "0.0.0.0";
      "builds.sr.ht".debug-port = mkDefault port;
      # Configures the SQLAlchemy connection string for the database.
      "builds.sr.ht".connection-string = mkDefault "postgresql:///${database}?user=${user}&host=/var/run/postgresql";
      # Set to "yes" to automatically run migrations on package upgrade.
      "builds.sr.ht".migrate-on-upgrade = mkDefault "yes";
      # builds.sr.ht's OAuth client ID and secret for meta.sr.ht
      # Register your client at meta.example.org/oauth
      "builds.sr.ht".oauth-client-id = mkDefault null;
      "builds.sr.ht".oauth-client-secret = mkDefault null;
      # The redis connection used for the celery worker
      "builds.sr.ht".redis = mkDefault "redis://${rcfg.bind}:${toString rcfg.port}/3";
      # The shell used for ssh
      "builds.sr.ht".shell = mkDefault "runner-shell";
      # Register the builds.sr.ht dispatcher
      "git.sr.ht::dispatch".${builtins.unsafeDiscardStringContext "${pkgs.sourcehut.buildsrht}/bin/buildsrht-keys"} = mkDefault "${user}:${user}";

      # Location for build logs, images, and control command
    } // lib.attrsets.optionalAttrs scfg.enableWorker {
      # Default worker stores logs that are accessible via this address:port
      "builds.sr.ht::worker".name = mkDefault "127.0.0.1:5020";
      "builds.sr.ht::worker".buildlogs = mkDefault "${scfg.statePath}/logs";
      "builds.sr.ht::worker".images = mkDefault "${image_dir}/images";
      "builds.sr.ht::worker".controlcmd = mkDefault "${image_dir}/images/control";
      "builds.sr.ht::worker".timeout = mkDefault "3m";
    };

    services.nginx.virtualHosts."logs.${cfg.originBase}" =
      if scfg.enableWorker then {
        listen = with builtins; let address = split ":" cfg.settings."builds.sr.ht::worker".name;
        in [{ addr = elemAt address 0; port = lib.toInt (elemAt address 2); }];
        locations."/logs".root = "${scfg.statePath}";
      } else { };

    services.nginx.virtualHosts."builds.${cfg.originBase}" = {
      forceSSL = true;
      locations."/".proxyPass = "http://${cfg.address}:${toString port}";
      locations."/query".proxyPass = "http://${cfg.address}:${toString (port + 100)}";
      locations."/static".root = "${pkgs.sourcehut.buildsrht}/${pkgs.sourcehut.python.sitePackages}/buildsrht";
    };
  };
}