Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a nix package when using buildPythonPackage?

Tags:

nix

The Python Part

I have a python application with multiple entrypoints, json_out and json_in. I can run them both with this default.nix

with import <nixpkgs> {};
(
  let jsonio = python37.pkgs.buildPythonPackage rec {
    pname = "jsonio";
    version = "0.0.1";
    src = ./.;
  };
  in python37.withPackages (ps: [ jsonio ])
).env

Like so:

$ nix-shell --run "json_out"
    { "a" : 1, "b", 2 }

$ nix-shell --run "echo { \"a\" : 1, \"b\", 2 } | json_in"
    keys: a,b
    values: 1,2

The System Part

I want to also invoke jq in the nix shell, like this:

$ nix-shell --run --pure "json_out | jq '.a' | json_in"

But I can't because it is not included. I know that I can include jq into the nix shell using this default.nix

with import <nixpkgs> {};

stdenv.mkDerivation rec {
  name = "jsonio-environment";
  buildInputs = [ pkgs.jq ];
}

And it works on its own:

$ nix-shell --run --pure "echo { \"a\" : 1, \"b\", 2 } | jq '.a'"
    { "a" : 1 }

But now I don't have my application:

$ nix-shell --run "json_out | jq '.a'"
    /tmp/nix-shell-20108-0/rc: line 1: json_out: command not found

The Question

What default.nix file can I provide that will include both my application and the jq package?

like image 938
MatrixManAtYrService Avatar asked Oct 19 '25 01:10

MatrixManAtYrService


2 Answers

My preferred way to achieve this is to use .overrideAttrs to add additional dependencies to the environment like so:

with import <nixpkgs> {};
(
  let jsonio = python37.pkgs.buildPythonPackage rec {
    pname = "jsonio";
    version = "0.0.1";
    src = ./.;
  };
  in python37.withPackages (ps: [jsonio ])
).env.overrideAttrs (drv: {
  buildInputs = [ jq ];
})
like image 68
Silvan Mosberger Avatar answered Oct 21 '25 13:10

Silvan Mosberger


I needed to:

  • provide the output of buildPythonPackage as part of the input of mkDerivation
  • omit the env. Based on a hint from an error message:

    Python 'env' attributes are intended for interactive nix-shell sessions, not for building!

Here's what I ended up with:

with import <nixpkgs> {};

let jsonio_installed = (
  let jsonio_module = (
    python37.pkgs.buildPythonPackage rec {
      pname = "jsonio";
      version = "0.0.1";
      src = ./.;
    }
  );
  in python37.withPackages (ps: [jsonio_module ])
);
in stdenv.mkDerivation rec {
  name = "jsonio-environment";
  buildInputs = [ pkgs.jq jsonio_installed ];
}
like image 35
MatrixManAtYrService Avatar answered Oct 21 '25 12:10

MatrixManAtYrService