From 4ddf706a2f5cb014fc6ffca3a8f561c24b3074af Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 11 Feb 2011 11:16:18 +0000 Subject: [PATCH] * New adventures in purity: get Spotify to work without a /usr/share/spotify symlink by rewriting open() and stat() calls. svn path=/nixpkgs/trunk/; revision=25892 --- pkgs/applications/audio/spotify/default.nix | 18 +++---- pkgs/applications/audio/spotify/preload.c | 59 +++++++++++++++++++++ 2 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 pkgs/applications/audio/spotify/preload.c diff --git a/pkgs/applications/audio/spotify/default.nix b/pkgs/applications/audio/spotify/default.nix index 006f48bd379..89c1b5ee7e5 100644 --- a/pkgs/applications/audio/spotify/default.nix +++ b/pkgs/applications/audio/spotify/default.nix @@ -1,4 +1,4 @@ -{ fetchurl, stdenv, dpkg, xlibs, qt4, alsaLib }: +{ fetchurl, stdenv, dpkg, xlibs, qt4, alsaLib, makeWrapper }: assert stdenv.system == "i686-linux" || stdenv.system == "x86_64-linux"; @@ -20,7 +20,7 @@ stdenv.mkDerivation { } else throw "Spotify not supported on this platform."; - buildInputs = [ dpkg ]; + buildInputs = [ dpkg makeWrapper ]; unpackPhase = "true"; @@ -35,6 +35,12 @@ stdenv.mkDerivation { --interpreter "$(cat $NIX_GCC/nix-support/dynamic-linker)" \ --set-rpath ${stdenv.lib.makeLibraryPath [ xlibs.libXScrnSaver qt4 alsaLib stdenv.gcc.gcc ]}:${stdenv.gcc.gcc}/lib64 \ $out/bin/spotify + + preload=$out/libexec/spotify/libpreload.so + mkdir -p $out/libexec/spotify + gcc -shared ${./preload.c} -o $preload -ldl -DOUT=\"$out\" + + wrapProgram $out/bin/spotify --set LD_PRELOAD $preload ''; # */ dontStrip = true; @@ -52,14 +58,6 @@ stdenv.mkDerivation { provides the Spotify client for Linux. At present, it does not work with free Spotify accounts; it requires a Premium or Unlimited account. - - Currently, the Spotify client requires a symlink from - /usr/share/spotify to its resources. Thus, you should do - something like: - - $ nix-env -i spotify - $ mkdir -p /usr/share - $ ln -s ~/.nix-profile/share/spotify /usr/share/ ''; }; } diff --git a/pkgs/applications/audio/spotify/preload.c b/pkgs/applications/audio/spotify/preload.c new file mode 100644 index 00000000000..d6e8fe41740 --- /dev/null +++ b/pkgs/applications/audio/spotify/preload.c @@ -0,0 +1,59 @@ +/* Spotify looks for its theme data in /usr/share/spotify/theme. This + LD_PRELOAD library intercepts open() and stat() calls to redirect + them to the corresponding location in $out. */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +char themeDir [] = "/usr/share/spotify/theme"; +char realThemeDir [] = OUT "/share/spotify/theme"; + +const char * rewrite(const char * path, char * buf) +{ + if (strncmp(path, themeDir, sizeof(themeDir) - 1) != 0) return path; + if (snprintf(buf, PATH_MAX, "%s%s", realThemeDir, path + sizeof(themeDir) - 1) >= PATH_MAX) + abort(); + return buf; +} + +int open(const char *path, int flags, ...) +{ + char buf[PATH_MAX]; + int (*_open) (const char *, int, mode_t) = dlsym(RTLD_NEXT, "open"); + mode_t mode = 0; + if (flags & O_CREAT) { + va_list ap; + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + } + return _open(rewrite(path, buf), flags, mode); +} + +int open64(const char *path, int flags, ...) +{ + char buf[PATH_MAX]; + int (*_open64) (const char *, int, mode_t) = dlsym(RTLD_NEXT, "open64"); + mode_t mode = 0; + if (flags & O_CREAT) { + va_list ap; + va_start(ap, flags); + mode = va_arg(ap, mode_t); + va_end(ap); + } + return _open64(rewrite(path, buf), flags, mode); +} + +int __xstat64(int ver, const char *path, struct stat64 *st) +{ + char buf[PATH_MAX]; + int (*___xstat64) (int ver, const char *, struct stat64 *) = dlsym(RTLD_NEXT, "__xstat64"); + return ___xstat64(ver, rewrite(path, buf), st); +}