about summary refs log tree commit diff
path: root/pkgs/development/python-modules/mujoco/default.nix
blob: 2392de3847bd9e42765401cce1f08aafe71ac50e (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
{ buildPythonPackage
, cmake
, fetchPypi
, glfw
, lib
, mujoco
, numpy
, perl
, pkgs
, pybind11
, python
, setuptools
}:

buildPythonPackage rec {
  pname = "mujoco";
  version = "3.0.1";

  pyproject = true;

  # We do not fetch from the repository because the PyPi tarball is
  # impurely build via
  # <https://github.com/google-deepmind/mujoco/blob/main/python/make_sdist.sh>
  # in the project's CI.
  src = fetchPypi {
    inherit pname version;
    hash = "sha256-pftecOk4q19qKBHs9hBBVenI+SgJg9VT7vc6NKuiY0s=";
  };

  nativeBuildInputs = [ cmake setuptools ];
  dontUseCmakeConfigure = true;
  buildInputs = [ mujoco pybind11 ];
  propagatedBuildInputs = [ glfw numpy ];

  pythonImportsCheck = [ "${pname}" ];

  env.MUJOCO_PATH = "${mujoco}";
  env.MUJOCO_PLUGIN_PATH = "${mujoco}/lib";
  env.MUJOCO_CMAKE_ARGS = "-DMUJOCO_SIMULATE_USE_SYSTEM_GLFW=ON";

  preConfigure =
    # Use system packages for pybind
    ''
      ${perl}/bin/perl -0777 -i -pe "s/(findorfetch\(.{3}USE_SYSTEM_PACKAGE.{3})(OFF)(.{3}PACKAGE_NAME.{3}pybind11.*\))/\1ON\3/gms" mujoco/CMakeLists.txt
    '' +
    # Use non-system eigen3, lodepng, abseil: Remove mirror info and prefill
    # dependency directory. $build from setuptools.
    (let
      # E.g. 3.11.2 -> "311"
      pythonVersionMajorMinor = with lib.versions;
        "${major python.pythonVersion}${minor python.pythonVersion}";
    in ''
      ${perl}/bin/perl -0777 -i -pe "s/GIT_REPO\n.*\n.*GIT_TAG\n.*\n//gm" mujoco/CMakeLists.txt
      ${perl}/bin/perl -0777 -i -pe "s/(FetchContent_Declare\(\n.*lodepng\n.*)(GIT_REPO.*\n.*GIT_TAG.*\n)(.*\))/\1\3/gm" mujoco/simulate/CMakeLists.txt

      build="/build/${pname}-${version}/build/temp.linux-x86_64-cpython-${pythonVersionMajorMinor}/"
      mkdir -p $build/_deps
      ln -s ${mujoco.pin.lodepng} $build/_deps/lodepng-src
      ln -s ${mujoco.pin.eigen3} $build/_deps/eigen-src
      ln -s ${mujoco.pin.abseil-cpp} $build/_deps/abseil-cpp-src
    '');

  meta = with lib; {
    description =
      "Python bindings for MuJoCo: a general purpose physics simulator.";
    homepage = "https://mujoco.org/";
    license = licenses.asl20;
    maintainers = with maintainers; [ tmplt ];
  };
}