I want to install R with the RPostgreSQL CRAN package, which also needs the
postgresql package installed to provide the library libpq.
I found a way to do it that works, but it with incosistencies.
mkdir R_with_packages
cd R_with_packages
cat > default.nix <<hereDoc
with import <nixpkgs> {}; # adapted from https://nixos.org/manual/nixpkgs/stable/#installation
{
myProject = stdenv.mkDerivation {
name = "myProject";
version = "1";
src = if lib.inNixShell then null else nix; # i am not sure what this line does
buildInputs = [
postgresql
R
rPackages.RPostgreSQL
];
};
}
hereDoc
nix-shell .
this works on macos on apple silicon.
but when I try it on linux on x86_64,
it gets an error like this:
[nix-shell:~/foo]$ R
R version 4.2.2 (2022-10-31) -- "Innocent and Trusting"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(RPostgreSQL)
Loading required package: DBI
Error: package or namespace load failed for āRPostgreSQLā in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/home/jeg/R/x86_64-pc-linux-gnu-library/4.2/RPostgreSQL/libs/RPostgreSQL.so':
libpq.so.5: cannot open shared object file: No such file or directory
>
I did find a workaround, if I start R like this, it works:
LD_LIBRARY_PATH=$(dirname $(ldd $(which psql) | awk '/libpq.so.5/{print $3}')) R
note that this kludge does not work on macos, because it macos does not have the ldd command, but it is not needed there becasue it just works there anyway.