git.iko.soy/typeable/cabal2nix
on master help sign in
Re-use nixos-types library.
commita86bc154c93a5714d320afc2a0ecb0e57a8779fb authorPeter Simons <simons@cryp.to> dateTue 23 Aug 2011, 15:12 (15 years ago) parentd19c3c2
6 files changed+5−335
Setup.hs+2−11 cabal2nix.cabal+3−21 src/Distribution/NixOS/Derivation/Cabal.hs−127 src/Distribution/NixOS/Derivation/License.hs−51 src/Distribution/NixOS/Derivation/Meta.hs−68 src/Distribution/NixOS/PrettyPrinting.hs−57
Setup.hs+2−11
@@ -1,15 +1,6 @@
1−module Main ( main ) where
1+module Main (main) where
22
33import Distribution.Simple
4−import Distribution.Simple.LocalBuildInfo ( withPrograms )
5−import Distribution.Simple.Program ( userSpecifyArgs )
64
75main :: IO ()
8−main = defaultMainWithHooks $
9− simpleUserHooks `modify_haddockHook` \oldHH pkg lbi hooks flags ->
10− (\lbi' -> oldHH pkg lbi' hooks flags) $
11− lbi `modify_withPrograms` \oldWP ->
12− userSpecifyArgs "haddock" ["--optghc=-D__HADDOCK__"] oldWP
13− where
14− modify_haddockHook hooks f = hooks { haddockHook = f (haddockHook hooks) }
15− modify_withPrograms lbi f = lbi { withPrograms = f (withPrograms lbi) }
6+main = defaultMain
cabal2nix.cabal+3−21
@@ -9,7 +9,7 @@ Homepage: http://github.com/haskell4nix/cabal2nix
99Category: Distribution
1010Synopsis: Convert Cabal files into Nix build instructions
1111Cabal-Version: >= 1.8
12−Build-Type: Custom
12+Build-Type: Simple
1313Tested-With: GHC == 6.12.3, GHC == 7.0.4, GHC == 7.2.1
1414Description:
1515 The cabal2nix utility converts Cabal files into Nix build instructions. The
@@ -41,21 +41,11 @@ Source-Repository head
4141 Type: git
4242 Location: git://github.com/haskell4nix/cabal2nix.git
4343
44−Library
45− Build-Depends: base >= 3 && < 5, regex-posix, pretty, Cabal >= 1.8
46− hs-source-dirs: src
47− Extensions: PatternGuards, CPP
48− Ghc-Options: -Wall
49− Exposed-Modules: Distribution.NixOS.Derivation.Cabal
50− Distribution.NixOS.Derivation.License
51− Distribution.NixOS.Derivation.Meta
52− other-modules: Distribution.NixOS.PrettyPrinting
53−
5444Executable cabal2nix
5545 main-is: Cabal2Nix.hs
5646 hs-source-dirs: src
5747 Build-Depends: base >= 3 && < 5, regex-posix, pretty, Cabal >= 1.8,
58− filepath, directory, process, HTTP
48+ filepath, directory, process, HTTP, nixos-types
5949 Extensions: PatternGuards, RecordWildCards, CPP
6050 Ghc-Options: -Wall
6151 other-modules: Cabal2Nix.CorePackages
@@ -66,16 +56,12 @@ Executable cabal2nix
6656 Cabal2Nix.Normalize
6757 Cabal2Nix.Package
6858 Cabal2Nix.PostProcess
69− Distribution.NixOS.Derivation.Cabal
70− Distribution.NixOS.Derivation.License
71− Distribution.NixOS.Derivation.Meta
72− Distribution.NixOS.PrettyPrinting
7359
7460Executable hackage4nix
7561 main-is: Hackage4Nix.hs
7662 hs-source-dirs: src
7763 Build-Depends: base >= 3 && < 5, regex-posix, pretty, Cabal >= 1.8,
78− mtl, containers, directory, filepath
64+ mtl, containers, directory, filepath, nixos-types
7965 Extensions: PatternGuards, RecordWildCards, CPP
8066 Ghc-Options: -Wall
8167 other-modules: Cabal2Nix.CorePackages
@@ -85,10 +71,6 @@ Executable hackage4nix
8571 Cabal2Nix.Normalize
8672 Cabal2Nix.Package
8773 Cabal2Nix.PostProcess
88− Distribution.NixOS.Derivation.Cabal
89− Distribution.NixOS.Derivation.License
90− Distribution.NixOS.Derivation.Meta
91− Distribution.NixOS.PrettyPrinting
9274
9375Executable packagelist
9476 main-is: PackageList.hs
src/Distribution/NixOS/Derivation/Cabal.hsdeleted−127
@@ -1,127 +0,0 @@
1−{-# LANGUAGE PatternGuards, CPP #-}
2−{- |
3− Module : Distribution.NixOS.Derivation.Cabal
4− License : BSD3
5−
6− Maintainer : nix-dev@cs.uu.nl
7− Stability : provisional
8− Portability : PatternGuards
9−
10− A represtation of Nix expressions based on Cabal builder defined in
11− @pkgs\/development\/libraries\/haskell\/cabal\/cabal.nix@.
12−-}
13−
14−module Distribution.NixOS.Derivation.Cabal
15− ( Derivation(..)
16− , parseDerivation
17− , module Distribution.NixOS.Derivation.Meta
18− , module Data.Version
19− )
20− where
21−
22−import Distribution.NixOS.Derivation.Meta
23−import Distribution.NixOS.PrettyPrinting
24−import Distribution.Text
25−import Distribution.Package
26−#ifdef __HADDOCK__
27−import Distribution.PackageDescription ( PackageDescription )
28−#endif
29−import Data.Version
30−import Data.List
31−import Data.Char
32−import Text.Regex.Posix
33−
34−-- | A represtation of Nix expressions for building Haskell packages.
35−-- The data type correspond closely to the definition of
36−-- 'PackageDescription' from Cabal.
37−--
38−-- Note that the "Text" instance definition provides pretty-printing,
39−-- but no parsing as of now!
40−
41−data Derivation = MkDerivation
42− { pname :: String
43− , version :: Version
44− , sha256 :: String
45− , isLibrary :: Bool
46− , isExecutable :: Bool
47− , buildDepends :: [String]
48− , buildTools :: [String]
49− , extraLibs :: [String]
50− , pkgConfDeps :: [String]
51− , runHaddock :: Bool
52− , metaSection :: Meta
53− }
54− deriving (Show, Eq, Ord)
55−
56−instance Text Derivation where
57− disp = renderDerivation
58− parse = error "parsing Distribution.NixOS.Derivation.Cabal.Derivation is not supported yet"
59−
60−instance Package Derivation where
61− packageId deriv = PackageIdentifier (PackageName (pname deriv)) (version deriv)
62−
63−renderDerivation :: Derivation -> Doc
64−renderDerivation deriv = funargs (map text ("cabal" : inputs)) $$ vcat
65− [ text ""
66− , text "cabal.mkDerivation" <+> lparen <> text "self" <> colon <+> lbrace
67− , nest 2 $ vcat
68− [ attr "pname" $ string (pname deriv)
69− , attr "version" $ doubleQuotes (disp (version deriv))
70− , attr "sha256" $ string (sha256 deriv)
71− , boolattr "isLibrary" (not (isLibrary deriv) || (isExecutable deriv)) (isLibrary deriv)
72− , boolattr "isExecutable" (not (isLibrary deriv) || (isExecutable deriv)) (isExecutable deriv)
73− , listattr "buildDepends" (buildDepends deriv)
74− , listattr "buildTools" (buildTools deriv)
75− , listattr "extraLibraries" (extraLibs deriv)
76− , listattr "pkgconfigDepends" (pkgConfDeps deriv)
77− , boolattr "noHaddock" (not (runHaddock deriv)) (not (runHaddock deriv))
78− , disp (metaSection deriv)
79− ]
80− , rbrace <> rparen
81− , text ""
82− ]
83− where
84− inputs = nub $ sortBy (\x y -> compare (map toLower x) (map toLower y)) $
85− filter (/="cabal") $ filter (/="Cabal") $
86− buildDepends deriv ++ buildTools deriv ++ extraLibs deriv ++ pkgConfDeps deriv
87−
88−-- | A very incomplete parser that extracts 'pname', 'version',
89−-- 'sha256', 'platforms', 'maintainers', and 'runHaddock' from the given
90−-- Nix expression.
91−
92−parseDerivation :: String -> Maybe Derivation
93−parseDerivation buf
94− | buf =~ "cabal.mkDerivation"
95− , [name] <- buf `regsubmatch` "pname *= *\"([^\"]+)\""
96− , [vers'] <- buf `regsubmatch` "version *= *\"([^\"]+)\""
97− , Just vers <- simpleParse vers'
98− , [sha] <- buf `regsubmatch` "sha256 *= *\"([^\"]+)\""
99− , plats <- buf `regsubmatch` "platforms *= *([^;]+);"
100− , maint <- buf `regsubmatch` "maintainers *= *\\[([^\"]+)]"
101− , noHaddock <- buf `regsubmatch` "noHaddock *= *(true|false) *;"
102− = Just $ MkDerivation
103− { pname = name
104− , version = vers
105− , sha256 = sha
106− , isLibrary = False
107− , isExecutable = False
108− , buildDepends = []
109− , buildTools = []
110− , extraLibs = []
111− , pkgConfDeps = []
112− , runHaddock = case noHaddock of "true":[] -> False
113− _ -> True
114− , metaSection = Meta
115− { homepage = ""
116− , description = ""
117− , license = Unknown Nothing
118− , maintainers = concatMap words maint
119− , platforms = concatMap words (map (map (\c -> if c == '+' then ' ' else c)) plats)
120− }
121− }
122− | otherwise = Nothing
123−
124−regsubmatch :: String -> String -> [String]
125−regsubmatch buf patt = let (_,_,_,x) = f in x
126− where f :: (String,String,String,[String])
127− f = match (makeRegexOpts compExtended execBlank patt) buf
src/Distribution/NixOS/Derivation/License.hsdeleted−51
@@ -1,51 +0,0 @@
1−{- |
2− Module : Distribution.NixOS.Derivation.License
3− License : BSD3
4−
5− Maintainer : nix-dev@cs.uu.nl
6− Stability : provisional
7− Portability : portable
8−
9− Known licenses in Nix expressions are represented using the
10− attributes defined in @pkgs\/lib\/licenses.nix@, and unknown licenses
11− are represented as a literal string.
12− -}
13−
14−module Distribution.NixOS.Derivation.License ( License(..) ) where
15−
16−import Distribution.NixOS.PrettyPrinting
17−import Distribution.Text
18−
19−-- | The representation for licenses used in Nix derivations. Known
20−-- licenses are Nix expressions -- such as @stdenv.lib.licenses.bsd3@
21−-- --, so their exact \"name\" is not generally known, because the path
22−-- to @stdenv@ depends on the context defined in the expression. In
23−-- Cabal expressions, for example, the BSD3 license would have to be
24−-- referred to as @self.stdenv.lib.licenses.bsd3@. Other expressions,
25−-- however, use different paths to the @licenses@ record.. Because of
26−-- this station, this library cannot provide an abstract data type that
27−-- encompasses all known licenses. Instead, the @License@ type just
28−-- distinguishes references to known and unknown licenses. The
29−-- difference between the two is in the way they are pretty-printed:
30−--
31−-- > > putStrLn (display (Known "stdenv.lib.license.gpl2"))
32−-- > stdenv.lib.license.gpl2
33−-- >
34−-- > > putStrLn (display (Unknown (Just "GPL")))
35−-- > "GPL"
36−-- >
37−-- > > putStrLn (display (Unknown Nothing))
38−-- > "unknown"
39−--
40−-- Note that the "Text" instance definition provides pretty-printing,
41−-- but no parsing as of now!
42−
43−data License = Known String
44− | Unknown (Maybe String)
45− deriving (Show, Eq, Ord)
46−
47−instance Text License where
48− disp (Known x) = text x
49− disp (Unknown x) = string (maybe "unknown" id x)
50− parse = error "parsing Distribution.NixOS.Derivation.License is not supported yet"
51−
src/Distribution/NixOS/Derivation/Meta.hsdeleted−68
@@ -1,68 +0,0 @@
1−{- |
2− Module : Distribution.NixOS.Derivation.Meta
3− License : BSD3
4−
5− Maintainer : nix-dev@cs.uu.nl
6− Stability : provisional
7− Portability : portable
8−
9− A representation of the @meta@ section used in Nix expressions. A
10− detailed description can be found in section 4, \"Meta-attributes\",
11− of the Nixpkgs manual at <http://nixos.org/nixpkgs/docs.html>.
12− -}
13−
14−module Distribution.NixOS.Derivation.Meta
15− ( Meta(..)
16− , module Distribution.NixOS.Derivation.License
17− )
18− where
19−
20−import Distribution.NixOS.PrettyPrinting
21−import Distribution.NixOS.Derivation.License
22−import Distribution.Text
23−
24−-- | A representation of the @meta@ section used in Nix expressions.
25−--
26−-- > > putStrLn (display (Meta "http://example.org" "an example package" (Unknown Nothing)
27−-- > > ["stdenv.lib.platforms."++x | x<-["unix","cygwin"]]
28−-- > > ["stdenv.lib.maintainers."++x | x<-["joe","jane"]]))
29−-- > meta = {
30−-- > homepage = "http://example.org";
31−-- > description = "an example package";
32−-- > license = "unknown";
33−-- > platforms =
34−-- > stdenv.lib.platforms.unix ++ stdenv.lib.platforms.cygwin;
35−-- > maintainers = [ stdenv.lib.maintainers.joe stdenv.lib.maintainers.jane ];
36−-- > };
37−--
38−-- Note that the "Text" instance definition provides pretty-printing,
39−-- but no parsing as of now!
40−
41−data Meta = Meta
42− { homepage :: String -- ^ URL of the package homepage
43− , description :: String -- ^ short description of the package
44− , license :: License -- ^ licensing terms
45− , platforms :: [String] -- ^ list of supported platforms from @pkgs\/lib\/platforms.nix@
46− , maintainers :: [String] -- ^ list of maintainers from @pkgs\/lib\/maintainers.nix@
47− }
48− deriving (Show, Eq, Ord)
49−
50−instance Text Meta where
51− disp = renderMeta
52− parse = error "parsing Distribution.NixOS.Derivation.Cabal.Meta is not supported yet"
53−
54−renderMeta :: Meta -> Doc
55−renderMeta meta = vcat
56− [ text "meta" <+> equals <+> lbrace
57− , nest 2 $ vcat
58− [ onlyIf (homepage meta) $ attr "homepage" $ string (homepage meta)
59− , onlyIf (description meta) $ attr "description" $ string (description meta)
60− , attr "license" $ disp (license meta)
61− , onlyIf (platforms meta) $ sep
62− [ text "platforms" <+> equals
63− , nest 2 ((fsep $ punctuate (text " ++") $ map text (platforms meta))) <> semi
64− ]
65− , listattr "maintainers" (maintainers meta)
66− ]
67− , rbrace <> semi
68− ]
src/Distribution/NixOS/PrettyPrinting.hsdeleted−57
@@ -1,57 +0,0 @@
1−{- |
2− Module : Distribution.NixOS.PrettyPrinting
3− License : BSD3
4−
5− Maintainer : nix-dev@cs.uu.nl
6− Stability : provisional
7− Portability : portable
8−
9− Internal pretty-printing helpers for Nix expressions.
10−-}
11−
12−module Distribution.NixOS.PrettyPrinting
13− ( onlyIf
14− , listattr
15− , boolattr
16− , attr
17− , string
18− , funargs
19− , module Text.PrettyPrint
20− )
21− where
22−
23−import Text.PrettyPrint
24−
25−attr :: String -> Doc -> Doc
26−attr n v = text n <+> equals <+> v <> semi
27−
28−onlyIf :: [a] -> Doc -> Doc
29−onlyIf p d = if not (null p) then d else empty
30−
31−boolattr :: String -> Bool -> Bool -> Doc
32−boolattr n p v = if p then attr n (bool v) else empty
33−
34−listattr :: String -> [String] -> Doc
35−listattr n vs = onlyIf vs $
36− sep [ text n <+> equals <+> lbrack,
37− nest 2 $ fsep $ map text vs,
38− rbrack <> semi
39− ]
40−
41−bool :: Bool -> Doc
42−bool True = text "true"
43−bool False = text "false"
44−
45−string :: String -> Doc
46−string = doubleQuotes . text
47−
48−prepunctuate :: Doc -> [Doc] -> [Doc]
49−prepunctuate _ [] = []
50−prepunctuate p (d:ds) = d : map (p <>) ds
51−
52−funargs :: [Doc] -> Doc
53−funargs xs = sep [
54− lbrace <+> (fcat $ prepunctuate (comma <> text " ") $
55− map (nest 2) xs),
56− rbrace <> colon
57− ]