Skip to content

Commit 2838e2a

Browse files
MagicRBmergify[bot]
authored andcommitted
Properly fully fix Cachix
Signed-off-by: magic_rb <[email protected]>
1 parent 698e734 commit 2838e2a

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

buildbot_nix/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from twisted.internet import defer
3030
from twisted.logger import Logger
3131

32+
from . import models
3233
from .common import (
3334
slugify_project_name,
3435
)
@@ -944,15 +945,15 @@ def configure(self, config: dict[str, Any]) -> None:
944945
eval_lock = util.MasterLock("nix-eval")
945946

946947
if self.config.cachix is not None:
947-
self.post_build_steps.append(
948-
steps.ShellCommand(
948+
self.config.post_build_steps.append(
949+
models.PostBuildStep(
949950
name="Upload cachix",
950-
env=self.cachix.cachix_env(),
951+
environment=self.config.cachix.environment,
951952
command=[
952953
"cachix",
953954
"push",
954-
self.cachix.name,
955-
util.Interpolate("result-%(prop:attr)s"),
955+
self.config.cachix.name,
956+
models.Interpolate("result-%(prop:attr)s"),
956957
],
957958
)
958959
)
@@ -994,7 +995,7 @@ def configure(self, config: dict[str, Any]) -> None:
994995
],
995996
)
996997
config["services"].append(backend.create_reporter())
997-
config.setdefault("secretProviders", [])
998+
config.setdefault("secretsProviders", [])
998999
config["secretsProviders"].extend(backend.create_secret_providers())
9991000

10001001
systemd_secrets = SecretInAFile(

buildbot_nix/models.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections.abc import Mapping
12
from enum import Enum
23
from pathlib import Path
34

@@ -22,6 +23,19 @@ class AuthBackendConfig(str, Enum):
2223
none = "none"
2324

2425

26+
# note that serialization isn't correct, as there is no way to *rename* the field `nix_type` to `_type`,
27+
# one must always specify `by_alias = True`, such as `model_dump(by_alias = True)`, relevant issue:
28+
# https://github.com/pydantic/pydantic/issues/8379
29+
class Interpolate(BaseModel):
30+
model_config = ConfigDict(populate_by_name=True)
31+
32+
nix_type: str = Field(alias="_type")
33+
value: str
34+
35+
def __init__(self, value: str) -> None:
36+
super().__init__(nix_type="interpolate", value=value)
37+
38+
2539
class CachixConfig(BaseModel):
2640
name: str
2741

@@ -42,10 +56,16 @@ def auth_token(self) -> str:
4256

4357
# TODO why did the original implementation return an empty env if both files were missing?
4458
@property
45-
def environment(self) -> dict[str, str]:
59+
def environment(self) -> Mapping[str, str | Interpolate]:
4660
environment = {}
47-
environment["CACHIX_SIGNING_KEY"] = util.Secret(self.signing_key_file)
48-
environment["CACHIX_AUTH_TOKEN"] = util.Secret(self.auth_token_file)
61+
if self.signing_key_file is not None:
62+
environment["CACHIX_SIGNING_KEY"] = Interpolate(
63+
f"%(secret:{self.signing_key_file})s"
64+
)
65+
if self.auth_token_file is not None:
66+
environment["CACHIX_AUTH_TOKEN"] = Interpolate(
67+
f"%(secret:{self.auth_token_file})s"
68+
)
4969
return environment
5070

5171
class Config:
@@ -133,19 +153,9 @@ def oauth_secret(self) -> str:
133153
return read_secret_file(self.oauth_secret_file)
134154

135155

136-
# note that serialization isn't correct, as there is no way to *rename* the field `nix_type` to `_type`,
137-
# one must always specify `by_alias = True`, such as `model_dump(by_alias = True)`, relevant issue:
138-
# https://github.com/pydantic/pydantic/issues/8379
139-
class Interpolate(BaseModel):
140-
model_config = ConfigDict(populate_by_name=True)
141-
142-
nix_type: str = Field(alias="_type")
143-
value: str
144-
145-
146156
class PostBuildStep(BaseModel):
147157
name: str
148-
environment: dict[str, str | Interpolate]
158+
environment: Mapping[str, str | Interpolate]
149159
command: list[str | Interpolate]
150160

151161
def to_buildstep(self) -> steps.BuildStep:
@@ -156,7 +166,7 @@ def maybe_interpolate(value: str | Interpolate) -> str | util.Interpolate:
156166

157167
return steps.ShellCommand(
158168
name=self.name,
159-
env={k: maybe_interpolate(k) for k in self.environment},
169+
env={k: maybe_interpolate(self.environment[k]) for k in self.environment},
160170
command=[maybe_interpolate(x) for x in self.command],
161171
)
162172

nix/master.nix

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,8 @@ in
613613
else
614614
{
615615
name = cfg.cachix.name;
616-
signing_key_file = if cfg.cachix.auth ? "signingKey" then cfg.cachix.auth.signingKey.file else null;
617-
auth_token_file = if cfg.cachix.auth ? "authToken" then cfg.cachix.authTokenFile else null;
616+
signing_key_file = if cfg.cachix.auth ? "signingKey" then "cachix-signing-key" else null;
617+
auth_token_file = if cfg.cachix.auth ? "authToken" then "cachix-auth-token" else null;
618618
};
619619
gitea =
620620
if !cfg.gitea.enable then
@@ -735,11 +735,11 @@ in
735735
++ lib.optional (cfg.authBackend == "gitea") "gitea-oauth-secret:${cfg.gitea.oauthSecretFile}"
736736
++ lib.optional (cfg.authBackend == "github") "github-oauth-secret:${cfg.github.oauthSecretFile}"
737737
++ lib.optional (
738-
cfg.cachix.enable && cfg.cachix ? "signingKey"
739-
) "cachix-signing-key:${builtins.toString cfg.cachix.signingKeyFile}"
738+
cfg.cachix.enable && cfg.cachix.auth ? "signingKey"
739+
) "cachix-signing-key:${builtins.toString cfg.cachix.auth.signingKey.file}"
740740
++ lib.optional (
741-
cfg.cachix.enable && cfg.cachix ? "authToken"
742-
) "cachix-auth-token:${builtins.toString cfg.cachix.authTokenFile}"
741+
cfg.cachix.enable && cfg.cachix.auth ? "authToken"
742+
) "cachix-auth-token:${builtins.toString cfg.cachix.auth.authToken.file}"
743743
++ lib.optionals cfg.gitea.enable [
744744
"gitea-token:${cfg.gitea.tokenFile}"
745745
"gitea-webhook-secret:${cfg.gitea.webhookSecretFile}"

0 commit comments

Comments
 (0)