Skip to content

Commit f56e432

Browse files
Mic92mergify[bot]
authored andcommitted
expose cachix options explictly
1 parent 00f4ee3 commit f56e432

File tree

3 files changed

+71
-24
lines changed

3 files changed

+71
-24
lines changed

buildbot_nix/__init__.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,25 @@ def nix_eval_config(
417417
)
418418

419419

420+
@dataclass
421+
class CachixConfig:
422+
name: str
423+
signing_key_secret_name: str | None = None
424+
auth_token_secret_name: str | None = None
425+
426+
def cachix_env(self) -> dict[str, str]:
427+
env = {}
428+
if self.signing_key_secret_name is not None:
429+
env["CACHIX_SIGNING_KEY"] = util.Secret(self.signing_key_secret_name)
430+
if self.auth_token_secret_name is not None:
431+
env["CACHIX_AUTH_TOKEN"] = util.Secret(self.auth_token_secret_name)
432+
return env
433+
434+
420435
def nix_build_config(
421436
project: GithubProject,
422437
worker_names: list[str],
423-
has_cachix_auth_token: bool = False,
424-
has_cachix_signing_key: bool = False,
438+
cachix: CachixConfig | None = None,
425439
outputs_path: Path | None = None,
426440
) -> util.BuilderConfig:
427441
"""
@@ -454,19 +468,15 @@ def nix_build_config(
454468
haltOnFailure=True,
455469
)
456470
)
457-
if has_cachix_auth_token or has_cachix_signing_key:
458-
if has_cachix_signing_key:
459-
env = dict(CACHIX_SIGNING_KEY=util.Secret("cachix-signing-key"))
460-
else:
461-
env = dict(CACHIX_AUTH_TOKEN=util.Secret("cachix-auth-token"))
471+
if cachix:
462472
factory.addStep(
463473
steps.ShellCommand(
464474
name="Upload cachix",
465-
env=env,
475+
env=cachix.cachix_env(),
466476
command=[
467477
"cachix",
468478
"push",
469-
util.Secret("cachix-name"),
479+
cachix.name,
470480
util.Interpolate("result-%(prop:attr)s"),
471481
],
472482
)
@@ -572,13 +582,13 @@ def token(self) -> str:
572582
def config_for_project(
573583
config: dict[str, Any],
574584
project: GithubProject,
575-
credentials: str,
576585
worker_names: list[str],
577586
github: GithubConfig,
578587
nix_supported_systems: list[str],
579588
nix_eval_worker_count: int,
580589
nix_eval_max_memory_size: int,
581590
eval_lock: util.WorkerLock,
591+
cachix: CachixConfig | None = None,
582592
outputs_path: Path | None = None,
583593
) -> Project:
584594
config["projects"].append(Project(project.name))
@@ -635,12 +645,6 @@ def config_for_project(
635645
),
636646
]
637647
)
638-
has_cachix_auth_token = os.path.isfile(
639-
os.path.join(credentials, "cachix-auth-token")
640-
)
641-
has_cachix_signing_key = os.path.isfile(
642-
os.path.join(credentials, "cachix-signing-key")
643-
)
644648
config["builders"].extend(
645649
[
646650
# Since all workers run on the same machine, we only assign one of them to do the evaluation.
@@ -657,8 +661,7 @@ def config_for_project(
657661
nix_build_config(
658662
project,
659663
worker_names,
660-
has_cachix_auth_token,
661-
has_cachix_signing_key,
664+
cachix=cachix,
662665
outputs_path=outputs_path,
663666
),
664667
nix_skipped_build_config(project, [SKIPPED_BUILDER_NAME]),
@@ -756,6 +759,7 @@ def __init__(
756759
nix_eval_worker_count: int | None,
757760
nix_eval_max_memory_size: int,
758761
nix_workers_secret_name: str = "buildbot-nix-workers",
762+
cachix: CachixConfig | None = None,
759763
outputs_path: str | None = None,
760764
) -> None:
761765
super().__init__()
@@ -765,7 +769,7 @@ def __init__(
765769
self.nix_supported_systems = nix_supported_systems
766770
self.github = github
767771
self.url = url
768-
self.systemd_credentials_dir = os.environ["CREDENTIALS_DIRECTORY"]
772+
self.cachix = cachix
769773
if outputs_path is None:
770774
self.outputs_path = None
771775
else:
@@ -803,13 +807,13 @@ def configure(self, config: dict[str, Any]) -> None:
803807
config_for_project(
804808
config,
805809
project,
806-
self.systemd_credentials_dir,
807810
worker_names,
808811
self.github,
809812
self.nix_supported_systems,
810813
self.nix_eval_worker_count or multiprocessing.cpu_count(),
811814
self.nix_eval_max_memory_size,
812815
eval_lock,
816+
self.cachix,
813817
self.outputs_path,
814818
)
815819

examples/default.nix

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,20 @@ in
3636
user = "mic92-buildbot";
3737
admins = [ "Mic92" ];
3838
};
39-
# optional
39+
# optional expose latest store path as text file
4040
# outputsPath = "/var/www/buildbot/nix-outputs";
4141

4242
# optional nix-eval-jobs settings
4343
# evalWorkerCount = 8; # limit number of concurrent evaluations
4444
# evalMaxMemorySize = "2048"; # limit memory usage per evaluation
45+
46+
# optional cachix
47+
#cachix = {
48+
# name = "my-cachix";
49+
# # One of the following is required:
50+
# signingKey = "/var/lib/secrets/cachix-key";
51+
# authToken = "/var/lib/secrets/cachix-token";
52+
#};
4553
};
4654
})
4755
buildbot-nix.nixosModules.buildbot-master

nix/master.nix

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,25 @@ in
1515
default = "postgresql://@/buildbot";
1616
description = "Postgresql database url";
1717
};
18+
cachix = {
19+
name = lib.mkOption {
20+
type = lib.types.nullOr lib.types.str;
21+
default = null;
22+
description = "Cachix name";
23+
};
24+
25+
signingKeyFile = lib.mkOption {
26+
type = lib.types.nullOr lib.types.path;
27+
default = null;
28+
description = "Cachix signing key";
29+
};
30+
31+
authTokenFile = lib.mkOption {
32+
type = lib.types.nullOr lib.types.str;
33+
default = null;
34+
description = "Cachix auth token";
35+
};
36+
};
1837
github = {
1938
tokenFile = lib.mkOption {
2039
type = lib.types.path;
@@ -107,6 +126,13 @@ in
107126
isSystemUser = true;
108127
};
109128

129+
assertions = [
130+
{
131+
assertion = cfg.cachix.name != null -> cfg.cachix.signingKeyFile != null || cfg.cachix.authTokenFile != null;
132+
message = "if cachix.name is provided, then cachix.signingKeyFile and cachix.authTokenFile must be set";
133+
}
134+
];
135+
110136
services.buildbot-master = {
111137
enable = true;
112138

@@ -118,7 +144,7 @@ in
118144
home = "/var/lib/buildbot";
119145
extraImports = ''
120146
from datetime import timedelta
121-
from buildbot_nix import GithubConfig, NixConfigurator
147+
from buildbot_nix import GithubConfig, NixConfigurator, CachixConfig
122148
'';
123149
configurators = [
124150
''
@@ -132,9 +158,14 @@ in
132158
buildbot_user=${builtins.toJSON cfg.github.user},
133159
topic=${builtins.toJSON cfg.github.topic},
134160
),
161+
cachix=${if cfg.cachix.name == null then "None" else "CachixConfig(
162+
name=${builtins.toJSON cfg.cachix.name},
163+
signing_key_secret_name=${if cfg.cachix.signingKeyFile != null then builtins.toJSON "cachix-signing-key" else "None"},
164+
auth_token_secret_name=${if cfg.cachix.authTokenFile != null then builtins.toJSON "cachix-auth-token" else "None"},
165+
"}),
135166
url=${builtins.toJSON config.services.buildbot-master.buildbotUrl},
136167
nix_eval_max_memory_size=${builtins.toJSON cfg.evalMaxMemorySize},
137-
nix_eval_worker_count=${if cfg.evalWorkerCount == null then "None" else builtins.toJSON cfg.evalWorkerCount},
168+
nix_eval_worker_count=${if cfg.evalWorkerCount == null then "None" else builtins.toString cfg.evalWorkerCount},
138169
nix_supported_systems=${builtins.toJSON cfg.buildSystems},
139170
outputs_path=${if cfg.outputsPath == null then "None" else builtins.toJSON cfg.outputsPath},
140171
)
@@ -166,7 +197,11 @@ in
166197
"github-webhook-secret:${cfg.github.webhookSecretFile}"
167198
"github-oauth-secret:${cfg.github.oauthSecretFile}"
168199
"buildbot-nix-workers:${cfg.workersFile}"
169-
];
200+
]
201+
++ lib.optional (cfg.cachix.signingKeyFile != null)
202+
"cachix-signing-key:${builtins.toString cfg.cachix.signingKeyFile}"
203+
++ lib.optional (cfg.cachix.authTokenFile != null)
204+
"cachix-auth-token:${builtins.toString cfg.cachix.authTokenFile}";
170205
};
171206
};
172207

0 commit comments

Comments
 (0)