git.iko.soy/typeable/cabal2nix
on master help sign in
Cabal2Nix: moved normalization code into a separate module
commitddc3d98283f7e0339069a60c40bcb65c12c5d23a authorPeter Simons <simons@cryp.to> dateTue 23 Aug 2011, 12:57 (15 years ago) parent980335d
4 files changed+80−41
cabal2nix.cabal+2 src/Cabal2Nix.hs+6−13 src/Cabal2Nix/Normalize.hs+65 src/Cabal2Nix/Package.hs+7−28
cabal2nix.cabal+2
@@ -61,6 +61,7 @@ Executable cabal2nix
6161 other-modules: Cabal2Nix.CorePackages
6262 Cabal2Nix.Hackage
6363 Cabal2Nix.Name
64+ Cabal2Nix.Normalize
6465 Cabal2Nix.Package
6566 Cabal2Nix.PostProcess
6667 Distribution.NixOS.Derivation.Cabal
@@ -77,6 +78,7 @@ Executable hackage4nix
7778 Ghc-Options: -Wall
7879 other-modules: Cabal2Nix.CorePackages
7980 Cabal2Nix.Name
81+ Cabal2Nix.Normalize
8082 Cabal2Nix.Package
8183 Cabal2Nix.PostProcess
8284 Distribution.NixOS.Derivation.Cabal
src/Cabal2Nix.hs+6−13
@@ -2,6 +2,7 @@ module Main ( main ) where
22
33import Cabal2Nix.Hackage ( hashPackage, readCabalFile )
44import Cabal2Nix.Package ( cabal2nix )
5+import Cabal2Nix.Normalize ( normalize )
56import Distribution.NixOS.Derivation.Cabal
67import Distribution.NixOS.Derivation.Meta
78
@@ -63,20 +64,13 @@ cmdlineError errMsg = hPutStrLn stderr errMsg >> cmdlineError ""
6364main :: IO ()
6465main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
6566 args' <- getArgs
66− (cfg',args) <- case getOpt Permute options args' of
67− (o,n,[] ) -> return (foldl (flip ($)) defaultConfiguration o,n)
68− (_,_,errs) -> cmdlineError (concatMap ("*** "++) errs)
67+ (cfg,args) <- case getOpt Permute options args' of
68+ (o,n,[] ) -> return (foldl (flip ($)) defaultConfiguration o,n)
69+ (_,_,errs) -> cmdlineError (concatMap ("*** "++) errs)
6970
70− when (optPrintHelp cfg') (putStr usage >> exitSuccess)
71+ when (optPrintHelp cfg) (putStr usage >> exitSuccess)
7172 when (length args /= 1) (cmdlineError "*** exactly one url-to-cabal-file must be specified\n")
7273
73− let maints = [ if '.' `elem` m then m else "self.stdenv.lib.maintainers." ++ m | m <- optMaintainer cfg' ]
74− plats = [ if '.' `elem` p then p else "self.stdenv.lib.platforms." ++ p | p <- optPlatform cfg' ]
75− cfg = cfg'
76− { optMaintainer = nub (sort maints)
77− , optPlatform = nub (sort (if null plats then ["self.ghc.meta.platforms"] else plats))
78− }
79−
8074 cabal' <- fmap parsePackageDescription (readCabalFile (head args))
8175 cabal <- case cabal' of
8276 ParseOk _ a -> return a
@@ -85,7 +79,6 @@ main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
8579 exitFailure
8680
8781 let packageId = package (packageDescription cabal)
88−
8982 sha <- if null (optSha256 cfg) then hashPackage packageId else return (optSha256 cfg)
9083
9184 let deriv = (cabal2nix cabal) { sha256 = sha, runHaddock = optHaddock cfg }
@@ -95,4 +88,4 @@ main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
9588 }
9689 }
9790
98− putStr (show (disp deriv'))
91+ putStr (show (disp (normalize (deriv'))))
src/Cabal2Nix/Normalize.hsnew file+65
@@ -0,0 +1,65 @@
1+{-# LANGUAGE RecordWildCards #-}
2+
3+module Cabal2Nix.Normalize ( normalize ) where
4+
5+import Cabal2Nix.License
6+import Cabal2Nix.PostProcess
7+import Data.Maybe
8+import Distribution.Compiler
9+import qualified Distribution.Package as Cabal
10+import qualified Distribution.PackageDescription as Cabal
11+import Distribution.PackageDescription.Configuration
12+import Distribution.System
13+import Distribution.Version
14+import Distribution.NixOS.Derivation.Cabal
15+import Cabal2Nix.Name
16+import Cabal2Nix.CorePackages
17+import Data.List
18+import Data.Char
19+
20+normalize :: Derivation -> Derivation
21+normalize deriv@(MkDerivation {..}) = deriv
22+ { buildDepends = normalizeNixNames (filter (`notElem` (pname : corePackages)) $ buildDepends)
23+ , buildTools = normalizeNixBuildTools (filter (`notElem` coreBuildTools) $ buildTools)
24+ , extraLibs = normalizeNixLibs extraLibs
25+ , pkgConfDeps = normalizeNixLibs pkgConfDeps
26+ , metaSection = normalizeMeta metaSection
27+ }
28+
29+normalizeMeta :: Meta -> Meta
30+normalizeMeta meta@(Meta {..}) = meta
31+ { description = normalizeDescription description
32+ , maintainers = normalizeMaintainers maintainers
33+ , platforms = normalizePlatforms platforms
34+ }
35+
36+normalizeDescription :: String -> String
37+normalizeDescription desc
38+ | null desc = []
39+ | last desc == '.' && length (filter ('.'==) desc) == 1 = normalizeDescription (reverse (tail (reverse desc)))
40+ | otherwise = unwords (words desc) >>= quote
41+
42+quote :: Char -> [Char]
43+quote '"' = "\\\""
44+quote c = [c]
45+
46+normalizeList :: [String] -> [String]
47+normalizeList = nub . sortBy (\x y -> compare (map toLower x) (map toLower y))
48+
49+normalizeNixNames :: [String] -> [String]
50+normalizeNixNames = normalizeList . map toNixName
51+
52+normalizeNixLibs :: [String] -> [String]
53+normalizeNixLibs = normalizeList . concatMap libNixName
54+
55+normalizeNixBuildTools :: [String] -> [String]
56+normalizeNixBuildTools = normalizeList . concatMap buildToolNixName
57+
58+normalizeMaintainers :: [String] -> [String]
59+normalizeMaintainers maints = normalizeList
60+ [ if '.' `elem` m then m else "self.stdenv.lib.maintainers." ++ m | m <- maints ]
61+
62+normalizePlatforms :: [String] -> [String]
63+normalizePlatforms [] = ["self.ghc.meta.platforms"]
64+normalizePlatforms plats = normalizeList
65+ [ if '.' `elem` p then p else "self.stdenv.lib.platforms." ++ p | p <- plats ]
src/Cabal2Nix/Package.hs+7−28
@@ -2,6 +2,7 @@ module Cabal2Nix.Package ( cabal2nix ) where
22
33import Cabal2Nix.License
44import Cabal2Nix.PostProcess
5+import Cabal2Nix.Normalize
56import Data.Maybe
67import Distribution.Compiler
78import qualified Distribution.Package as Cabal
@@ -16,20 +17,20 @@ import Data.List
1617import Data.Char
1718
1819cabal2nix :: Cabal.GenericPackageDescription -> Derivation
19−cabal2nix cabal = postProcess $ MkDerivation
20+cabal2nix cabal = normalize $ postProcess $ MkDerivation
2021 { pname = let Cabal.PackageName x = Cabal.pkgName pkg in x
2122 , version = Cabal.pkgVersion pkg
2223 , sha256 = "cabal2nix left the she256 field undefined"
2324 , isLibrary = isJust (Cabal.library tpkg)
2425 , isExecutable = not (null (Cabal.executables tpkg))
25− , buildDepends = normalizeNixNames (filter (`notElem` (name : corePackages)) $ map unDep deps)
26− , buildTools = normalizeNixBuildTools (filter (`notElem` coreBuildTools) $ map unDep tools)
27− , extraLibs = normalizeNixLibs libs
28− , pkgConfDeps = normalizeNixLibs pcs
26+ , buildDepends = map unDep deps
27+ , buildTools = map unDep tools
28+ , extraLibs = libs
29+ , pkgConfDeps = pcs
2930 , runHaddock = True
3031 , metaSection = Meta
3132 { homepage = Cabal.homepage descr
32− , description = normalizeDescription (Cabal.synopsis descr)
33+ , description = Cabal.synopsis descr
3334 , license = fromCabalLicense (Cabal.license descr)
3435 , platforms = []
3536 , maintainers = []
@@ -52,25 +53,3 @@ cabal2nix cabal = postProcess $ MkDerivation
5253
5354unDep :: Cabal.Dependency -> String
5455unDep (Cabal.Dependency (Cabal.PackageName x) _) = x
55−
56−normalizeDescription :: String -> String
57−normalizeDescription desc
58− | null desc = []
59− | last desc == '.' && length (filter ('.'==) desc) == 1 = normalizeDescription (reverse (tail (reverse desc)))
60− | otherwise = unwords (words desc) >>= quote
61−
62−quote :: Char -> [Char]
63−quote '"' = "\\\""
64−quote c = [c]
65−
66−normalizeList :: [String] -> [String]
67−normalizeList = nub . sortBy (\x y -> compare (map toLower x) (map toLower y))
68−
69−normalizeNixNames :: [String] -> [String]
70−normalizeNixNames = normalizeList . map toNixName
71−
72−normalizeNixLibs :: [String] -> [String]
73−normalizeNixLibs = normalizeList . concatMap libNixName
74−
75−normalizeNixBuildTools :: [String] -> [String]
76−normalizeNixBuildTools = normalizeList . concatMap buildToolNixName