0

here is my default.nix:

with import <nixpkgs> {}; {
  cimgEnv = stdenv.mkDerivation {
  name = "cimgdev";
  buildInputs = [ pkgconfig stdenv cimg xorg.libX11 boost jdk gradle (opencv4.override { enableGtk2 = true; })];
};
environment.variables = rec {
  LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/nix/store/j71chi4b06bh8rpd5611d6q8asrwf9z6-opencv-4.7.0/lib/";
};
}

but when after nix-shell i do a echo $LD_LIBRARY_PATH

i have nothing just an empty string

Regards

1

1 Answer 1

1

What you likely want is this default.nix:

let
  pkgs = import <nixpkgs> {};
  opencv4 = pkgs.opencv4.override {enableGtk2 = true;};
in
  pkgs.stdenv.mkDerivation {
    name = "cimgdev";
    buildInputs = with pkgs; [pkgconfig stdenv cimg xorg.libX11 boost jdk gradle opencv4];
    shellHook = ''
      export LD_LIBRARY_PATH="${opencv4}/lib:$LD_LIBRARY_PATH"
    '';
  }

If you want to know why this works and your code doesn't, read on:

A derivation in Nix is essentially just a set of instructions, which tells Nix how to build a package. A derivation consists of a name, a builder which is run during the build process, and a system where it can be built on. It may also contain any other attributes, which are passed to the builder as environment variables.

nix-shell takes a derivation (in default.nix or shell.nix) as input, and runs an interactive shell the same way the builder would be called, so with all the attributes exposed as env vars. It additionally reads the shellHook and executes the commands in there before running the shell.

The reason we can't directly set LD_LIBRARY_PATH without a shellHook is that it'd overwrite any previous values, instead of adding to it.

I also changed your code to reference the opencv4 package in LD_LIBRARY_PATH (with your override) instead of directly referencing the store path, because with your method it'd break if the package (or one of its dependencies) ever got updated.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.