git.iko.soy/typeable/cabal2nix
on master help sign in
Commit to Cabal 3.x.
commit5efdfe042c0b787951277e60ce69704041bfc151 authorPeter Simons <simons@cryp.to> dateSat 21 Sep 2019, 16:46 (7 years ago) parent449ad2b
9 files changed+62−41
cabal2nix.cabal+2−2 hackage2nix/Main.hs+8−7 src/Cabal2nix.hs+8−10 src/Distribution/Nixpkgs/Haskell/Constraint.hs+1−1 src/Distribution/Nixpkgs/Haskell/Derivation.hs+2−2 src/Distribution/Nixpkgs/Haskell/FromCabal.hs+7−7 src/Distribution/Nixpkgs/Haskell/FromCabal/PostProcess.hs+1−1 src/Distribution/Nixpkgs/Haskell/OrphanInstances.hs+24−3 src/Distribution/Nixpkgs/Haskell/PackageSourceSpec.hs+9−8
cabal2nix.cabal+2−2
@@ -1,5 +1,5 @@
11name: cabal2nix
2−version: 2.14.4
2+version: 2.15.0
33synopsis: Convert Cabal files into Nix build instructions.
44description:
55 Convert Cabal files into Nix build instructions. Users of Nix can install the latest
@@ -108,7 +108,7 @@ library
108108 other-modules: Paths_cabal2nix
109109 hs-source-dirs: src
110110 build-depends: base > 4.11
111− , Cabal > 2.4
111+ , Cabal > 3
112112 , aeson > 1
113113 , ansi-wl-pprint
114114 , bytestring
hackage2nix/Main.hs+8−7
@@ -23,6 +23,7 @@ import Distribution.Nixpkgs.Haskell.Constraint
2323import Distribution.Nixpkgs.Haskell.FromCabal
2424import Distribution.Nixpkgs.Haskell.FromCabal.Configuration as Config
2525import Distribution.Nixpkgs.Haskell.FromCabal.Flags
26+import Distribution.Nixpkgs.Haskell.OrphanInstances ( )
2627import Distribution.Nixpkgs.Meta
2728import Distribution.Nixpkgs.PackageMap
2829import Distribution.Package
@@ -62,7 +63,7 @@ main = do
6263 pinfo :: ParserInfo CLI
6364 pinfo = info
6465 ( helper
65− <*> infoOption ("hackage2nix " ++ display Main.version) (long "version" <> help "Show version number")
66+ <*> infoOption ("hackage2nix " ++ prettyShow Main.version) (long "version" <> help "Show version number")
6667 <*> cliOptions
6768 )
6869 ( fullDesc
@@ -92,7 +93,7 @@ main = do
9293 latestVersionSet = Map.map Set.findMax (Map.filter (not . Set.null) (Map.mapWithKey (enforcePreferredVersions preferredVersions) hackage))
9394
9495 defaultPackageOverridesSet :: PackageSet
95− defaultPackageOverridesSet = Map.fromList [ (name, resolveConstraint c hackage) | c@(Dependency name _) <- defaultPackageOverrides config ]
96+ defaultPackageOverridesSet = Map.fromList [ (name, resolveConstraint c hackage) | c@(Dependency name _ _) <- defaultPackageOverrides config ]
9697
9798 generatedDefaultPackageSet :: PackageSet
9899 generatedDefaultPackageSet = (defaultPackageOverridesSet `Map.union` latestVersionSet) `Map.difference` corePackageSet
@@ -105,7 +106,7 @@ main = do
105106
106107 extraPackageSet :: PackageMultiSet
107108 extraPackageSet = Map.unionsWith Set.union
108− [ Map.singleton name (Set.singleton (resolveConstraint c hackage)) | c@(Dependency name _) <- extraPackages config ]
109+ [ Map.singleton name (Set.singleton (resolveConstraint c hackage)) | c@(Dependency name _ _) <- extraPackages config ]
109110
110111 db :: PackageMultiSet
111112 db = Map.unionsWith Set.union [ Map.map Set.singleton generatedDefaultPackageSet
@@ -115,7 +116,7 @@ main = do
115116 ]
116117
117118 haskellResolver :: Dependency -> Bool
118− haskellResolver (Dependency name vrange)
119+ haskellResolver (Dependency name vrange _)
119120 | Just v <- Map.lookup name corePackageSet = v `withinRange` vrange
120121 | Just v <- Map.lookup name generatedDefaultPackageSet = v `withinRange` vrange
121122 | otherwise = False
@@ -137,7 +138,7 @@ main = do
137138 let isInDefaultPackageSet, isHydraEnabled, isBroken :: Bool
138139 isInDefaultPackageSet = (== Just v) (Map.lookup name generatedDefaultPackageSet)
139140 isHydraEnabled = isInDefaultPackageSet && not (isBroken || name `Set.member` dontDistributePackages config)
140− isBroken = any (withinRange v) [ vr | Dependency pn vr <- brokenPackages config, pn == name ]
141+ isBroken = any (withinRange v) [ vr | Dependency pn vr _ <- brokenPackages config, pn == name ]
141142
142143 droppedPlatforms :: Set Platform
143144 droppedPlatforms = Map.findWithDefault mempty name (unsupportedPlatforms config)
@@ -164,7 +165,7 @@ main = do
164165 & metaSection.homepage .~ ""
165166
166167 overrides :: Doc
167− overrides = fcat $ punctuate space [ disp b <> semi | b <- Set.toList (view (dependencies . each) drv `Set.union` view extraFunctionArgs drv), not (isFromHackage b) ]
168+ overrides = fcat $ punctuate space [ pPrint b <> semi | b <- Set.toList (view (dependencies . each) drv `Set.union` view extraFunctionArgs drv), not (isFromHackage b) ]
168169 return $ render $ nest 2 $
169170 hang (doubleQuotes (text attr) <+> equals <+> text "callPackage") 2 (parens (pPrint drv)) <+> (braces overrides <> semi)
170171
@@ -204,7 +205,7 @@ resolveConstraint c = fromMaybe (error msg) . resolveConstraint' c
204205 ]
205206
206207resolveConstraint' :: Constraint -> Hackage -> Maybe Version
207−resolveConstraint' (Dependency name vrange) hackage
208+resolveConstraint' (Dependency name vrange _) hackage
208209 | Just vset' <- Map.lookup name hackage
209210 , vset <- Set.filter (`withinRange` vrange) vset'
210211 , not (Set.null vset) = Just (Set.findMax vset)
src/Cabal2nix.hs+8−10
@@ -18,7 +18,6 @@ import Data.Monoid ( (<>) )
1818import qualified Data.Set as Set
1919import Data.String
2020import Data.Time
21−import qualified Distribution.Compat.ReadP as P
2221import Distribution.Compiler
2322import Distribution.Nixpkgs.Fetch
2423import Distribution.Nixpkgs.Haskell
@@ -26,13 +25,14 @@ import Distribution.Nixpkgs.Haskell.FromCabal
2625import Distribution.Nixpkgs.Haskell.FromCabal.Flags
2726import qualified Distribution.Nixpkgs.Haskell.FromCabal.PostProcess as PP (pkg)
2827import qualified Distribution.Nixpkgs.Haskell.Hackage as DB
28+import Distribution.Nixpkgs.Haskell.OrphanInstances ( )
2929import Distribution.Nixpkgs.Haskell.PackageSourceSpec
3030import Distribution.Nixpkgs.Meta
31−import Distribution.PackageDescription ( mkFlagName, mkFlagAssignment, FlagAssignment )
3231import Distribution.Package ( packageId )
32+import Distribution.PackageDescription ( mkFlagName, mkFlagAssignment, FlagAssignment )
33+import Distribution.Parsec as P
3334import Distribution.Simple.Utils ( lowercase )
3435import Distribution.System
35−import Distribution.Text
3636import Language.Nix
3737import Options.Applicative
3838import Paths_cabal2nix ( version )
@@ -94,8 +94,8 @@ options = Options
9494 <*> optional (strOption $ long "hackage-db" <> metavar "PATH" <> help "path to the local hackage db in tar format")
9595 <*> switch (long "shell" <> help "generate output suitable for nix-shell")
9696 <*> many (strOption $ short 'f' <> long "flag" <> help "Cabal flag (may be specified multiple times)")
97− <*> option (readP parse) (long "compiler" <> help "compiler to use when evaluating the Cabal file" <> value buildCompilerId <> showDefaultWith display)
98− <*> option (maybeReader parsePlatform) (long "system" <> help "host system (in either short Nix format or full LLVM style) to use when evaluating the Cabal file" <> value buildPlatform <> showDefaultWith display)
97+ <*> option parseCabal (long "compiler" <> help "compiler to use when evaluating the Cabal file" <> value buildCompilerId <> showDefaultWith prettyShow)
98+ <*> option (maybeReader parsePlatform) (long "system" <> help "host system (in either short Nix format or full LLVM style) to use when evaluating the Cabal file" <> value buildPlatform <> showDefaultWith prettyShow)
9999 <*> optional (strOption $ long "subpath" <> metavar "PATH" <> help "Path to Cabal file's directory relative to the URI (default is root directory)")
100100 <*> optional (option utcTimeReader (long "hackage-snapshot" <> help "hackage snapshot time, ISO format"))
101101 <*> pure (\i -> Just (binding # (i, path # [ident # "pkgs", i])))
@@ -110,10 +110,8 @@ utcTimeReader = eitherReader $ \arg ->
110110 Nothing -> Left $ "Cannot parse date, ISO format used ('2017-11-20T12:18:35Z'): " ++ arg
111111 Just utcTime -> Right utcTime
112112
113−readP :: P.ReadP a a -> ReadM a
114−readP p = eitherReader $ \s -> case [ r' | (r',"") <- P.readP_to_S p s ] of
115− (r:_) -> Right r
116− _ -> Left ("invalid value " ++ show s)
113+parseCabal :: Parsec a => ReadM a
114+parseCabal = eitherReader eitherParsec
117115
118116-- | Replicate the normalization performed by GHC_CONVERT_CPU in GHC's aclocal.m4
119117-- since the output of that is what Cabal parses.
@@ -164,7 +162,7 @@ parsePlatformParts = \case
164162pinfo :: ParserInfo Options
165163pinfo = info
166164 ( helper
167− <*> infoOption ("cabal2nix " ++ display version) (long "version" <> help "Show version number")
165+ <*> infoOption ("cabal2nix " ++ prettyShow version) (long "version" <> help "Show version number")
168166 <*> options
169167 )
170168 ( fullDesc
src/Distribution/Nixpkgs/Haskell/Constraint.hs+1−1
@@ -9,7 +9,7 @@ import Distribution.Nixpkgs.Haskell.OrphanInstances ( )
99type Constraint = Dependency
1010
1111satisfiesConstraint :: PackageIdentifier -> Constraint -> Bool
12−satisfiesConstraint (PackageIdentifier pn v) (Dependency cn vr) = (pn /= cn) || (v `withinRange` vr)
12+satisfiesConstraint (PackageIdentifier pn v) (Dependency cn vr _) = (pn /= cn) || (v `withinRange` vr)
1313
1414satisfiesConstraints :: PackageIdentifier -> [Constraint] -> Bool
1515satisfiesConstraints p = all (satisfiesConstraint p)
src/Distribution/Nixpkgs/Haskell/Derivation.hs+2−2
@@ -112,8 +112,8 @@ instance Pretty Derivation where
112112 pPrint drv@MkDerivation {..} = funargs (map text ("mkDerivation" : toAscList inputs)) $$ vcat
113113 [ text "mkDerivation" <+> lbrace
114114 , nest 2 $ vcat
115− [ attr "pname" $ doubleQuotes $ disp (packageName _pkgid)
116− , attr "version" $ doubleQuotes $ disp (packageVersion _pkgid)
115+ [ attr "pname" $ doubleQuotes $ pPrint (packageName _pkgid)
116+ , attr "version" $ doubleQuotes $ pPrint (packageVersion _pkgid)
117117 , pPrint _src
118118 , onlyIf (_subpath /= ".") $ attr "postUnpack" postUnpack
119119 , onlyIf (_revision > 0) $ attr "revision" $ doubleQuotes $ int _revision
src/Distribution/Nixpkgs/Haskell/FromCabal.hs+7−7
@@ -58,14 +58,14 @@ finalizeGenericPackageDescription haskellResolver arch compiler flags constraint
5858 }
5959
6060 jailbroken :: HaskellResolver -> HaskellResolver
61− jailbroken resolver (Dependency pkg _) = resolver (Dependency pkg anyVersion)
61+ jailbroken resolver (Dependency pkg _ _) = resolver (Dependency pkg anyVersion mempty)
6262
6363 withInternalLibs :: HaskellResolver -> HaskellResolver
6464 withInternalLibs resolver d = depPkgName d `elem` internalNames || resolver d
6565
6666 internalNames :: [PackageName]
6767 internalNames = [ unqualComponentNameToPackageName n | (n,_) <- condSubLibraries genDesc ]
68− ++ [ unqualComponentNameToPackageName n | Just n <- libName <$> subLibraries (packageDescription genDesc) ]
68+ ++ [ unqualComponentNameToPackageName n | LSubLibName n <- libName <$> subLibraries (packageDescription genDesc) ]
6969
7070 in case finalize (jailbroken (withInternalLibs haskellResolver)) of
7171 Left m -> case finalize (const True) of
@@ -119,7 +119,7 @@ fromPackageDescription haskellResolver nixpkgsResolver missingDeps flags Package
119119 nixLicense = either fromSPDXLicense fromCabalLicense licenseRaw
120120
121121 resolveInHackage :: Identifier -> Binding
122− resolveInHackage i | (i^.ident) `elem` [ unPackageName n | (Dependency n _) <- missingDeps ] = bindNull i
122+ resolveInHackage i | (i^.ident) `elem` [ unPackageName n | (Dependency n _ _) <- missingDeps ] = bindNull i
123123 | otherwise = binding # (i, path # ["self",i]) -- TODO: "self" shouldn't be hardcoded.
124124
125125 -- TODO: This is all very confusing. Haskell packages refer to the Nixpkgs
@@ -144,11 +144,11 @@ fromPackageDescription haskellResolver nixpkgsResolver missingDeps flags Package
144144 | otherwise = bindNull i
145145
146146 resolveInHackageThenNixpkgs :: Identifier -> Binding
147− resolveInHackageThenNixpkgs i | haskellResolver (Dependency (mkPackageName (i^.ident)) anyVersion) = resolveInHackage i
147+ resolveInHackageThenNixpkgs i | haskellResolver (Dependency (mkPackageName (i^.ident)) anyVersion mempty) = resolveInHackage i
148148 | otherwise = resolveInNixpkgs i
149149
150150 internalLibNames :: [PackageName]
151− internalLibNames = fmap unqualComponentNameToPackageName . catMaybes $ libName <$> subLibraries
151+ internalLibNames = [ unqualComponentNameToPackageName n | LSubLibName n <- libName <$> subLibraries ]
152152
153153 doHaddockPhase :: Bool
154154 doHaddockPhase | not (null internalLibNames) = False
@@ -158,7 +158,7 @@ fromPackageDescription haskellResolver nixpkgsResolver missingDeps flags Package
158158 convertBuildInfo :: Cabal.BuildInfo -> Nix.BuildInfo
159159 convertBuildInfo Cabal.BuildInfo {..} | not buildable = mempty
160160 convertBuildInfo Cabal.BuildInfo {..} = mempty
161− & haskell .~ Set.fromList [ resolveInHackage (toNixName x) | (Dependency x _) <- targetBuildDepends, x `notElem` internalLibNames ]
161+ & haskell .~ Set.fromList [ resolveInHackage (toNixName x) | (Dependency x _ _) <- targetBuildDepends, x `notElem` internalLibNames ]
162162 & system .~ Set.fromList [ resolveInNixpkgs y | x <- extraLibs, y <- libNixName x ]
163163 & pkgconfig .~ Set.fromList [ resolveInNixpkgs y | PkgconfigDependency x _ <- pkgconfigDepends, y <- libNixName (unPkgconfigName x) ]
164164 & tool .~ Set.fromList (map resolveInHackageThenNixpkgs . concatMap buildToolNixName
@@ -166,7 +166,7 @@ fromPackageDescription haskellResolver nixpkgsResolver missingDeps flags Package
166166
167167 convertSetupBuildInfo :: Cabal.SetupBuildInfo -> Nix.BuildInfo
168168 convertSetupBuildInfo bi = mempty
169− & haskell .~ Set.fromList [ resolveInHackage (toNixName x) | (Dependency x _) <- Cabal.setupDepends bi ]
169+ & haskell .~ Set.fromList [ resolveInHackage (toNixName x) | (Dependency x _ _) <- Cabal.setupDepends bi ]
170170
171171bindNull :: Identifier -> Binding
172172bindNull i = binding # (i, path # ["null"])
src/Distribution/Nixpkgs/Haskell/FromCabal/PostProcess.hs+1−1
@@ -21,7 +21,7 @@ import Language.Nix
2121
2222postProcess :: Derivation -> Derivation
2323postProcess deriv =
24− foldr (.) id [ f | (Dependency n vr, f) <- hooks, packageName deriv == n, packageVersion deriv `withinRange` vr ]
24+ foldr (.) id [ f | (Dependency n vr _, f) <- hooks, packageName deriv == n, packageVersion deriv `withinRange` vr ]
2525 . fixGtkBuilds
2626 . fixBuildDependsForTools
2727 $ deriv
src/Distribution/Nixpkgs/Haskell/OrphanInstances.hs+24−3
@@ -9,9 +9,12 @@ import qualified Data.Text as T
99import Data.Yaml
1010import Distribution.Compiler
1111import Distribution.Package
12+import Distribution.Parsec
1213import Distribution.System
13−import Distribution.Text
14+import Distribution.Pretty as Cabal
15+import qualified Data.Version as Base
1416import Distribution.Version
17+import Language.Nix.PrettyPrinting as Nix
1518
1619instance NFData CompilerInfo
1720instance NFData AbiTag
@@ -61,7 +64,25 @@ instance FromJSON CompilerInfo where
6164 parseJSON (String s) = return (unknownCompilerInfo (fromString (T.unpack s)) NoAbiTag)
6265 parseJSON s = fail ("parseJSON: " ++ show s ++ " is not a valid Haskell compiler")
6366
67+instance Nix.Pretty Version where
68+ pPrint = pretty
69+
70+instance Nix.Pretty PackageName where
71+ pPrint = pretty
72+
73+instance Nix.Pretty PackageIdentifier where
74+ pPrint = pretty
75+
76+instance Nix.Pretty CompilerId where
77+ pPrint = pretty
78+
79+instance Nix.Pretty Platform where
80+ pPrint = pretty
81+
82+instance Nix.Pretty Base.Version where
83+ pPrint = text . Base.showVersion
84+
6485-- parsing tools
6586
66−text2isString :: Text a => String -> String -> a
67−text2isString t s = fromMaybe (error ("fromString: " ++ show s ++ " is not a valid " ++ t)) (simpleParse s)
87+text2isString :: Parsec a => String -> String -> a
88+text2isString t s = fromMaybe (error ("fromString: " ++ show s ++ " is not a valid " ++ t)) (simpleParsec s)
src/Distribution/Nixpkgs/Haskell/PackageSourceSpec.hs+9−8
@@ -11,16 +11,18 @@ import qualified Data.ByteString.Char8 as BS
1111import Data.List ( isSuffixOf, isPrefixOf )
1212import qualified Data.Map as DB
1313import Data.Maybe
14+import qualified Data.Text as T
15+import qualified Data.Text.Encoding as T
1416import Data.Time
1517import Distribution.Nixpkgs.Fetch
1618import Distribution.Nixpkgs.Hashes
1719import qualified Distribution.Nixpkgs.Haskell.Hackage as DB
20+import Distribution.Nixpkgs.Haskell.OrphanInstances ( )
1821import qualified Distribution.Package as Cabal
1922import Distribution.PackageDescription
2023import qualified Distribution.PackageDescription as Cabal
2124import Distribution.PackageDescription.Parsec as Cabal
22−import Distribution.Parsec.Common (showPError)
23−import Distribution.Text ( simpleParse, display )
25+import Distribution.Parsec
2426import Distribution.Version
2527import qualified Hpack.Config as Hpack
2628import qualified Hpack.Render as Hpack
@@ -29,8 +31,7 @@ import System.Directory ( doesDirectoryExist, doesFileExist, createDirectoryIfMi
2931import System.Exit ( exitFailure )
3032import System.FilePath ( (</>), (<.>) )
3133import System.IO
32−import qualified Data.Text as T
33−import qualified Data.Text.Encoding as T
34+import Text.PrettyPrint.HughesPJClass hiding ( first )
3435
3536data HpackUse
3637 = ForceHpack
@@ -107,12 +108,12 @@ fromDB hackageDBIO pkg = do
107108 return (ds, setCabalFileHash (DB.cabalFileSha256 vd) (DB.cabalFile vd))
108109 where
109110 pkgId :: Cabal.PackageIdentifier
110− pkgId = fromMaybe (error ("invalid Haskell package id " ++ show pkg)) (simpleParse pkg)
111+ pkgId = fromMaybe (error ("invalid Haskell package id " ++ show pkg)) (simpleParsec pkg)
111112 name = Cabal.packageName pkgId
112113
113− unknownPackageError = fail $ "No such package " ++ display pkgId ++ " in the cabal database. Did you run cabal update?"
114+ unknownPackageError = fail $ "No such package " ++ prettyShow pkgId ++ " in the cabal database. Did you run cabal update?"
114115
115− url = "mirror://hackage/" ++ display pkgId ++ ".tar.gz"
116+ url = "mirror://hackage/" ++ prettyShow pkgId ++ ".tar.gz"
116117
117118 version :: Version
118119 version = Cabal.packageVersion pkgId
@@ -175,7 +176,7 @@ sourceFromHackage optHash pkgId cabalDir = do
175176 exitFailure
176177
177178showPackageIdentifier :: Cabal.GenericPackageDescription -> String
178−showPackageIdentifier pkgDesc = name ++ "-" ++ display version where
179+showPackageIdentifier pkgDesc = name ++ "-" ++ prettyShow version where
179180 pkgId = Cabal.package . Cabal.packageDescription $ pkgDesc
180181 name = Cabal.unPackageName (Cabal.packageName pkgId)
181182 version = Cabal.packageVersion pkgId