Correct way for adding dependencies to `buildRustPackage`?

Hello guys!
So I’ve got an understanding question.
There are multiple ways in buildRustPackage to declare the buildInputs and nativeBuildInputs dependencies but what is the right way?

Version 1

Here I’m declaring the dependencies through the function arguments.

# here I'm listing the dependencies here
{ lib, fetchFromGitHub, rustPlatform, dep1, dep2, ... }:

rustPlatform.buildRustPackage rec {
  pname = "ripgrep";
  version = "12.1.1";

  buildInputs = [
    dep1
  ];

  nativeBuildInputs = [
    dep2
  ];

  src = fetchFromGitHub {
    owner = "BurntSushi";
    repo = pname;
    rev = version;
    hash = "sha256-+s5RBC3XSgb8omTbUNLywZnP6jSxZBKSS1BmXOjRF8M=";
  };

  cargoHash = "sha256-jtBw4ahSl88L0iuCXxQgZVm1EcboWRJMNtjxLVTtzts=";

  meta = with lib; {
    description = "A fast line-oriented regex search tool, similar to ag and ack";
    homepage = "https://github.com/BurntSushi/ripgrep";
    license = licenses.unlicense;
    maintainers = [];
  };
}

Version 2

In this version, I’m using pkgs to declare the dependencies.

{ lib, fetchFromGitHub, rustPlatform }:

rustPlatform.buildRustPackage rec {
  pname = "ripgrep";
  version = "12.1.1";

  buildInputs = with pkgs; [
    dep1
  ];

  nativeBuildInputs = with pkgs; [
    dep2
  ];

  src = fetchFromGitHub {
    owner = "BurntSushi";
    repo = pname;
    rev = version;
    hash = "sha256-+s5RBC3XSgb8omTbUNLywZnP6jSxZBKSS1BmXOjRF8M=";
  };

  cargoHash = "sha256-jtBw4ahSl88L0iuCXxQgZVm1EcboWRJMNtjxLVTtzts=";

  meta = with lib; {
    description = "A fast line-oriented regex search tool, similar to ag and ack";
    homepage = "https://github.com/BurntSushi/ripgrep";
    license = licenses.unlicense;
    maintainers = [];
  };
}

Is there a big difference between those two versions? Which should I prefer?
I was told on the unofficial nixos-discord server, that I shouldn’t mention pkgs here due to black magic of pkgs.callPackage and nativeBuildInputs and buildInputs so I’d like to know more about it.

Where is pkgs coming from in Version 2? I think if you try that, you’ll find it doesn’t actually work.

This manual page and the one that follows it, describes why the first version you showed is preferred. Indeed nixpkgs is designed around the callPackage pattern. Something that the manual gets into in Chapter 13, but I’m linking Chapter 12 as it also addresses more of how/why in terms of how to author a new package derivation.

2 Likes

Somewhere in the previous scope like:

{
  inputs = {
    nixpkgs.url = "...";
  };

  outputs = { nixpkgs, ...}:
  let
    pkgs = import nixpkgs {
        system = "x86_64-linux";
    };

    mkPkg = {rustPlatform, ...}: {
        buildInputs = with pkgs; [
            # ...
        };
    };
  in
  {
    packages."x86_64-linux".default = pkgs.callPackage mkPkg {};
  };
}

thank you!

1 Like