feat: add builder mode for centralized Nix builds #2

Merged
torjus merged 4 commits from feat/builder into master 2026-02-10 21:16:05 +00:00
2 changed files with 102 additions and 10 deletions
Showing only changes of commit 00899489ac - Show all commits

View File

@@ -322,14 +322,47 @@ Default `deploySubjects`:
| `package` | package | from flake | Package to use | | `package` | package | from flake | Package to use |
| `natsUrl` | string | required | NATS server URL | | `natsUrl` | string | required | NATS server URL |
| `nkeyFile` | path | required | Path to NKey seed file | | `nkeyFile` | path | required | Path to NKey seed file |
| `configFile` | path | required | Path to builder configuration file | | `configFile` | path | `null` | Path to builder config file (alternative to `settings`) |
| `settings.repos` | attrs | `{}` | Repository configuration (see below) |
| `timeout` | int | `1800` | Build timeout per host in seconds | | `timeout` | int | `1800` | Build timeout per host in seconds |
| `environment` | attrs | `{}` | Additional environment variables | | `environment` | attrs | `{}` | Additional environment variables |
| `metrics.enable` | bool | `false` | Enable Prometheus metrics endpoint | | `metrics.enable` | bool | `false` | Enable Prometheus metrics endpoint |
| `metrics.address` | string | `":9973"` | Metrics HTTP server address | | `metrics.address` | string | `":9973"` | Metrics HTTP server address |
| `metrics.openFirewall` | bool | `false` | Open firewall for metrics port | | `metrics.openFirewall` | bool | `false` | Open firewall for metrics port |
Example builder configuration: Each entry in `settings.repos` is an attribute set with:
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `url` | string | required | Git flake URL (must start with `git+https://`, `git+ssh://`, or `git+file://`) |
| `defaultBranch` | string | `"master"` | Default branch to build when not specified |
Example builder configuration using `settings`:
```nix
services.homelab-deploy.builder = {
enable = true;
natsUrl = "nats://nats.example.com:4222";
nkeyFile = "/run/secrets/homelab-deploy-builder-nkey";
settings.repos = {
nixos-servers = {
url = "git+https://git.example.com/org/nixos-servers.git";
defaultBranch = "master";
};
homelab = {
url = "git+ssh://git@github.com/user/homelab.git";
defaultBranch = "main";
};
};
metrics = {
enable = true;
address = ":9973";
openFirewall = true;
};
};
```
Alternatively, you can use `configFile` to point to an external YAML file:
```nix ```nix
services.homelab-deploy.builder = { services.homelab-deploy.builder = {
@@ -337,11 +370,6 @@ services.homelab-deploy.builder = {
natsUrl = "nats://nats.example.com:4222"; natsUrl = "nats://nats.example.com:4222";
nkeyFile = "/run/secrets/homelab-deploy-builder-nkey"; nkeyFile = "/run/secrets/homelab-deploy-builder-nkey";
configFile = "/etc/homelab-deploy/builder.yaml"; configFile = "/etc/homelab-deploy/builder.yaml";
metrics = {
enable = true;
address = ":9973";
openFirewall = true;
};
}; };
``` ```

View File

@@ -5,6 +5,20 @@ let
listenerCfg = config.services.homelab-deploy.listener; listenerCfg = config.services.homelab-deploy.listener;
builderCfg = config.services.homelab-deploy.builder; builderCfg = config.services.homelab-deploy.builder;
# Generate YAML config from settings
generatedConfigFile = pkgs.writeText "builder.yaml" (lib.generators.toYAML {} {
repos = lib.mapAttrs (name: repo: {
url = repo.url;
default_branch = repo.defaultBranch;
}) builderCfg.settings.repos;
});
# Use provided configFile or generate from settings
builderConfigFile =
if builderCfg.configFile != null
then builderCfg.configFile
else generatedConfigFile;
# Build command line arguments for listener from configuration # Build command line arguments for listener from configuration
listenerArgs = lib.concatStringsSep " " ([ listenerArgs = lib.concatStringsSep " " ([
"--hostname ${lib.escapeShellArg listenerCfg.hostname}" "--hostname ${lib.escapeShellArg listenerCfg.hostname}"
@@ -26,7 +40,7 @@ let
builderArgs = lib.concatStringsSep " " ([ builderArgs = lib.concatStringsSep " " ([
"--nats-url ${lib.escapeShellArg builderCfg.natsUrl}" "--nats-url ${lib.escapeShellArg builderCfg.natsUrl}"
"--nkey-file ${lib.escapeShellArg builderCfg.nkeyFile}" "--nkey-file ${lib.escapeShellArg builderCfg.nkeyFile}"
"--config ${lib.escapeShellArg builderCfg.configFile}" "--config ${builderConfigFile}"
"--timeout ${toString builderCfg.timeout}" "--timeout ${toString builderCfg.timeout}"
] ]
++ lib.optionals builderCfg.metrics.enable [ ++ lib.optionals builderCfg.metrics.enable [
@@ -161,11 +175,52 @@ in
}; };
configFile = lib.mkOption { configFile = lib.mkOption {
type = lib.types.path; type = lib.types.nullOr lib.types.path;
description = "Path to builder configuration file (YAML)"; default = null;
description = ''
Path to builder configuration file (YAML).
If not specified, a config file will be generated from the `settings` option.
'';
example = "/etc/homelab-deploy/builder.yaml"; example = "/etc/homelab-deploy/builder.yaml";
}; };
settings = {
repos = lib.mkOption {
type = lib.types.attrsOf (lib.types.submodule {
options = {
url = lib.mkOption {
type = lib.types.str;
description = "Git flake URL for the repository";
example = "git+https://git.example.com/org/nixos-configs.git";
};
defaultBranch = lib.mkOption {
type = lib.types.str;
default = "master";
description = "Default branch to build when not specified in request";
example = "main";
};
};
});
default = {};
description = ''
Repository configuration for the builder.
Each key is the repository name used in build requests.
'';
example = lib.literalExpression ''
{
nixos-servers = {
url = "git+https://git.example.com/org/nixos-servers.git";
defaultBranch = "master";
};
homelab = {
url = "git+ssh://git@github.com/user/homelab.git";
defaultBranch = "main";
};
}
'';
};
};
timeout = lib.mkOption { timeout = lib.mkOption {
type = lib.types.int; type = lib.types.int;
default = 1800; default = 1800;
@@ -198,6 +253,15 @@ in
}; };
config = lib.mkMerge [ config = lib.mkMerge [
(lib.mkIf builderCfg.enable {
assertions = [
{
assertion = builderCfg.configFile != null || builderCfg.settings.repos != {};
message = "services.homelab-deploy.builder: either configFile or settings.repos must be specified";
}
];
})
(lib.mkIf listenerCfg.enable { (lib.mkIf listenerCfg.enable {
systemd.services.homelab-deploy-listener = { systemd.services.homelab-deploy-listener = {
description = "homelab-deploy listener"; description = "homelab-deploy listener";