git.iko.soy/typeable/cabal2nix
on master help sign in
Generate output using a pretty printer.
commite1ce619bb4b8bb45a10bfce72ba8e68b34ef35b2 authorAndres Loeh <mail@andres-loeh.de> dateSat 6 Aug 2011, 21:49 (15 years ago) parent45d2185
3 files changed+36−22
cabal2nix.cabal+1−1 src/Cabal2Nix/Name.hs−1 src/Cabal2Nix/Package.hs+35−20
cabal2nix.cabal+1−1
@@ -37,5 +37,5 @@ Source-Repository head
3737Executable cabal2nix
3838 main-is: Cabal2Nix.hs
3939 hs-source-dirs: src
40− Build-Depends: base >= 3 && < 5, Cabal, process, HTTP
40+ Build-Depends: base >= 3 && < 5, Cabal, process, HTTP, pretty
4141 Ghc-Options: -Wall
src/Cabal2Nix/Name.hs−1
@@ -4,7 +4,6 @@ import Data.Char
44
55toNixName :: String -> String
66toNixName [] = error "toNixName: empty string is not a valid argument"
7−toNixName "Cabal" = "cabal"
87toNixName name = f name
98 where
109 f [] = []
src/Cabal2Nix/Package.hs+35−20
@@ -5,6 +5,7 @@ import Data.Maybe
55import Distribution.Package
66import Distribution.PackageDescription
77import Distribution.Version
8+import Text.PrettyPrint
89
910import Cabal2Nix.License
1011import Cabal2Nix.Name
@@ -29,28 +30,42 @@ data Pkg = Pkg PkgName
2930 deriving (Show)
3031
3132toNix :: Pkg -> String
32−toNix (Pkg name ver sha256 url desc lic deps libs) =
33− "{" ++ exprArgs ++"}:\n\n"
34− ++ "cabal.mkDerivation (self : {\n"
35− ++ " pname = " ++ show name ++ ";\n"
36− ++ " version = \"" ++ showVer ++ "\";\n"
37− ++ " sha256 = " ++ show sha256 ++ ";\n"
38− ++ " propagatedBuildInputs = [" ++ depList ++ "];\n"
39− ++ " meta = {\n"
40− ++ " homepage = \"" ++ url ++ "\";\n"
41− ++ " description = " ++ show desc ++ ";\n"
42− ++ " license = " ++ showLic lic ++ ";\n"
43− ++ " };\n"
44− ++ "})\n"
33+toNix (Pkg name ver sha256 url desc lic deps libs) = render doc
4534 where
46− exprArgs = concat (intersperse "," ("cabal":pkgDeps))
47− showVer = concat (intersperse "." (map show ver))
48− depList = concat (intersperse " " pkgDeps)
35+ doc = braces (fsep $ punctuate comma $ map text ("cabal" : pkgDeps)) <+>
36+ colon $$ text "" $$
37+ vcat [
38+ text "cabal.mkDerivation" <+> lparen <> text "self" <+>
39+ colon <+> lbrace,
40+ nest 2 $ vcat [
41+ attr "pname" $ doubleQuotes (text name),
42+ attr "version" $ showVer,
43+ attr "sha256" $ doubleQuotes (text sha256),
44+ sep [
45+ text "propagatedBuildInputs" <+> equals <+> lbrack,
46+ nest 2 $ fsep $ map text pkgDeps,
47+ rbrack <> semi
48+ ],
49+ vcat [
50+ text "meta" <+> equals <+> lbrace,
51+ nest 2 $ vcat [
52+ attr "homepage" $ doubleQuotes (text url),
53+ attr "description" $ doubleQuotes (text desc),
54+ attr "license" $ text (showLic lic)
55+ ],
56+ rbrace <> semi
57+ ]
58+ ],
59+ rbrace <> rparen
60+ ]
61+ attr n v = text n <+> equals <+> v <> semi
62+ showVer = hcat (punctuate (text ".") (map int ver))
4963 pkgDeps :: [String]
50− pkgDeps = filter (/="cabal") $ nub $ sort $ map toNixName $
51− libs ++ [ n | dep <- deps, Dependency (PackageName n) _ <- condTreeConstraints dep
52− , n `notElem` corePackages
53− ]
64+ pkgDeps = nub $ sort $ map toNixName $
65+ libs ++ [ n | dep <- deps,
66+ Dependency (PackageName n) _ <- condTreeConstraints dep,
67+ n `notElem` corePackages
68+ ]
5469
5570-- | List of packages shipped with ghc and therefore at the moment not in
5671-- nixpkgs. This should probably be configurable at first. Later, it might