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