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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
{ pkgs, depot, lib, ... }:

let
  bins = depot.nix.getBins pkgs.sqlite [ "sqlite3" ]
    // depot.nix.getBins pkgs.util-linux [ "unshare" ]
    // depot.nix.getBins pkgs.coreutils [ "echo" ]
    // depot.nix.getBins pkgs.gnused [ "sed" ]
    // depot.nix.getBins pkgs.squashfuse [ "squashfuse" ]
    // depot.nix.getBins pkgs.jq [ "jq" ];

  mpv-script = (pkgs.runCommand "lyric-mpv-script" { } ''
    mkdir -p $out/share/mpv/scripts
    cp ${pkgs.writeText "lyric.lua" (
      lib.replaceStrings
        [ "@get_subtitles_command@" ]
        [ (toString lyric-to-temp-file) ]
        (builtins.readFile ./lyric-mpv-script.lua)
    )} $out/share/mpv/scripts/lyric.lua
  '') // { scriptName = "lyric.lua"; };

  timing-mpv-script = (pkgs.runCommand "lyric-timing-mpv-script" { } ''
    mkdir -p $out/share/mpv/scripts
    cp ${pkgs.writeText "lyric-timing.lua" (builtins.readFile ./lyric-timing-mpv-script.lua)} $out/share/mpv/scripts/lyric-timing.lua
  '') // { scriptName = "lyric-timing.lua"; };

  lyric-to-temp-file = depot.nix.writeExecline "lyric-to-temp-file" { readNArgs = 1; } [
    "backtick"
    "-E"
    "cache"
    [ depot.users.Profpatsch.xdg-cache-home ]
    "if"
    [ "mkdir" "-p" "\${cache}/lyric/as-files" ]
    "if"
    [
      "redirfd"
      "-w"
      "1"
      "\${cache}/lyric/as-files/\${1}.lrc"
      lyric
      "$1"
    ]
    "printf"
    "\${cache}/lyric/as-files/\${1}.lrc"
  ];

  # looool
  escapeSqliteString = depot.nix.writeExecline "escape-sqlite-string" { readNArgs = 1; } [
    "pipeline"
    [
      "printf"
      "%s"
      "$1"
    ]
    bins.sed
    "s/''/''''/g"
  ];

  # Display lyrics for the given search string;
  # search string can contain a substring of band name, album name, song title
  #
  # Use the database dump from https://lrclib.net/db-dumps and place it in ~/.cache/lyric/lrclib-db-dump.sqlite3
  #
  # TODO: put in the nodejs argh
  lyric =
    (depot.nix.writeExecline "lyric" { readNArgs = 1; } [
      "backtick"
      "-E"
      "cache"
      [ depot.users.Profpatsch.xdg-cache-home ]
      # make sure the squashfuse is only mounted while the command is running
      bins.unshare
      "--user"
      "--mount"
      "--pid"
      "--map-root-user"
      "--kill-child"
      "if"
      [ "mkdir" "-p" "\${cache}/lyric/dump" ]
      # TODO: provide a command that takes an url of a lyric.gz and converts it to this here squash image
      "if"
      [ bins.squashfuse "\${cache}/lyric/lyric-db.squash" "\${cache}/lyric/dump" ]
      # please help me god
      "backtick"
      "-E"
      "searchstring"
      [ escapeSqliteString "$1" ]
      "pipeline"
      [
        "pipeline"
        [
          "echo"
          (''
            .mode json
            select * from (
              -- first we try to find if we can find the track verbatim
              select * from (select
                  synced_lyrics,
                  has_synced_lyrics,
                  plain_lyrics
              from
                  tracks_fts('' + "'\${searchstring}'" + '') tf
                join tracks t on t.rowid = tf.rowid
                join lyrics l on t.rowid = l.track_id
            order by
                has_synced_lyrics desc, t.id
            )
            UNION ALL
            select * from (select
                synced_lyrics,
                has_synced_lyrics,
                plain_lyrics
            from
                tracks_fts('' + "'\${searchstring}'" + '') tf
                join tracks t on t.rowid = tf.rowid
                join lyrics l on t.rowid = l.track_id
            order by
                has_synced_lyrics desc, t.id
            )
          )
          where synced_lyrics is not null and synced_lyrics != ''''
          and plain_lyrics is not null and plain_lyrics != ''''
          limit
              1;
        ''
          )
        ]
        bins.sqlite3
        "file:\${cache}/lyric/dump/lrclib-db-dump.sqlite3?immutable=1"
      ]
      bins.jq
      "-r"
      ''
        if .[0] == null
        then ""
        else
        .[0]
            | if .has_synced_lyrics == 1
            then .synced_lyrics
            else .plain_lyrics
            end
        end
      ''
    ]);


  # TODO: the browser extension is currently not built.
  #
  # It was built with napalm, which (a) fails with "cannot coerce null to a
  # string" while resolving the node_modules closure, and (b) fetched itself
  # with `builtins.fetchGit` at *evaluation* time, which is illegal in a pure
  # flake evaluation and so broke `nix build` for the whole repository.
  #
  # Rewrite this in something that does not need a node build (Go, say) rather
  # than reviving the npm dependency closure.
  #
  # js = depot.users.Profpatsch.napalm.buildPackage ./. { };

in
{
  inherit
    lyric
    mpv-script
    timing-mpv-script;
}