DEV Community

Cover image for Turv: A Simple and Secure Way to Manage Environment Variables per Directory ๐Ÿ› ๏ธ
Patrick P
Patrick P

Posted on

Turv: A Simple and Secure Way to Manage Environment Variables per Directory ๐Ÿ› ๏ธ

Introduction 

If youโ€™ve ever switched between projects with different configurations, youโ€™ve probably run into conflicts with environment variables. Maybe youโ€™re using different Terraform versions, managing multiple Git credentials, or handling cloud provider configurations. Manually setting and unsetting environment variables can be a painโ€”this is where Turv comes in.

This constant reconfiguration creates friction and introduces the risk of using incorrect settings at crucial moments. 

Turv automatically manages environment variables, aliases, and tool configurations based on your current directory, making project switching seamless. Unlike similar tools, Turv emphasizes security through an approval system that prevents unintended execution of environment files.

Turv is a directory-based environment loader that automatically sets environment variables when you enter a project directory and removes them when you leave. Inspired by tools like direnv, autoenv, and dirsh, Turv is built with security, simplicity, and flexibility in mind. 


Key Features 

  • โœ… Comprehensive Environment Handling โ€“ Variables, aliases, and functions are loaded when entering a directory and unloaded when leaving.
  • ๐Ÿ”— Tool Compatibility โ€“ Works alongside zoxide, broot, Vault CLI, and 1Password CLI.
  • ๐Ÿ” Security-First Design โ€“ Prompts for approval before sourcing new environment files.
  • ๐Ÿ”„ Change Detection โ€“ Requires re-approval if environment files are modified.
  • โš™๏ธ Minimal Dependencies โ€“ Works with standard utilities like jq or yq.
  • ๐Ÿ–ฅ๏ธ Shell Compatibility โ€“ Supports both Bash and Zsh.

Practical Use Cases 

Here's where Turv really shines:

  • โšก Project-Specific Aliases โ€“ Load custom shortcuts and functions that make sense only within specific projects.
  • ๐Ÿ”‘ Authentication Context โ€“ Switch between different Vault or 1Password CLI profiles automatically.
  • ๐Ÿš€ Integration with Navigation Tools โ€“ Works perfectly alongside zoxide or broot for seamless directory changes.
  • ๐ŸŒณ Git Worktrees โ€“ Each worktree can have its own environment configuration.
  • ๐Ÿ“ Custom Shell Functions โ€“ Define project-specific functions that only exist when needed.
  • ๐Ÿ—๏ธ Version Management โ€“ Automatically switch between different versions of Node.js, Python, or other tools as you move between project directories.

Examples:

Some private pet project:

pet-project-envrc

One of many different work projects

work-envrc
work-additional-file

As you can see, at least the Gitlab token is clashing. By combining multiple tools, this is no longer a concern and by using Turv, all of this is loaded automatically.


How Turv Works 

1๏ธโƒฃ Enter a directory with an environment file (default: .envrc), and Turv prompts for approval before sourcing it.
2๏ธโƒฃ If approved, Turv loads the environment variables, aliases, and functions automatically.
3๏ธโƒฃ When leaving the directory, Turv unloads those configurations to prevent conflicts.
4๏ธโƒฃ If the .envrc file changes, Turv detects modifications and asks for re-approval.

This approach combines convenience with security: you get automated configuration without risking unintended execution. 

When you navigate to this directory, Turv will automatically load these aliases and exports, allowing you to use the correct tool versions without manual configuration. 


The Security Advantage 

Unlike some alternatives, Turv's built-in approval system protects you by:

1๏ธโƒฃ Never executing environment files without explicit approval.
2๏ธโƒฃ Storing approval status securely using JSON/YAML.
3๏ธโƒฃ Detecting file changes to prevent unauthorized modifications.
4๏ธโƒฃ Allowing you to inspect files before approval.


Getting More From Turv 

Advanced users can extend Turv by implementing custom _load and _unload functions in their environment files, enabling project-specific setup and teardown operations that go beyond simple aliases and exports. ๐ŸŽฏ


๐Ÿ’ก Try it out today:
GitLab Repository
Github Mirror

Credits & Inspiration ๐ŸŒ

Turv is inspired by the following projects:

  • ๐Ÿ”— direnv โ€“ A shell extension for managing project-based environments.
  • ๐Ÿ”— autoenv โ€“ A tool for per-directory shell environment settings.
  • ๐Ÿ”— dirsh โ€“ A concept for automatic directory-based environment loading.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post