1 { config, lib, pkgs, ... }:
5 cfg = config.services.sourcehut;
7 rcfg = config.services.redis;
8 iniKey = "builds.sr.ht";
10 drv = pkgs.sourcehut.buildsrht;
13 options.services.sourcehut.builds = {
16 default = "buildsrht";
18 User for builds.sr.ht.
26 Port on which the "builds" module should listen.
32 default = "builds.sr.ht";
34 PostgreSQL database name for builds.sr.ht.
38 statePath = mkOption {
40 default = "${cfg.statePath}/buildsrht";
42 State path for builds.sr.ht.
46 enableWorker = mkOption {
50 Run workers for builds.sr.ht.
55 type = types.attrsOf (types.attrsOf (types.attrsOf types.package));
57 example = lib.literalExample ''(let
58 # Pinning unstable to allow usage with flakes and limit rebuilds.
59 pkgs_unstable = builtins.fetchGit {
60 url = "https://github.com/NixOS/nixpkgs";
61 rev = "ff96a0fa5635770390b184ae74debea75c3fd534";
62 ref = "nixos-unstable";
64 image_from_nixpkgs = pkgs_unstable: (import ("${pkgs.sourcehut.buildsrht}/lib/images/nixos/image.nix") {
65 pkgs = (import pkgs_unstable {});
69 nixos.unstable.x86_64 = image_from_nixpkgs pkgs_unstable;
73 Images for builds.sr.ht. Each package should be distro.release.arch and point to a /nix/store/package/root.img.qcow2.
79 config = with scfg; let
80 image_dirs = lib.lists.flatten (
81 lib.attrsets.mapAttrsToList
83 lib.attrsets.mapAttrsToList
85 lib.attrsets.mapAttrsToList
87 pkgs.runCommandNoCC "buildsrht-images" { } ''
88 mkdir -p $out/${distro}/${rev}/${arch}
89 ln -s ${image}/*.qcow2 $out/${distro}/${rev}/${arch}/root.img.qcow2
94 image_dir_pre = pkgs.symlinkJoin {
95 name = "builds.sr.ht-worker-images-pre";
96 paths = image_dirs ++ [
97 "${pkgs.sourcehut.buildsrht}/lib/images"
100 image_dir = pkgs.runCommandNoCC "builds.sr.ht-worker-images" { } ''
102 cp -Lr ${image_dir_pre}/* $out/images
105 lib.mkIf (cfg.enable && elem "builds" cfg.services) {
111 extraGroups = lib.optionals cfg.builds.enableWorker [ "docker" ];
112 description = "builds.sr.ht user";
121 virtualisation.docker.enable = true;
123 services.postgresql = {
125 local ${database} ${user} trust
127 ensureDatabases = [ database ];
131 ensurePermissions = { "DATABASE \"${database}\"" = "ALL PRIVILEGES"; };
138 "d ${statePath} 0755 ${user} ${user} -"
139 ] ++ (lib.optionals cfg.builds.enableWorker
140 [ "d ${statePath}/logs 0775 ${user} ${user} - -" ]
144 buildsrht = import ./service.nix { inherit config pkgs lib; } scfg drv iniKey
146 after = [ "postgresql.service" "network.target" ];
147 requires = [ "postgresql.service" ];
148 wantedBy = [ "multi-user.target" ];
150 description = "builds.sr.ht website service";
152 serviceConfig.ExecStart = "${cfg.python}/bin/gunicorn ${drv.pname}.app:app -b ${cfg.address}:${toString port}";
154 # Hack to bypass this hack: https://git.sr.ht/~sircmpwn/core.sr.ht/tree/master/item/srht-update-profiles#L6
155 } // { preStart = " "; };
158 enable = scfg.enableWorker;
159 after = [ "postgresql.service" "network.target" ];
160 requires = [ "postgresql.service" ];
161 wantedBy = [ "multi-user.target" ];
162 partOf = [ "buildsrht.service" ];
163 description = "builds.sr.ht worker service";
164 path = [ pkgs.openssh pkgs.docker ];
165 preStart = let qemuPackage = pkgs.qemu_kvm;
167 if [[ "$(docker images -q qemu:latest 2> /dev/null)" == "" || "$(cat ${statePath}/docker-image-qemu 2> /dev/null || true)" != "${qemuPackage.version}" ]]; then
168 # Create and import qemu:latest image for docker
170 pkgs.dockerTools.streamLayeredImage {
173 contents = [ qemuPackage ];
176 # Mark down current package version
177 printf "%s" "${qemuPackage.version}" > ${statePath}/docker-image-qemu
186 serviceConfig.ExecStart = "${pkgs.sourcehut.buildsrht}/bin/builds.sr.ht-worker";
191 services.sourcehut.settings = {
192 # URL builds.sr.ht is being served at (protocol://domain)
193 "builds.sr.ht".origin = mkDefault "http://builds.${cfg.originBase}";
194 # Address and port to bind the debug server to
195 "builds.sr.ht".debug-host = mkDefault "0.0.0.0";
196 "builds.sr.ht".debug-port = mkDefault port;
197 # Configures the SQLAlchemy connection string for the database.
198 "builds.sr.ht".connection-string = mkDefault "postgresql:///${database}?user=${user}&host=/var/run/postgresql";
199 # builds.sr.ht's OAuth client ID and secret for meta.sr.ht
200 # Register your client at meta.example.org/oauth
201 "builds.sr.ht".oauth-client-id = mkDefault null;
202 "builds.sr.ht".oauth-client-secret = mkDefault null;
203 # The redis connection used for the celery worker
204 "builds.sr.ht".redis = mkDefault "redis://${rcfg.bind}:${toString rcfg.port}/3";
205 # The shell used for ssh
206 "builds.sr.ht".shell = mkDefault "runner-shell";
207 # Register the builds.sr.ht dispatcher
208 "git.sr.ht::dispatch".${builtins.unsafeDiscardStringContext "${pkgs.sourcehut.buildsrht}/bin/buildsrht-keys"} = mkDefault "${user}:${user}";
210 # Location for build logs, images, and control command
211 } // lib.attrsets.optionalAttrs scfg.enableWorker {
212 # Default worker stores logs that are accessible via this address:port
213 "builds.sr.ht::worker".name = mkDefault "127.0.0.1:5020";
214 "builds.sr.ht::worker".buildlogs = mkDefault "${scfg.statePath}/logs";
215 "builds.sr.ht::worker".images = mkDefault "${image_dir}/images";
216 "builds.sr.ht::worker".controlcmd = mkDefault "${image_dir}/images/control";
217 "builds.sr.ht::worker".timeout = mkDefault "3m";
220 services.nginx.virtualHosts."logs.${cfg.originBase}" =
221 if scfg.enableWorker then {
222 listen = with builtins; let address = split ":" cfg.settings."builds.sr.ht::worker".name;
223 in [{ addr = elemAt address 0; port = lib.toInt (elemAt address 2); }];
224 locations."/logs".root = "${scfg.statePath}";
227 services.nginx.virtualHosts."builds.${cfg.originBase}" = {
229 locations."/".proxyPass = "http://${cfg.address}:${toString port}";
230 locations."/query".proxyPass = "http://${cfg.address}:${toString (port + 100)}";
231 locations."/static".root = "${pkgs.sourcehut.buildsrht}/${pkgs.sourcehut.python.sitePackages}/buildsrht";