about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/compilers/cmdstan/default.nix
blob: ad84e13d369ecdf3f75969507d4cf151c8ee8d71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{ lib
, stdenv
, fetchFromGitHub
, python3
, stanc
, buildPackages
, runtimeShell
, runCommandCC
, cmdstan
}:

stdenv.mkDerivation rec {
  pname = "cmdstan";
  version = "2.33.1";

  src = fetchFromGitHub {
    owner = "stan-dev";
    repo = pname;
    rev = "v${version}";
    fetchSubmodules = true;
    hash = "sha256-c+L/6PjW7YgmXHuKhKjiRofBRAhKYCzFCZ6BOX5AmC4=";
  };

  postPatch = ''
    substituteInPlace stan/lib/stan_math/make/libraries \
      --replace "/usr/bin/env bash" "bash"
  '';

  nativeBuildInputs = [
    python3
    stanc
  ];

  preConfigure = ''
    patchShebangs test-all.sh runCmdStanTests.py stan/
  ''
  # Fix inclusion of hardcoded paths in PCH files, by building in the store.
  + ''
    mkdir -p $out/opt
    cp -R . $out/opt/cmdstan
    cd $out/opt/cmdstan
    mkdir -p bin
    ln -s ${buildPackages.stanc}/bin/stanc bin/stanc
  '';

  makeFlags = [
    "build"
  ] ++ lib.optionals stdenv.isDarwin [
    "arch=${stdenv.hostPlatform.darwinArch}"
  ];

  # Disable inclusion of timestamps in PCH files when using Clang.
  env.CXXFLAGS = lib.optionalString stdenv.cc.isClang "-Xclang -fno-pch-timestamp";

  enableParallelBuilding = true;

  installPhase = ''
    runHook preInstall

    mkdir -p $out/bin
    ln -s $out/opt/cmdstan/bin/stanc $out/bin/stanc
    ln -s $out/opt/cmdstan/bin/stansummary $out/bin/stansummary
    cat > $out/bin/stan <<EOF
    #!${runtimeShell}
    make -C $out/opt/cmdstan "\$(realpath "\$1")"
    EOF
    chmod a+x $out/bin/stan

    runHook postInstall
  '';

  passthru.tests = {
    test = runCommandCC "cmdstan-test" { } ''
      cp -R ${cmdstan}/opt/cmdstan cmdstan
      chmod -R +w cmdstan
      cd cmdstan
      ./runCmdStanTests.py -j$NIX_BUILD_CORES src/test/interface
      touch $out
    '';
  };

  meta = with lib; {
    description = "Command-line interface to Stan";
    longDescription = ''
      Stan is a probabilistic programming language implementing full Bayesian
      statistical inference with MCMC sampling (NUTS, HMC), approximate Bayesian
      inference with Variational inference (ADVI) and penalized maximum
      likelihood estimation with Optimization (L-BFGS).
    '';
    homepage = "https://mc-stan.org/interfaces/cmdstan.html";
    license = licenses.bsd3;
    maintainers = with maintainers; [ wegank ];
    platforms = platforms.unix;
  };
}