Refine the residency documentation to better support implementation-focused learning. Highlights: - introduce the three operational coaching modes and their continuous learning loop in AGENTS.md - replace machine-specific references with portable repository links - remove Obsidian application metadata from version control - update the roadmap to standardize on Pyright - document Docker volume management and container workflows - document responsibilities of uv, direnv and Nix devShells - add detailed notes on devShells for future milestones - record lessons learned, mistakes and next objectives in the June 26 journal These changes establish the residency workflow while documenting decisions made during the initial project setup.
88 lines
6.8 KiB
Markdown
88 lines
6.8 KiB
Markdown
# devShells, direnv, env files and secret management
|
|
|
|
With NixOS already providing `direnv`, when does it become necessary or advantageous to introduce `devShell`s? which responsibilities belong to each?
|
|
|
|
To answer this accurately, we must establish clear problem statement: managing software dependencies spans multiple layers —from system-level shared libraries to language-specific packages to environment variables. Relying on a single tool to handle all layers inevitably leads to fragile environments, specially on a non-FHS (Filesystem Hierarchy Standard) distribution like NixOS
|
|
|
|
Let's see an overview of these tools, understand what problems they solve, and how their responsibilities should be divided
|
|
|
|
## [uv: The Python Speed Demon](https://docs.astral.sh/uv/)
|
|
|
|
`uv` is powerful Python package and project manager. Just like `pip` or similar package managers like `npm` in the JavaScript ecosystem, `uv`'s primary's job is making sure every dependency used in your project is properly resolved, locked, and installed into an isolated virtual environment at lightning speed. It strictly handles the *language-layer* of your project.
|
|
|
|
If your project relies on pure Python code, `uv` is practically all you need. However, the illusion breaks the moment your Python package require compiling C-extensions or linking against native system libraries (think `numpy`, `pyscopg2`, or `cryptography`).
|
|
|
|
To build these packages, `uv` assumes your operating system follows traditional Linux conventions. It blindly reaches into standard directories like `/usr/lib` or `usr/include`, expecting to find compilers, headers, and C-libraries waiting for it. On NixOS, those FHS directories essentially do not exists. As a result, `uv` hits a brick wall, throwing a massive compilation error simply because it cannot find the system-level components it needs.
|
|
|
|
This is exactly where we need to step down a layer and bring our next tool to bridge the gap.
|
|
|
|
## Nix devShell (`nix develop`)
|
|
|
|
Think of a **devShell** as a temporary, isolated workshop for your project.
|
|
|
|
Many system-level libraries `uv` often crash trying to find them in NixOS. Instead of cluttering your OS with global installs, a devShell quietly rewires your terminal's environment variables (like `PATH` and `LD_LIBRARY_PATH`) to point directly to the exact dependencies you need.
|
|
|
|
**The Result:** Seamless, crash-free installations that succeed perfectly every time—whether you're on NixOS, macOS, or Ubuntu.
|
|
|
|
*(Expanded [[devShells]] guide coming soon.)*
|
|
|
|
|
|
## [direnv](https://direnv.net/)
|
|
|
|
If devShell is your workshop, `direnv` could be an automated assistant, who makes sure the tools are ready to use the second you walk through the door.
|
|
|
|
`direnv` is a shell extension that keeps a watchful eye on your directories. The moment you `cd` into a project folder containing an `.envrc` file, it automatically loads—or unloads- your environment variables and shell settings.
|
|
|
|
That way, you don't need to `source .venv/bin/activate` or each time you enter your project.
|
|
|
|
|
|
## The Responsibility Matrix
|
|
|
|
|**Tool**|**Scope**|**Responsibilities**|
|
|
|---|---|---|
|
|
|**Nix devShell**|System / OS level|Providing Python interpreters, C compilers, system libraries (e.g., PostgreSQL client libs), and non-Python CLI tools (e.g., `jq`, `docker`).|
|
|
|**uv**|Python userland|Resolving and downloading Python packages (FastAPI, SQLAlchemy), writing the lockfile, and managing the local `.venv`.|
|
|
|**direnv**|Workflow / Shell level|Automating the activation of the `devShell` and exporting runtime secrets/environment variables (e.g., `DATABASE_URL`).|
|
|
|
|
## When does it become advantageous to introduce devShells?
|
|
|
|
If you're developing a pure-Python script that relies on standard libraries or pure-Python packages, you can survive on NixOS with just `uv` (assuming you have a globally installed Python interpreter).
|
|
|
|
However, introducing a `devShell` becomes **necessary** (not just advantageous), under the following conditions:
|
|
|
|
- **You require native C extensions:** The moment your FastAPI project interacts with databases using libraries like `psycopg` (PostgreSQL) or handles geospatial data (PostGIS), `uv` will attempt to build these from source or download manylinux wheels. On NixOS, dynamically linked manylinux wheels often fail to execute because the interpreter cannot find the required `.so` files. A `devShell` allows you to inject `stdenv.cc.cc.lib`, `zlib`, or `postgresql` directly into the environment's `LD_LIBRARY_PATH`.
|
|
|
|
- **You need reproducible system tools:** If your project requires a specific version of a database migration tool, a Node.js frontend build step, or a specific formatting tool, defining these in a `devShell` ensures any developer cloning the repository gets the exact same binaries.
|
|
|
|
- **You are collaborating across OS boundaries:** A `devShell` abstracts away the host operating system. A colleague on macOS or Ubuntu will get the same underlying system dependencies as you do on NixOS.
|
|
|
|
### Analyzing the Trade-offs
|
|
|
|
Choosing to implement this trifecta (`devShell` + `direnv` + `uv`) introduces specific trade-offs:
|
|
|
|
- **Complexity vs. Reproducibility:** * _Cost:_ You are introducing Nix syntax and flake evaluation into a Python project. Developers unfamiliar with Nix may struggle to update system dependencies.
|
|
|
|
- _Benefit:_ Absolute guarantee that the environment can be reproduced identically years from now.
|
|
|
|
- **Performance vs. Isolation:**
|
|
|
|
- _Cost:_ Evaluating a complex `flake.nix` takes time, though `nix-direnv` mitigates this heavily through caching.
|
|
|
|
- _Benefit:_ Total isolation. Your FastAPI project's dependencies will not bleed into or be corrupted by your host system's updates.
|
|
|
|
- **Maintenance Cost:** You now have two sources of truth for dependencies: `flake.nix` for system libraries and `pyproject.toml`/`uv.lock` for Python libraries. You must deliberately decide where a dependency belongs to avoid conflicts (e.g., do not install `ruff` via Nix if you are already managing it via `uv`).
|
|
|
|
---
|
|
## Should I use a devShell for BiteTrack
|
|
|
|
The project itself may require some PostgreSQL extension or package in the long run, but adding the complexity of a devShell now is overkill, a sensible approach is:
|
|
|
|
- **Keep using `direnv` now.**
|
|
- Introduce `devShell` later, when the environment itself becomes part of the project.
|
|
|
|
A good mental model is:
|
|
|
|
- **`.envrc`** answers: _"What environment variables should be available?"_
|
|
- **`devShell`** answers: _"What software must exist on this machine to work on this project?"_
|
|
|
|
Right now the project is simple enough that `direnv` is sufficient. Once we face a brick-wall fighting with installing specific versions of tools like `uv`, PostgreSQL clients, Ruff, Pyright, or other binaries, a `devShell` will become valuable because anyone entering the project gets the exact same toolchain. |