git.iko.soy/typeable/cabal2nix
on master help sign in
Hackage4Nix/Nix.hs: this module contains code to convert a parsed Nix expression into our custom Pkg type
commit1ff74fcf3231a078c9071b230a4e8cf8255427c5 authorPeter Simons <simons@cryp.to> dateFri 12 Aug 2011, 00:40 (15 years ago) parentfe7ad4c
1 file changed+67
src/Hackage4Nix/Nix.hs+67
src/Hackage4Nix/Nix.hsnew file+67
@@ -0,0 +1,67 @@
1+module Hackage4Nix.Nix where
2+
3+import Hackage4Nix.Nix.Lex
4+import Hackage4Nix.Nix.Par
5+import Hackage4Nix.Nix.ErrM
6+import Hackage4Nix.Nix.Abs
7+import Cabal2Nix.Package
8+import Cabal2Nix.License
9+import Data.List
10+import Distribution.Package
11+import Distribution.Version
12+
13+nixExpr2CabalExpr :: Expr -> Pkg
14+nixExpr2CabalExpr (Expr _ _ dict) =
15+ Pkg name version sha256 homepage description license isLib isExe
16+ deps buildTools extraLibs pkgconfDeps platforms maintainers
17+ where
18+ name = getString "pname" dict
19+ version = getString "version" dict
20+ sha256 = getString "sha256" dict
21+ homepage = getURL "homepage" (getDict "meta" dict)
22+ description = getString "description" (getDict "meta" dict)
23+ license = MIT
24+ isLib = getBool "isLibrary" dict
25+ isExe = getBool "isExecutable" dict
26+ deps = [ Dependency (PackageName dep) anyVersion | dep <- getRefList "buildDepends" dict ]
27+ buildTools = [ Dependency (PackageName dep) anyVersion | dep <- getRefList "buildTools" dict ]
28+ extraLibs = getRefList "extraLibraries" dict
29+ pkgconfDeps = getRefList "pkgconfDepends" dict
30+ platforms = getRefList "platforms" (getDict "meta" dict)
31+ maintainers = getRefList "maintainers" (getDict "meta" dict)
32+
33+get' ref (Dictionary attrs) = [ v | Attribute (Reference r) v <- attrs, r == [Ident ref] ]
34+get ref dict
35+ | [v] <- get' ref dict = v
36+ | otherwise = error $ "attribute " ++ ref ++ " doesn't exist in " ++ show dict
37+getBool ref dict = let BoolV b = get ref dict in if b == Yes then True else False
38+getString ref dict = let StringV x = get ref dict in x
39+getDict ref dict = let DictionaryV x = get ref dict in x
40+getURL ref dict
41+ | URLV (URL x) <- get ref dict = x
42+ | StringV x <- get ref dict = x
43+
44+getRefList ref dict =
45+ let vals = get' ref dict
46+ rs = [ refs | ListV vs <- vals, AttributeV refs <- vs ]
47+ in
48+ map flattenReference rs
49+
50+flattenReference (Reference rs) = concat (intersperse "." [ r | Ident r <- rs ])
51+
52+parseNixExpr :: Monad m => String -> m Expr
53+parseNixExpr = run pExpr
54+
55+type ParseFun a = [Token] -> Err a
56+
57+run :: Monad m => ParseFun a -> String -> m a
58+run p s = case p (myLexer s) of
59+ Bad err -> fail err
60+ Ok tree -> return tree
61+
62+runTest = do
63+ buf <- readFile "/home/simons/.nix-defexpr/pkgs/development/libraries/haskell/hsemail/default.nix"
64+ tree <- parseNixExpr buf
65+ print tree
66+ let pkg = nixExpr2CabalExpr tree
67+ putStr (showNixPkg pkg)