Referring to another derivation (dependency) in the build step

Let’s say some package foo depends on a package abc such that when foo is built, it expects abc to be under /usr/local/bin or at a location referred by an environment variable, ABC_BIN_DIR. How can one access the location of abc (under /nix/store/...) in order to set the ABC_BIN_DIR env var in the builder script.

The following derivation snippet can be used as an example:

with import <nixpkgs> {};

stdenv.mkDerivation {
  name = "foo";
  src = fetchFromGitHub {
    owner = "xyz";
    repo = "foo";
    rev = "zzz...";
    sha256 = "aaa...";
    fetchSubmodules = true;
  };
  buildInputs = [ (import ./abc.nix { }) ];
  builder = ./builder.sh;
}

Can abc's location be access via the outPath attribute (as suggested in NixOS - Nix Pills)?

Does one need to set an attribute such as in:

let
  abc =  import ./abc.nix { };
in
  stdenv.mkDerivation {
    # ...
   inherit abc;
  }

and then in builder.sh:

source $stdenv/setup

export ABC_BIN_DIR=$abc.outPath

when you interpolate a derivation, you get the store path:

  buildPhase = ''
    export ABC_BIN_DIR=${abc}/bin
  '';

I go over some basic nix usage in some of my videos:
https://www.youtube.com/watch?v=LiEqN8r-BRw

I’m planning to do a more “refined” series once the 20.09 release is done

3 Likes