Last active
August 29, 2015 14:09
-
-
Save 4z3/866dbdb8104b357e8235 to your computer and use it in GitHub Desktop.
nix development environment example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# usage: | |
# nix-env -f env.nix -i | |
# load-env-example-1 | |
# | |
# If the directory ~/.history/ exists, then the development environment | |
# will use a dedicated shell history. | |
# | |
# Note: this is currently only compatible with nixos-14.11 (nixos-unstable). | |
# | |
{ nixpkgs ? import <nixpkgs> {} }: | |
let | |
pname = "example"; | |
version = "1"; | |
buildInputs = with pkgs; [ | |
hsEnv | |
closurecompiler | |
]; | |
extraCmds = with pkgs; '' | |
if test -d "\$HOME/.history"; then | |
export HISTFILE="\$HOME/.history/env-${pname}" | |
fi | |
$(grep export ${hsEnv.outPath}/bin/ghc) | |
${mkExports staticPkgs} | |
''; | |
hsEnv = hsPkgs.ghcWithPackages (self: with self; | |
example.nativeBuildInputs ++ | |
[ | |
cabalInstall | |
] | |
); | |
hsPkgs = | |
let | |
hsPkgs0 = pkgs.haskellPackages_ghc783_profiling; | |
callPackage = pkgs.lib.callPackageWith hsPkgs; | |
in | |
pkgs.recurseIntoAttrs (hsPkgs0.override { | |
extension = self: super: rec { | |
# generate with `cabal2nix .` (requires example.cabal) | |
example = callPackage ./pkgs/example.nix { | |
transformers = transformers_0_4; | |
}; | |
# generate with `cabal2nix cabal://transformers-0.4.1.0` | |
transformers_0_4 = callPackage ./pkgs/transformers-0.4.1.0.nix {}; | |
# generate with `cabal2nix git://github.com/4z3/xintmap.git` | |
xintmap = callPackage ./pkgs/xintmap.nix {}; | |
}; | |
}); | |
pkgs = nixpkgs // staticPkgs; | |
# Static packages will be made available as variables in the development environment, e.g. $d3js | |
staticPkgs = with nixpkgs; { | |
d3js = #{{{ | |
pkgs.stdenv.mkDerivation rec { | |
name = "d3js-${version}"; | |
version = "3.4.13"; | |
src = pkgs.fetchzip { | |
url = "https://github.com/mbostock/d3/archive/v${version}.zip"; | |
sha256 = "1m8alxrll7siryig8z994qxc1hy99d2xfslr9w0jn98m8qh6szfx"; | |
}; | |
buildInputs = [ ]; | |
phases = [ "installPhase" ]; | |
installPhase = '' | |
mkdir -p $out | |
cp -r $src/d3.js $out/ | |
cp -r $src/d3.min.js $out/ | |
''; | |
} | |
; #}}} | |
}; | |
#{{{ mkExports : set -> string | |
# Create shell script that exports a set's attributes. | |
mkExports = set: with builtins; with pkgs.lib.strings; | |
let | |
# XXX attribute names are not escaped, they have to be sane | |
# XXX the value should not contain <newline> | |
mkExport = k: "export ${k}=${escapeSh (getAttr k set)}"; | |
escapeSh = stringAsChars (c: "\\${c}"); | |
in | |
concatStringsSep "\n" (map mkExport (attrNames set)); | |
#}}} | |
in pkgs.myEnvFun { | |
name = "${pname}-${version}"; | |
inherit buildInputs extraCmds; | |
} | |
# vim: set fdm=marker : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment