about summary refs log tree commit diff
path: root/users/Profpatsch/ytextr/default.nix
blob: ac630603b90ce708c4f2144aebf47a7b4c451bbc (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
{ depot, pkgs, lib, ... }:

# ytextr is a wrapper arount yt-dlp (previously youtube-dl)
# that extracts a single video according to my preferred settings.
#
# It will be sandboxed to the current directory, since I don’t particularly
# trust the massive codebase of that tool (with hundreds of contributors).
#
# Since the rules for downloading videos is usually against the wishes
# of proprietary vendors, and a video is many megabytes anyway,
# it will be fetched from the most recent nixpkgs unstable channel before running.

let
  bins = depot.nix.getBins pkgs.nix [ "nix-build" ]
    // depot.nix.getBins pkgs.bubblewrap [ "bwrap" ];

  # Run a command, with the given packages in scope, and `packageNamesAtRuntime` being fetched at the start in the given nix `channel`.
  nix-run-with-channel =
    {
      # The channel to get `packageNamesAtRuntime` from
      channel
    , # executable to run with `packageNamesAtRuntime` in PATH
      # and the argv
      executable
    , # A list of nixpkgs package attribute names that should be put into PATH when running `command`.
      packageNamesAtRuntime
    ,
    }: depot.nix.writeExecline "nix-run-with-channel-${channel}" { } [
      # TODO: prevent race condition by writing a temporary gc root
      "backtick"
      "-iE"
      "storepath"
      [
        bins.nix-build
        "-I"
        "nixpkgs=channel:${channel}"
        "--arg"
        "packageNamesAtRuntimeJsonPath"
        (pkgs.writeText "packageNamesAtRuntime.json" (builtins.toJSON packageNamesAtRuntime))
        ./create-symlink-farm.nix
      ]
      "importas"
      "-ui"
      "PATH"
      "PATH"
      "export"
      "PATH"
      "\${storepath}/bin:\${PATH}"
      executable
      "$@"
    ];

in
nix-run-with-channel {
  channel = "nixos-unstable";
  packageNamesAtRuntime = [ "yt-dlp" ];
  executable = depot.nix.writeExecline "ytextr" { readNArgs = 1; } [
    "getcwd"
    "-E"
    "cwd"
    bins.bwrap
    "--ro-bind"
    "/nix/store"
    "/nix/store"
    "--ro-bind"
    "/etc"
    "/etc"
    "--bind"
    "$cwd"
    "$cwd"
    "yt-dlp"
    "--no-playlist"
    "--write-sub"
    "--all-subs"
    "--embed-subs"
    "--merge-output-format"
    "mkv"
    "-f"
    "bestvideo[height<=?1080]+bestaudio/best"
    "$1"
  ];
}