Skip to content

Instantly share code, notes, and snippets.

@proger
Last active January 13, 2017 23:29
Show Gist options
  • Save proger/15f5dbfd8a0f5124b1d9 to your computer and use it in GitHub Desktop.
Save proger/15f5dbfd8a0f5124b1d9 to your computer and use it in GitHub Desktop.
stack2nix
{ pkgs ? import (builtins.fetchTarball https://github.com/nixos/nixpkgs-channels/archive/nixos-unstable.tar.gz) {} }:
let
yaml = ./stack.yaml;
cabal2nix = "${pkgs.cabal2nix}/bin/cabal2nix --no-check --no-haddock";
in
pkgs.writeScript "build.sh" ''
export NIX_PATH=nixpkgs=${pkgs.path}
resolver=$(awk '/resolver:/{print $2}' ${yaml} | sed 's,\.,_,g')
echo '{ pkgs ? import (builtins.fetchTarball https://github.com/nixos/nixpkgs-channels/archive/nixos-unstable.tar.gz) {} }:'
echo '
let
inherit (pkgs) lib;
hs = pkgs.haskell.packages.'$resolver'.override { overrides = a: b: removeAttrs (overrides a b) ["process"]; };
overrides = self: super: {
'
for tuple in $(awk -v OFS='#' '/location:/ {l=1;next} l && /git:/ {git=$2;next} l && /commit:/ {commit=$2;next} git && commit && l { print git, commit; l=0}'< ${yaml}); do
_IFS=$IFS
IFS='#'
set -- $tuple
IFS=$_IFS
f=$(mktemp)
cabal2nix --revision $2 $1 > $f
name=$(sed -n '/pname/s/pname = "\(.*\)";$/\1/p' $f)
printf "%s = self.callPackage (%s) {};\n" $name "$(cat $f)"
rm -f $f
done
sed -n '/^extra-deps:/,/^[^-]/s|^- \(\(.*\)-\([0-9.]\+\)$\)|printf "%s = self.callPackage (%s) {};\\n" \2 "$(${cabal2nix} cabal://\1)"|p' ${yaml} | sh
echo '};
this =
'
${cabal2nix} .
echo ';
in pkgs.stdenv.lib.overrideDerivation
(hs.callPackage this { process = (overrides hs {}).process; })
(drv: {
configurePhase =
# allow mixing process-1.2 and process-1.3
builtins.replaceStrings ["exit 1"] [": exit 1"] drv.configurePhase;
})
'
''
@proger
Copy link
Author

proger commented Nov 13, 2015

Usage:

$(nix-build stack2.nix) > default.nix
nix-build

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment