GithubHelp home page GithubHelp logo

saturday06 / hsenv.sh Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 1.15 MB

(deprecated) Virtual Haskell Environment builder written in shellscript

License: BSD 3-Clause "New" or "Revised" License

Shell 64.68% Emacs Lisp 3.31% Assembly 0.26% Makefile 0.01% CSS 2.73% Groff 12.38% HTML 16.56% PHP 0.07%

hsenv.sh's Introduction

(Deprecated) Please use stack !

hsenv - Virtual Haskell Environment Build Status

What is it?

hsenv is a tool (inspired by Python's virtualenv) to create isolated Haskell environments. This is the clone of tmhedberg/hsenv written in shellscript.

What does it do?

It creates a sandboxed environment in a .hsenv/ subdirectory of your project, which, when activated, allows you to use regular Haskell tools (ghc, ghci, ghc-pkg, cabal) to manage your Haskell code and environment. It's possible to create an environment, that uses a different GHC version than your currently installed system GHC version. Very simple emacs integration mode is included.

Installation

  1. Check out hsenv.sh into ~/.hsenv.

    git clone https://github.com/saturday06/hsenv.sh.git ~/.hsenv
  2. Add ~/.hsenv/bin to your $PATH for access to the hsenv command-line utility.

    export PATH="$HOME/.hsenv/bin:$PATH"

Basic usage

First, choose a directory where you want to keep your sandboxed Haskell environment, usually a good choice is a directory containing your cabalized project (if you want to work on a few projects (perhaps an app and its dependent library), just choose any of them, it doesn't really matter). Enter that directory:

cd ~/projects/foo

Next, create your new isolated Haskell environment (this is a one-time-only (per environment) step):

hsenv

Now, every time you want to use this environment, you have to activate it:

source .hsenv/bin/activate

That's it! Now it's possible to use all regular Haskell tools like usual, but it won't affect your global/system Haskell environment, and also your per-user environment (from ~/.cabal and ~/.ghc) will stay the same. All cabal-installed packages will be private to this environment, and the external environments (global and user) will not affect it (this environment will only inherit very basic packages, mostly ghc and Cabal and their deps).

When you're done working with this environment, enter command deactivate_hsenv, or just close the current shell (with exit).

deactivate_hsenv

Named vs Unnamed Environments

By default, hsenv creates an "unnamed" environment, but sometimes for particular use cases you might want to create different environments under the same directory. This is achievable creating a "named" environment. To do that, just pass the flag --name=<ENVIRONMENT_NAME> to hsenv:

hsenv --name=<ENVIRONMENT_NAME>

This will make hsenv generate a folder of the form .hsenv_<ENVIRONMENT_NAME>.

Customization

If you want to customize activation and deactivation, create one or more of the following files in ~/.hsenv/bin/: pre-activate, post-activate, pre-deactivate, post-deactivate. These shell scripts will be sourced automatically by the main activation script.

Advanced usage

Here's the most advanced usage of hsenv. Let's say you want to:

  • Hack on a json library
  • Do so comfortably
  • Use your own version of the parsec library
  • And do all this using the nightly version of GHC

First, download the binary distribution of GHC for your platform (e.g. ghc-7.3.20111105-i386-unknown-linux.tar.bz2).

Create a directory for you environment:

mkdir /tmp/test
cd /tmp/test

Then, create a new environment using that GHC:

hsenv --name=test --ghc=/path/to/ghc-7.3.20111105-i386-unknown-linux.tar.bz2

Activate it:

source .hsenv_test/bin/activate

Download a copy of json library and your private version of parsec:

darcs get http://patch-tag.com/r/Paczesiowa/parsec
cabal unpack json

Install parsec:

cd parsec2
cabal install

Install the rest of the json deps:

cd ../json-0.5
cabal install --only-dependencies

Now, let's say you want to hack on Parsec module of json library. Open it in emacs:

emacsclient Text/JSON/Parsec.hs

Activate the virtual environment (hsenv must be required earlier):

M-x hsenv-activate <RET> /tmp/test/ <RET>

Edit some code and load it in ghci using 'C-c C-l'. If it type checks, you can play around with the code using nightly version of ghci running in your virtual environment. When you're happy with the code, exit emacs and install your edited json library:

cabal install

And that's it.

Fetching and downloading a remote version of GHC

Recent versions of hsenv include the possibility to automatically download and install a GHC version directly from the GHC main repository. To do that, all you need to do is to pass the desired version of GHC you want to install:

hsenv --ghc=7.4.1

Or a valid URL pointing to a compressed GHC archive:

hsenv --ghc=http://www.haskell.org/ghc/dist/7.6.3/ghc-7.6.3-x86_64-apple-darwin.tar.bz2

Misc

hsenv has been tested on Linux, Mac OS X, FreeBSD, and MSYS(1&2) systems, but it should work on any POSIX platform. The external (from tarball) GHC feature requires a binary GHC distribution compiled for your platform which that can be extracted with tar and installed with "./configure --prefix=PATH; make install".

FAQ

Q: Can I use it together with tools like cabal-dev or capri?

A: No. All these tools work more or less the same (wrapping cabal command, setting GHC_PACKAGE_PATH env variable), so something will probably break.

Q: Using GHC from tarball fails with a bunch of make tool gibberish on FreeBSD. What do I do?

A: Try the '--make-cmd=gmake' switch.

Q: Can I use hsenv inside hsenv?

A: No. It may be supported in future versions.

Q: Does it work on x64 systems?

A: Yes.

Q: Will it work on Mac?

A: Yes.

Q: Will it work on Windows?

A: Yes. It has been tested on MSYS and MSYS2.

Q: Does it require Bash?

A: No, it should work with any POSIX-compliant shell. It's been tested with bash, bash --posix, dash, zsh and ksh.

Q: Can I use it with a different haskell package repository than hackage?

A: Yes, just adjust the url in .hsenv/cabal/config file.

Q: How do I remove the whole virtual environment?

A: If it's activated - 'deactivate_hsenv' it. Then, delete the .hsenv/ directory.

Q: Is every environment completely separate from other environments and the system environment?

A: Yes. The only (minor) exception is ghci history - there's only one per user history file. Also, if you alter your system's GHC, then virtual environments using system's GHC copy will probably break. Virtual environments using GHC from a tarball should continue to work.

Q: Can I share one cabalized project directory among multiple environments (e.g. build a cabalized project in the same directory using different environments)?

A: Yes. hsenv also overrides cabal with a wrapper, that will force using different build directories, so there shouldn't be even any recompilation between different environments.

Q: Is it possible to activate/deactivate an environment upon entering/leaving its directory?

A: Yes, if you really know what you're doing. Here's a snippet for bash, which will activate both named and unnamed environments:

precmd() {
  local search_dir=$PWD
  while [ $search_dir != "/" ]; do
    local hsenv_found=false
    for dir in `cd $search_dir && find . -maxdepth 1 -type d -name ".hsenv*"`; do
      if $hsenv_found; then
        echo multiple environments in $search_dir , manual activaton required
        return        
      elif [ -n "$dir" ] && [ -e $search_dir/$dir/bin/activate ]; then
        hsenv_found=true
      fi
    done
    if ! $hsenv_found; then
      search_dir=`cd $search_dir/.. && pwd`
      continue
    fi
    if [ -n "$HSENV" ] && [ "$HSENV" != "$search_dir" ]; then
      deactivate_hsenv
    fi
    if [ -z "$HSENV" ]; then
      pwd_backup=$PWD
      cd $search_dir
      source .hsenv*/bin/activate
      cd $pwd_backup
    fi
    return
  done
  if [ -n "$HSENV" ]; then
    deactivate_hsenv
  fi
}
export PROMPT_COMMAND=precmd

hsenv.sh's People

Contributors

saturday06 avatar

Stargazers

Shigeki Karita avatar  avatar

Watchers

James Cloos avatar  avatar

hsenv.sh's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.