@@ -2,10 +2,11 @@
22
33module Main ( main ) where
44
5−import Cabal2Nix.Generate
5+import Cabal2Nix.Generate ( cabal2nix' )
66import Cabal2Nix.Hackage ( readHashedHackage, Hackage )
77-- import Cabal2Nix.Name
88import Cabal2Nix.Package
9+import Cabal2Nix.Flags ( configureCabalFlags )
910import Control.Monad
1011import Control.Monad.Par.Combinator
1112import Control.Monad.Par.IO
@@ -26,6 +27,9 @@ import Distribution.PackageDescription hiding ( buildDepends, extraLibs, buildTo
2627import Distribution.Text
2728import Distribution.Version
2829-- import Options.Applicative hiding ( empty )
30+import Distribution.PackageDescription.Configuration
31+import Distribution.System
32+import Distribution.Compiler
2933
3034main :: IO ()
3135main = do
@@ -71,6 +75,24 @@ generatePackage hackage nixpkgs name version descr = do
7175 srcSpec <- liftIO $ sourceFromHackage Nothing (name ++ "-" ++ display version)
7276 let Just cabalFileHash = lookup "x-cabal-file-hash" (customFieldsPD (packageDescription descr))
7377
78+ matches :: PackageIdentifier -> Dependency -> Bool
79+ matches (PackageIdentifier pn v) (Dependency dn vr) = pn == dn && v `withinRange` vr
80+
81+ resolver :: Dependency -> Bool
82+ resolver dep@(Dependency (PackageName pkg) vrange) =
83+ any (`matches` dep) corePackages ||
84+ maybe False (not . Map.null . Map.filterWithKey (\k _ -> k `withinRange` vrange)) (Map.lookup pkg hackage)
85+
86+ -- TODO: Include list of broken dependencies in the generated output.
87+ (missingDeps,drv') <- case cabal2nix resolver descr of
88+ Left ds -> do let Right x = cabal2nix (const True) descr
89+ return (ds,x)
90+ Right x -> return ([],x)
91+
92+ let drv = drv' { src = srcSpec
93+ , editedCabalFile = if revision drv == 0 then "" else cabalFileHash
94+ }
95+
7496 selectHackageNames :: Set String -> Set String
7597 selectHackageNames = Set.intersection (Map.keysSet hackage)
7698
@@ -95,11 +117,7 @@ generatePackage hackage nixpkgs name version descr = do
95117 overrides :: Doc
96118 overrides = conflictOverrides $+$ missingOverrides
97119
98− drv = (cabal2nix descr) { src = srcSpec
99− , editedCabalFile = if revision drv == 0 then "" else cabalFileHash
100− }
101−
102− return $ (drv { metaSection = (metaSection drv) { broken = not (Set.null missing) } }, overrides)
120+ return $ (drv { metaSection = (metaSection drv) { broken = not (Set.null missing) || not (null missingDeps) } }, overrides)
103121
104122isKnownNixpkgAttribute :: PackageMap -> Hackage -> String -> Bool
105123isKnownNixpkgAttribute nixpkgs hackage name
@@ -170,3 +188,50 @@ selectors = map (\s -> maybe (error (show s ++ " is not a valid package selector
170188-- ( fullDesc
171189-- <> header "hackage2nix -- convert the Hackage database into Nix build instructions"
172190-- )
191+
192+cabal2nix :: (Dependency -> Bool) -> GenericPackageDescription -> Either [Dependency] Derivation
193+cabal2nix resolver cabal = case finalize of
194+ Left ds -> Left ds
195+ Right (descr, flags) -> Right (cabal2nix' descr) { configureFlags = [ "-f" ++ (if b then "" else "-") ++ n | (FlagName n, b) <- flags ] }
196+ where
197+ finalize :: Either [Dependency] (PackageDescription, FlagAssignment)
198+ finalize = finalizePackageDescription
199+ (configureCabalFlags (package (packageDescription cabal)))
200+ resolver
201+ (Platform X86_64 Linux) -- shouldn't be hardcoded
202+ (CompilerId GHC (Version [7,8,4] [])) -- dito
203+ []
204+ cabal
205+
206+corePackages :: [PackageIdentifier]
207+corePackages = map (\s -> maybe (error (show s ++ " is not a valid core package")) id (simpleParse s))
208+ [ "Cabal-1.18.1.5"
209+ , "array-0.5.0.0"
210+ , "base-4.7.0.2"
211+ , "bin-package-db-0.0.0.0"
212+ , "binary-0.7.1.0"
213+ , "bytestring-0.10.4.0"
214+ , "containers-0.5.5.1"
215+ , "deepseq-1.3.0.2"
216+ , "directory-1.2.1.0"
217+ , "filepath-1.3.0.2"
218+ , "ghc-7.8.4"
219+ , "ghc-prim-0.3.1.0"
220+ , "haskeline-0.7.1.2"
221+ , "haskell2010-1.1.2.0"
222+ , "haskell98-2.0.0.3"
223+ , "hoopl-3.10.0.1"
224+ , "hpc-0.6.0.1"
225+ , "integer-gmp-0.5.1.0"
226+ , "old-locale-1.0.0.6"
227+ , "old-time-1.1.0.2"
228+ , "pretty-1.1.1.1"
229+ , "process-1.2.0.0"
230+ , "rts-1.0"
231+ , "template-haskell-2.9.0.0"
232+ , "terminfo-0.4.0.0"
233+ , "time-1.4.2"
234+ , "transformers-0.3.0.0"
235+ , "unix-2.7.0.1"
236+ , "xhtml-3000.2.1"
237+ ]