Skip to content

Commit c20baa2

Browse files
committed
Store per-component info in 'Derivation'
We delay the folding of components to pretty printing
1 parent 2ceff8f commit c20baa2

5 files changed

Lines changed: 84 additions & 68 deletions

File tree

cabal2nix/src/Cabal2nix.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() ->
175175

176176
hpackOverrides :: Derivation -> Derivation
177177
hpackOverrides = over phaseOverrides (++ "prePatch = \"hpack\";")
178-
. set (libraryDepends . tool . contains (PP.pkg "hpack")) True
178+
. set (focusBuildInfo libraryDepends . tool . contains (PP.pkg "hpack")) True
179179

180180
cabal2nix' :: Options -> IO (Either Doc Derivation)
181181
cabal2nix' opts@Options{..} = do

cabal2nix/src/Distribution/Nixpkgs/Haskell/Derivation.hs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE DeriveGeneric #-}
2+
{-# LANGUAGE RankNTypes #-}
23
{-# LANGUAGE RecordWildCards #-}
34
{-# LANGUAGE TemplateHaskell #-}
45
{-# LANGUAGE TupleSections #-}
@@ -10,6 +11,7 @@ module Distribution.Nixpkgs.Haskell.Derivation
1011
, cabalFlags, runHaddock, jailbreak, doCheck, doBenchmark, testFlags, testTargets, hyperlinkSource
1112
, enableLibraryProfiling, enableExecutableProfiling, phaseOverrides, editedCabalFile, metaSection
1213
, dependencies, setupDepends, benchmarkDepends, enableSeparateDataOutput, extraAttributes
14+
, focusBuildInfo
1315
)
1416
where
1517

@@ -47,10 +49,10 @@ data Derivation = MkDerivation
4749
, _extraFunctionArgs :: Set Binding
4850
, _extraAttributes :: Map String String
4951
, _setupDepends :: BuildInfo
50-
, _libraryDepends :: BuildInfo
51-
, _executableDepends :: BuildInfo
52-
, _testDepends :: BuildInfo
53-
, _benchmarkDepends :: BuildInfo
52+
, _libraryDepends :: [(BuildInfo, Bool)]
53+
, _executableDepends :: [(BuildInfo, Bool)]
54+
, _testDepends :: [(BuildInfo, Bool)]
55+
, _benchmarkDepends :: [(BuildInfo, Bool)]
5456
, _configureFlags :: Set String
5557
, _cabalFlags :: FlagAssignment
5658
, _runHaddock :: Bool
@@ -103,7 +105,14 @@ nullDerivation = MkDerivation
103105

104106
makeLenses ''Derivation
105107

106-
makeLensesFor (fmap (,"dependencies") ["_setupDepends", "_libraryDepends", "_executableDepends", "_testDepends", "_benchmarkDepends"]) ''Derivation
108+
makeLensesFor (fmap (,"nonSetupDependencies") ["_libraryDepends", "_executableDepends", "_testDepends", "_benchmarkDepends"]) ''Derivation
109+
110+
dependencies :: Traversal' Derivation BuildInfo
111+
dependencies = traversal $ \focus drv ->
112+
liftA2 (set setupDepends) (focus $ view setupDepends drv) ((nonSetupDependencies . traverse . _1) focus drv)
113+
114+
focusBuildInfo :: Lens' Derivation [(BuildInfo, Bool)] -> Traversal' Derivation BuildInfo
115+
focusBuildInfo l = l . traverse . _1
107116

108117
instance Package Derivation where
109118
packageId = view pkgid
@@ -125,10 +134,10 @@ instance Pretty Derivation where
125134
, boolattr "isExecutable" (not _isLibrary || _isExecutable) _isExecutable
126135
, boolattr "enableSeparateDataOutput" _enableSeparateDataOutput _enableSeparateDataOutput
127136
, pPrintBuildInfo "setup" _setupDepends
128-
, pPrintBuildInfo "library" _libraryDepends
129-
, pPrintBuildInfo "executable" _executableDepends
130-
, pPrintBuildInfo "test" _testDepends
131-
, pPrintBuildInfo "benchmark" _benchmarkDepends
137+
, pPrintBuildInfo' "library" _libraryDepends
138+
, pPrintBuildInfo' "executable" _executableDepends
139+
, pPrintBuildInfo' "test" _testDepends
140+
, pPrintBuildInfo' "benchmark" _benchmarkDepends
132141
, boolattr "enableLibraryProfiling" _enableLibraryProfiling _enableLibraryProfiling
133142
, boolattr "enableExecutableProfiling" _enableExecutableProfiling _enableExecutableProfiling
134143
, boolattr "doHaddock" (not _runHaddock) _runHaddock
@@ -145,6 +154,9 @@ instance Pretty Derivation where
145154
, rbrace
146155
]
147156
where
157+
pPrintBuildInfo' :: String -> [(BuildInfo, Bool)] -> Doc
158+
pPrintBuildInfo' name = pPrintBuildInfo name . foldMap fst . filter snd
159+
148160
inputs :: Set String
149161
inputs = Set.unions [ Set.map (view (localName . ident)) _extraFunctionArgs
150162
, setOf (dependencies . each . folded . localName . ident) drv

cabal2nix/src/Distribution/Nixpkgs/Haskell/FromCabal.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE OverloadedStrings #-}
33
{-# LANGUAGE RecordWildCards #-}
4+
{-# LANGUAGE TupleSections #-}
45

56
module Distribution.Nixpkgs.Haskell.FromCabal
67
( HaskellResolver, NixpkgsResolver
@@ -163,8 +164,8 @@ fromPackageDescription haskellResolver nixpkgsResolver missingDeps flags Package
163164
& Nix.broken .~ not (null missingDeps)
164165
)
165166
where
166-
deps :: (a -> Cabal.BuildInfo) -> [a] -> Nix.BuildInfo
167-
deps getBuildInfo = foldMap (convertBuildInfo . getBuildInfo)
167+
deps :: (a -> Cabal.BuildInfo) -> [a] -> [(Nix.BuildInfo, Bool)]
168+
deps getBuildInfo = map (convertBuildInfo . getBuildInfo)
168169

169170
xrev = maybe 0 read (lookup "x-revision" customFieldsPD)
170171

@@ -211,9 +212,8 @@ fromPackageDescription haskellResolver nixpkgsResolver missingDeps flags Package
211212
| Just l <- library = not (null (exposedModules l))
212213
| otherwise = True
213214

214-
convertBuildInfo :: Cabal.BuildInfo -> Nix.BuildInfo
215-
convertBuildInfo Cabal.BuildInfo {..} | not buildable = mempty
216-
convertBuildInfo Cabal.BuildInfo {..} = mempty
215+
convertBuildInfo :: Cabal.BuildInfo -> (Nix.BuildInfo, Bool)
216+
convertBuildInfo Cabal.BuildInfo {..} = (, buildable) $ mempty
217217
& haskell .~ Set.fromList [ resolveInHackage (toNixName x) | (Dependency x _ _) <- targetBuildDepends, x `notElem` internalLibNames ]
218218
& system .~ Set.fromList [ resolveInNixpkgs y | x <- extraLibs, y <- libNixName x ]
219219
& pkgconfig .~ Set.fromList [ resolveInNixpkgs y | PkgconfigDependency x _ <- pkgconfigDepends, y <- libNixName (unPkgconfigName x) ]

cabal2nix/src/Distribution/Nixpkgs/Haskell/FromCabal/Normalize.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
{-# LANGUAGE FlexibleContexts #-}
12
{-# LANGUAGE OverloadedStrings #-}
3+
{-# LANGUAGE RankNTypes #-}
24

35
module Distribution.Nixpkgs.Haskell.FromCabal.Normalize ( normalize ) where
46

@@ -19,7 +21,8 @@ normalize drv = drv
1921
& over metaSection normalizeMeta
2022
& jailbreak %~ (&& (packageName drv /= "jailbreak-cabal"))
2123
where
22-
deps f = over f $ normalizeBuildInfo (packageName drv)
24+
deps :: Lens' Derivation [(BuildInfo, Bool)] -> Derivation -> Derivation
25+
deps f = over (focusBuildInfo f) $ normalizeBuildInfo (packageName drv)
2326

2427
normalizeBuildInfo :: PackageName -> BuildInfo -> BuildInfo
2528
normalizeBuildInfo pname bi = bi

0 commit comments

Comments
 (0)