3

I have a derivation (default.nix) defined as:

with import <nixpkgs> {};
let
  version = "7.5.1";
in  
stdenv.mkDerivation {
  name = "gurobi-${version}";
  src = fetchurl {
    url = http://packages.gurobi.com/7.5/gurobi7.5.1_linux64.tar.gz;
    sha256 = "7f5c8b0c3d3600ab7a1898f43e238a9d9a32ac1206f2917fb60be9bbb76111b6";
  };
  installPhase = ''
    cp -R linux64 $out
    patchelf --set-interpreter \
      ${stdenv.glibc}/lib/ld-linux-x86-64.so.2 $out/bin/*
    patchelf --set-rpath ${stdenv.glibc}/lib $out/bin/*
  '';
  GUROBI_HOME = "$out";
}

Aside: even when I have cp -R linux64 $out/, it seems the contents of linux64 are copied into $out, rather than linux64 itself being copied into $out ... which seems non-standard, I think.

A second file (gurobi-shell.nix) is:

with import <nixpkgs> {};
let
  myGurobi = (import ./default.nix);
in stdenv.mkDerivation  {
  name = "gurobi-shell";
  buildInputs = [ myGurobi ];
  shellHook = ''
   export GUROBI_HOME="${myGurobi.GUROBI_HOME}"
   export GUROBI_PATH="${myGurobi.GUROBI_HOME}"
  '';
}  

When I run nix-shell gurobi-shell.nix and then echo $GUROBI_HOME, I get: /nix/store/jsy0q02hyprz6mllblfb0gim3l8077d8-gurobi-shell

However, I expect and want it to be: /nix/store/s2kj78cnpnnbwr6q8qylg0n02m0sm32a-gurobi-7.5.1

EDIT: I guess that this may have something to do with lazy evaluation, but am not sure. In any case, I found a workaround, which is to use myGurobi.out instead of myGurobi.GUROBI_HOME.

bbarker
  • 7,931
  • 5
  • 30
  • 44

1 Answers1

1

To begin with, as an answer to your aside: when the installPhase is run, $out has not been created yet. If the destination does not exist, cp will copy the the directory linux64 to create the directory named with the value of $out, and so the contents of $out will be the same as the contents of linux64. You might want to mkdir -p $out at the beginning of your installPhase if you expect $out to exist as a directory.

For the main part of your question: "$out" in your GUROBI_HOME setting is setting GUROBI_HOME to the literal value $out, not to the expanded output path of the derivation. When you reference ${myGurobi.GUROBI_HOME} in your shellHook, this is expanding to export GUROBI_HOME="$out". Since nix-shell is expected to recreate the environment used when building the derivation, $out is set to the output directory of the gurobi-shell derivation, which is why you are getting it set to the wrong value.

Peter Amidon
  • 1,435
  • 5
  • 14