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 = {
17 default = "buildsrht";
19 User for builds.sr.ht.
27 Port on which the "builds" module should listen.
33 default = "builds.sr.ht";
35 PostgreSQL database name for builds.sr.ht.
39 enableWorker = mkOption {
43 Run workers for builds.sr.ht.
48 type = types.attrsOf (types.attrsOf (types.attrsOf types.package));
50 example = lib.literalExample ''(let
51 # Pinning unstable to allow usage with flakes and limit rebuilds.
52 pkgs_unstable = builtins.fetchGit {
53 url = "https://github.com/NixOS/nixpkgs";
54 rev = "ff96a0fa5635770390b184ae74debea75c3fd534";
55 ref = "nixos-unstable";
57 image_from_nixpkgs = pkgs_unstable: (import ("${pkgs.sourcehut.buildsrht}/lib/images/nixos/image.nix") {
58 pkgs = (import pkgs_unstable {});
62 nixos.unstable.x86_64 = image_from_nixpkgs pkgs_unstable;
66 Images for builds.sr.ht. Each package should be distro.release.arch and point to a /nix/store/package/root.img.qcow2.
72 config = with scfg; let
73 image_dirs = lib.lists.flatten (
74 lib.attrsets.mapAttrsToList
76 lib.attrsets.mapAttrsToList
78 lib.attrsets.mapAttrsToList
80 pkgs.runCommandNoCC "buildsrht-images" { } ''
81 mkdir -p $out/${distro}/${rev}/${arch}
82 ln -s ${image}/*.qcow2 $out/${distro}/${rev}/${arch}/root.img.qcow2
87 image_dir_pre = pkgs.symlinkJoin {
88 name = "builds.sr.ht-worker-images-pre";
89 paths = image_dirs ++ [
90 "${pkgs.sourcehut.buildsrht}/lib/images"
93 image_dir = pkgs.runCommandNoCC "builds.sr.ht-worker-images" { } ''
95 cp -Lr ${image_dir_pre}/* $out/images
98 lib.mkIf (cfg.enable && elem "builds" cfg.services) {
104 extraGroups = lib.optionals cfg.builds.enableWorker [ "docker" ];
105 description = "builds.sr.ht user";
114 virtualisation.docker.enable = true;
116 services.postgresql = {
118 local ${database} ${user} trust
120 ensureDatabases = [ database ];
124 ensurePermissions = { "DATABASE \"${database}\"" = "ALL PRIVILEGES"; };
131 buildsrht = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey
133 after = [ "postgresql.service" "network.target" ];
134 requires = [ "postgresql.service" ];
135 wantedBy = [ "multi-user.target" ];
137 description = "builds.sr.ht website service";
139 serviceConfig.ExecStart = "${cfg.python}/bin/gunicorn ${drv.pname}.app:app -b ${cfg.address}:${toString port}";
141 # Hack to bypass this hack: https://git.sr.ht/~sircmpwn/core.sr.ht/tree/master/item/srht-update-profiles#L6
142 } // { preStart = " "; };
145 enable = scfg.enableWorker;
146 after = [ "postgresql.service" "network.target" ];
147 requires = [ "postgresql.service" ];
148 wantedBy = [ "multi-user.target" ];
149 partOf = [ "buildsrht.service" ];
150 description = "builds.sr.ht worker service";
151 path = [ pkgs.openssh pkgs.docker ];
152 preStart = let qemuPackage = pkgs.qemu_kvm;
154 if [[ "$(docker images -q qemu:latest 2> /dev/null)" == "" || "$(cat ${statePath}/docker-image-qemu 2> /dev/null || true)" != "${qemuPackage.version}" ]]; then
155 # Create and import qemu:latest image for docker
157 pkgs.dockerTools.streamLayeredImage {
160 contents = [ qemuPackage ];
163 # Mark down current package version
164 printf "%s" "${qemuPackage.version}" > ${statePath}/docker-image-qemu
172 LogsDirectory = [ "sourcehut/builds" ];
174 serviceConfig.ExecStart = "${pkgs.sourcehut.buildsrht}/bin/builds.sr.ht-worker";
179 services.sourcehut.settings = {
180 # Register the builds.sr.ht dispatcher
181 "git.sr.ht::dispatch".${builtins.unsafeDiscardStringContext "${pkgs.sourcehut.buildsrht}/bin/buildsrht-keys"} = mkDefault "${user}:${user}";
182 # Location for build logs, images, and control command
183 } // lib.attrsets.optionalAttrs scfg.enableWorker {
184 # Default worker stores logs that are accessible via this address:port
185 "builds.sr.ht::worker".name = mkDefault "127.0.0.1:5020";
186 "builds.sr.ht::worker".buildlogs = mkDefault "/var/log/sourcehut/builds";
187 "builds.sr.ht::worker".images = mkDefault "${image_dir}/images";
188 "builds.sr.ht::worker".controlcmd = mkDefault "${image_dir}/images/control";
189 "builds.sr.ht::worker".timeout = mkDefault "3m";
192 services.nginx.virtualHosts."logs.${cfg.originBase}" =
193 if scfg.enableWorker then {
194 listen = with builtins; let address = split ":" cfg.settings."builds.sr.ht::worker".name;
195 in [{ addr = elemAt address 0; port = lib.toInt (elemAt address 2); }];
196 locations."/logs".alias = cfg.settings."builds.sr.ht::worker".buildlogs + "/";
199 services.nginx.virtualHosts."builds.${cfg.originBase}" = {
201 locations."/".proxyPass = "http://${cfg.address}:${toString port}";
202 locations."/query".proxyPass = "http://${cfg.address}:${toString (port + 100)}";
203 locations."/static".root = "${pkgs.sourcehut.buildsrht}/${pkgs.sourcehut.python.sitePackages}/buildsrht";