about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/libraries/rnnoise/default.nix
blob: eb77106fd29faf8137f7cdfa45655e3d36020dbb (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{ stdenv, lib, fetchurl, fetchzip, autoreconfHook, writeScript, fetchpatch
, modelUrl ? "", modelHash ? "" # Allow overriding the model URL and hash
}:

let
  modelVersionJSON = lib.importJSON ./model-version.json;

  # Copy from https://gitlab.xiph.org/xiph/rnnoise/-/raw/v${version}/model_version
  default_model_version = modelVersionJSON.version;

  # Either use the default model or the one provided by package override
  model_url = if (modelUrl == "") then "https://media.xiph.org/rnnoise/models/rnnoise_data-${default_model_version}.tar.gz" else modelUrl;
  model_hash = if (modelHash == "") then modelVersionJSON.hash else modelHash;

in stdenv.mkDerivation (finalAttrs: {
  pname = "rnnoise";
  version = "0.2";

  src = fetchzip {
    urls = [
      "https://gitlab.xiph.org/xiph/rnnoise/-/archive/v${finalAttrs.version}/rnnoise-v${finalAttrs.version}.tar.gz"
      "https://github.com/xiph/rnnoise/archive/v${finalAttrs.version}.tar.gz"
    ];
    hash = "sha256-Qaf+0iOprq7ILRWNRkBjsniByctRa/lFVqiU5ZInF/Q=";
  };

  patches = [
    # remove when updating
    (fetchpatch {
      url = "https://github.com/xiph/rnnoise/commit/372f7b4b76cde4ca1ec4605353dd17898a99de38.patch";
      hash = "sha256-Dzikb59hjVxd1XIEj/Je4evxtGORkaNcqE+zxOJMSvs=";
    })
  ];

  model = fetchurl {
    url = model_url;
    hash = model_hash;
  };

  postPatch = ''
    tar xvomf ${finalAttrs.model}
  '';

  nativeBuildInputs = [ autoreconfHook ];

  postInstall = ''
    install -Dt $out/bin examples/.libs/rnnoise_demo
  '';

  passthru.updateScript = writeScript "update-rnnoise.sh" ''
    #!/usr/bin/env nix-shell
    #!nix-shell -i bash -p curl jq common-updater-scripts nix nix-prefetch findutils moreutils

    prefetch-sri() {
        nix-prefetch-url "$1" | xargs nix hash to-sri --type sha256
    }

    res="$(curl ''${GITHUB_TOKEN:+-u ":$GITHUB_TOKEN"} \
      -sL "https://api.github.com/repos/xiph/rnnoise/tags?per_page=1")"

    version="$(echo $res | jq '.[0].name | split("v") | .[1]' --raw-output)"
    update-source-version ${finalAttrs.pname} "$version" --ignore-same-hash

    model_version=$(curl -sL "https://raw.githubusercontent.com/xiph/rnnoise/v$version/model_version")
    model_url="https://media.xiph.org/rnnoise/models/rnnoise_data-$model_version.tar.gz"
    model_hash="$(prefetch-sri $model_url)"

    modelJson=pkgs/development/libraries/rnnoise/model-version.json

    jq --arg version "$model_version" \
        --arg hash "$model_hash" \
        '.version = $version | .hash = $hash' \
        "$modelJson" | sponge "$modelJson"
  '';

  meta = {
    description = "Recurrent neural network for audio noise reduction";
    homepage = "https://people.xiph.org/~jm/demo/rnnoise/";
    license = lib.licenses.bsd3;
    maintainers = with lib.maintainers; [ nh2 ];
    mainProgram = "rnnoise_demo";
    platforms = lib.platforms.all;
  };
})