From a95d366047ee137bf04c970892436d71d29a07ba Mon Sep 17 00:00:00 2001
From: jD91mZM2 <me@krake.one>
Date: Tue, 11 Jun 2019 17:00:06 +0200
Subject: [PATCH] Fix PREFIX_BINARY for NixOS users

---
 mk/prefix.mk |  2 ++
 shell.nix    | 32 ++++++++++++++++++++++++++------
 2 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/mk/prefix.mk b/mk/prefix.mk
index 01e649fe8..19f49ff80 100644
--- a/mk/prefix.mk
+++ b/mk/prefix.mk
@@ -92,6 +92,8 @@ $(PREFIX)/gcc-install: $(PREFIX)/gcc-install.tar.gz
 	rm -rf "$@.partial" "$@"
 	mkdir -p "$@.partial"
 	tar --extract --file "$<" --directory "$@.partial" --strip-components=1
+	[ -n "$$NIX_INTERPRETER" ] && find "$@.partial" -executable -type f -exec \
+		patchelf --set-interpreter "$$NIX_INTERPRETER" "{}" \;
 	touch "$@.partial"
 	mv "$@.partial" "$@"
 
diff --git a/shell.nix b/shell.nix
index d314b073f..5536dc485 100644
--- a/shell.nix
+++ b/shell.nix
@@ -1,9 +1,29 @@
-with import <nixpkgs> {};
-stdenv.mkDerivation {
-  name = "redox";
-
+let
+  pkgs = import <nixpkgs> {
+    overlays = [
+      (import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz))
+    ];
+  };
+  rust = (pkgs.rustChannelOf {
+    date = "2019-04-06";
+    channel = "nightly";
+  }).rust;
+in pkgs.mkShell rec {
   hardeningDisable = [ "all" ];
 
-  nativeBuildInputs = [ gnumake cmake nasm pkgconfig gcc automake autoconf bison gperf qemu ];
-  buildInputs = [ openssl gettext libtool flex libpng perl perlPackages.HTMLParser ];
+  # used in mk/prefix.mk to patch interpreter when PREFIX_BINARY=1
+  NIX_INTERPRETER = "${pkgs.stdenv.cc.libc}/lib/ld-linux-x86-64.so.2";
+
+  LIBRARY_PATH = pkgs.lib.makeLibraryPath [
+    pkgs.gcc-unwrapped pkgs.stdenv.cc.libc
+    (toString prefix/x86_64-unknown-redox)
+  ];
+  LD_LIBRARY_PATH = LIBRARY_PATH;
+
+  nativeBuildInputs = with pkgs; [ gnumake cmake nasm pkgconfig gcc automake autoconf bison gperf qemu rust ];
+  buildInputs = with pkgs; [ fuse openssl gettext libtool flex libpng perl perlPackages.HTMLParser ];
+
+  shellHook = ''
+    export PATH="/run/wrappers/bin:$PATH"
+  '';
 }
-- 
GitLab