From 61a2798d4f032b024de8e24dd6815dcf5147f389 Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Thu, 15 Oct 2026 19:27:30 -0500 Subject: [PATCH] build(nix): unify Python dev environment via .venv symlink and add pyright to scripts/test - Extract shared pythonEnv in flake.nix and symlink .venv in devShell shellHook so IDEs, terminal, and Nix checks share one Python 3.14 env. - Add pyright static type checking to scripts/test and align pre-commit with the unified Nix environment. - Remove personal domain from flake.nix description and ignore .venv. --- .githooks/pre-commit | 16 +++++++--------- .gitignore | 1 + flake.nix | 24 +++++++++++++----------- scripts/test | 15 ++++++++++++--- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 7535af4..0b4ac43 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,9 +1,9 @@ #!/usr/bin/env bash -set -e +set -euo pipefail # ============================================================================== # Homelab Core Pre-Commit Invariant & Quality Gate Hook -# Runs linter (Ruff) and architecture & security test suite (Pytest) +# Runs linter (Ruff), static type checker (Pyright), and test suite (Pytest) # ============================================================================== REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" @@ -13,26 +13,24 @@ echo "๐Ÿ” [pre-commit] Executing Homelab Core invariant checks..." # 1. Run Ruff Linter echo " โ†ณ Linting repository with Ruff..." -if command -v uv >/dev/null 2>&1; then - uv run ruff check . -elif command -v ruff >/dev/null 2>&1; then +if command -v ruff >/dev/null 2>&1; then ruff check . elif command -v nix >/dev/null 2>&1 && [ -f flake.nix ]; then nix develop --command ruff check . else - echo "โš ๏ธ [pre-commit] Neither uv, ruff, nor nix found; skipping ruff" + echo "โš ๏ธ [pre-commit] Neither ruff nor nix found; skipping ruff" fi # 2. Run Pytest Suite echo " โ†ณ Running structural and security test suite with Pytest..." -if command -v uv >/dev/null 2>&1; then - uv run --with pytest --with pyyaml pytest tests/ +if [ -x ".venv/bin/pytest" ]; then + .venv/bin/pytest tests/ elif command -v pytest >/dev/null 2>&1; then pytest tests/ elif command -v nix >/dev/null 2>&1 && [ -f flake.nix ]; then nix develop --command pytest tests/ else - echo "โš ๏ธ [pre-commit] Neither uv, pytest, nor nix found; skipping pytest" + echo "โš ๏ธ [pre-commit] Neither pytest nor nix found; skipping pytest" fi echo "โœ… [pre-commit] All repository checks passed successfully!" diff --git a/.gitignore b/.gitignore index 2b30b53..e9fd555 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ secrets.txt backup.zip .envrc .direnv +.venv # SSL / ACME Certificates & Keys config/letsencrypt/* diff --git a/flake.nix b/flake.nix index a6cea8f..099373c 100644 --- a/flake.nix +++ b/flake.nix @@ -1,5 +1,5 @@ { - description = "Homelab Core โ€” Turnkey Declarative NixOS Appliance for roadtotech.me"; + description = "Homelab Core โ€” Turnkey Declarative NixOS Appliance"; inputs = { nixpkgs.url = "https://channels.nixos.org/nixos-unstable/nixexprs.tar.zst"; @@ -14,6 +14,12 @@ let system = "x86_64-linux"; pkgs = nixpkgs.legacyPackages.${system}; + pythonEnv = pkgs.python3.withPackages (ps: with ps; [ + pyyaml + types-pyyaml + pytest + pydantic + ]); in { nixosConfigurations.server = nixpkgs.lib.nixosSystem { @@ -27,17 +33,16 @@ devShells.${system}.default = pkgs.mkShell { buildInputs = with pkgs; [ - (python3.withPackages (ps: with ps; [ - pyyaml - types-pyyaml - pytest - pydantic - ])) + pythonEnv pyright ruff ]; shellHook = '' git config core.hooksPath .githooks 2>/dev/null || true + if [ -d .venv ] && [ ! -L .venv ]; then + rm -rf .venv + fi + ln -sfn ${pythonEnv} .venv ''; }; @@ -45,10 +50,7 @@ test-suite = pkgs.runCommand "core-tests" { nativeBuildInputs = with pkgs; [ git - python3 - python3Packages.pytest - python3Packages.pyyaml - python3Packages.pydantic + pythonEnv ruff ]; } '' diff --git a/scripts/test b/scripts/test index 3bd693e..7cc5a49 100755 --- a/scripts/test +++ b/scripts/test @@ -8,14 +8,23 @@ echo "๐Ÿ” Running code linters (ruff)..." if command -v ruff &>/dev/null; then ruff check . else - nix-shell -p ruff --run "ruff check ." + nix develop --command ruff check . +fi + +echo "๐Ÿ”Ž Running static type analysis (pyright)..." +if command -v pyright &>/dev/null; then + pyright +else + nix develop --command pyright fi echo "๐Ÿงช Running architecture & security test suite (pytest)..." -if command -v pytest &>/dev/null; then +if [ -x ".venv/bin/pytest" ]; then + .venv/bin/pytest tests/ +elif command -v pytest &>/dev/null; then pytest tests/ else - nix-shell -p python3Packages.pytest python3Packages.pyyaml python3Packages.pydantic --run "pytest tests/" + nix develop --command pytest tests/ fi echo "โ„๏ธ Verifying NixOS flake & sandbox checks..."