about summary refs log tree commit diff stats
path: root/nix/package.nix
blob: f4d6eac011a7cb6eec05d1ff4da152d0f19b1945 (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
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
142
143
144
# yt - A fully featured command line YouTube client
#
# Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This file is part of Yt.
#
# You should have received a copy of the License along with this program.
# If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
{
  lib,
  rustPlatform,
  installShellFiles,
  # buildInputs
  mpv-unwrapped,
  python3Packages,
  python3,
  ffmpeg,
  openssl,
  libffi,
  # NativeBuildInputs
  makeWrapper,
  llvmPackages_latest,
  glibc,
  sqlite,
  fd,
  pkg-config,
  SDL2,
  # Passthru
  tree-sitter-yts,
}:
rustPlatform.buildRustPackage (finalAttrs: {
  pname = "yt";
  inherit
    ((builtins.fromTOML (builtins.readFile
          ../Cargo.toml)).workspace.package)
    version
    ;

  src = lib.cleanSourceWith {
    src = lib.cleanSource ./..;
    filter = name: type:
      (type == "directory")
      || (builtins.elem (builtins.baseNameOf name) [
        "Cargo.toml"
        "Cargo.lock"
        "mkdb.sh"
        "help.str"
        "raw_error_warning.txt"
      ])
      || (lib.strings.hasSuffix ".rs" (builtins.baseNameOf name))
      || (lib.strings.hasSuffix ".h" (builtins.baseNameOf name))
      || (lib.strings.hasSuffix ".sql" (builtins.baseNameOf name));
  };

  nativeBuildInputs = [
    installShellFiles
    makeWrapper
    sqlite
    fd
    pkg-config
  ];

  buildInputs = [
    mpv-unwrapped.dev
    ffmpeg
    openssl
    libffi
  ];

  checkInputs = [
    # Needed for the tests in `libmpv2`
    SDL2
  ];

  env = let
    clang_version =
      lib.versions.major
      llvmPackages_latest.clang-unwrapped.version;
  in {
    # Needed for the compile time sqlite checks.
    DATABASE_URL = "sqlite://database.sqlx";

    # Required by yt_dlp
    FFMPEG_LOCATION = "${lib.getExe ffmpeg}";

    # Needed for the libmpv2.
    C_INCLUDE_PATH = "${glibc.dev}/include";
    LIBCLANG_INCLUDE_PATH = "${llvmPackages_latest.clang-unwrapped.lib}/lib/clang/${clang_version}/include";
    LIBCLANG_PATH = "${llvmPackages_latest.clang-unwrapped.lib}/lib/libclang.so";
  };

  doCheck = true;

  prePatch = ''
    # Generate the sqlite db, so that we can run the comp-time sqlite checks.
    bash ./scripts/mkdb.sh
  '';

  passthru = {
    inherit tree-sitter-yts;
  };

  cargoLock = {
    lockFile = ../Cargo.lock;
    outputHashes = {
      "ruff_python_ast-0.0.0" = "sha256-/CVpNBOBpvQhz7X80nUHC2x7ZxxCJH8O0WAABJKEriA=";
      "rustpython-0.4.0" = "sha256-hsWqLRiqA0pUNFkHPwvu+Myh5a3p/VXotkPq5ZexqaQ=";
      "rustpython-doc-0.3.0" = "sha256-34ERuLFKzUD9Xmf1zlafe42GLWZfUlw17ejf/NN6yH4=";
    };
  };

  postInstall = let
    collectDeps = pkg: let
      next = pkg.propagatedBuildInputs or [];
    in
      [pkg]
      ++ next
      ++ (lib.flatten (builtins.map collectDeps next));

    loadPythonDep = der: "${der}/lib/python${lib.versions.majorMinor python3.version}/site-packages";

    pythonPath = builtins.concatStringsSep ":" (lib.lists.unique (
      builtins.map loadPythonDep (
        (collectDeps python3Packages.yt-dlp)
        ++ [
          # HACK(@bpeetz): These packages are not picked up in the traversal up top. <2025-06-16>
          python3Packages.chardet
        ]
      )
    ));
  in ''
    installShellCompletion --cmd yt \
      --bash <(COMPLETE=bash $out/bin/yt) \
      --fish <(COMPLETE=fish $out/bin/yt) \
      --zsh <(COMPLETE=zsh $out/bin/yt)

    # NOTE: We cannot clear the path, because we need access to the $EDITOR. <2025-04-04>
    wrapProgram $out/bin/yt \
      --prefix PATH : ${lib.makeBinPath finalAttrs.buildInputs} \
      --set YTDLP_NO_PLUGINS 1 \
      --set PYTHONPATH ${pythonPath}
  '';
})