devenv icon indicating copy to clipboard operation
devenv copied to clipboard

`.env` are not recognised when dotenv is enabled.

Open Eveeifyeve opened this issue 1 year ago • 4 comments

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 avatar Jul 05 '24 06:07 Eveeifyeve

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

domenkozar avatar Jul 05 '24 11:07 domenkozar

:(

Eveeifyeve avatar Jul 05 '24 12:07 Eveeifyeve

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

domenkozar avatar Jul 05 '24 12:07 domenkozar

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.

Eveeifyeve avatar Jul 05 '24 12:07 Eveeifyeve

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

JenSeReal avatar Jul 30 '24 06:07 JenSeReal

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 avatar Jul 31 '24 09:07 Eveeifyeve

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 avatar Jul 31 '24 13:07 Eveeifyeve

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 avatar Jul 31 '24 13:07 Eveeifyeve

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.

Eveeifyeve avatar Aug 01 '24 07:08 Eveeifyeve

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)
  '';
}

chitter99 avatar Aug 08 '24 14:08 chitter99

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.

Eveeifyeve avatar Aug 11 '24 14:08 Eveeifyeve

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.

Doesn't work, look it's not worth getting this working as there is agenix-shell so thanks for your help, but I think this is not worth it.

Eveeifyeve avatar Sep 04 '24 06:09 Eveeifyeve