~evhan/beaker

6bfb584b378e2a07ff11a8ad9b377ad581d8eb0f — Evan Hanson 1 year, 8 months ago 9e6f782
Add "eggCache" and "eggProgram" Nix helpers
1 files changed, 67 insertions(+), 0 deletions(-)

A default.nix
A default.nix => default.nix +67 -0
@@ 0,0 1,67 @@
{ pkgs ? import <nixpkgs> {} }:

rec {
  repositoryPath = "lib/chicken/${toString pkgs.chicken.binaryVersion}";

  eggCache = {
    name ? "eggs",
    hash,
    overrideFile,
  }: with pkgs; runCommand name {
    buildInputs = [ chicken ];
    outputHash = hash;
    outputHashMode = "recursive";
    CHICKEN_EGG_CACHE = "eggs";
  } ''
    chicken-install -retrieve -recursive -from-list ${overrideFile}
    mv $CHICKEN_EGG_CACHE $out
    rm $out/*/STATUS
    rm $out/*/TIMESTAMP
  '';

  eggProgram = {
    name, src,
    buildInputs ? [],
    eggCache ? null,
    preUnpack ? "",
    preBuild ? "",
    ...
  } @ args: with pkgs; stdenv.mkDerivation {
    name = name;
    src = src;
    buildInputs = [ chicken makeWrapper ] ++ buildInputs;

    CHICKEN_EGG_CACHE = "/build/eggs";
    CHICKEN_INSTALL_PREFIX = "/build/out";
    CHICKEN_INSTALL_REPOSITORY = "/build/out/${repositoryPath}";
    CHICKEN_REPOSITORY_PATH = "/build/out/${repositoryPath}";

    preUnpack = lib.optionalString (eggCache != null) ''
      cp -R -L ${eggCache} $CHICKEN_EGG_CACHE
      chmod -R +w $CHICKEN_EGG_CACHE
    '' + preUnpack;

    preBuild = lib.optionalString (eggCache != null) ''
      chicken-install -cached $(ls $CHICKEN_EGG_CACHE)
    '' + preBuild;

    buildPhase = ''
      runHook preBuild
      chicken-install
      runHook postBuild
    '';

    installPhase = ''
      runHook preInstall
      mv $CHICKEN_INSTALL_PREFIX $out
      find $out/lib -type f -not -name \*.so -delete
      for bin in $out/bin/*; do
        wrapProgram $bin \
          --prefix CHICKEN_INCLUDE_PATH : $out/share \
          --prefix CHICKEN_REPOSITORY_PATH : $out/${repositoryPath} \
          --prefix LD_LIBRARY_PATH : ${chicken}/lib
      done
      runHook postInstall
    '';
  } // removeAttrs args [ "preUnpack" "preBuild" ];
}