summaryrefslogtreecommitdiff
path: root/pkgs/by-name/mo/moshi/package.nix
blob: ed683e799bf01e8a2f042436c525f1c56a524fdc (about) (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
{
  lib,
  stdenv,
  rustPlatform,
  fetchFromGitHub,

  # nativeBuildInputs
  pkg-config,
  python3,
  autoPatchelfHook,
  autoAddDriverRunpath,

  # buildInputs
  libopus,
  openssl,
  sentencepiece,
  alsa-lib,

  # passthru
  moshi,
  nix-update-script,

  config,
  cudaPackages,
  cudaCapability ? null,
}:

let
  minRequiredCudaCapability = "6.1"; # build fails with 6.0
  inherit (cudaPackages.cudaFlags) cudaCapabilities;
  cudaCapabilityString =
    if cudaCapability == null then
      (builtins.head (
        (builtins.filter (cap: lib.versionAtLeast cap minRequiredCudaCapability) cudaCapabilities)
        ++ [
          (lib.warn "moshi doesn't support ${lib.concatStringsSep " " cudaCapabilities}" minRequiredCudaCapability)
        ]
      ))
    else
      cudaCapability;
  cudaCapability' = lib.toInt (cudaPackages.cudaFlags.dropDot cudaCapabilityString);
in
rustPlatform.buildRustPackage (finalAttrs: {
  pname = "moshi";
  version = "0.2.3";

  src = fetchFromGitHub {
    owner = "kyutai-labs";
    repo = "moshi";
    tag = "v${finalAttrs.version}";
    hash = "sha256-tQQTMwvJauzF24S1N2m2slZAHZvklCkPOTrhLvlsNVg=";
  };

  sourceRoot = "${finalAttrs.src.name}/rust";

  # Upstream does not track their Cargo.lock
  # https://github.com/kyutai-labs/moshi/issues/256
  cargoLock = {
    lockFile = ./Cargo.lock;
  };

  postPatch = ''
    ln -s ${./Cargo.lock} Cargo.lock
  '';

  nativeBuildInputs =
    [
      pkg-config
      python3
    ]
    ++ lib.optionals stdenv.hostPlatform.isDarwin [
      # Unable to find libclang: "couldn't find any valid shared libraries matching: ['libclang.dylib']
      rustPlatform.bindgenHook
    ]
    ++ lib.optionals config.cudaSupport [
      # WARNING: autoAddDriverRunpath must run AFTER autoPatchelfHook
      # Otherwise, autoPatchelfHook removes driverLink from RUNPATH
      autoPatchelfHook
      autoAddDriverRunpath

      cudaPackages.cuda_nvcc
    ];

  buildInputs =
    [
      libopus
      openssl
      sentencepiece
    ]
    ++ lib.optionals stdenv.hostPlatform.isLinux [
      alsa-lib
    ]
    ++ lib.optionals config.cudaSupport [
      cudaPackages.cuda_cccl
      cudaPackages.cuda_cudart
      cudaPackages.cuda_nvrtc
      cudaPackages.libcublas
      cudaPackages.libcurand
    ];

  buildFeatures =
    lib.optionals stdenv.hostPlatform.isDarwin [ "metal" ]
    ++ lib.optionals config.cudaSupport [ "cuda" ];

  env = lib.optionalAttrs config.cudaSupport {
    CUDA_COMPUTE_CAP = cudaCapability';

    # We already list CUDA dependencies in buildInputs
    # We only set CUDA_TOOLKIT_ROOT_DIR to satisfy some redundant checks from upstream
    CUDA_TOOLKIT_ROOT_DIR = lib.getDev cudaPackages.cuda_cudart;
  };

  appendRunpaths = lib.optionals config.cudaSupport [
    (lib.makeLibraryPath [
      cudaPackages.libcublas
      cudaPackages.libcurand
    ])
  ];

  passthru = {
    tests = {
      withCuda = lib.optionalAttrs stdenv.hostPlatform.isLinux (
        moshi.override { config.cudaSupport = true; }
      );
    };
    updateScript = nix-update-script {
      extraArgs = [ "--generate-lockfile" ];
    };
  };

  meta = {
    description = "Rust implementation of moshi, a real-time voice AI";
    homepage = "https://github.com/kyutai-labs/moshi";
    # The rust implementation is licensed under Apache
    # https://github.com/kyutai-labs/moshi/tree/main/rust#license
    license = with lib.licenses; [ asl20 ];
    maintainers = with lib.maintainers; [ GaetanLepage ];
    platforms = lib.platforms.all;
    mainProgram = "moshi-cli";
  };
})