Merge pull request #174176 from hercules-ci/buildFromCabalSdist

haskellPackages: Add buildFromCabalSdist (faster, tested)
This commit is contained in:
Robert Hensing 2022-06-28 10:06:27 +02:00 committed by GitHub
commit d64780ea0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 106 additions and 0 deletions

View File

@ -300,6 +300,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.callPackage ./local/generated.nix {};
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,12 @@
# nix run ../../../../..#cabal2nix -- ./.
{ mkDerivation, base, lib }:
mkDerivation {
pname = "local";
version = "0.1.0.0";
src = ./.;
isLibrary = false;
isExecutable = true;
executableHaskellDepends = [ base ];
description = "Nixpkgs test case";
license = lib.licenses.mit;
}

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 { };