Hyprx.DotEnv 0.0.0-alpha.3

This is a prerelease version of Hyprx.DotEnv.
dotnet add package Hyprx.DotEnv --version 0.0.0-alpha.3
                    
NuGet\Install-Package Hyprx.DotEnv -Version 0.0.0-alpha.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hyprx.DotEnv" Version="0.0.0-alpha.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hyprx.DotEnv" Version="0.0.0-alpha.3" />
                    
Directory.Packages.props
<PackageReference Include="Hyprx.DotEnv" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Hyprx.DotEnv --version 0.0.0-alpha.3
                    
#r "nuget: Hyprx.DotEnv, 0.0.0-alpha.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Hyprx.DotEnv@0.0.0-alpha.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Hyprx.DotEnv&version=0.0.0-alpha.3&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Hyprx.DotEnv&version=0.0.0-alpha.3&prerelease
                    
Install as a Cake Tool

Hyprx.DotEnv

Overview

Parse, read, and write dotenv (.env) files.

This library can:

  • Perserve the order of the environment variables defined in the file.
  • Parse and write comments.
  • Load multiple files.
  • Enables other multiline delimiters using {}, ---, or `.
  • Avoids reflection to help with AOT scenarios.

For variable expansion and command substitution use the "Hyprx.DotEnv.Expansion" library.

The Serializer converts to and from a new type called DotEnvDocument and OrderedDictionary<string,string>.

Usage

using Hyprx.DotEnv;


// load directly to environment variables
DotEnvLoader.Load(new DotEnvLoadOptions() {
    Files = ["./path/.env", "./path/prod.env"]
});

// parse multiple files and then load.
var doc3 = DotEnvLoader.Parse(new DotEnvLoadOptions() {
    Files = ["./path/.env", "./path/prod.env"]
});

// do something with values.
doc3["SOME_VAR"] = "test";

// then load to environment variables and 
// overwrite the existing ones.
DotEnvLoader.Load(doc3, true);

// use the serializer directly.
var doc = DotEnvSerializer.DeserializeDocument(
"""
# COMMENT
KEY=VALUE

MULTILINE=" my 
value
spans
multiple
lines"
single='single value'
""");

foreach(var node in doc) 
{
    if (node is DotEnvEmptyLine)
        Console.WriteLine("");

    if (node is DotEnvComment comment)
        Console.WriteLine($"#{comment.Value}");

    if (node is DotEnvEntry entry)
        Console.WriteLine($"{entry.Key}=\"{entry.Value}\"");
}

var content = DotEnvSerializer.SerializeDocument(doc);
Console.WriteLine(content);

// use the DotEnvDocument to create a new file.

// 
// # NODE VARS
// NODE_ENV=production
// OTHER_VAR=test
var doc2 = new DotEnvDocument();
doc2.AddEmptyLine();
doc2.AddComment("NODE VARS");
doc2.Add("NODE_ENV", "production");
doc2["OTHER_VAR"] = "test";

var content2 = DotEnvSerializer.SerializeDocument(doc);
Console.WriteLine(content2);
File.WriteAllText(".env", content2);

Escaping

Escaping only works when using double quotes or backticks (`).

  • \b backspace
  • \n newline
  • \r carriage return
  • \f form feed
  • \v vertical tab
  • \t tab
  • \" escape double quote
  • \\' escape single quote
  • \uFFFF escape unicode characters.

Defaults

By default, only basic dotenv parsing is performed. There are options to enable using bacticks, json, and yaml delimiters for multiline value parsing to make things easier.

AllowJson

The AllowJson option will allow using {} to notate the start and end of json in an env file.

MY_JSON={
    "one": 1,
    "person": "bob"
    "nested": {
        "person": "alice"
    }
}

The above value will be capture as a multiline string.

AllowYaml

The AllowYaml option will allow using --- to notate the start and end of yaml in an env file.

MY_YAML=---
one: 1
person: bob
nested:
  person: alice
---
NEXT_SECRET="test"

AllowBackticks

The AllowBackticks options will allow using ``` to notate the start and end of a multiline value without using quotes so that single quotes and doubles may be used without the need to escape them. Backticks functions similarly to using double quotes.

MYVALUE=`this
will be "captured"`
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Hyprx.DotEnv:

Package Downloads
Hyprx.Rex.Console

The Hyprx.Rex.Console package enables you to create a cli task runner or a make-like experience in .NET within console projects or using `dotnet run app.cs` (single files).

Hyprx.DotEnv.Expansion

Expand variables and perform command substitution in dotenv (.env) files.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.0.0-alpha.3 120 7/10/2025
0.0.0-alpha.2 114 7/10/2025
0.0.0-alpha.1 122 6/25/2025
0.0.0-alpha.0 131 6/17/2025

# Hyprx.DotEnv Changelog

## 0.0.0-alpha.2

Moved variable expansion to new library and remove dependency
on Hyprx.Core.

Change deserialize dictionary to return an `OrderedDictionary<string, string>`
instead of `Dictionary<string, string>`.

Enable better escaping for double quoted values including unicode
characters, new lines, vertical tabs, and carriage returns.

Improve value termination for unquoted single line values. Instead of
terminating the value when the first space is found, all characters
on a single line is captures until a line break or comment is found. Then
the variable value removes any trailing whitespace characters starting
from the end.

The value already trims leading whitespace characters, so this
change only affects the end of the value.

## 0.0.0-alpha.0

An implementation of dotenv for parsing and writing .env files.

This library can:

- expand environment variables such as `MYVAR=${HOME}/.config`
- perserves the order of the environment variables defined in the file.
- can parse and write comments.
- can load multiple files and expand the environment variables.
- can handle extra parsing features such as json, yaml, and bacticks.  
- avoids reflection to help with AOT scenarios.