Skip to content

Commit 5bf597a

Browse files
authored
feat: add cudaCapabilities option to NixOS module (#20)
* feat: add cudaCapabilities option to NixOS module - Add optional cudaCapabilities setting that maps to nixpkgs.config - Document common CUDA compute capability values with examples - Improve AGENTS.md with comprehensive development guidelines - Fix platform constraints for CUDA packages (x86_64-linux only) Closes #19 * fix: restore dockerImage for aarch64-linux The previous change incorrectly restricted dockerImage to x86_64-linux only. This broke ARM64 Docker builds in CI. Now: - dockerImage is available for all Linux platforms - cuda and dockerImageCuda remain x86_64-linux only (as intended) * docs: clarify cudaCapabilities doesn't affect pre-built PyTorch wheels Address review feedback by adding explicit documentation that: - Pre-built PyTorch wheels already support all GPU architectures - This setting primarily benefits other system CUDA packages - The setting affects global nixpkgs configuration Thanks to the reviewer for the excellent and thorough review!
1 parent 74b2481 commit 5bf597a

File tree

4 files changed

+158
-25
lines changed

4 files changed

+158
-25
lines changed

AGENTS.md

Lines changed: 115 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,135 @@
1-
# Repository Guidelines
1+
# Repository Guidelines for Agents
22

3-
## Project Structure & Module Organization
3+
## Purpose
44
This repo is a Nix flake that packages ComfyUI plus curated custom nodes.
5+
Use this file as the single source of truth for build, lint, test, and style
6+
expectations when working in this repository.
7+
8+
## Project Structure & Module Organization
59
- `nix/`: flake helpers, package definitions, module, checks, and version pins.
610
- `src/custom_nodes/`: bundled custom nodes (Python and frontend assets).
711
- `src/patches/`: patches applied to upstream components.
812
- `docs/`: additional documentation (setup guides, etc.).
913
- `scripts/`: maintenance scripts (template inputs, Cachix, model downloads).
1014
- `data/`: local runtime data for container examples and demos.
1115

12-
## Build, Test, and Development Commands
13-
- `nix run`: run ComfyUI (add `-- --open`, `--port=XXXX`, `--listen 0.0.0.0` as needed).
14-
- `nix run .#cuda`: CUDA build on Linux/NVIDIA; use only when GPU support is required.
16+
## Development Environment
17+
- Use `nix develop` to enter the dev shell when running linting or formatting.
18+
- The dev shell provides `ruff`, `pyright`, `nixfmt`, and `shellcheck`.
19+
- Python runtime target is 3.12 (see `pyproject.toml`).
20+
21+
## Build, Lint, Format, and Test Commands
22+
### Core Nix commands
1523
- `nix build`: build the default package without running it.
16-
- `nix develop`: enter the dev shell with `ruff`, `pyright`, and `nixfmt`.
24+
- `nix run`: run ComfyUI (add `-- --open`, `--port=XXXX`, `--listen 0.0.0.0`).
25+
- `nix run .#cuda`: CUDA build on Linux/NVIDIA only.
1726
- `nix flake check`: run all checks (build, ruff, pyright, nixfmt, shellcheck).
1827
- `nix fmt`: format Nix files with `nixfmt-rfc-style`.
1928
- `nix run .#update`: check for ComfyUI version updates.
20-
- `nix run .#buildDocker` / `nix run .#buildDockerCuda`: build CPU/CUDA images locally.
29+
- `nix run .#buildDocker`: build CPU Docker image.
30+
- `nix run .#buildDockerCuda`: build CUDA Docker image.
31+
32+
### Python linting and formatting
33+
- `ruff check src/`: run linting on all Python sources.
34+
- `ruff check src/path/to/file.py`: lint a single Python file.
35+
- `ruff format src/`: format Python sources.
36+
- `ruff format src/path/to/file.py`: format a single Python file.
37+
38+
### Python type checking
39+
- `pyright src/`: run type checks for the source tree.
40+
- `pyright src/path/to/file.py`: run type checks on a single Python file.
41+
42+
### Shell scripting
43+
- `shellcheck scripts/*.sh`: lint shell scripts.
44+
- `shellcheck scripts/path/to/script.sh`: lint a single script.
45+
46+
### Tests
47+
- There are no unit-test suites in this repo today.
48+
- The primary quality gate is `nix flake check`.
49+
- If tests are added in the future, prefer `pytest path/to/test.py::test_name`
50+
for a single test and `pytest path/to/test.py` for a single file.
51+
52+
## Coding Style & Conventions
53+
### Python formatting
54+
- Python is formatted with `ruff format`.
55+
- Use 4-space indentation and a 100-character line length.
56+
- Use double quotes for strings unless escaping makes it noisy.
57+
- Prefer trailing commas in multi-line collections.
58+
59+
### Imports
60+
- Order imports as standard library, third-party, then local.
61+
- Ruff enforces import ordering (isort).
62+
- Avoid unused imports except in `__init__.py` where they are allowed.
63+
- Star imports are allowed only in `src/custom_nodes/**` modules.
64+
65+
### Naming
66+
- `snake_case` for functions, modules, variables.
67+
- `PascalCase` for classes.
68+
- `UPPER_SNAKE_CASE` for constants.
69+
- Avoid single-letter variable names unless the context is obvious.
70+
71+
### Types
72+
- Python version is 3.12 (`pyright` configured accordingly).
73+
- Type checking mode is `basic`; be pragmatic with annotations.
74+
- Add type hints to new public functions and key data structures.
75+
- Prefer `typing` (e.g., `Iterable`, `Mapping`, `Sequence`) over concrete
76+
container types in APIs.
77+
- Use `typing.cast` only when necessary and document intent.
78+
- Use `from __future__ import annotations` when it improves readability.
79+
80+
### Docstrings
81+
- Google-style docstrings are preferred.
82+
- Document public functions and classes when behavior is non-obvious.
83+
- Keep docstrings concise and action-oriented.
84+
85+
### Error handling
86+
- Raise specific exceptions with actionable messages.
87+
- Preserve context by chaining (`raise ... from err`) when wrapping errors.
88+
- Avoid bare `except:`; catch explicit exception types.
89+
- It is acceptable to use `print` for status updates (ComfyUI convention).
90+
91+
### Filesystem usage
92+
- `os.path` is acceptable (the repo does not mandate `pathlib`).
93+
- Prefer `pathlib.Path` only when it improves clarity or APIs require it.
94+
- Avoid writing outside the repo unless required by a script.
95+
96+
### Performance and readability
97+
- Keep functions small and focused.
98+
- Avoid deeply nested conditionals; early returns are encouraged.
99+
- Prefer comprehensions when they remain readable.
100+
- Do not micro-optimize unless it is on a hot path.
101+
102+
## Repo-Specific Ruff and Pyright Notes
103+
- Ruff targets Python 3.12 with 100-character lines.
104+
- Ruff ignores `T20` (print statements) and `G004` (f-strings in logging).
105+
- Ruff allows unused imports in `__init__.py` and star imports in
106+
`src/custom_nodes/**`.
107+
- Ruff complexity guard: `max-complexity = 15` (McCabe).
108+
- Pyright uses `basic` type checking with lenient missing import handling.
109+
- Pyright is configured to warn (not fail) on missing imports.
110+
- Pyright uses `typings/` as the stub path when available.
111+
112+
## Nix Style
113+
- Format Nix files with `nix fmt`.
114+
- Keep attrsets aligned and avoid reformatting unrelated sections.
115+
- Prefer existing patterns in `nix/` for adding new derivations.
21116

22-
## Coding Style & Naming Conventions
23-
- Python: 4-space indentation, 100-char lines, double quotes (`ruff format`).
24-
- Imports: standard library, third-party, then local; `ruff` enforces ordering.
25-
- Naming: `snake_case` for functions/vars, `PascalCase` for classes.
26-
- Nix: format with `nix fmt` (nixfmt RFC style).
27-
- Shell: scripts in `scripts/` should pass `shellcheck`.
117+
## Shell Style
118+
- Shell scripts should pass `shellcheck`.
119+
- Prefer `set -euo pipefail` for new scripts when appropriate.
120+
- Avoid bashisms if the script might be invoked by `sh`.
121+
- Keep scripts small and focused; prefer helpers in `scripts/`.
28122

29-
## Testing Guidelines
30-
There are no unit-test suites in the repo today. Quality gates are:
31-
- `nix flake check` (ruff + pyright + nixfmt + shellcheck).
32-
- Optional manual runs: `ruff check src/` and `pyright src/` inside `nix develop`.
123+
## Cursor and Copilot Rules
124+
- No `.cursorrules` or `.cursor/rules/` found in this repo.
125+
- No `.github/copilot-instructions.md` found in this repo.
33126

34127
## Commit & Pull Request Guidelines
35-
- Commit style follows a conventional prefix, e.g. `docs: ...`, `ci: ...`, `refactor: ...`.
36-
- Keep messages short and specific to the change.
37-
- PRs should include a concise description, linked issues if applicable, and the checks run
38-
(e.g. `nix flake check` or relevant `nix run`/`nix build` commands).
128+
- Commit style follows a conventional prefix, e.g. `docs: ...`, `ci: ...`,
129+
`refactor: ...`.
130+
- Keep commit messages short and specific to the change.
131+
- PRs should include a concise description, linked issues if applicable, and
132+
the checks run (e.g. `nix flake check`, `nix build`).
39133

40134
## Agent-Specific Instructions
41135
- After any file edits, run `git add` so the flake can see changes correctly.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ nix profile add github:utensils/comfyui-nix#cuda
302302
services.comfyui = {
303303
enable = true;
304304
cuda = true; # Enable NVIDIA GPU acceleration (recommended for most users)
305+
# cudaCapabilities = [ "8.9" ]; # Optional: optimize system CUDA packages for RTX 40xx
306+
# Note: Pre-built PyTorch wheels already support all GPU architectures
305307
enableManager = true; # Enable the built-in ComfyUI Manager
306308
port = 8188;
307309
listenAddress = "127.0.0.1"; # Use "0.0.0.0" for network access
@@ -319,6 +321,7 @@ nix profile add github:utensils/comfyui-nix#cuda
319321
| --------------- | -------------------- | ------------------------------------------------ |
320322
| `enable` | `false` | Enable the ComfyUI service |
321323
| `cuda` | `false` | Enable NVIDIA GPU acceleration |
324+
| `cudaCapabilities` | `null` | Optional CUDA compute capability list |
322325
| `enableManager` | `false` | Enable the built-in ComfyUI Manager |
323326
| `port` | `8188` | Port for the web interface |
324327
| `listenAddress` | `"127.0.0.1"` | Listen address (`"0.0.0.0"` for network access) |
@@ -332,6 +335,9 @@ nix profile add github:utensils/comfyui-nix#cuda
332335
| `customNodes` | `{}` | Declarative custom nodes (see below) |
333336
| `requiresMounts`| `[]` | Mount units to wait for before starting |
334337

338+
`cudaCapabilities` maps to `nixpkgs.config.cudaCapabilities`, so setting it will
339+
apply to other CUDA packages in the system configuration as well.
340+
335341
**Note:** When `dataDir` is under `/home/`, `ProtectHome` is automatically disabled to allow access.
336342

337343
### Using a Home Directory

flake.nix

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,11 @@
152152
dockerImageLinuxArm64 = linuxArm64Packages.dockerImage;
153153
}
154154
// pkgs.lib.optionalAttrs pkgs.stdenv.isLinux {
155+
dockerImage = nativePackages.dockerImage;
156+
}
157+
// pkgs.lib.optionalAttrs (pkgs.stdenv.isLinux && pkgs.stdenv.isx86_64) {
155158
# CUDA package uses pre-built wheels (supports all GPU architectures)
156159
cuda = nativePackagesCuda.default;
157-
dockerImage = nativePackages.dockerImage;
158160
dockerImageCuda = nativePackagesCuda.dockerImageCuda;
159161
};
160162
in
@@ -223,12 +225,12 @@
223225
comfyui-nix = self.legacyPackages.${final.system};
224226
comfyui = self.packages.${final.system}.default;
225227
comfy-ui = self.packages.${final.system}.default;
226-
# CUDA variant (Linux only) - uses pre-built wheels supporting all GPU architectures
228+
# CUDA variant (x86_64 Linux only) - uses pre-built wheels supporting all GPU architectures
227229
comfy-ui-cuda =
228-
if final.stdenv.isLinux then
230+
if final.stdenv.isLinux && final.stdenv.isx86_64 then
229231
self.packages.${final.system}.cuda
230232
else
231-
throw "comfy-ui-cuda is only available on Linux";
233+
throw "comfy-ui-cuda is only available on x86_64 Linux";
232234
# Add custom nodes to overlay
233235
comfyui-custom-nodes = self.legacyPackages.${final.system}.customNodes;
234236
};

nix/modules/comfyui.nix

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ in
8181
'';
8282
};
8383

84+
cudaCapabilities = lib.mkOption {
85+
type = lib.types.nullOr (lib.types.listOf lib.types.str);
86+
default = null;
87+
description = ''
88+
Optional list of CUDA compute capabilities to use for builds that honor
89+
`nixpkgs.config.cudaCapabilities`. When set, this updates the global
90+
nixpkgs configuration, so it affects other CUDA packages too.
91+
92+
Note: ComfyUI's pre-built PyTorch wheels already support all GPU
93+
architectures (Pascal through Hopper). This setting is primarily useful
94+
for optimizing other CUDA-enabled packages in your system configuration.
95+
96+
Example: [ "8.9" ] for Ada Lovelace (RTX 40xx) GPUs.
97+
98+
Common values:
99+
- "6.1": Pascal (GTX 1080/1070)
100+
- "7.0": Volta (V100)
101+
- "7.5": Turing (RTX 20xx, GTX 16xx)
102+
- "8.0": Ampere (A100)
103+
- "8.6": Ampere (RTX 30xx)
104+
- "8.9": Ada Lovelace (RTX 40xx)
105+
- "9.0": Hopper (H100)
106+
107+
See: https://developer.nvidia.com/cuda-gpus
108+
'';
109+
};
110+
84111
enableManager = lib.mkOption {
85112
type = lib.types.bool;
86113
default = false;
@@ -216,6 +243,10 @@ in
216243
};
217244

218245
config = lib.mkIf cfg.enable {
246+
nixpkgs.config = lib.mkIf (cfg.cudaCapabilities != null) {
247+
cudaCapabilities = cfg.cudaCapabilities;
248+
};
249+
219250
# Create system user/group only when using default "comfyui" names
220251
users.users = lib.mkIf (cfg.createUser && isSystemUser) {
221252
${cfg.user} = {

0 commit comments

Comments
 (0)