1 { config, lib, pkgs, ... }:
5 cfg = config.services.sourcehut;
7 rcfg = config.services.redis;
8 iniKey = "builds.sr.ht";
9 statePath = "/var/lib/sourcehut/buildsrht";
11 drv = pkgs.sourcehut.buildsrht;
14 options.services.sourcehut.builds = {
15 enable = mkEnableOption "builds service";
19 default = "buildsrht";
21 User for builds.sr.ht.
29 Port on which the "builds" module should listen.
35 default = "builds.sr.ht";
37 PostgreSQL database name for builds.sr.ht.
41 enableWorker = mkOption {
45 Run workers for builds.sr.ht.
50 type = types.attrsOf (types.attrsOf (types.attrsOf types.package));
52 example = lib.literalExample ''(let
53 # Pinning unstable to allow usage with flakes and limit rebuilds.
54 pkgs_unstable = builtins.fetchGit {
55 url = "https://github.com/NixOS/nixpkgs";
56 rev = "ff96a0fa5635770390b184ae74debea75c3fd534";
57 ref = "nixos-unstable";
59 image_from_nixpkgs = pkgs_unstable: (import ("${pkgs.sourcehut.buildsrht}/lib/images/nixos/image.nix") {
60 pkgs = (import pkgs_unstable {});
64 nixos.unstable.x86_64 = image_from_nixpkgs pkgs_unstable;
68 Images for builds.sr.ht. Each package should be distro.release.arch and point to a /nix/store/package/root.img.qcow2.
74 config = with scfg; let
75 image_dirs = lib.lists.flatten (
76 lib.attrsets.mapAttrsToList
78 lib.attrsets.mapAttrsToList
80 lib.attrsets.mapAttrsToList
82 pkgs.runCommandNoCC "buildsrht-images" { } ''
83 mkdir -p $out/${distro}/${rev}/${arch}
84 ln -s ${image}/*.qcow2 $out/${distro}/${rev}/${arch}/root.img.qcow2
89 image_dir_pre = pkgs.symlinkJoin {
90 name = "builds.sr.ht-worker-images-pre";
91 paths = image_dirs ++ [
92 "${pkgs.sourcehut.buildsrht}/lib/images"
95 image_dir = pkgs.runCommandNoCC "builds.sr.ht-worker-images" { } ''
97 cp -Lr ${image_dir_pre}/* $out/images
100 lib.mkIf (cfg.enable && scfg.enable) {
106 extraGroups = lib.optionals cfg.builds.enableWorker [ "docker" ];
107 description = "builds.sr.ht user";
116 virtualisation.docker.enable = true;
118 services.postgresql = {
120 local ${database} ${user} trust
122 ensureDatabases = [ database ];
126 ensurePermissions = { "DATABASE \"${database}\"" = "ALL PRIVILEGES"; };
133 buildsrht = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey
135 after = [ "postgresql.service" "network.target" ];
136 requires = [ "postgresql.service" ];
137 wantedBy = [ "multi-user.target" ];
139 description = "builds.sr.ht website service";
141 serviceConfig.ExecStart = "${cfg.python}/bin/gunicorn ${drv.pname}.app:app -b ${cfg.address}:${toString port}";
143 # Hack to bypass this hack: https://git.sr.ht/~sircmpwn/core.sr.ht/tree/master/item/srht-update-profiles#L6
144 } // { preStart = " "; };
147 enable = scfg.enableWorker;
148 after = [ "postgresql.service" "network.target" ];
149 requires = [ "postgresql.service" ];
150 wantedBy = [ "multi-user.target" ];
151 partOf = [ "buildsrht.service" ];
152 description = "builds.sr.ht worker service";
153 path = [ pkgs.openssh pkgs.docker ];
154 preStart = let qemuPackage = pkgs.qemu_kvm;
156 if [[ "$(docker images -q qemu:latest 2> /dev/null)" == "" || "$(cat ${statePath}/docker-image-qemu 2> /dev/null || true)" != "${qemuPackage.version}" ]]; then
157 # Create and import qemu:latest image for docker
159 pkgs.dockerTools.streamLayeredImage {
162 contents = [ qemuPackage ];
165 # Mark down current package version
166 printf "%s" "${qemuPackage.version}" > ${statePath}/docker-image-qemu
174 LogsDirectory = [ "sourcehut/builds" ];
176 serviceConfig.ExecStart = "${pkgs.sourcehut.buildsrht}/bin/builds.sr.ht-worker";
181 services.sourcehut.settings = {
182 # Register the builds.sr.ht dispatcher
183 "git.sr.ht::dispatch".${builtins.unsafeDiscardStringContext "${pkgs.sourcehut.buildsrht}/bin/buildsrht-keys"} = mkDefault "${user}:${user}";
184 # Location for build logs, images, and control command
185 } // lib.attrsets.optionalAttrs scfg.enableWorker {
186 # Default worker stores logs that are accessible via this address:port
187 "builds.sr.ht::worker".name = mkDefault "127.0.0.1:5020";
188 "builds.sr.ht::worker".buildlogs = mkDefault "/var/log/sourcehut/builds";
189 "builds.sr.ht::worker".images = mkDefault "${image_dir}/images";
190 "builds.sr.ht::worker".controlcmd = mkDefault "${image_dir}/images/control";
191 "builds.sr.ht::worker".timeout = mkDefault "3m";
194 services.nginx.virtualHosts."logs.${cfg.originBase}" =
195 if scfg.enableWorker then {
196 listen = with builtins; let address = split ":" cfg.settings."builds.sr.ht::worker".name;
197 in [{ addr = elemAt address 0; port = lib.toInt (elemAt address 2); }];
198 locations."/logs".alias = cfg.settings."builds.sr.ht::worker".buildlogs + "/";
201 services.nginx.virtualHosts."builds.${cfg.originBase}" = {
203 locations."/".proxyPass = "http://${cfg.address}:${toString port}";
204 locations."/query".proxyPass = cfgIni."meta.sr.ht".api-origin;
205 locations."/static".root = "${pkgs.sourcehut.buildsrht}/${pkgs.sourcehut.python.sitePackages}/buildsrht";