tweag / HaskellR

The full power of R in Haskell.

Home Page:https://tweag.github.io/HaskellR

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Trouble compiling program using inline-r and R libraries in nix

Xitian9 opened this issue · comments

This is probably more of a nix problem than an inline-r problem, so feel free to close if inappropriate.

I'm having a lot of trouble getting my program depending on inline-r to run in a nix environment. It will compile successfully, but when run it will complain

cannot find system Renviron
Fatal error: unable to open the base package

If I have R libraries installed in my home directory in the usual place, it will complain about being unable to find the imported libraries. These messages suggest that nix sandboxing is somehow being broken, and it's not looking for the local copy of R being used.

I've tried the solutions in #344, #287, and #328, all of which match errors I've seen trying to solve this in various ways, but they all have the same result: calling R in the nix shell works fine, and importing the R libraries succeeds, but any attempt to compile the program and run it will not be able to find the library.

I'm looking either for nix-build to work, or for nix-shell --run "cabal build". This seems like a reasonably common use-case, so featuring an example for the documentation might be helpful (which I'm happy to provide if I get this working).

The helpful people over at the NixOS discourse site have been unable to get this working, but we've made some progress. Here is a minimal (non-)working example.

My knowledge of nix is quite superficial but this works for me with your example code:

{ nixpkgs ? import <nixpkgs> {},
  doBenchmark ? false }:

let

  inherit (nixpkgs) pkgs;

f = { mkDerivation, base, inline-r, R, stdenv
    , template-haskell }:
mkDerivation {
  pname = "mrp";
  version = "1.0.0";
  src = ./.;
  isLibrary = false;
  isExecutable = true;
  executableHaskellDepends = [
    base
    (nixpkgs.haskell.lib.dontCheck inline-r)
    template-haskell ];
  executableSystemDepends = [
    R
    pkgs.rPackages.dplyr
    pkgs.rPackages.ggplot2
  ];
  license = stdenv.lib.licenses.bsd3;
};

  haskellPackages = pkgs.haskellPackages;

  variant = if doBenchmark then pkgs.haskell.lib.doBenchmark else pkgs.lib.id;

  drv = variant (haskellPackages.callPackage f {});

in

  if pkgs.lib.inNixShell then drv.env else drv
[nix-shell:~/nix-inline-r-mwe]$ ghci -ilibrary -fno-ghci-sandbox
GHCi, version 8.6.5: http://www.haskell.org/ghc/  :? for help
Prelude> :l executable/Main.hs
[1 of 2] Compiling MWE              ( library/MWE.hs, interpreted )
[2 of 2] Compiling Main             ( executable/Main.hs, interpreted )
Ok, two modules loaded.
*Main> main

Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union

[1] "Test"

I think we can close this as there is a solution. But please re-open it if necessary.