Flakes: `devShell` and `shellHook`?

I am attempting to use flakes to maintain development shells for different projects that are not using Nix. Flakes feel ideal for this because I can maintain a flake per project in my own github repositories and use them as needed.

The problem I am running into is that nix develop does not seem to run the commands listed in the shellHook option of, e.g., mkShell:

{
  description = "A very basic flake";

  outputs = { self, nixpkgs }: {
    devShell =
      let
        pkgs = nixpkgs.legacyPackages.x86_64-linux;
      in pkgs.mkShell {
        buildInputs = [
          pkgs.cc.cc.lib
        ];
        shellHook = ''
          echo hi
          LD_LIBRARY_PATH=${pkgs.cc.cc.lib}/lib/
        '';
      };
  };
}

When I run nix develop, I get a shell but I don’t see hi echoed. (My ultimate reason for wanting to do this is to get libstdc++.so into my library path, as detailed here: Packaging/Quirks and Caveats - NixOS Wiki)

For reference, @jacg asked about something related in this topic: Help making a flake (for Geant4 (particle physics framework) examples) - #4 by jacg but AFAIK he never received an answer.

Thanks!

roni

2 Likes

That doesn’t sound normal. nix develop should run the commands specified with shellHook.

I tried your flake and it worked after some modifications:

# flake.nix
{
  description = "A very basic flake";

  outputs = { self, nixpkgs }: {
    devShell.x86_64-linux =
      let
        pkgs = nixpkgs.legacyPackages.x86_64-linux;
      in pkgs.mkShell {
        buildInputs = [
          pkgs.gcc.cc.lib
        ];
        shellHook = ''
          echo hi
          LD_LIBRARY_PATH=${pkgs.gcc.cc.lib}/lib/
        '';
      };
  };
}

I had to replace pkgs.cc with pkgs.gcc, since the former doesn’t seem to exist on my system. I also changed the output from devShell to devShell.x86_64-linux. This is what I get when I do nix develop:

hi

[...]$ echo $LD_LIBRARY_PATH
/nix/store/50msfhkz5wbyk8i78pjv3y9lxdrp7dlm-gcc-10.3.0-lib/lib/

I also tried to answer one of the questions there, but I am not sure if that was the one you are referring.

5 Likes

THANK YOU!

I realized after posting my question that the output needed the platform bit, but by that time I had removed the shellHook spec from my flake and sort of forgot about it. After making that change and the others you suggested, I see that things are working precisely as I had originally expected. Excellent!

Thanks also for answering the questions in that other thread.

roni