1 { lib, pkgs, config, myconfig, ... }:
3 varDir = "/var/lib/buildbot";
4 buildbot_common = pkgs.python3Packages.buildPythonPackage rec {
5 name = "buildbot_common";
9 mkdir -p $out/${pkgs.python3.pythonForBuild.sitePackages}
10 cp -a $src $out/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_common
13 buildbot = pkgs.python3Packages.buildbot-full;
17 myServices.buildbot.enable = lib.mkOption {
18 type = lib.types.bool;
21 Whether to enable buildbot.
26 config = lib.mkIf config.myServices.buildbot.enable {
27 ids.uids.buildbot = myconfig.env.buildbot.user.uid;
28 ids.gids.buildbot = myconfig.env.buildbot.user.gid;
30 users.groups.buildbot.gid = config.ids.gids.buildbot;
31 users.users.buildbot = {
33 uid = config.ids.uids.buildbot;
35 description = "Buildbot user";
37 extraGroups = [ "keys" ];
40 services.websites.env.tools.watchPaths = lib.attrsets.mapAttrsToList
41 (k: project: "/var/secrets/buildbot/${project.name}/webhook-httpd-include")
42 myconfig.env.buildbot.projects;
44 services.websites.env.tools.vhostConfs.git.extraConfig = lib.attrsets.mapAttrsToList (k: project: ''
45 RedirectMatch permanent "^/buildbot/${project.name}$" "/buildbot/${project.name}/"
47 RewriteRule ^/buildbot/${project.name}/ws(.*)$ unix:///run/buildbot/${project.name}.sock|ws://git.immae.eu/ws$1 [P,NE,QSA,L]
48 ProxyPass /buildbot/${project.name}/ unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/
49 ProxyPassReverse /buildbot/${project.name}/ unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/
50 <Location /buildbot/${project.name}/>
52 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
54 SetEnvIf X-Url-Scheme https HTTPS=1
57 <Location /buildbot/${project.name}/change_hook/base>
60 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
61 Include /var/secrets/buildbot/${project.name}/webhook-httpd-include
64 '') myconfig.env.buildbot.projects;
66 system.activationScripts = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
67 deps = [ "users" "wrappers" ];
68 text = project.activationScript;
69 }) myconfig.env.buildbot.projects;
73 lib.attrsets.mapAttrsToList (k: project:
74 lib.attrsets.mapAttrsToList (k: v:
80 dest = "buildbot/${project.name}/${k}";
88 text = lib.optionalString (lib.attrsets.hasAttr "webhookTokens" project) ''
89 Require expr "req('Access-Key') in { ${builtins.concatStringsSep ", " (map (x: "'${x}'") project.webhookTokens)} }"
91 dest = "buildbot/${project.name}/webhook-httpd-include";
94 ) myconfig.env.buildbot.projects
101 text = myconfig.env.buildbot.ldap.password;
102 dest = "buildbot/ldap";
105 permissions = "0600";
108 text = builtins.readFile "${myconfig.privateFiles}/buildbot_ssh_key";
109 dest = "buildbot/ssh_key";
113 services.filesWatcher = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
116 "/var/secrets/buildbot/ldap"
117 "/var/secrets/buildbot/ssh_key"
118 ] ++ lib.attrsets.mapAttrsToList (k: v: "/var/secrets/buildbot/${project.name}/${k}") project.secrets;
119 }) myconfig.env.buildbot.projects;
121 systemd.services = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
122 description = "Buildbot Continuous Integration Server ${project.name}.";
123 after = [ "network-online.target" ];
124 wantedBy = [ "multi-user.target" ];
125 path = project.packages pkgs ++ (project.pythonPackages buildbot.pythonModule pkgs);
127 master-cfg = "${buildbot_common}/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_common/master.cfg";
128 tac_file = pkgs.writeText "buildbot.tac" ''
131 from twisted.application import service
132 from buildbot.master import BuildMaster
134 basedir = '${varDir}/${project.name}'
135 rotateLength = 10000000
137 configfile = '${master-cfg}'
139 # Default umask for server
142 # if this is a relocatable tac file, get the directory containing the TAC
145 basedir = os.path.abspath(os.path.dirname(__file__))
147 # note: this line is matched against to check that this is a buildmaster
148 # directory; do not edit it.
149 application = service.Application('buildmaster')
150 from twisted.python.logfile import LogFile
151 from twisted.python.log import ILogObserver, FileLogObserver
152 logfile = LogFile.fromFullPath(os.path.join(basedir, "twistd.log"), rotateLength=rotateLength,
153 maxRotatedFiles=maxRotatedFiles)
154 application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
156 m = BuildMaster(basedir, configfile, umask)
157 m.setServiceParent(application)
158 m.log_rotation.rotateLength = rotateLength
159 m.log_rotation.maxRotatedFiles = maxRotatedFiles
162 if [ ! -f ${varDir}/${project.name}/buildbot.tac ]; then
163 ${buildbot}/bin/buildbot create-master -c "${master-cfg}" "${varDir}/${project.name}"
164 rm -f ${varDir}/${project.name}/master.cfg.sample
165 rm -f ${varDir}/${project.name}/buildbot.tac
167 ln -sf ${tac_file} ${varDir}/${project.name}/buildbot.tac
168 # different buildbots may be trying that simultaneously, add the || true to avoid complaining in case of race
169 install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/ssh_key ${varDir}/buildbot_key || true
170 buildbot_secrets=${varDir}/${project.name}/secrets
171 install -m 0700 -o buildbot -g buildbot -d $buildbot_secrets
172 install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/ldap $buildbot_secrets/ldap
173 ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList
174 (k: v: "install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/${project.name}/${k} $buildbot_secrets/${k}") project.secrets
178 project_env = lib.attrsets.mapAttrs' (k: v: lib.attrsets.nameValuePair "BUILDBOT_${k}" v) project.environment;
179 buildbot_config = pkgs.python3Packages.buildPythonPackage (rec {
180 name = "buildbot_config-${project.name}";
181 src = ./projects + "/${project.name}";
184 mkdir -p $out/${pkgs.python3.pythonForBuild.sitePackages}
185 cp -a $src $out/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_config
188 HOME = "${varDir}/${project.name}";
189 PYTHONPATH = "${buildbot.pythonModule.withPackages (self: project.pythonPackages self pkgs ++ [
190 pkgs.python3Packages.wokkel
191 pkgs.python3Packages.treq pkgs.python3Packages.ldap3 buildbot
192 pkgs.python3Packages.buildbot-worker
193 buildbot_common buildbot_config
194 ])}/${buildbot.pythonModule.sitePackages}${if project.pythonPathHome then ":${varDir}/${project.name}/.local/${pkgs.python3.pythonForBuild.sitePackages}" else ""}";
195 in project_env // { inherit PYTHONPATH HOME; };
201 RuntimeDirectory = "buildbot";
202 RuntimeDirectoryPreserve = "yes";
203 StateDirectory = "buildbot";
204 SupplementaryGroups = "keys";
205 WorkingDirectory = "${varDir}/${project.name}";
206 ExecStart = "${buildbot}/bin/buildbot start";
208 }) myconfig.env.buildbot.projects;