summaryrefslogtreecommitdiffstats
path: root/pkgs/by-name/ka/kaldi/package.nix
blob: ddc848a2b026214310bc25b802fd44b6f7c75576 (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
{
  lib,
  stdenv,
  openblas,
  blas,
  lapack,
  icu,
  cmake,
  pkg-config,
  fetchFromGitHub,
  fetchurl,
  python3,
  _experimental-update-script-combinators,
  common-updater-scripts,
  ripgrep,
  unstableGitUpdater,
  writeShellScript,
}:

assert blas.implementation == "openblas" && lapack.implementation == "openblas";
stdenv.mkDerivation (finalAttrs: {
  pname = "kaldi";
  version = "0-unstable-2025-09-22";

  src = fetchFromGitHub {
    owner = "kaldi-asr";
    repo = "kaldi";
    rev = "e02e35f0254bb033fab73d1df99fc34123e31d56";
    sha256 = "sha256-ZnVSQTETrMeU+pkqy50ldAe8g1pbnG7VS1utcUy28ls=";
  };

  postPatch = ''
    cp ${
      fetchurl {
        url = "https://raw.githubusercontent.com/rhasspy/rhasspy-speech/8a7bcc977d9c12fed89ab990089ed64524e54d2a/kaldi/src/online2bin/online2-cli-nnet3-decode-faster.cc";
        hash = "sha256-eKItaomBw3xE9unVuMSmzUwJfnpfLExxusKW+bhvaJI=";
      }
    } src/online2bin/online2-cli-nnet3-decode-faster.cc

    substituteInPlace src/online2bin/Makefile \
      --replace-fail "ivector-randomize" "ivector-randomize online2-cli-nnet3-decode-faster"
  '';

  cmakeFlags = [
    "-DKALDI_BUILD_TEST=off"
    "-DBUILD_SHARED_LIBS=on"
    "-DBLAS_LIBRARIES=-lblas"
    "-DLAPACK_LIBRARIES=-llapack"
    "-DFETCHCONTENT_SOURCE_DIR_OPENFST:PATH=${finalAttrs.passthru.sources.openfst}"
    # Fix the build with CMake 4 (openfst does not contain cmake_minimum_required)
    "-DCMAKE_POLICY_VERSION_MINIMUM=3.10"
  ];

  buildInputs = [
    openblas
    icu
  ];

  nativeBuildInputs = [
    cmake
    pkg-config
    python3
  ];

  preConfigure = ''
    cmakeFlagsArray+=(
      # Extract version without the need for git.
      # https://github.com/kaldi-asr/kaldi/blob/71f38e62cad01c3078555bfe78d0f3a527422d75/cmake/VersionHelper.cmake
      # Patch number is not actually used by default so we can just ignore it.
      # https://github.com/kaldi-asr/kaldi/blob/71f38e62cad01c3078555bfe78d0f3a527422d75/CMakeLists.txt#L214
      "-DKALDI_VERSION=$(cat src/.version)"
    )
  '';

  postInstall = ''
    mkdir -p $out/share/kaldi
    cp -r ../egs $out/share/kaldi
  '';

  dontCheckForBrokenSymlinks = true; # TODO: investigate

  passthru = {
    sources = {
      # rev from https://github.com/kaldi-asr/kaldi/blob/master/cmake/third_party/openfst.cmake
      openfst = fetchFromGitHub {
        owner = "kkm000";
        repo = "openfst";
        rev = "338225416178ac36b8002d70387f5556e44c8d05";
        hash = "sha256-9xsL78mkR40zkoRYWsH+iaPa5MYc4BzwslzxGKv4j4I=";
        postFetch = ''
          cd "$out"
          # https://github.com/kkm000/openfst/issues/59
          patch -p1 < ${./gcc14.patch}
          # Patch for compiling openfst with gcc >= 15
          patch -p1 < ${./fix-gcc15-copy-constructor.patch}
        '';
      };
    };

    updateScript =
      let
        updateSource = unstableGitUpdater { };
        updateOpenfst = writeShellScript "update-openfst" ''
          hash=$(${ripgrep}/bin/rg --multiline --pcre2 --only-matching 'FetchContent_Declare\(\s*openfst[^)]*GIT_TAG\s*([0-9a-f]{40})' --replace '$1' "${finalAttrs.src}/cmake/third_party/openfst.cmake")
          ${common-updater-scripts}/bin/update-source-version kaldi.sources.openfst "$hash" --source-key=out "--version-key=rev"
        '';
      in
      _experimental-update-script-combinators.sequence [
        updateSource
        updateOpenfst
      ];
  };

  meta = {
    description = "Speech Recognition Toolkit";
    homepage = "https://kaldi-asr.org";
    license = lib.licenses.mit;
    maintainers = with lib.maintainers; [ mic92 ];
    platforms = lib.platforms.unix;
  };
})