Installing rPackages.data_table with zlib headers with default.nix and direnv

I have a multi-user installation of nix on ubuntu 22.04. I’m trying to develop an R package that depends on data.table , but am unable to install data.table with zlib headers so that I can use the fwrite function with gzip compression. Other solutions I’ve found say to add zlib and pkg-config using nix-shell, but how is this done when I’m trying to install it into a nix-shell with direnv:

Here is the default.nix I’m using with direnv :

with import <nixpkgs> {};

let
    rlibs = with rPackages; [
      R
      Rcpp
      data_table
      roxygen2
      pkgbuild
    ];

    _libs = with pkgs; [
        zlib
        ccls
        pkg-config
    ];

in mkShell {
    nativeBuildInputs = [
      rlibs
      _libs
    ];
}

Everything installs fine and runs, but when I call

fwrite(iris, "iris.txt.gz', compress="gzip") I get

Compression in fwrite uses zlib library. Its header files were not found at the time data.table was compiled. To enable fwrite compression, please reinstall data.table and study the output for further guidance.

At first I didn’t have zlib in default.nix so I added it and tried reinstalling. This required garbage collecting the full set of packages. Reinstaling did not solve the problem though. I don’t see any output to inspect either.

How do I specify zlib as a dependency? Also is there a way to re-install a package with nix? I can’t find any documentation online about it - any results are about reinstalling nix as a whole, not a single package.

UPDATE:
I’ve also tried

nix-shell -p pkg-config zlib R rPackages.data_table .

During the data.table install, there are messages that pkg-config was not found and data.table complains about not finding zlib headers. However, in that shell, running pkg-config --libs zlib gives

-L/nix/store/37a5krk4a1a8vhl93q2bm9nbv8hymyii-zlib-1.2.13/lib -lz

Thanks for identifying this issue, zlib support wasn’t correctly being enabled as a pkg-config dependency was introduced some time ago and wasn’t noticed. I’ve resolved this in rPackages.data_table: add required pkg-config dependency by jbedo · Pull Request #236549 · NixOS/nixpkgs · GitHub.

FYI though, the way to do something like this in your shell expression is to override the data_table derivation with an overlay. Here’s an example adding the missing pkg-config dependency, modifying what you have above:

with import <nixpkgs> {
  overlays = [
    (_: super: {
      rPackages = super.rPackages.override {
        overrides = {
          data_table = super.rPackages.data_table.overrideDerivation (old: {
            nativeBuildInputs = old.nativeBuildInputs ++ [super.pkg-config];
          });
        };
      };
    })
  ];
}; let
  rlibs = with rPackages; [
    R
    Rcpp
    data_table
    roxygen2
    pkgbuild
  ];
in
  mkShell {
    nativeBuildInputs = [
      rlibs
    ];
  }
1 Like