Search code examples
nix

Nix: adding other derivation's subdirectory to PATH


I want to make a foo.nix file that when you nix-shell foo.nix and enter the sand-boxed shell, the PATH includes some other derivation's bin/ subdirectory. How can I do that?

--- minimal reproducible example ---

Following is my best attempt so far.

This is my current .nix file

let git = builtins.fetchGit {
        url = https://github.com/ingun37/haskell-script.git;
        rev = "e96ac79d6bf338c98697bb4ba510f22cb5f502ac";
    };
    haskellScriptD = import git {};
in  derivation {
    name = "a"; # giving any name because I won't actually build it.
    builder = "a"; # Same here
    system = builtins.currentSystem;
    script = haskellScriptD;
}

and this is what I run in order to run the executable in the derivative's bin/ subdirectory. You can see I have to append the path before the executable because I don't know how to configure PATH.

nix-shell a.nix --run "\$script/bin/json-generator"

What I want to run is

nix-shell a.nix --run json-generator

How can I do it?


Solution

  • Thanks to @DavidGrayson, I solved it by using stdenv.mkDerivative instead of derivative and including the derivative into buildInputs.

    This is the working script

    with import <nixpkgs> {};
    let git = builtins.fetchGit {
            url = https://github.com/ingun37/haskell-script.git;
            rev = "e96ac79d6bf338c98697bb4ba510f22cb5f502ac";
        };
        haskellScriptD = import git {};
    in stdenv.mkDerivation {
        name = "a";
        buildInputs = [ haskellScriptD ];
    }
    

    Now I can run

    nix-shell a.nix --run json-generator