<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Patrick P</title>
    <description>The latest articles on DEV Community by Patrick P (@ppfenning92).</description>
    <link>https://dev.to/ppfenning92</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2882182%2Fa1af5673-738f-42f3-9350-7a4ecc8225b9.jpeg</url>
      <title>DEV Community: Patrick P</title>
      <link>https://dev.to/ppfenning92</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ppfenning92"/>
    <language>en</language>
    <item>
      <title>Turv: A Simple and Secure Way to Manage Environment Variables per Directory 🛠️</title>
      <dc:creator>Patrick P</dc:creator>
      <pubDate>Wed, 19 Feb 2025 11:43:38 +0000</pubDate>
      <link>https://dev.to/ppfenning92/turv-a-simple-and-secure-way-to-manage-environment-variables-per-directory-47m5</link>
      <guid>https://dev.to/ppfenning92/turv-a-simple-and-secure-way-to-manage-environment-variables-per-directory-47m5</guid>
      <description>&lt;h2&gt;
  
  
  Introduction 
&lt;/h2&gt;

&lt;p&gt;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 &lt;strong&gt;Turv&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;This constant reconfiguration creates friction and introduces the risk of using incorrect settings at crucial moments. &lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

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




&lt;h2&gt;
  
  
  Key Features 
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;✅ &lt;strong&gt;Comprehensive Environment Handling&lt;/strong&gt; – Variables, aliases, and functions are loaded when entering a directory and unloaded when leaving.&lt;/li&gt;
&lt;li&gt;🔗 &lt;strong&gt;Tool Compatibility&lt;/strong&gt; – Works alongside &lt;code&gt;zoxide&lt;/code&gt;, &lt;code&gt;broot&lt;/code&gt;, &lt;code&gt;Vault CLI&lt;/code&gt;, and &lt;code&gt;1Password CLI&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;🔐 &lt;strong&gt;Security-First Design&lt;/strong&gt; – Prompts for approval before sourcing new environment files.&lt;/li&gt;
&lt;li&gt;🔄 &lt;strong&gt;Change Detection&lt;/strong&gt; – Requires re-approval if environment files are modified.&lt;/li&gt;
&lt;li&gt;⚙️ &lt;strong&gt;Minimal Dependencies&lt;/strong&gt; – Works with standard utilities like &lt;code&gt;jq&lt;/code&gt; or &lt;code&gt;yq&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;🖥️ &lt;strong&gt;Shell Compatibility&lt;/strong&gt; – Supports both Bash and Zsh.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Practical Use Cases 
&lt;/h2&gt;

&lt;p&gt;Here's where Turv really shines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;⚡ &lt;strong&gt;Project-Specific Aliases&lt;/strong&gt; – Load custom shortcuts and functions that make sense only within specific projects.&lt;/li&gt;
&lt;li&gt;🔑 &lt;strong&gt;Authentication Context&lt;/strong&gt; – Switch between different &lt;code&gt;Vault&lt;/code&gt; or &lt;code&gt;1Password CLI&lt;/code&gt; profiles automatically.&lt;/li&gt;
&lt;li&gt;🚀 &lt;strong&gt;Integration with Navigation Tools&lt;/strong&gt; – Works perfectly alongside &lt;code&gt;zoxide&lt;/code&gt; or &lt;code&gt;broot&lt;/code&gt; for seamless directory changes.&lt;/li&gt;
&lt;li&gt;🌳 &lt;strong&gt;Git Worktrees&lt;/strong&gt; – Each worktree can have its own environment configuration.&lt;/li&gt;
&lt;li&gt;📝 &lt;strong&gt;Custom Shell Functions&lt;/strong&gt; – Define project-specific functions that only exist when needed.&lt;/li&gt;
&lt;li&gt;🏗️ &lt;strong&gt;Version Management&lt;/strong&gt; – Automatically switch between different versions of &lt;code&gt;Node.js&lt;/code&gt;, &lt;code&gt;Python&lt;/code&gt;, or other tools as you move between project directories.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Examples:
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Some private pet project:
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8aq8le24265j97oewjh8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8aq8le24265j97oewjh8.jpg" alt="pet-project-envrc" width="800" height="319"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  One of many different work projects
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh685u79uwawpwzft2tyo.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh685u79uwawpwzft2tyo.jpg" alt="work-envrc" width="800" height="136"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5iki6m2htntwaxv7afil.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5iki6m2htntwaxv7afil.jpg" alt="work-additional-file" width="800" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  How Turv Works 
&lt;/h2&gt;

&lt;p&gt;1️⃣ &lt;strong&gt;Enter a directory&lt;/strong&gt; with an environment file (default: &lt;code&gt;.envrc&lt;/code&gt;), and Turv &lt;strong&gt;prompts for approval&lt;/strong&gt; before sourcing it.&lt;br&gt;
2️⃣ If approved, Turv &lt;strong&gt;loads the environment variables, aliases, and functions&lt;/strong&gt; automatically.&lt;br&gt;
3️⃣ When leaving the directory, Turv &lt;strong&gt;unloads those configurations&lt;/strong&gt; to prevent conflicts.&lt;br&gt;
4️⃣ If the &lt;code&gt;.envrc&lt;/code&gt; file changes, Turv &lt;strong&gt;detects modifications&lt;/strong&gt; and asks for re-approval.&lt;/p&gt;

&lt;p&gt;This approach combines convenience with security: you get automated configuration without risking unintended execution. &lt;/p&gt;

&lt;p&gt;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. &lt;/p&gt;




&lt;h2&gt;
  
  
  The Security Advantage 
&lt;/h2&gt;

&lt;p&gt;Unlike some alternatives, Turv's built-in approval system protects you by:&lt;/p&gt;

&lt;p&gt;1️⃣ &lt;strong&gt;Never executing environment files without explicit approval.&lt;/strong&gt;&lt;br&gt;
2️⃣ &lt;strong&gt;Storing approval status securely using JSON/YAML.&lt;/strong&gt;&lt;br&gt;
3️⃣ &lt;strong&gt;Detecting file changes to prevent unauthorized modifications.&lt;/strong&gt;&lt;br&gt;
4️⃣ &lt;strong&gt;Allowing you to inspect files before approval.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Getting More From Turv 
&lt;/h2&gt;

&lt;p&gt;Advanced users can extend Turv by implementing custom &lt;code&gt;_load&lt;/code&gt; and &lt;code&gt;_unload&lt;/code&gt; functions in their environment files, enabling project-specific setup and teardown operations that go beyond simple aliases and exports. 🎯&lt;/p&gt;




&lt;p&gt;💡 &lt;strong&gt;Try it out today&lt;/strong&gt;: &lt;br&gt;
&lt;a href="https://gitlab.com/patrick.pfenning.92/turv" rel="noopener noreferrer"&gt;GitLab Repository&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/ppfenning92/turv" rel="noopener noreferrer"&gt;Github Mirror&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Credits &amp;amp; Inspiration 🌍
&lt;/h3&gt;

&lt;p&gt;Turv is inspired by the following projects:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🔗 &lt;a href="https://github.com/direnv/direnv" rel="noopener noreferrer"&gt;direnv&lt;/a&gt; – A shell extension for managing project-based environments.&lt;/li&gt;
&lt;li&gt;🔗 &lt;a href="https://github.com/hyperupcall/autoenv" rel="noopener noreferrer"&gt;autoenv&lt;/a&gt; – A tool for per-directory shell environment settings.&lt;/li&gt;
&lt;li&gt;🔗 &lt;a href="https://blog.tarkalabs.com/dirsh-5d4650008c65" rel="noopener noreferrer"&gt;dirsh&lt;/a&gt; – A concept for automatic directory-based environment loading.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>devops</category>
      <category>tooling</category>
      <category>cli</category>
      <category>developer</category>
    </item>
  </channel>
</rss>
