Packages installed via direnv flake don't get reused

i have two folders that use direnev with flake. They have some common packages listed. I though nix was going to download common packages only once, but it turns out it re-downloads for each folder.

Is it possible to ask nix to reuse such packages?

my direnv comes from github.com/nix-community/nix-direnv

And for folder1 I use this flake.nix

{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  inputs.flake-utils.url = "github:numtide/flake-utils";

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let pkgs = nixpkgs.legacyPackages.${system};
      in {
        devShells.default = pkgs.mkShell {
          buildInputs = with pkgs; [
            ansible_2_13
            nixfmt
          ];
        };
      });
}

And folder2’s flake.nix

{
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
  inputs.flake-utils.url = "github:numtide/flake-utils";

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let pkgs = nixpkgs.legacyPackages.${system};
      in {
        devShells.default = pkgs.mkShell {
          buildInputs = with pkgs; [
            ansible_2_13
          ];
        };
      });
}

Check if the two flakes use the same version of nixpkgs. The flake.lock will probably differ a bit.

One way to avoid this is to omit the nixpkgs version and rely on the host flake registry. Maybe you can symlink your flake.lock, too.

The idea is that you want to ensure consistent use of a specific nixpkgs version, so that your project always works. Flakes sometimes deliberately use subtly different packages for this reason - presumably some library ansible depends on was updated and therefore the package can’t be the same.

Version mismatch was the cause. Thanks a lot!

I now use registry pinning to make sure they use the same version. Hope that works.