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

.env are not recognised when dotenv is enabled. #1321

Open
Eveeifyeve opened this issue Jul 5, 2024 · 11 comments
Open

.env are not recognised when dotenv is enabled. #1321

Eveeifyeve opened this issue Jul 5, 2024 · 11 comments
Labels
bug Something isn't working

Comments

@Eveeifyeve
Copy link

Eveeifyeve commented Jul 5, 2024

Describe the bug
When using dotenv it doesn't grab dotenv files.

Things I have tried to fix it

  • I have tried enabling it and specify it but nothing works.
  • I have tried it without direnv using nix develop --impure to see if direnv was the issue.
  • Swtiching Branches of nixpkgs repo from NixOS/nixpkgs/nixos-unstable to NixOS/nixpkgs/nixpkgs-unstable & NixOS/nixpkgs/nixpkgs-unstable to cachix/devenv-nixpkgs/rolling

To reproduce
https://gist.github.com/Eveeifyeve/fb7d6ce15e4e632e69dfe129023cac47

Logs:

direnv: loading ~/projects/TeaClient/API/.envrc                                                                                                                                                                                   
direnv: using flake . --impure
direnv: ([/nix/store/q71rzcnj4kpv6pp7alasws5si5dp216c-direnv-2.34.0/bin/direnv export zsh]) is taking a while to execute. Use CTRL-C to give up.
direnv: nix-direnv: Renewed cache
💡 The dotenv file '.env' was not found.

   To create this file, you can copy the example file:

   $ cp .env.example .env

direnv: export +AR +AS +CARGO_INSTALL_ROOT +CC +CFLAGS +CONFIG_SHELL +CXX +DATABASE_URL +DEVENV_DOTFILE +DEVENV_PROFILE +DEVENV_ROOT +DEVENV_RUNTIME +DEVENV_STATE +GIT_EXTERNAL_DIFF +IN_NIX_SHELL +LD +LD_DYLD_PATH +MACOSX_DEPLOYMENT_TARGET +NIX_BINTOOLS +NIX_BINTOOLS_WRAPPER_TARGET_HOST_aarch64_apple_darwin +NIX_CC +NIX_CC_WRAPPER_TARGET_HOST_aarch64_apple_darwin +NIX_CFLAGS_COMPILE +NIX_DONT_SET_RPATH +NIX_DONT_SET_RPATH_FOR_BUILD +NIX_ENFORCE_NO_NATIVE +NIX_HARDENING_ENABLE +NIX_IGNORE_LD_THROUGH_GCC +NIX_LDFLAGS +NIX_LDFLAGS_FOR_BUILD +NIX_NO_SELF_RPATH +NIX_PKG_CONFIG_WRAPPER_TARGET_HOST_aarch64_apple_darwin +NIX_STORE +NM +PATH_LOCALE +PKG_CONFIG +RANLIB +RUSTDOCFLAGS +RUSTFLAGS +RUST_SRC_PATH +SIZE +SOURCE_DATE_EPOCH +STRINGS +STRIP +XDG_DATA_DIRS +ZERO_AR_DATE +__darwinAllowLocalNetworking +__impureHostDeps +__propagatedImpureHostDeps +__propagatedSandboxProfile +__sandboxProfile +cmakeFlags +configureFlags +mesonFlags +name +system -HOST_PATH -NIX_BUILD_CORES -__structuredAttrs -buildInputs -buildPhase -builder -depsBuildBuild -depsBuildBuildPropagated -depsBuildTarget -depsBuildTargetPropagated -depsHostHost -depsHostHostPropagated -depsTargetTarget -depsTargetTargetPropagated -doCheck -doInstallCheck -dontAddDisableDepTrack -nativeBuildInputs -out -outputs -patches -phases -preferLocalBuild -propagatedBuildInputs -propagatedNativeBuildInputs -shell -shellHook -stdenv -strictDeps ~PATH ~XPC_SERVICE_NAME

Well I expect to happen is all of my .env variables to be available in the dev shell.

Version

I am using flakes and it's on version 1.0.7.

@Eveeifyeve Eveeifyeve added the bug Something isn't working label Jul 5, 2024
@domenkozar
Copy link
Member

We should error out since this feature doesn't work with Flakes.

@Eveeifyeve
Copy link
Author

:(

@domenkozar
Copy link
Member

@Eveeifyeve why do you need to use flakes? Maybe there's a way to use devenv cli.

@Eveeifyeve
Copy link
Author

Well here is the thing I have all of my devenv in flakes and flakes are a much easier way to write than a yaml file.

@JenSeReal
Copy link

JenSeReal commented Jul 30, 2024

If you are still looking for a solution:

  • My problem was that my .env was in my .gitignore - so it wasn't tracked by git, so nix can't track it either
  • In your .envrc (if you are using direnv) you can call dotenv, which loads even .env files that are not tracked by git
if ! has nix_direnv_version || ! nix_direnv_version 2.2.1; then
  source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.2.1/direnvrc" "sha256-zelF0vLbEl5uaqrfIzbgNzJWGmLzCmYAkInj/LNxvKs="
fi

dotenv
watch_file flake.nix
watch_file flake.lock
if ! use flake . --impure
then
  echo "devenv could not be built. The devenv environment was not loaded. Make the necessary changes to devenv.nix and hit enter to try again." >&2
fi

@Eveeifyeve
Copy link
Author

If you are still looking for a solution:

  • My problem was that my .env was in my .gitignore - so it wasn't tracked by git, so nix can't track it either

  • In your .envrc (if you are using direnv) you can call dotenv, which loads even .env files that are not tracked by git


if ! has nix_direnv_version || ! nix_direnv_version 2.2.1; then

  source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.2.1/direnvrc" "sha256-zelF0vLbEl5uaqrfIzbgNzJWGmLzCmYAkInj/LNxvKs="

fi



dotenv

watch_file flake.nix

watch_file flake.lock

if ! use flake . --impure

then

  echo "devenv could not be built. The devenv environment was not loaded. Make the necessary changes to devenv.nix and hit enter to try again." >&2

fi

I will get around trying the direnv method that you have suggested I will let you know asap how it goes.

@Eveeifyeve
Copy link
Author

Eveeifyeve commented Jul 31, 2024

If you are still looking for a solution:

  • My problem was that my .env was in my .gitignore - so it wasn't tracked by git, so nix can't track it either
  • In your .envrc (if you are using direnv) you can call dotenv, which loads even .env files that are not tracked by git

if ! has nix_direnv_version || ! nix_direnv_version 2.2.1; then

  source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.2.1/direnvrc" "sha256-zelF0vLbEl5uaqrfIzbgNzJWGmLzCmYAkInj/LNxvKs="

fi



dotenv

watch_file flake.nix

watch_file flake.lock

if ! use flake . --impure

then

  echo "devenv could not be built. The devenv environment was not loaded. Make the necessary changes to devenv.nix and hit enter to try again." >&2

fi

I will get around trying the direnv method that you have suggested I will let you know asap how it goes.

Update I have tried it still doesn't work. I have specified in one project with .env.development and .env.production I can try with another project and see what happens.

It seems the dotenv in .envrc adds it to direnv not devenv I am talking specifically about devenv.

It seems to be a git issue with nix not including files that are in gitignore like .env.

@Eveeifyeve
Copy link
Author

Eveeifyeve commented Jul 31, 2024

We should error out since this feature doesn't work with Flakes.

It actually does, it's because nix is not including files that are in gitignore.

@Eveeifyeve
Copy link
Author

Eveeifyeve commented Aug 1, 2024

Also the .env files thing is really bad practises as it gets included in nix store on eval time.

This is why stuff like agenix that secure it that way if a hacker get access to your file system and transfer the files to their computer, they won't be able to decrypt without being on the actual system.

@chitter99
Copy link

Another options is to use enterShell to load the .env dynamically. So it does not need to be in the nix store and not stracked by git.

{
  enterShell = ''
    [ ! -f .env ] || export $(grep -v '^#' .env | xargs)
  '';
}

@Eveeifyeve
Copy link
Author

Another options is to use enterShell to load the .env dynamically. So it does not need to be in the nix store and not stracked by git.

{
  enterShell = ''
    [ ! -f .env ] || export $(grep -v '^#' .env | xargs)
  '';
}

I will try this in a few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants