WIP autoscaler agent

This commit is contained in:
Abel Luck 2026-02-27 11:59:16 +01:00
parent c610a3e284
commit 28059dcedf
34 changed files with 2409 additions and 35 deletions

224
flake.nix
View file

@ -1,51 +1,207 @@
{
inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.1";
description = "nix-builder-autoscaler - autoscaler daemon for Nix remote builders on EC2 Spot";
inputs = {
nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.1";
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
pyproject-nix = {
url = "github:pyproject-nix/pyproject.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
uv2nix = {
url = "github:pyproject-nix/uv2nix";
inputs.pyproject-nix.follows = "pyproject-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
pyproject-build-systems = {
url = "github:pyproject-nix/build-system-pkgs";
inputs.pyproject-nix.follows = "pyproject-nix";
inputs.uv2nix.follows = "uv2nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{ self, nixpkgs }:
{
self,
nixpkgs,
treefmt-nix,
pyproject-nix,
uv2nix,
pyproject-build-systems,
...
}:
let
systems = [
"x86_64-linux"
];
systems = [ "x86_64-linux" ];
forAllSystems = fn: nixpkgs.lib.genAttrs systems (system: fn nixpkgs.legacyPackages.${system});
workspace = uv2nix.lib.workspace.loadWorkspace { workspaceRoot = ./agent; };
overlay = workspace.mkPyprojectOverlay { sourcePreference = "wheel"; };
in
{
#packages = forAllSystems (pkgs: {
# default = pkgs.callPackage ./package.nix { };
#});
formatter = forAllSystems (
pkgs: (treefmt-nix.lib.evalModule pkgs ./treefmt.nix).config.build.wrapper
);
checks = forAllSystems (pkgs: {
# todo add tests
devShell = self.devShells.${pkgs.stdenv.hostPlatform.system}.default;
}
# future nixos test
# // pkgs.lib.optionalAttrs pkgs.stdenv.isLinux {
# nixos-module = pkgs.testers.runNixOSTest (import ./nixos-test.nix self);
# }
packages = forAllSystems (
pkgs:
let
pythonSet =
(pkgs.callPackage pyproject-nix.build.packages {
python = pkgs.python312;
}).overrideScope
(
pkgs.lib.composeManyExtensions [
pyproject-build-systems.overlays.default
overlay
]
);
venv = pythonSet.mkVirtualEnv "nix-builder-autoscaler-env" workspace.deps.default;
in
{
nix-builder-autoscaler = venv;
default = venv;
}
);
apps = forAllSystems (
pkgs:
let
venv = self.packages.${pkgs.stdenv.hostPlatform.system}.nix-builder-autoscaler;
in
{
nix-builder-autoscaler = {
type = "app";
program = "${venv}/bin/python";
meta.description = "Nix builder autoscaler daemon";
};
autoscalerctl = {
type = "app";
program = "${venv}/bin/autoscalerctl";
meta.description = "Autoscaler CLI";
};
default = {
type = "app";
program = "${venv}/bin/autoscalerctl";
meta.description = "Autoscaler CLI";
};
}
);
checks = forAllSystems (
pkgs:
let
pythonSet =
(pkgs.callPackage pyproject-nix.build.packages {
python = pkgs.python312;
}).overrideScope
(
pkgs.lib.composeManyExtensions [
pyproject-build-systems.overlays.default
overlay
]
);
testVenv = pythonSet.mkVirtualEnv "nix-builder-autoscaler-test-env" {
nix-builder-autoscaler = [ "dev" ];
};
src = ./agent;
in
{
devShell = self.devShells.${pkgs.stdenv.hostPlatform.system}.default;
nix-builder-autoscaler-unit-tests = pkgs.stdenv.mkDerivation {
name = "nix-builder-autoscaler-unit-tests";
inherit src;
dontConfigure = true;
dontBuild = true;
nativeBuildInputs = [ testVenv ];
checkPhase = ''
runHook preCheck
export HOME=$(mktemp -d)
pytest nix_builder_autoscaler/tests/ --ignore=nix_builder_autoscaler/tests/integration/ -v
runHook postCheck
'';
doCheck = true;
installPhase = ''
mkdir -p $out
touch $out/passed
'';
};
nix-builder-autoscaler-integration-tests = pkgs.stdenv.mkDerivation {
name = "nix-builder-autoscaler-integration-tests";
inherit src;
dontConfigure = true;
dontBuild = true;
nativeBuildInputs = [ testVenv ];
checkPhase = ''
runHook preCheck
export HOME=$(mktemp -d)
# Exit code 5 means no tests collected — tolerate until integration tests are written
pytest nix_builder_autoscaler/tests/integration/ -v || test $? -eq 5
runHook postCheck
'';
doCheck = true;
installPhase = ''
mkdir -p $out
touch $out/passed
'';
};
nix-builder-autoscaler-ruff = pkgs.stdenv.mkDerivation {
name = "nix-builder-autoscaler-ruff";
inherit src;
dontConfigure = true;
dontBuild = true;
nativeBuildInputs = [ testVenv ];
checkPhase = ''
runHook preCheck
ruff check nix_builder_autoscaler/
ruff format --check nix_builder_autoscaler/
runHook postCheck
'';
doCheck = true;
installPhase = ''
mkdir -p $out
touch $out/passed
'';
};
nix-builder-autoscaler-pyright = pkgs.stdenv.mkDerivation {
name = "nix-builder-autoscaler-pyright";
inherit src;
dontConfigure = true;
dontBuild = true;
nativeBuildInputs = [
testVenv
pkgs.nodejs
];
checkPhase = ''
runHook preCheck
export HOME=$(mktemp -d)
pyright nix_builder_autoscaler/
runHook postCheck
'';
doCheck = true;
installPhase = ''
mkdir -p $out
touch $out/passed
'';
};
}
);
devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
packages = with pkgs; [
# TODO populate devshell for the project
uv
ruff
pyright
];
};
});
# TODO export module
# nixosModules = {
# default =
# {
# config,
# lib,
# pkgs,
# ...
# }:
# {
# imports = [ ./nixos-module.nix ];
# services.TODSERVICENAME.package =
# lib.mkDefault
# self.packages.${pkgs.stdenv.hostPlatform.system}.default;
# };
# };
};
}