Version: 1.1.0 License: MIT Author: Human + Claude collaboration
kpenv is a command-line tool for managing .env files using KeePassXC as a secure backend. It allows you to:
- 🔄 Sync
.env.exampleto.envwith interactive prompts - 💾 Backup
.envfiles to KeePass entry notes - 📥 Restore
.envfiles from KeePass - 🔐 Secure storage without committing secrets to git
- 🌍 Multi-environment support (development, staging, production)
Perfect for:
- Teams sharing environment configurations securely
- Developers working across multiple machines
- Projects with complex environment setups
- Avoiding secrets in version control
✅ Easy Installation - One-command installation script
✅ Project Initialization - kpenv init sets up projects automatically
✅ Auto .gitignore - Ensures .env files are never committed
✅ Flexible Configuration - User and project-level JSON configs
✅ Sync from Example - Interactively create .env from example files
✅ Backup to KeePass - Store environment files in KeePass database notes
✅ Restore from KeePass - Retrieve environment files on new machines
✅ Auto Project Detection - Smart project name detection from git/path
✅ Multi-Environment - Support for development, staging, production
✅ Secure - Password never stored, read from stdin or env var
✅ Safe Restore - Creates .env.fetched if local .env exists
- PHP 8.2+ (uses
declare(strict_types=1),str_starts_with) - KeePassXC with CLI tool (
keepassxc-cli) - KeePass database file (
.kdbx)
Linux/macOS:
# Clone or download the repository
git clone https://github.com/stacmv/env-manager.git
cd env-manager
# Run installer
./install.shThe installer will:
- ✅ Check PHP 8.2+ is installed
- ✅ Check for KeePassXC CLI
- ✅ Install
kpenvto/usr/local/bin - ✅ Create
~/.kpenv/config.jsonwith defaults - ✅ Make kpenv globally available
Windows:
# Prerequisites: Install Scoop package manager first
# Visit https://scoop.sh or run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression
# Clone or download the repository
git clone https://github.com/stacmv/env-manager.git
cd env-manager
# Run installer
.\install.ps1The Windows installer will:
- ✅ Check Scoop is installed (required for dependency management)
- ✅ Offer to install PHP 8.2+ via Scoop if not found
- ✅ Offer to install KeePassXC via Scoop if not found
- ✅ Offer to install Cmder (Unix-like shell) via Scoop if no bash found
- ✅ Install
kpenvto user directory with PATH access - ✅ Create
%USERPROFILE%\.kpenv\config.jsonwith defaults - ✅ Make kpenv globally available
Note for Windows users: kpenv works best in Unix-like environments (Git Bash, MSYS2, WSL, Cmder). The installer can optionally install Cmder for you.
# 1. Install dependencies
# Ubuntu/Debian
sudo apt install php-cli keepassxc
# macOS
brew install php keepassxc
# 2. Copy kpenv to PATH
sudo cp kpenv /usr/local/bin/kpenv
sudo chmod +x /usr/local/bin/kpenv
# 3. Create config directory
mkdir -p ~/.kpenv
# 4. Create default config
cat > ~/.kpenv/config.json <<EOF
{
"base_dev_folder": "$HOME/dev",
"keepass_db": "$HOME/Documents/work-secrets.kdbx",
"default_env": "development",
"example_files": [".env.example", "env.example", ".env.dist"],
"auto_gitignore": true
}
EOFLinux/macOS:
./uninstall.shWindows:
.\uninstall.ps1The installer automatically sets up shell completions. For manual setup:
# System-wide (requires sudo)
sudo cp completions/kpenv.bash /etc/bash_completion.d/kpenv
# Or user-local
mkdir -p ~/.local/share/bash-completion/completions
cp completions/kpenv.bash ~/.local/share/bash-completion/completions/kpenv
# Reload completions
source ~/.bashrc# Copy completion file
mkdir -p ~/.zsh/completions
cp completions/_kpenv ~/.zsh/completions/_kpenv
# Add to ~/.zshrc (if not already present)
fpath=(~/.zsh/completions $fpath)
autoload -Uz compinit && compinit
# Reload
source ~/.zshrcAfter installation, use Tab to complete:
kpenv <Tab> # Shows: init sync-example backup-env restore-env help
kpenv backup-env --<Tab> # Shows: --env --password
kpenv backup-env --env=<Tab> # Shows: development staging productionSet up kpenv in your project directory:
kpenv initThis command will:
- ✅ Detect your project name from git or directory
- ✅ Find
.env.exampleor other example files - ✅ Check if
.envis in.gitignore(and add it if not) - ✅ Create
.kpenv.jsonproject configuration - ✅ Optionally sync
.env.exampleto.env
Example:
$ cd my-project
$ kpenv init
🔧 Initializing kpenv in current project...
Project detected: my-project
Current directory: /home/user/dev/my-project
✓ Found .env.example
✓ .env does not exist (will be created)
✓ KeePass database: /home/user/Documents/work-secrets.kdbx
✓ keepassxc-cli found
Checking .gitignore...
⚠ .env not found in .gitignore
Add .env to .gitignore? [Y/n]: y
✓ Added .env to .gitignore
Create .kpenv.json config? [Y/n]: y
✓ Created .kpenv.json
Sync .env.example to .env now? [Y/n]: y
✅ Project initialized successfully!
Create or update .env from .env.example with interactive prompts:
kpenv sync-example
# With specific environment
kpenv sync-example --env=stagingExample:
$ kpenv sync-example
Syncing .env.example with local .env...
New key 'DATABASE_HOST' found. Enter value: localhost
New key 'DATABASE_PASSWORD' found. Enter value: ********
Sync completed. 2 keys added to .env.
Backup your .env file to KeePass:
kpenv backup-env
# With password from environment variable
KEEPASS_PASSWORD=mypass kpenv backup-env
# With specific environment
kpenv backup-env --env=productionWhat happens:
- Reads
.envfile - Stores content in KeePass entry notes at path:
{project-name}/{environment} - Creates KeePass groups if needed
Restore .env file from KeePass on a new machine:
kpenv restore-env
# With password as argument (less secure)
kpenv restore-env --password=mypass
# Specific environment
kpenv restore-env --env=stagingWhat happens:
- Retrieves content from KeePass entry:
{project-name}/{environment} - Creates
.envfile - If
.envexists, creates.env.fetchedinstead (safe mode)
kpenv uses a hierarchical configuration system:
Priority (highest to lowest):
- CLI arguments (
--env=production) - Environment variables (
KPENV_DB,KPENV_BASE_FOLDER) - Project config (
.kpenv.json) - User config (
~/.kpenv/config.json) - Defaults
Location: ~/.kpenv/config.json
{
"base_dev_folder": "/home/username/dev",
"keepass_db": "/home/username/Documents/work-secrets.kdbx",
"default_env": "development",
"example_files": [
".env.example",
"env.example",
".env.dist",
"example.env"
],
"auto_gitignore": true
}Options:
base_dev_folder- Base directory for project detectionkeepass_db- Path to KeePass database filedefault_env- Default environment name (development, staging, production)example_files- List of example file names to search for (in order)auto_gitignore- Automatically add.envto.gitignoreduring init
Location: .kpenv.json (in project root)
Created automatically by kpenv init, or create manually:
{
"project_name": "my-awesome-app",
"example_file": ".env.example",
"environments": [
"development",
"staging",
"production"
]
}Options:
project_name- Explicit project name (overrides auto-detection)example_file- Specific example file to useenvironments- List of available environments
Override configuration on-the-fly:
# Override KeePass database path
KPENV_DB=/path/to/other.kdbx kpenv backup-env
# Override base folder
KPENV_BASE_FOLDER=/home/user/projects kpenv sync-example
# Pass KeePass password (non-interactive)
KEEPASS_PASSWORD=mypassword kpenv restore-env# 1. Initialize project
kpenv init
# This will guide you through setup
# 2. Backup to KeePass
kpenv backup-env
# Enter KeePass password when prompted# 1. Clone the project
git clone https://github.com/username/project.git
cd project
# 2. Restore .env from KeePass
kpenv restore-env
# Enter KeePass password when prompted
# 3. Ready to work!# Development environment
kpenv backup-env --env=development
# Staging environment
kpenv backup-env --env=staging
# Production environment
kpenv backup-env --env=production
# Restore specific environment
kpenv restore-env --env=productionenv-manager/
├── kpenv # Main executable
├── install.sh # Installation script (Linux/macOS)
├── install.ps1 # Installation script (Windows)
├── uninstall.sh # Uninstallation script (Linux/macOS)
├── uninstall.ps1 # Uninstallation script (Windows)
├── README.md # This file
├── LICENSE # MIT License
├── DISTRIBUTION_STRATEGY.md # Distribution plan
├── composer.json # PHP package metadata
├── completions/
│ ├── kpenv.bash # Bash completion script
│ └── _kpenv # Zsh completion script
├── docs/
│ ├── prd.md # Product Requirements Document
│ └── planning/ # Planning Framework
│ ├── FRAMEWORK.md
│ ├── implementation-plan.md
│ ├── session-log.md
│ └── decisions.md
├── examples/
│ └── .env.example # Example environment file
└── tests/ # Future: PHPUnit tests
~/.kpenv/
└── config.json # User configuration
your-project/
└── .kpenv.json # Project configuration (created by init)
Environment files are stored in KeePass as entry notes:
KeePass Database
└── my-project/
├── development (entry)
│ └── Notes: [.env content]
├── staging (entry)
│ └── Notes: [.env.staging content]
└── production (entry)
└── Notes: [.env.production content]
The tool automatically detects your project name from the directory path:
Current directory: /home/username/dev/my-awesome-project/
Project name: my-awesome-project
KeePass entry: my-awesome-project/development
✅ Password Handling
- Never stored in code or config
- Read from stdin (hidden input) or
KEEPASS_PASSWORDenv var - Passed to
keepassxc-clivia stdin
✅ KeePass Database
- Encrypted with master password
- Supports key files and hardware keys (via KeePassXC)
✅ Safe Restore
- Won't overwrite existing
.envfiles - Creates
.env.fetchedfor manual review
- Don't commit
.envfiles to git (add to.gitignore) - Use strong KeePass master password
- Regularly backup your KeePass database
- Limit access to KeePass database file
# Check if installed
which keepassxc-cli
# Install KeePassXC (includes CLI)
# See Installation section above# Verify database path in kpenv configuration
# Default: ~/Documents/work-secrets.kdbx
# Create a new database if needed (via KeePassXC GUI)- Verify KeePass password is correct
- Check that backup was successful
- Verify project name matches (check
./kpenv backup-envoutput)
The tool requires your project to be under the configured $baseDevFolder:
// In kpenv
$baseDevFolder = "/home/username/dev";
// Your project must be at:
// /home/username/dev/{project-name}/- Core sync/backup/restore functionality
- Multi-environment support
- Interactive prompts
- Safe restore mode
- Auto project detection
- Installation script (
install.sh) - Configuration system (JSON-based)
-
kpenv initcommand - Auto .gitignore management
- Smart project name detection
- Global PATH installation
- List all backed-up projects (
listcommand) - Diff local vs KeePass (
diffcommand) - PHPUnit test suite
- CI/CD integration
- Composer package distribution
- Refactor to PSR-4 classes
- APT/Homebrew packages
- Plugin system for other backends (1Password, Bitwarden)
- GUI wrapper (Electron/Tauri)
This project uses the Planning Framework for structured development. See /docs/planning/FRAMEWORK.md for details.
# 1. Clone/copy to your dev environment
# 2. Review planning documents
cat docs/planning/implementation-plan.md
cat docs/planning/session-log.md
# 3. Make changes
# 4. Update planning docs
# 5. Run tests (when implemented)
# 6. Submit PRMIT License - see LICENSE file
Created by Human + Claude collaboration as part of the VAT Event Classifier project.
Extracted to standalone tool: 2025-11-05
- Planning Framework:
/docs/planning/FRAMEWORK.md - PRD:
/docs/prd.md - Session Log:
/docs/planning/session-log.md - KeePassXC: https://keepassxc.org/
- KeePassXC CLI Docs: https://keepassxc.org/docs/KeePassXC_UserGuide.html#_command_line_interface
Made with ❤️ and AI assistance