1{ lib, stdenv, fetchFromGitHub, stanc, python3, buildPackages, runtimeShell }:
2
3stdenv.mkDerivation rec {
4 pname = "cmdstan";
5 version = "2.32.2";
6
7 src = fetchFromGitHub {
8 owner = "stan-dev";
9 repo = pname;
10 rev = "v${version}";
11 fetchSubmodules = true;
12 hash = "sha256-obV+R1ZjBgunXndCNry+MEne1nQawo81IV2DWwYbbIQ=";
13 };
14
15 nativeBuildInputs = [ stanc ];
16
17 buildFlags = [ "build" ];
18 enableParallelBuilding = true;
19
20 doCheck = true;
21 nativeCheckInputs = [ python3 ];
22
23 CXXFLAGS = lib.optionalString stdenv.isDarwin "-D_BOOST_LGAMMA";
24
25 postPatch = ''
26 substituteInPlace stan/lib/stan_math/make/libraries \
27 --replace "/usr/bin/env bash" "bash"
28 patchShebangs .
29 '' + lib.optionalString stdenv.isAarch64 ''
30 sed -z -i "s/TEST(CommandStansummary, check_console_output).*TEST(CommandStansummary, check_csv_output)/TEST(CommandStansummary, check_csv_output)/" \
31 src/test/interface/stansummary_test.cpp
32 '';
33
34 preConfigure = ''
35 mkdir -p bin
36 ln -s ${buildPackages.stanc}/bin/stanc bin/stanc
37 '';
38
39 makeFlags = lib.optional stdenv.isDarwin "arch=${stdenv.hostPlatform.darwinArch}";
40
41 checkPhase = ''
42 ./runCmdStanTests.py -j$NIX_BUILD_CORES src/test/interface
43 '';
44
45 installPhase = ''
46 mkdir -p $out/opt $out/bin
47 cp -r . $out/opt/cmdstan
48 ln -s $out/opt/cmdstan/bin/stanc $out/bin/stanc
49 ln -s $out/opt/cmdstan/bin/stansummary $out/bin/stansummary
50 cat > $out/bin/stan <<EOF
51 #!${runtimeShell}
52 make -C $out/opt/cmdstan "\$(realpath "\$1")"
53 EOF
54 chmod a+x $out/bin/stan
55 '';
56
57 # Hack to ensure that patchelf --shrink-rpath get rids of a $TMPDIR reference.
58 preFixup = "rm -rf stan";
59
60 meta = with lib; {
61 description = "Command-line interface to Stan";
62 longDescription = ''
63 Stan is a probabilistic programming language implementing full Bayesian
64 statistical inference with MCMC sampling (NUTS, HMC), approximate Bayesian
65 inference with Variational inference (ADVI) and penalized maximum
66 likelihood estimation with Optimization (L-BFGS).
67 '';
68 homepage = "https://mc-stan.org/interfaces/cmdstan.html";
69 license = licenses.bsd3;
70 maintainers = with maintainers; [ wegank ];
71 platforms = platforms.unix;
72 };
73}