lol
1{
2 lib,
3 stdenv,
4 fetchFromGitHub,
5 mpiCheckPhaseHook,
6 cmake,
7 python3,
8 gfortran,
9 blas,
10 lapack,
11 dbcsr,
12 fftw,
13 libint,
14 libvori,
15 libxc,
16 dftd4,
17 simple-dftd3,
18 tblite,
19 mpi,
20 gsl,
21 scalapack,
22 makeWrapper,
23 libxsmm,
24 spglib,
25 which,
26 pkg-config,
27 plumed,
28 zlib,
29 hdf5-fortran,
30 sirius,
31 libvdwxc,
32 spla,
33 spfft,
34 trexio,
35 toml-f,
36 greenx,
37 gmp,
38 enableElpa ? false,
39 elpa,
40 cudaPackages,
41 rocmPackages,
42 newScope,
43 mctc-lib,
44 jonquil,
45 multicharge,
46 mstore,
47 test-drive,
48 config,
49 gpuBackend ? (
50 if config.cudaSupport then
51 "cuda"
52 else if config.rocmSupport then
53 "rocm"
54 else
55 "none"
56 ),
57 # Change to a value suitable for your target GPU.
58 # see https://github.com/cp2k/cp2k/blob/master/CMakeLists.txt#L433
59 hipTarget ? "gfx908",
60 cudaTarget ? "80",
61}:
62
63assert builtins.elem gpuBackend [
64 "none"
65 "cuda"
66 "rocm"
67];
68
69let
70 grimmeCmake = lib.makeScope newScope (self: {
71 mctc-lib = mctc-lib.override {
72 buildType = "cmake";
73 inherit (self) jonquil;
74 };
75
76 toml-f = toml-f.override {
77 buildType = "cmake";
78 inherit (self) test-drive;
79 };
80
81 dftd4 = dftd4.override {
82 buildType = "cmake";
83 inherit (self) mstore mctc-lib multicharge;
84 };
85
86 jonquil = jonquil.override {
87 buildType = "cmake";
88 inherit (self) toml-f test-drive;
89 };
90
91 mstore = mstore.override {
92 buildType = "cmake";
93 inherit (self) mctc-lib;
94 };
95
96 multicharge = multicharge.override {
97 buildType = "cmake";
98 inherit (self) mctc-lib mstore;
99 };
100
101 test-drive = test-drive.override { buildType = "cmake"; };
102
103 simple-dftd3 = simple-dftd3.override {
104 buildType = "cmake";
105 inherit (self) mctc-lib mstore toml-f;
106 };
107
108 tblite = tblite.override {
109 buildType = "cmake";
110 inherit (self)
111 mctc-lib
112 mstore
113 toml-f
114 multicharge
115 dftd4
116 simple-dftd3
117 ;
118 };
119
120 sirius = sirius.override {
121 inherit (self)
122 mctc-lib
123 toml-f
124 multicharge
125 dftd4
126 simple-dftd3
127 ;
128 };
129 });
130
131in
132stdenv.mkDerivation rec {
133 pname = "cp2k";
134 version = "2025.2";
135
136 src = fetchFromGitHub {
137 owner = "cp2k";
138 repo = "cp2k";
139 rev = "v${version}";
140 hash = "sha256-vfl5rCoFeGtYuZ7LcsVsESjKxFbN5IYDvBSzOqsd64w=";
141 fetchSubmodules = true;
142 };
143
144 patches = [
145 # Remove the build command line from the source.
146 # This avoids dependencies to .dev inputs
147 ./remove-compiler-options.patch
148
149 # Fix pkg-config path generation
150 ./pkgconfig.patch
151 ];
152
153 nativeBuildInputs = [
154 python3
155 cmake
156 which
157 makeWrapper
158 pkg-config
159 gfortran
160 ]
161 ++ lib.optional (gpuBackend == "cuda") cudaPackages.cuda_nvcc;
162
163 buildInputs = [
164 fftw
165 gsl
166 libint
167 libvori
168 libxc
169 libxsmm
170 mpi
171 spglib
172 scalapack
173 blas
174 lapack
175 dbcsr
176 plumed
177 zlib
178 hdf5-fortran
179 spla
180 spfft
181 libvdwxc
182 trexio
183 greenx
184 gmp
185 grimmeCmake.dftd4
186 grimmeCmake.simple-dftd3
187 grimmeCmake.tblite
188 grimmeCmake.sirius
189 grimmeCmake.toml-f
190 ]
191 ++ lib.optional enableElpa elpa
192 ++ lib.optionals (gpuBackend == "cuda") [
193 cudaPackages.cuda_cudart
194 cudaPackages.libcublas
195 cudaPackages.cuda_nvrtc
196 ]
197 ++ lib.optionals (gpuBackend == "rocm") [
198 rocmPackages.clr
199 rocmPackages.rocm-core
200 rocmPackages.hipblas
201 rocmPackages.hipfft
202 rocmPackages.rocblas
203 ];
204
205 propagatedBuildInputs = [ (lib.getBin mpi) ];
206 propagatedUserEnvPkgs = [ mpi ];
207
208 postPatch = ''
209 patchShebangs tools exts/dbcsr/tools/build_utils exts/dbcsr/.cp2k
210 substituteInPlace exts/build_dbcsr/Makefile \
211 --replace '/usr/bin/env python3' '${python3}/bin/python' \
212 --replace 'SHELL = /bin/sh' 'SHELL = bash'
213 '';
214
215 cmakeFlags = [
216 (lib.strings.cmakeBool "CP2K_USE_DFTD4" true)
217 (lib.strings.cmakeBool "CP2K_USE_TBLITE" true)
218 (lib.strings.cmakeBool "CP2K_USE_FFTW3" true)
219 (lib.strings.cmakeBool "CP2K_USE_HDF5" true)
220 (lib.strings.cmakeBool "CP2K_USE_LIBINT2" true)
221 (lib.strings.cmakeBool "CP2K_USE_LIBXC" true)
222 (lib.strings.cmakeBool "CP2K_USE_MPI" true)
223 (lib.strings.cmakeBool "CP2K_USE_VORI" true)
224 (lib.strings.cmakeBool "CP2K_USE_TREXIO" true)
225 (lib.strings.cmakeBool "CP2K_USE_SPGLIB" true)
226 (lib.strings.cmakeBool "CP2K_USE_SPLA" true)
227 (lib.strings.cmakeBool "CP2K_USE_LIBXSMM" true)
228 (lib.strings.cmakeBool "CP2K_USE_SIRIUS" true)
229 (lib.strings.cmakeBool "CP2K_USE_LIBVDWXC" true)
230 (lib.strings.cmakeBool "CP2K_USE_PLUMED" true)
231 (lib.strings.cmakeBool "CP2K_USE_GREENX" true)
232 (lib.strings.cmakeBool "CP2K_USE_ELPA" enableElpa)
233 (lib.strings.cmakeBool "BUILD_SHARED_LIBS" (!stdenv.hostPlatform.isStatic))
234 ]
235 ++ lib.optionals (gpuBackend == "rocm") [
236 (lib.strings.cmakeFeature "CP2K_USE_ACCEL" "HIP")
237 (lib.strings.cmakeFeature "CMAKE_HIP_ARCHITECTURES" hipTarget)
238 ]
239 ++ lib.optionals (gpuBackend == "cuda") [
240 (lib.strings.cmakeFeature "CP2K_USE_ACCEL" "CUDA")
241 (lib.strings.cmakeFeature "CMAKE_CUDA_ARCHITECTURES" cudaTarget)
242 ];
243
244 nativeCheckInputs = [
245 mpiCheckPhaseHook
246 ];
247
248 passthru = {
249 inherit mpi;
250 };
251
252 outputs = [
253 "out"
254 "dev"
255 ];
256
257 postInstall = ''
258 mkdir -p $out/share/cp2k
259 cp -r ../data/* $out/share/cp2k
260
261 for i in $out/bin/*; do
262 wrapProgram $i \
263 --set-default CP2K_DATA_DIR $out/share/cp2k \
264 --set-default OMP_NUM_THREADS 1
265 done
266 '';
267
268 doInstallCheck = gpuBackend == "none";
269
270 installCheckPhase = ''
271 runHook preInstallCheck
272
273 for TEST in $out/bin/{dbt_tas,dbt,libcp2k,parallel_rng_types,gx_ac}_unittest.psmp; do
274 mpirun -n 2 $TEST
275 done
276
277 runHook postInstallCheck
278 '';
279
280 meta = {
281 description = "Quantum chemistry and solid state physics program";
282 homepage = "https://www.cp2k.org";
283 license = lib.licenses.gpl2Plus;
284 maintainers = [ lib.maintainers.sheepforce ];
285 platforms = [ "x86_64-linux" ];
286 };
287}