From 8e1d85b5e5bd56d54ff6d463c8581c57ab5526d9 Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Fri, 25 May 2018 04:33:15 +0200 Subject: [PATCH] Use stack's nix support (#168) * Add stack --nix support * Update README for nix shell --- README.md | 11 +++++--- nix/src.json | 6 ++++ nix/update | 26 ++++++++++++++++++ shell.nix | 55 +++++++++++++++++++++++++++++++++++++ stack.yaml | 4 +++ tools/userchroot.nix | 65 -------------------------------------------- 6 files changed, 98 insertions(+), 69 deletions(-) create mode 100644 nix/src.json create mode 100755 nix/update create mode 100644 shell.nix delete mode 100644 tools/userchroot.nix diff --git a/README.md b/README.md index f5955ba..88ae229 100644 --- a/README.md +++ b/README.md @@ -108,11 +108,14 @@ After running the script to install system dependencies, build the project with ## Build on NixOS -`tools/userchroot.nix` expression contains definitions to open -chroot-environment containing necessary dependencies. Type +The `shell.nix` provides an environment containing the necessary +dependencies. To build, run: - $ nix-shell tools/userchroot.nix - $ stack build --system-ghc + $ stack --nix build + +or alternatively you can run + + $ nix-shell to enter the environment and build the project. Note, that it is an emulation of common Linux environment rather than full-featured Nix package expression. diff --git a/nix/src.json b/nix/src.json new file mode 100644 index 0000000..c7a3f45 --- /dev/null +++ b/nix/src.json @@ -0,0 +1,6 @@ +{ + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "729da53a147eda1a1678a313754fcda3a8791279", + "sha256": "17l19rbhb3wdrrcyjgq34bk3p658jbldzxp9q1scdvrvz0g99psb" +} diff --git a/nix/update b/nix/update new file mode 100755 index 0000000..734e09c --- /dev/null +++ b/nix/update @@ -0,0 +1,26 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i bash -p bash nix curl jq +# vim: filetype=sh +# Updates the nixpkgs.json to the latest release +# See https://gist.github.com/zimbatm/de5350245874361762b6a4dfe5366530 +set -euo pipefail + +cd "$(dirname "$0")" || exit 1 + +branch=release-17.09 + +owner=NixOS +repo=nixpkgs +rev=$(curl -sfL https://api.github.com/repos/$owner/$repo/git/refs/heads/$branch | jq -r .object.sha) +url=https://github.com/$owner/$repo/archive/$rev.tar.gz + +release_sha256=$(nix-prefetch-url --unpack "$url") + +cat < {}; + nixpkgs = _nixpkgs.fetchFromGitHub (_nixpkgs.lib.importJSON ./nix/src.json); + pkgs = import nixpkgs {}; + + # Either use specified GHC or use GHC 8.2.2 (which we need for LTS 11.9) + myghc = if isNull ghc then pkgs.haskell.compiler.ghc822 else ghc; + + # Fetch tensorflow library + tensorflow-c = pkgs.stdenv.mkDerivation { + name = "tensorflow-c"; + src = pkgs.fetchurl { + url = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-1.8.0.tar.gz"; + sha256 = "0qzy15rc3x961cyi3bqnygrcnw4x69r28xkwhpwrv1r0gi6k73ha"; + }; + + # Patch library to use our libc, libstdc++ and others + buildCommand = '' + . $stdenv/setup + mkdir -pv $out + tar -C $out -xzf $src + chmod +w $out/lib/libtensorflow.so + ${pkgs.patchelf}/bin/patchelf --set-rpath "${pkgs.stdenv.cc.libc}/lib:${pkgs.stdenv.cc.cc.lib}/lib" $out/lib/libtensorflow.so + chmod -w $out/lib/libtensorflow.so + ''; + }; + + # Wrapped stack executable that uses the nix-provided GHC + stack = pkgs.stdenv.mkDerivation { + name = "stack-system-ghc"; + builder = pkgs.writeScript "stack" '' + source $stdenv/setup + mkdir -p $out/bin + makeWrapper ${pkgs.stack}/bin/stack $out/bin/stack \ + --add-flags --system-ghc + ''; + buildInputs = [ pkgs.makeWrapper ]; + }; +in + pkgs.haskell.lib.buildStackProject { + ghc = myghc; + stack = stack; + name = "tf-env"; + buildInputs = + [ + pkgs.snappy + pkgs.zlib + pkgs.protobuf3_3 + tensorflow-c + stack + ]; + } diff --git a/stack.yaml b/stack.yaml index 6c8a0dd..cb655a1 100644 --- a/stack.yaml +++ b/stack.yaml @@ -29,3 +29,7 @@ extra-lib-dirs: - /usr/local/lib extra-include-dirs: - /usr/local/include + +nix: + enable: false + shell-file: shell.nix diff --git a/tools/userchroot.nix b/tools/userchroot.nix deleted file mode 100644 index 6394e23..0000000 --- a/tools/userchroot.nix +++ /dev/null @@ -1,65 +0,0 @@ -# -# This script is provided by @grwlf (grwlf@gmail.com) and is supported on best -# effort basis. TensorFlow development team does not regularly test this script -# and can't answer any questions about it. -# -{ pkgs ? import {} }: - -with pkgs; - -let - tensorflow-c = stdenv.mkDerivation { - - name = "tensorflow-c"; - - src = fetchurl { - url = "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-1.8.0.tar.gz"; - sha256 = "0a8e334d7c20879df9857c762472329d70cbf2f316af113d0b26f5c17209fe63"; - }; - - buildCommand = '' - . $stdenv/setup - mkdir -pv $out - tar -C $out -xzf $src - ''; - }; - - fhs = pkgs.buildFHSUserEnv { - name = "fhs"; - targetPkgs = pkgs: - with pkgs.python3Packages; - with pkgs.haskellPackages; - with pkgs; - assert stack.version >= "1.4"; - assert ghc.version >= "8"; - [ - snappy - snappy.dev - protobuf3_2 - stack - ghc - iana-etc - pkgs.zlib - pkgs.zlib.dev - tensorflow-c - gcc - gdb - ]; - runScript = "bash"; - profile = '' - export USE_CCACHE=1 - export LIBRARY_PATH=/usr/lib64 - export LD_LIBRARY_PATH=$(pwd):/usr/lib64 - export ACLOCAL_PATH=/usr/share/aclocal - export LANG=C - export LC_ALL=en_US.utf-8 - export PATH="$HOME/.local/bin:$PATH" - ''; - }; - -in -stdenv.mkDerivation { - name = fhs.name + "-env"; - nativeBuildInputs = [ fhs ]; - shellHook = "exec fhs"; -}