Skip to content

Add hpack as a built-tool so it could be invoked to get a cabal file #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/Cabal2Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import Data.Char (toUpper)
import System.FilePath
import Data.ByteString (ByteString)

import Distribution.Compat.Lens
import qualified Distribution.Types.BuildInfo.Lens as L
import Distribution.Types.CondTree
import Distribution.Types.Library
import Distribution.Types.ForeignLib
Expand Down Expand Up @@ -74,14 +76,21 @@ cabalFilePkgName = dropExtension . takeFileName . cabalFilePath
genExtra :: CabalFileGenerator -> NExpr
genExtra Hpack = mkNonRecSet [ "cabal-generator" $= mkStr "hpack" ]

amendGpdForGen :: CabalFileGenerator -> GenericPackageDescription -> GenericPackageDescription
amendGpdForGen Hpack gpd =
let hpackTool = ExeDependency "hpack" "hpack" anyVersion
in gpd & over L.traverseBuildInfos (& L.buildToolDepends %~ (hpackTool:))

cabal2nix :: Maybe Src -> CabalFile -> IO NExpr
cabal2nix src = \case
(OnDisk path) -> fmap (gpd2nix src Nothing)
$ readGenericPackageDescription normal path
(InMemory gen _ body) -> fmap (gpd2nix src (genExtra <$> gen))
$ case (runParseResult (parseGenericPackageDescription body)) of
(_, Left (_, err)) -> (error ("Failed to parse in-memory cabal file: " ++ show err))
(_, Right desc) -> pure desc
(_, Right desc) -> case gen of
Just g -> pure $ amendGpdForGen g desc
Nothing -> pure desc

gpd2nix :: Maybe Src -> Maybe NExpr -> GenericPackageDescription -> NExpr
gpd2nix src extra gpd = mkFunction args $ toNix gpd $//? (toNix <$> src) $//? extra
Expand Down