Nix-shell, C++ developer environments, custom libraries and pkg-config

Try this @jkt:

  1. Create a shell.nix inside your source code:
{ pkgs ? import <nixpkgs> { } }:

pkgs.mkShell {
  name = "cpp_project";
  buildInputs = with pkgs; [
    cmake
    ninja
    swig
    pcre
    bison
    flex
    openssl
  ];

  nativeBuildInputs = [ pkgs.pkg-config ];

  shellHook = ''
    export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:$HOME/project/libmanolo/"
  '';
}

Change buildInputs to your needs. Keep pkgs.pkg-config in nativeBuildInputs. In shellHook you put any variable you want to export or some commands you want to run on the creation of the new shell. In this example I’m extending $PKG_CONFIG_PATH.

  1. Inside the folder with the shell.nix file run the command nix-shell.

  2. If you print PKG_CONFIG_PATH it’ll show you that PKG_CONFIG_PATH was extended with $HOME/project/libmanolo/.

echo $PKG_CONFIG_PATH 
/nix/store/izfyh5hr9as4ihx2vwi2yrnkiz6gn4qi-pcre-8.44-dev/lib/pkgconfig:/nix/store/lwcrmj44j6s5ww3j0ybar2jc7kf9ddzq-openssl-1.1.1g-dev/lib/pkgconfig:/nix/store/izfyh5hr9as4ihx2vwi2yrnkiz6gn4qi-pcre-8.44-dev/lib/pkgconfig:/nix/store/lwcrmj44j6s5ww3j0ybar2jc7kf9ddzq-openssl-1.1.1g-dev/lib/pkgconfig:/home/thiago/project/libmanolo/
1 Like