haskellPackages: Add buildFromCabalSdist (faster, tested)

This commit is contained in:
Robert Hensing 2022-05-23 21:39:01 +02:00
parent d06c3e06e5
commit cf5e2d5103
7 changed files with 94 additions and 0 deletions

View file

@ -299,6 +299,9 @@ rec {
directly. The effect is that the package is built as if it were published
on hackage. This can be used as a test for the source distribution,
assuming the build fails when packaging mistakes are in the cabal file.
A faster implementation using `cabal-install` is available as
`buildFromCabalSdist` in your Haskell package set.
*/
buildFromSdist = pkg: overrideCabal (drv: {
src = "${sdistTarball pkg}/${pkg.pname}-${pkg.version}.tar.gz";

View file

@ -538,4 +538,44 @@ in package-set { inherit pkgs lib callPackage; } self // {
withHoogle = self.ghcWithHoogle;
};
/*
Run `cabal sdist` on a source.
Unlike `haskell.lib.sdistTarball`, this does not require any dependencies
to be present, as it uses `cabal-install` instead of building `Setup.hs`.
This makes `cabalSdist` faster than `sdistTarball`.
*/
cabalSdist = {
src,
name ? if src?name then "${src.name}-sdist.tar.gz" else "source.tar.gz"
}:
pkgs.runCommandNoCCLocal name
{
inherit src;
nativeBuildInputs = [ buildHaskellPackages.cabal-install ];
dontUnpack = false;
} ''
unpackPhase
cd "''${sourceRoot:-.}"
patchPhase
mkdir out
HOME=$PWD cabal sdist --output-directory out
mv out/*.tar.gz $out
'';
/*
Like `haskell.lib.buildFromSdist`, but using `cabal sdist` instead of
building `./Setup`.
Unlike `haskell.lib.buildFromSdist`, this does not require any dependencies
to be present. This makes `buildFromCabalSdist` faster than `haskell.lib.buildFromSdist`.
*/
buildFromCabalSdist = pkg:
haskellLib.overrideSrc
{
src = self.cabalSdist { inherit (pkg) src; };
version = pkg.version;
}
pkg;
}

View file

@ -0,0 +1,28 @@
{ lib, haskellPackages, runCommand }:
let
localRaw = haskellPackages.callCabal2nix "local" ./local {};
in
lib.recurseIntoAttrs rec {
helloFromCabalSdist = haskellPackages.buildFromCabalSdist haskellPackages.hello;
# A more complicated example with a cabal hook.
hercules-ci-cnix-store = haskellPackages.buildFromCabalSdist haskellPackages.hercules-ci-cnix-store;
localFromCabalSdist = haskellPackages.buildFromCabalSdist localRaw;
assumptionLocalHasDirectReference = runCommand "localHasDirectReference" {
drvPath = builtins.unsafeDiscardOutputDependency localRaw.drvPath;
} ''
grep ${./local} $drvPath >/dev/null
touch $out
'';
localHasNoDirectReference = runCommand "localHasNoDirectReference" {
drvPath = builtins.unsafeDiscardOutputDependency localFromCabalSdist.drvPath;
} ''
grep -v ${./local} $drvPath >/dev/null
touch $out
'';
}

View file

@ -0,0 +1,5 @@
# Revision history for local
## 0.1.0.0 -- YYYY-mm-dd
* First version. Released on an unsuspecting world.

View file

@ -0,0 +1,4 @@
module Main where
main :: IO ()
main = putStrLn "Hello, Haskell!"

View file

@ -0,0 +1,13 @@
cabal-version: 2.4
name: local
version: 0.1.0.0
synopsis: Nixpkgs test case
license: MIT
extra-source-files: CHANGELOG.md
executable local
main-is: Main.hs
build-depends: base
hs-source-dirs: app
default-language: Haskell2010

View file

@ -2,6 +2,7 @@
lib.recurseIntoAttrs {
shellFor = callPackage ./shellFor { };
cabalSdist = callPackage ./cabalSdist { };
documentationTarball = callPackage ./documentationTarball { };
setBuildTarget = callPackage ./setBuildTarget { };
writers = callPackage ./writers { };