[doc] Which parameters do nix-build and nix-instatiate pass when evaluating a derivation?

I copied and adapted an example from the documentation on how to use the fetchGit fetcher.

{ stdenv, fetchgit }:

stdenv.mkDerivation {
  name = "simple-dlna-browser";
  src = fetchgit {
    url = "https://github.com/javier-lopez/learn.git";
    sparseCheckout = [
      "sh/tools/simple-dlna-browser"
    ];
    sha256 = "0000000000000000000000000000000000000000000000000000";
  };
  installPhase = ''
    mkdir $out/bin
    chmod +x sh/tools/simple-dlna-browser
    cp sh/tools/simple-dlna-browser $out/bin/
  '';
}

But it fails

$ nix-build simple-dlna-browser.nix 
error: cannot evaluate a function that has an argument without a value ('stdenv')

       Nix attempted to evaluate a function as a top level expression; in
       this case it must have its arguments supplied either by default
       values, or passed explicitly with '--arg' or '--argstr'. See
       https://nixos.org/manual/nix/stable/expressions/language-constructs.html#functions.

       at /home/nixos/nixos/derivations/scripts/simple-dlna-browser.nix:1:3:

            1| { stdenv, fetchgit }:
             |   ^
            2|

So I went to the nix-build documentation and nix-instantiate documentation, but it doesn’t explain which parameters it passes when evaluating the expression. I assume pkgs but what else? Is this documented somewhere?

It doesn’t pass any parameters unless specifically given on the command line, it just passes an empty attrset if the file contains a function.

For a derivation file formatted for callPackage like this, you’ll need to invoke that, either on the command line or with a default.nix to glue it together for nix-build:
nix-build -E '(import <nixpkgs> {}).callPackage ./foo.nix {}'

1 Like

Thanks. I create an issue on github to improve the documentation: nix-build is missing which parameters is passes expressions · Issue #7544 · NixOS/nix · GitHub