git.iko.soy/typeable/cabal2nix
on master help sign in
Cabal2Nix: moved normalization code into a separate module
ddc3d98Peter Simons
cabal2nix.cabal+2
@@ -61,6 +61,7 @@ Executable cabal2nix
61 other-modules: Cabal2Nix.CorePackages
61 other-modules: Cabal2Nix.CorePackages
62 Cabal2Nix.Hackage
62 Cabal2Nix.Hackage
63 Cabal2Nix.Name
63 Cabal2Nix.Name
64+ Cabal2Nix.Normalize
64 Cabal2Nix.Package
65 Cabal2Nix.Package
65 Cabal2Nix.PostProcess
66 Cabal2Nix.PostProcess
66 Distribution.NixOS.Derivation.Cabal
67 Distribution.NixOS.Derivation.Cabal
@@ -77,6 +78,7 @@ Executable hackage4nix
77 Ghc-Options: -Wall
78 Ghc-Options: -Wall
78 other-modules: Cabal2Nix.CorePackages
79 other-modules: Cabal2Nix.CorePackages
79 Cabal2Nix.Name
80 Cabal2Nix.Name
81+ Cabal2Nix.Normalize
80 Cabal2Nix.Package
82 Cabal2Nix.Package
81 Cabal2Nix.PostProcess
83 Cabal2Nix.PostProcess
82 Distribution.NixOS.Derivation.Cabal
84 Distribution.NixOS.Derivation.Cabal
src/Cabal2Nix.hs+6−13
@@ -2,6 +2,7 @@ module Main ( main ) where
2
2
3import Cabal2Nix.Hackage ( hashPackage, readCabalFile )
3import Cabal2Nix.Hackage ( hashPackage, readCabalFile )
4import Cabal2Nix.Package ( cabal2nix )
4import Cabal2Nix.Package ( cabal2nix )
5+import Cabal2Nix.Normalize ( normalize )
5import Distribution.NixOS.Derivation.Cabal
6import Distribution.NixOS.Derivation.Cabal
6import Distribution.NixOS.Derivation.Meta
7import Distribution.NixOS.Derivation.Meta
7
8
@@ -63,20 +64,13 @@ cmdlineError errMsg = hPutStrLn stderr errMsg >> cmdlineError ""
63main :: IO ()
64main :: IO ()
64main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
65main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
65 args' <- getArgs
66 args' <- getArgs
66− (cfg',args) <- case getOpt Permute options args' of
67+ (cfg,args) <- case getOpt Permute options args' of
67− (o,n,[] ) -> return (foldl (flip ($)) defaultConfiguration o,n)
68+ (o,n,[] ) -> return (foldl (flip ($)) defaultConfiguration o,n)
68− (_,_,errs) -> cmdlineError (concatMap ("*** "++) errs)
69+ (_,_,errs) -> cmdlineError (concatMap ("*** "++) errs)
69
70
70− when (optPrintHelp cfg') (putStr usage >> exitSuccess)
71+ when (optPrintHelp cfg) (putStr usage >> exitSuccess)
71 when (length args /= 1) (cmdlineError "*** exactly one url-to-cabal-file must be specified\n")
72 when (length args /= 1) (cmdlineError "*** exactly one url-to-cabal-file must be specified\n")
72
73
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−
80 cabal' <- fmap parsePackageDescription (readCabalFile (head args))
74 cabal' <- fmap parsePackageDescription (readCabalFile (head args))
81 cabal <- case cabal' of
75 cabal <- case cabal' of
82 ParseOk _ a -> return a
76 ParseOk _ a -> return a
@@ -85,7 +79,6 @@ main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
85 exitFailure
79 exitFailure
86
80
87 let packageId = package (packageDescription cabal)
81 let packageId = package (packageDescription cabal)
88−
89 sha <- if null (optSha256 cfg) then hashPackage packageId else return (optSha256 cfg)
82 sha <- if null (optSha256 cfg) then hashPackage packageId else return (optSha256 cfg)
90
83
91 let deriv = (cabal2nix cabal) { sha256 = sha, runHaddock = optHaddock cfg }
84 let deriv = (cabal2nix cabal) { sha256 = sha, runHaddock = optHaddock cfg }
@@ -95,4 +88,4 @@ main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
95 }
88 }
96 }
89 }
97
90
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
2
2
3import Cabal2Nix.License
3import Cabal2Nix.License
4import Cabal2Nix.PostProcess
4import Cabal2Nix.PostProcess
5+import Cabal2Nix.Normalize
5import Data.Maybe
6import Data.Maybe
6import Distribution.Compiler
7import Distribution.Compiler
7import qualified Distribution.Package as Cabal
8import qualified Distribution.Package as Cabal
@@ -16,20 +17,20 @@ import Data.List
16import Data.Char
17import Data.Char
17
18
18cabal2nix :: Cabal.GenericPackageDescription -> Derivation
19cabal2nix :: Cabal.GenericPackageDescription -> Derivation
19−cabal2nix cabal = postProcess $ MkDerivation
20+cabal2nix cabal = normalize $ postProcess $ MkDerivation
20 { pname = let Cabal.PackageName x = Cabal.pkgName pkg in x
21 { pname = let Cabal.PackageName x = Cabal.pkgName pkg in x
21 , version = Cabal.pkgVersion pkg
22 , version = Cabal.pkgVersion pkg
22 , sha256 = "cabal2nix left the she256 field undefined"
23 , sha256 = "cabal2nix left the she256 field undefined"
23 , isLibrary = isJust (Cabal.library tpkg)
24 , isLibrary = isJust (Cabal.library tpkg)
24 , isExecutable = not (null (Cabal.executables tpkg))
25 , isExecutable = not (null (Cabal.executables tpkg))
25− , buildDepends = normalizeNixNames (filter (`notElem` (name : corePackages)) $ map unDep deps)
26+ , buildDepends = map unDep deps
26− , buildTools = normalizeNixBuildTools (filter (`notElem` coreBuildTools) $ map unDep tools)
27+ , buildTools = map unDep tools
27− , extraLibs = normalizeNixLibs libs
28+ , extraLibs = libs
28− , pkgConfDeps = normalizeNixLibs pcs
29+ , pkgConfDeps = pcs
29 , runHaddock = True
30 , runHaddock = True
30 , metaSection = Meta
31 , metaSection = Meta
31 { homepage = Cabal.homepage descr
32 { homepage = Cabal.homepage descr
32− , description = normalizeDescription (Cabal.synopsis descr)
33+ , description = Cabal.synopsis descr
33 , license = fromCabalLicense (Cabal.license descr)
34 , license = fromCabalLicense (Cabal.license descr)
34 , platforms = []
35 , platforms = []
35 , maintainers = []
36 , maintainers = []
@@ -52,25 +53,3 @@ cabal2nix cabal = postProcess $ MkDerivation
52
53
53unDep :: Cabal.Dependency -> String
54unDep :: Cabal.Dependency -> String
54unDep (Cabal.Dependency (Cabal.PackageName x) _) = x
55unDep (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