Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dotenv: support export declarations in .env files #1226

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/modules/integrations/dotenv.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ pkgs, config, lib, self, ... }:
{ config, lib, self, ... }:

let
cfg = config.dotenv;
Expand All @@ -9,12 +9,11 @@ let

parseLine = line:
let
parts = builtins.match "([^[:space:]=#]+)[[:space:]]*=[[:space:]]*(.*)" line;
parts = builtins.match "([[:space:]]*export[[:space:]]+)?([^[:space:]=#]+)[[:space:]]*=[[:space:]]*(.*)" line;
in
if (!builtins.isNull parts) && (builtins.length parts) == 2 then
{ name = builtins.elemAt parts 0; value = builtins.elemAt parts 1; }
else
null;
if parts != null && builtins.length parts == 3
then { name = builtins.elemAt parts 1; value = builtins.elemAt parts 2; }
else null;

parseEnvFile = content: builtins.listToAttrs (lib.filter (x: !builtins.isNull x) (map parseLine (lib.splitString "\n" content)));

Expand Down
11 changes: 8 additions & 3 deletions tests/dotenv/.setup.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
echo "{ env.LOCAL = \"1\";}" > devenv.local.nix
echo "FOO=1\nBAR=2\nBAZ=3" > .env
echo "BAZ=5" > .env.bar
echo '{ env.LOCAL = "1";}' > devenv.local.nix
cat <<EOF > .env
FOO=1
BAR=2
BAZ=3
export CHAZ=4
EOF
echo 'BAZ=5' > .env.bar
1 change: 1 addition & 0 deletions tests/dotenv/.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
set -ex
env | grep FOO=1
env | grep BAR=1
env | grep CHAZ=4
env | grep BAZ=5
Loading