1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 python3,
6 stanc,
7 buildPackages,
8 runtimeShell,
9 runCommandCC,
10 cmdstan,
11}:
12
13stdenv.mkDerivation rec {
14 pname = "cmdstan";
15 version = "2.36.0";
16
17 src = fetchFromGitHub {
18 owner = "stan-dev";
19 repo = "cmdstan";
20 tag = "v${version}";
21 fetchSubmodules = true;
22 hash = "sha256-9Dan86C0nxxxkIXaOSKExY0hngAgWTpL4RlI3rTnBZo=";
23 };
24
25 postPatch = ''
26 substituteInPlace stan/lib/stan_math/make/libraries \
27 --replace "/usr/bin/env bash" "bash"
28 '';
29
30 nativeBuildInputs = [
31 python3
32 stanc
33 ];
34
35 preConfigure = ''
36 patchShebangs test-all.sh runCmdStanTests.py stan/
37 ''
38 # Fix inclusion of hardcoded paths in PCH files, by building in the store.
39 + ''
40 mkdir -p $out/opt
41 cp -R . $out/opt/cmdstan
42 cd $out/opt/cmdstan
43 mkdir -p bin
44 ln -s ${buildPackages.stanc}/bin/stanc bin/stanc
45 '';
46
47 makeFlags = [
48 "build"
49 ]
50 ++ lib.optionals stdenv.hostPlatform.isDarwin [
51 "arch=${stdenv.hostPlatform.darwinArch}"
52 ];
53
54 # Disable inclusion of timestamps in PCH files when using Clang.
55 env.CXXFLAGS = lib.optionalString stdenv.cc.isClang "-Xclang -fno-pch-timestamp";
56
57 enableParallelBuilding = true;
58
59 installPhase = ''
60 runHook preInstall
61
62 mkdir -p $out/bin
63 ln -s $out/opt/cmdstan/bin/stanc $out/bin/stanc
64 ln -s $out/opt/cmdstan/bin/stansummary $out/bin/stansummary
65 cat > $out/bin/stan <<EOF
66 #!${runtimeShell}
67 make -C $out/opt/cmdstan "\$(realpath "\$1")"
68 EOF
69 chmod a+x $out/bin/stan
70
71 runHook postInstall
72 '';
73
74 passthru.tests = {
75 test = runCommandCC "cmdstan-test" { } ''
76 cp -R ${cmdstan}/opt/cmdstan cmdstan
77 chmod -R +w cmdstan
78 cd cmdstan
79 ./runCmdStanTests.py -j$NIX_BUILD_CORES src/test/interface
80 touch $out
81 '';
82 };
83
84 meta = with lib; {
85 description = "Command-line interface to Stan";
86 longDescription = ''
87 Stan is a probabilistic programming language implementing full Bayesian
88 statistical inference with MCMC sampling (NUTS, HMC), approximate Bayesian
89 inference with Variational inference (ADVI) and penalized maximum
90 likelihood estimation with Optimization (L-BFGS).
91 '';
92 homepage = "https://mc-stan.org/interfaces/cmdstan.html";
93 license = licenses.bsd3;
94 maintainers = with maintainers; [ wegank ];
95 platforms = platforms.unix;
96 };
97}