Skip to content

Conversation

@andrasbacsai
Copy link
Member

@andrasbacsai andrasbacsai commented Nov 27, 2025

Summary

Fixes environment variable duplication issues in buildtime.env and adds proper support for nixpacks plan variables with a clear priority system.

Problem

  1. Environment variables were being duplicated in buildtime.env when the same variable was defined in multiple sources (nixpacks plan, COOLIFY_* variables, SERVICE_* variables, and user-defined variables)
  2. Nixpacks plan variables were completely filtered out from user-defined variables, preventing users from overriding nixpacks-detected values

Solution

Implemented a priority-based environment variable system using an associative array for automatic deduplication:

Priority Order (lowest to highest):

  1. Nixpacks plan variables - Automatically detected by nixpacks (e.g., NODE_ENV, NIXPACKS_*)
  2. COOLIFY_ variables* - System-generated variables for internal use
  3. SERVICE_ variables* - Docker Compose service metadata (SERVICE_NAME, SERVICE_FQDN, SERVICE_URL)
  4. User-defined variables - Highest priority, can override any of the above

Key Changes

  • app/Jobs/ApplicationDeploymentJob.php:
    • Changed from using a Collection to an associative array ($envs_dict) to prevent duplicates
    • Added nixpacks plan variables as the first (lowest priority) source
    • Removed the filter excluding NIXPACKS_% variables from user-defined variables
    • User-defined variables now properly override nixpacks plan values
    • Added debug logging to show when user variables override nixpacks values
    • Skip COOLIFY_* and SERVICE_* variables from nixpacks plan to prevent conflicts

Benefits

  • No more duplicate environment variables in buildtime.env
  • Users can now override nixpacks-detected variables (e.g., override detected NODE_ENV)
  • Clear priority system makes behavior predictable
  • Better debugging with override logging in dev mode

Testing

  • Verified no duplicate variables are written to buildtime.env
  • Tested that user-defined variables successfully override nixpacks plan variables
  • Confirmed COOLIFY_* and SERVICE_* variables maintain their expected values

🤖 Generated with Claude Code

andrasbacsai and others added 2 commits November 28, 2025 10:10
Refactors generate_buildtime_environment_variables() to use an associative
array (dictionary) approach instead of sequential push() calls. This prevents
duplicate variable declarations in the buildtime.env file.

**Problem:**
After adding nixpacks plan variables to buildtime.env, the same variable
could appear twice in the file:
- Once from nixpacks plan (e.g., NIXPACKS_NODE_VERSION='22')
- Once from user-defined variables (e.g., NIXPACKS_NODE_VERSION="22")

This caused shell errors and undefined behavior during Docker builds.

**Root Cause:**
The push() method adds items sequentially without checking for duplicate
keys. When a variable existed in both nixpacks plan AND user-defined vars,
both would be written to the file.

**Solution:**
- Use associative array ($envs_dict) for automatic deduplication
- Establish clear override precedence:
  1. Nixpacks plan variables (lowest priority)
  2. COOLIFY_* variables (medium priority)
  3. SERVICE_* variables (medium priority)
  4. User-defined variables (highest priority - can override everything)
- Convert to collection format at the end
- Add debug logging when user variables override plan variables

**Benefits:**
- Automatic deduplication (array keys are unique by nature)
- User variables properly override nixpacks plan values
- Clear, explicit precedence order
- No breaking changes to existing functionality

Fixes #7114

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

Co-Authored-By: Claude <[email protected]>
@andrasbacsai andrasbacsai force-pushed the add-nixpacks-plan-variables branch from 62ae80c to be2b017 Compare November 28, 2025 09:10
@andrasbacsai andrasbacsai changed the title Add support for nixpacks plan variables in buildtime environment fix: prevent duplicate environment variables in buildtime.env and support nixpacks plan variable overrides Nov 28, 2025
@andrasbacsai andrasbacsai merged commit 0209bb6 into next Nov 28, 2025
4 checks passed
@andrasbacsai andrasbacsai deleted the add-nixpacks-plan-variables branch November 28, 2025 09:39
@andrasbacsai andrasbacsai mentioned this pull request Nov 28, 2025
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