NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
18.3k stars 14.27k forks source link

postgresml #197268

Open happysalada opened 2 years ago

happysalada commented 2 years ago

Project description The project adds an extension to postgres and enables to do machine learning directly with postgres

Metadata

I'm leaving this to somebody better than me. This is a postgres extension built with rust. Here is how far I got

{ lib
, stdenv
, fetchFromGitHub
, rustPlatform
, python3
, postgresql
, cmake
, llvmPackages_12
, cargo-pgx
, rustfmt
, dmlc-core
, openblas
, pkg-config
}:

let
  version = "2.0.2";
  envVar =
    if
      (lib.versionAtLeast postgresql.version "11" && lib.versionOlder postgresql.version "12") then
      { PG11_PG_CONFIG = "${postgresql}/bin/pg_config"; } else if
      (lib.versionAtLeast postgresql.version "12" && lib.versionOlder postgresql.version "13") then
      { PG12_PG_CONFIG = "${postgresql}/bin/pg_config"; } else if
      (lib.versionAtLeast postgresql.version "13" && lib.versionOlder postgresql.version "14") then
      { PG13_PG_CONFIG = "${postgresql}/bin/pg_config"; } else if
      (lib.versionAtLeast postgresql.version "14" && lib.versionOlder postgresql.version "15") then
      { PG14_PG_CONFIG = "${postgresql}/bin/pg_config"; } else { };
in
rustPlatform.buildRustPackage (envVar // {
  pname = "postgresml";
  inherit version;

  src = fetchFromGitHub {
    owner = "postgresml";
    repo = "postgresml";
    rev = "v${version}";
    sha256 = "sha256-x8w6t4hXPWeXOzm64dkUllGxV0qIBzJMP4KNrnTQ1eo=";
    fetchSubmodules = true;
  };

  preBuild = ''
    export PGX_HOME="$TMPDIR";
    cargo pgx init 
    export RUSTFLAGS="-L ${lib.makeLibraryPath [ dmlc-core ]}"
  '';

  sourceRoot = "source/pgml-extension";

  cargoSha256 = "sha256-PP6rRgdifkaPKcR5Hp37ceH7BrHKKLZ2aoHlHy7+3J4=";

  buildNoDefaultFeatures = true;
  buildFeatures = [ "python" ] ++
    lib.optional (lib.versionAtLeast postgresql.version "11" && lib.versionOlder postgresql.version "12") "pg11" ++
    lib.optional (lib.versionAtLeast postgresql.version "12" && lib.versionOlder postgresql.version "13") "pg12" ++
    lib.optional (lib.versionAtLeast postgresql.version "13" && lib.versionOlder postgresql.version "14") "pg13" ++
    lib.optional (lib.versionAtLeast postgresql.version "14" && lib.versionOlder postgresql.version "15") "pg14";

  nativeBuildInputs = [
    cmake
    python3
    llvmPackages_12.stdenv
    llvmPackages_12.clang
    cargo-pgx
    rustfmt
    dmlc-core
  ];

  buildInputs = [  ] ++ lib.optionals stdenv.isDarwin [ ];

  LIBCLANG_PATH = "${llvmPackages_12.libclang.lib}/lib";

  meta = with lib; {
    description = " PostgresML is an end-to-end machine learning system. It enables you to train models and make online predictions using only SQL, without your data ever leaving your favorite database";
    homepage = "https://github.com/postgresml/postgresml";
    license = with licenses; [ mit ];
    maintainers = with maintainers; [ happysalada ];
  };
})

and here is the error I can't figure out

 = note: /nix/store/i38w7ashc8bvz4zh1i8gzbb3c5z3l71x-binutils-2.39/bin/ld: cannot find -lopenblas: No such file or directory

I've tried setting LIBRARY_PATH and LD_LIBRARY_PATH , but to no avail, nothing works

happysalada commented 2 years ago

dmlc-core is a simple package

{ config
, stdenv
, lib
, fetchFromGitHub
, cmake
, llvmPackages
}:

stdenv.mkDerivation rec {
  pname = "dmlc-core";
  version = "0.3";

  src = fetchFromGitHub {
    owner = "dmlc";
    repo = pname;
    rev = "v${version}";
    fetchSubmodules = true;
    sha256 = "sha256-JWrWFZ70Pq9YWkO40iGjbnhmjycgnI5KyB/4VAFefvk=";
  };

  nativeBuildInputs = [
    cmake
  ] ++ lib.optionals stdenv.isDarwin [
    llvmPackages.openmp
  ];

  installPhase = ''
    runHook preInstall
    mkdir -p $out/lib
    cp -r ../include $out
    install -Dm755 libdmlc.a $out/lib/libdmlc.a
    runHook postInstall
  '';

  meta = with lib; {
    description = "A common bricks library for building scalable and portable distributed machine learning.";
    homepage = "https://github.com/dmlc/dmlc-core";
    license = licenses.asl20;
    platforms = platforms.unix;
    maintainers = with maintainers; [ happysalada ];
  };
}