Skip to content

Comments

Pure Nix Flake Refactor#12

Merged
jamesbrink merged 41 commits intomainfrom
flake-refactor-pure
Dec 24, 2025
Merged

Pure Nix Flake Refactor#12
jamesbrink merged 41 commits intomainfrom
flake-refactor-pure

Conversation

@jamesbrink
Copy link
Member

Summary

This is a comprehensive refactor of the ComfyUI Nix flake to follow pure Nix best practices. The flake has been completely restructured for maintainability, reproducibility, and proper NixOS integration.

Key Changes

Architecture Refactor

  • Modularized flake.nix into separate files under nix/:
    • packages.nix - Main package with inline launcher using writeShellApplication
    • apps.nix - Flake apps (default, cuda, update, buildDocker, buildDockerCuda)
    • checks.nix - Flake checks (ruff, pyright, nixfmt)
    • docker.nix - Docker image builders with reproducible timestamps
    • versions.nix - All version pins in one place
    • python-overrides.nix - Python package overrides
    • vendored-packages.nix - Vendored wheel packages
    • modules/comfyui.nix - NixOS service module with systemd hardening
    • lib/custom-nodes.nix - Foundation for declarative custom nodes

Removed Legacy Code

  • Removed all shell scripts (scripts/*.sh) - functionality now in pure Nix
  • Removed src/persistence/ - no longer needed with --base-directory approach
  • Removed mutable mode - flake is now fully pure
  • Removed decision graph workflows and docs
  • Removed redundant dev tool apps (lint, format, type-check) - use nix flake check instead

New Features

  • Declarative Custom Nodes: NixOS module option services.comfyui.customNodes for pure, reproducible custom node management
  • Platform-specific data directories: Linux uses ~/.config/comfy-ui, macOS uses ~/Library/Application Support/comfy-ui
  • Reproducible Docker images: Uses release date for created timestamp instead of "now"
  • Native ARM builds: CI uses ubuntu-24.04-arm runners instead of QEMU emulation
  • MPS FP8 fix: Patch for Apple Silicon dequantization

CI/CD Improvements

  • Consolidated CI and Docker workflows into single docker.yml
  • Added nix flake check to CI pipeline
  • Fixed Cachix push/pull configuration
  • Multi-arch Docker images (amd64 + arm64) with proper manifests

Documentation

  • Streamlined README from 500+ lines to ~140 lines
  • Updated CLAUDE.md with complete project documentation
  • Added custom nodes usage examples

Breaking Changes

  • Data directory location changed (automatic migration not provided)
  • Shell scripts removed - use nix run instead
  • pythonRuntime package no longer exposed (internal use only)
  • Dev tool apps removed - use nix flake check or nix develop

Migration Guide

  1. Data migration: Copy your data from old location to new:

    • Linux: ~/.config/comfy-ui/
    • macOS: ~/Library/Application Support/comfy-ui/
  2. Custom nodes: For NixOS users, use the new declarative option:

    services.comfyui.customNodes = {
      ComfyUI-Impact-Pack = pkgs.fetchFromGitHub { ... };
    };
  3. Running: Use nix run github:utensils/comfyui-nix -- --open

Test Plan

  • nix flake check passes (ruff, pyright, nixfmt, package build)
  • nix build succeeds for default package
  • nix build .#cuda succeeds for CUDA package
  • nix build .#dockerImage succeeds
  • nix build .#dockerImageCuda succeeds
  • NixOS module evaluates correctly
  • CI pipeline passes on branch

Stats

  • 40 commits in this PR
  • -4,742 lines removed, +1,378 lines added (net reduction of 3,364 lines)
  • 39 files changed

🤖 Generated with Claude Code

- Split 837-line flake.nix into focused modules (~160 lines now):
  - nix/versions.nix: Version pins for ComfyUI and vendored wheels
  - nix/python-overrides.nix: Python package customizations
  - nix/vendored-packages.nix: PyPI wheel packages
  - nix/packages.nix: Main package derivation
  - nix/docker.nix: DRY Docker image builder with mkDockerImage helper
  - nix/apps.nix: App definitions using writeShellApplication
  - nix/checks.nix: CI check definitions
  - nix/modules/comfyui.nix: Full NixOS systemd service module

- NixOS module features:
  - listenAddress option (default 127.0.0.1, use 0.0.0.0 for network)
  - Automatic user/group creation
  - Firewall integration
  - Mutable mode support
  - tmpfiles.rules for directory management

- Improvements:
  - Use stdenv.mkDerivation instead of buildPythonApplication
  - Add meta.maintainers to package
  - Add passthru for introspection
  - Use replaceVars consistently
  - Remove Cachix references (not used)
  - Fix $PWD escaping in app scripts
  - Document all NixOS module options in README
- Fix model downloader being installed with Nix store hash prefix
  by copying contents into properly named directory
- Add missing workflow-templates dependencies:
  - comfyui-workflow-templates-core
  - comfyui-workflow-templates-media-api
  - comfyui-workflow-templates-media-video
  - comfyui-workflow-templates-media-image
  - comfyui-workflow-templates-media-other
- Refactor vendored-packages.nix with mkWheel helper function
Template input files (images, videos) are static assets needed for
workflow examples to function properly. These should be downloaded
regardless of mode, as they don't modify code - they're just
media files referenced by workflow templates.
This commit removes all mutable mode support from the flake, simplifying
the codebase by eliminating ~300 lines of code related to venv creation,
pip installs, ComfyUI-Manager, and dynamic GPU detection.

## Nix Changes

- nix/apps.nix: Remove `mutable` app and `mutableScript`
- nix/packages.nix: Remove `COMFY_MODE` env var and `comfy` CLI wrapper
- nix/modules/comfyui.nix: Remove `mutable` option from NixOS service
- nix/docker.nix: Remove `COMFY_MODE`, `CUDA_VERSION` env vars and venv
  path references

## Script Changes

- scripts/config.sh: Remove mutable-specific variables (COMFY_MODE,
  CUDA_VERSION, COMFY_VENV, COMFY_MANAGER_DIR, MODEL_DOWNLOADER_PERSISTENT_DIR),
  BASE_PACKAGES/ADDITIONAL_PACKAGES arrays, and mutable path logic
- scripts/install.sh: Remove 4 mutable-only functions (~170 lines):
  - install_comfyui_manager() - ComfyUI-Manager git clone/update
  - install_model_downloader() - mutable persistent install
  - detect_pytorch_version() - runtime GPU detection
  - setup_venv() - Python venv creation and pip installs
- scripts/persistence.sh: Remove mutable-specific symlinks for
  ComfyUI-Manager and model_downloader

## Documentation

- README.md: Update modes section, features, environment variables,
  NixOS module options table, and remove all mutable mode references
- CLAUDE.md: Simplify to reflect pure-only operation

## What Remains

- Pure mode: All Python dependencies provided by Nix
- CUDA support: Via `nix run .#cuda` (Nix-provided CUDA PyTorch)
- Model downloader: Bundled into CODE_DIR (works in pure mode)
- Frontend: Packaged from Nix, no runtime downloads
- Docker images: CPU and CUDA variants, both pure

All `nix flake check` validations pass.
On Darwin, av uses ffmpeg 7.x and torchaudio uses ffmpeg 6.x. These
versions are mutually incompatible:
- av 14.1.0 requires ffmpeg 7.x+ (uses AV_HWDEVICE_TYPE_D3D12VA)
- torchaudio 2.5.1 requires ffmpeg 6.x (uses deprecated channel_layout API)

The runtime warning about duplicate Objective-C classes is unavoidable
until nixpkgs updates torchaudio to a ffmpeg 7-compatible version.
The warning is harmless in practice.
Major refactoring to follow Nix best practices:
- Replace ~700 lines of bash scripts with ~50 line inline launcher using writeShellApplication
- Run ComfyUI directly from Nix store using native --base-directory and --front-end-root flags
- Remove scripts/ directory (no longer needed)
- Remove src/persistence/ module (ComfyUI handles paths natively)

Fixes:
- Add --database-url to store SQLite DB in user data dir (not read-only Nix store)
- Fix model_downloader symlink to handle stale directories
- Remove .disabled file blocking model_downloader from loading
- Patch comfy_api_nodes canary to skip PyAV version check (API nodes need external credentials anyway)

Cleanup:
- Remove Docker cross-compile from Darwin (kornia-rs platform issues, CI builds on Linux)
- Simplify apps.nix with conditional Docker app exports
- Update documentation for new architecture
Instead of patching out the PyAV version check, override the av package
to version 14.2.0 which passes the canary check natively. This allows
all comfy_api_nodes (OpenAI, Stability, Runway, etc.) to load properly
for users who have the required API credentials.
jamesbrink and others added 11 commits December 23, 2025 21:14
Platform-specific improvements:
- Use ~/Library/Application Support/comfy-ui on macOS (Apple convention)
- Use ~/.config/comfy-ui on Linux (XDG convention)
- Set DYLD_LIBRARY_PATH only on macOS, LD_LIBRARY_PATH only on Linux
- Extract --port from args for browser opening (fixes hardcoded 8188)

NixOS module improvements:
- Add systemd security hardening (NoNewPrivileges, ProtectSystem, etc.)
- Add GPU access via video/render supplementary groups
- Clarify createUser option behavior in documentation
- Improve service description

Code quality fixes:
- Fix pyright --pythonpath argument (remove /bin/python suffix)
- Document COMFY_ALLOW_MANAGER environment variable

Documentation sync:
- Update CLAUDE.md and README.md with platform-specific paths
- Add COMFY_ALLOW_MANAGER to environment variables section
- Update NixOS module option descriptions

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add releaseDate to versions.nix for deterministic Docker timestamps
- Use releaseDate instead of "now" in docker.nix for reproducible images
- Fix source filter to exclude all result* symlinks (not just "result")
- Add xdg-utils to launcher runtimeInputs on Linux for --open flag
- Remove redundant Features section from README

Docker images now show the actual ComfyUI release date (2025-12-18)
instead of build time, ensuring reproducible builds while maintaining
meaningful timestamps in `docker images` output.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Reduce from 481 to 119 lines while preserving all key information:
quick start, options, installation, NixOS module, Docker, and
development commands.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Remove pythonRuntime/pythonRuntimeCuda from public packages (internal only)
- Remove lint, lint-fix, format, type-check, check-all apps
- Users can use `nix develop` for ruff/pyright or `nix flake check` for CI
- Pass pythonRuntime directly to checks.nix instead of via packages

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Runs nix flake check on push/PR to validate:
- Package builds
- Python linting (ruff)
- Python type checking (pyright)
- Nix formatting (nixfmt)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Switch from QEMU emulation to GitHub's native ARM runners
(ubuntu-24.04-arm) for much faster aarch64-linux builds.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Merge flake check into main CI workflow
- Run checks before Docker builds (builds depend on check passing)
- Remove separate ci.yml

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add customNodes option to services.comfyui for pure, reproducible
  custom node management via fetchFromGitHub or local paths
- Add preStart script to symlink declared nodes before service start
- Create nix/lib/custom-nodes.nix as foundation for future curated nodes
- Fix .gitignore lib/ pattern to not exclude nix/lib/
- Fix nixfmt check deprecation warning (use find instead of directory)
- Update README and CLAUDE.md with custom nodes documentation

Usage:
  services.comfyui = {
    enable = true;
    customNodes = {
      ComfyUI-Impact-Pack = pkgs.fetchFromGitHub {
        owner = "ltdrdata";
        repo = "ComfyUI-Impact-Pack";
        rev = "v1.0.0";
        hash = "sha256-...";
      };
    };
  };

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Clean up CI workflow to only target main branch and version tags,
preparing for merge to main.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Resolved conflicts by keeping flake-refactor-pure changes:
- Discarded main's shell script updates (scripts removed in pure refactor)
- Kept pure Nix flake.nix structure
- Kept streamlined README.md and CLAUDE.md
- Kept .gitignore with .codex/ instead of .deciduous/

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@jamesbrink jamesbrink merged commit c18dbf1 into main Dec 24, 2025
1 of 2 checks passed
@jamesbrink jamesbrink deleted the flake-refactor-pure branch February 20, 2026 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant