git.iko.soy/typeable/cabal2nix
on master help sign in
cabal2nix.hs: adapted toNixName function to coding guidelines
commit19babcb14ea83697cf6562983fb11ab50c081320 authorPeter Simons <simons@cryp.to> dateFri 5 Aug 2011, 18:31 (15 years ago) parent6c9f207
1 file changed+61−28
cabal2nix.hs+61−28
cabal2nix.hs+61−28
@@ -1,6 +1,14 @@
1+-- cabal2nix.hs
2+--
3+-- Copyright (c) 20011 Peter Simons <simons@cryp.to>
4+-- See LICENSE file for licensing details.
5+
16module Main ( main ) where
27
8+import System.IO
39import System.Environment
10+import Control.Exception
11+import System.Exit
412import Distribution.PackageDescription.Parse
513import Distribution.Verbosity ( silent )
614import Distribution.PackageDescription
@@ -8,35 +16,53 @@ import Distribution.Package
816import Distribution.License
917import Data.Version
1018import Data.List
19+import Control.Monad
20+import Data.Char
21+
22+type PkgName = String
23+type PkgVersion = [Int]
24+type PkgSHA256 = String
25+type PkgURL = String
26+type PkgDescription = String
27+type PkgLicense = License
28+type PkgDependencies = [CondTree ConfVar [Dependency] ()]
29+type PkgExtraLibs = [String]
1130
12−data Pkg = Pkg String [Int] String String String License [CondTree ConfVar [Dependency] ()] [String]
31+data Pkg = Pkg PkgName PkgVersion PkgSHA256 PkgURL PkgDescription PkgLicense PkgDependencies PkgExtraLibs
32+ deriving (Show)
33+
34+toNixName :: String -> String
35+toNixName name = f name
36+ where
37+ f [] = []
38+ f ('-':c:cs) | c `notElem` "-" = toUpper c : f cs
39+ f ('-':_) = error ("unexpected package name " ++ show name)
40+ f (c:cs) = c : f cs
1341
14−instance Show Pkg where
15− show (Pkg name ver sha256 url desc lic deps libs) =
16− " " ++ attrib ++ " = callPackage ({cabal," ++ depArgs ++"}: cabal.mkDerivation (self : {\n"
17− ++ " pname = " ++ show name ++ ";\n"
18− ++ " version = \"" ++ showVer ++ "\";\n"
19− ++ " sha256 = " ++ show sha256 ++ ";\n"
20− ++ " propagatedBuildInputs = [" ++ depList ++ "];\n"
21− ++ " meta = {\n"
22− ++ " homepage = \"" ++ url ++ "\";\n"
23− ++ " description = " ++ show desc ++ ";\n"
24− ++ " license = " ++ showLic lic ++ ";\n"
25− ++ " platforms = self.stdenv.lib.platforms.haskellPlatforms;\n"
26− ++ " maintainers = [ self.stdenv.lib.maintainers.simons ];\n"
27− ++ " };\n"
28− ++ " })) {};\n"
42+toNix :: Pkg -> String
43+toNix (Pkg name ver sha256 url desc lic deps libs) =
44+ "{" ++ exprArgs ++"}:\n\n"
45+ ++ "cabal.mkDerivation (self : {\n"
46+ ++ " pname = " ++ show name ++ ";\n"
47+ ++ " version = \"" ++ showVer ++ "\";\n"
48+ ++ " sha256 = " ++ show sha256 ++ ";\n"
49+ ++ " propagatedBuildInputs = [" ++ depList ++ "];\n"
50+ ++ " meta = {\n"
51+ ++ " homepage = \"" ++ url ++ "\";\n"
52+ ++ " description = " ++ show desc ++ ";\n"
53+ ++ " license = " ++ showLic lic ++ ";\n"
54+ ++ " };\n"
55+ ++ "})\n"
2956 where
30− attrib = concat (intersperse "_" (nixName name : (map show ver)))
57+ exprArgs = concat (intersperse "," ((if "cabal" `notElem` pkgDeps then ["cabal"] else []) ++ pkgDeps))
3158 showVer = concat (intersperse "." (map show ver))
3259 depList = concat (intersperse " " pkgDeps)
33− depArgs = concat (intersperse "," pkgDeps)
3460 pkgDeps :: [String]
35− pkgDeps = map nixName $
36− nub $ sort $
37− libs ++ [ n | dep <- deps, Dependency (PackageName n) _ <- condTreeConstraints dep
38− , n `notElem` ["base","containers"]
39− ]
61+ pkgDeps = map toNixName $
62+ nub $ sort $ libs ++
63+ [ n | dep <- deps, Dependency (PackageName n) _ <- condTreeConstraints dep
64+ , n `notElem` ["base","containers"]
65+ ]
4066 showLic (GPL Nothing) = show "GPL"
4167 showLic (GPL (Just (Version [2] []))) = "self.stdenv.lib.licenses.gpl2"
4268 showLic (GPL (Just (Version [3] []))) = "self.stdenv.lib.licenses.gpl3"
@@ -51,8 +77,6 @@ instance Show Pkg where
5177 showLic OtherLicense = "unknown"
5278 showLic l = error $ "unknown license: " ++ show l
5379
54− nixName = map (\c -> if c == '-' then '_' else c)
55−
5680readPackage :: FilePath -> String -> IO Pkg
5781readPackage cabalFile sha256 = do
5882 cabal <- readPackageDescription silent cabalFile
@@ -68,6 +92,7 @@ readPackage cabalFile sha256 = do
6892 libs = concat [ extraLibs (libBuildInfo (condTreeData x)) | x <- libDeps ]
6993 libs' = concat [ extraLibs (buildInfo (condTreeData x)) | x <- exeDeps ]
7094 return (Pkg pkgname pkgver sha256 url desc lic (map simplify libDeps ++ map simplify exeDeps) (libs++libs'))
95+ where
7196
7297simplify :: CondTree ConfVar [Dependency] a -> CondTree ConfVar [Dependency] ()
7398simplify (CondNode _ deps nodes) = CondNode () deps (map simp nodes)
@@ -75,7 +100,15 @@ simplify (CondNode _ deps nodes) = CondNode () deps (map simp nodes)
75100 simp (cond,tree,mtree) = (cond, simplify tree, maybe Nothing (Just . simplify) mtree)
76101
77102main :: IO ()
78−main = do
79− cabalFile:sha256:[] <- getArgs
103+main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
104+ args <- getArgs
105+
106+ when (length args /= 2) $ do
107+ mapM_ (hPutStrLn stderr) [ "*** invalid command line syntax"
108+ , "Usage: cabal2nix cabal-file sha256-hash"
109+ ]
110+ exitFailure
111+
112+ let cabalFile:sha256:[] = args
80113 pkg <- readPackage cabalFile sha256
81− putStr (show pkg)
114+ putStr (toNix pkg)