blitz_api/flake.nix
fusion44 3e34201f6a
refactor(flake): expose lib.mkBlitzApi for consumer-pkgs builds
Extract the Python virtualenv build into `mkBlitzApi { pkgs, python ?
null }` and expose it as `lib.mkBlitzApi`. Rewrite `overlays.default`
to call the builder against the consumer's `prev` instead of pulling
from `self.packages.${system}` (which captured this flake's pinned
nixpkgs at flake-eval time and shut consumer overlays out of the
build hook).

Motivation: NixBlitz pulls blitz-api's flake via `builtins.getFlake`
to keep the plugin self-contained. Without a pkgs-agnostic builder,
the operator's nixpkgs overlays (Pi 5 jemalloc page-size patch and
similar platform fixes) couldn't reach blitz-api's build, so the
package would either fail to compile on Pi 5 or silently link
against this flake's pinned nixpkgs and pull a second snapshot of
glibc / openssl / etc. into the operator's closure.

The `packages.${system}.${name}` attribute is preserved as a
convenience pre-built using this flake's pinned nixpkgs; consumers
that want consumer-pkgs builds should use `overlays.default` or
`lib.mkBlitzApi` directly.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 12:44:11 +02:00

114 lines
3.5 KiB
Nix

{
description = "A management backend for the RaspiBlitz project written in Python / FastAPI";
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
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,
flake-utils,
pyproject-nix,
uv2nix,
pyproject-build-systems,
...
}: let
name = "blitz-api";
# Load the uv workspace once. Pure Nix value — independent of
# the consumer's pkgs, only depends on this repo's source tree.
workspace = uv2nix.lib.workspace.loadWorkspace {workspaceRoot = ./.;};
# Pyproject-nix overlay derived from pyproject.toml + uv.lock.
# Prefer prebuilt wheels — a handful of deps (grpcio, psutil,
# pyzmq) are painful to compile from source and wheels work
# fine on linux/darwin. Also pkgs-independent.
pyprojectOverlay = workspace.mkPyprojectOverlay {
sourcePreference = "wheel";
};
# Build the blitz-api Python virtualenv against the CALLER's
# pkgs. Exposed as `lib.mkBlitzApi` so consumers (NixBlitz
# plugin, downstream embedders) can build the package using
# their own nixpkgs — important when the consumer needs a
# platform-specific patch that this flake's pinned
# nixos-unstable doesn't carry yet.
#
# Without this, the package would be built once at THIS flake's
# eval time using THIS flake's pinned nixpkgs, and consumer
# overlays couldn't reach the build hook.
mkBlitzApi = {
pkgs,
python ? null,
}: let
py =
if python != null
then python
else pkgs.python312;
pythonSet = (pkgs.callPackage pyproject-nix.build.packages {python = py;})
.overrideScope (
pkgs.lib.composeManyExtensions [
pyproject-build-systems.overlays.default
pyprojectOverlay
]
);
in
pythonSet.mkVirtualEnv "${name}-env" workspace.deps.default;
# Library output — what callers reach for to build the package
# under their own pkgs.
libOutput = {
lib = {inherit mkBlitzApi;};
};
systems = flake-utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages.${system};
in {
packages = {
default = self.packages.${system}.${name};
# Convenience: a pre-built package using THIS flake's pinned
# nixpkgs. Most consumers should use `overlays.default` or
# `lib.mkBlitzApi` instead so the package builds against
# their pkgs.
${name} = mkBlitzApi {inherit pkgs;};
};
});
overlays.overlays = {
# Re-runs `mkBlitzApi` against the consumer's `prev` pkgs so the
# consumer's overlays (Pi 5 page-size patch, custom uv pin, etc.)
# are in scope when the package is built.
default = final: prev: {
${name} = mkBlitzApi {pkgs = prev;};
};
};
module = {
nixosModules.default = {...}: {
imports = [./modules/blitz_api.nix];
nixpkgs.overlays = [self.overlays.default];
};
};
in
systems // overlays // module // libOutput;
}