git.iko.soy/typeable/cabal2nix
on master help sign in
Merged "nixos-types" package back into this project.

We created that separate package because we believed that it would be useful independently form cabal2nix, but experience suggests that this is not the case, really; these two packages are very tightly coupled and it probably doesn't make sense to release them separately. Besides, the name "nixos-types" was an unfortunate choice anyway.

commit29a00bd51b6de9640816df76ecacf90b092a0f32 authorPeter Simons <simons@cryp.to> dateTue 14 Feb 2012, 17:31 (15 years ago) parent3e8bc6a3914e3a
6 files changed+333−6
Setup.hs+11−2 cabal2nix.cabal+11−4 src/Distribution/NixOS/Derivation/Cabal.hs+135 src/Distribution/NixOS/Derivation/License.hs+51 src/Distribution/NixOS/Derivation/Meta.hs+68 src/Distribution/NixOS/PrettyPrinting.hs+57
Setup.hs+11−2
@@ -1,6 +1,15 @@
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 )
46
57main :: IO ()
6−main = defaultMain
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) }
cabal2nix.cabal+11−4
@@ -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: Simple
12+Build-Type: Custom
1313Tested-With: GHC == 6.12.3, GHC == 7.0.4, GHC == 7.4.1
1414Data-files: README.md
1515Description:
@@ -46,7 +46,7 @@ Executable cabal2nix
4646 main-is: Cabal2Nix.hs
4747 hs-source-dirs: src
4848 Build-Depends: base >= 3 && < 5, regex-posix, pretty, Cabal >= 1.8,
49− filepath, directory, process, HTTP, nixos-types >= 1.2
49+ filepath, directory, process, HTTP
5050 Extensions: PatternGuards, RecordWildCards, CPP
5151 Ghc-Options: -Wall
5252 other-modules: Cabal2Nix.CorePackages
@@ -57,13 +57,16 @@ Executable cabal2nix
5757 Cabal2Nix.Name
5858 Cabal2Nix.Normalize
5959 Cabal2Nix.PostProcess
60+ Distribution.NixOS.Derivation.Cabal
61+ Distribution.NixOS.Derivation.License
62+ Distribution.NixOS.Derivation.Meta
63+ Distribution.NixOS.PrettyPrinting
6064
6165Executable hackage4nix
6266 main-is: Hackage4Nix.hs
6367 hs-source-dirs: src
6468 Build-Depends: base >= 3 && < 5, regex-posix, pretty, Cabal >= 1.8,
65− mtl, containers, directory, filepath, nixos-types >= 1.2,
66− hackage-db
69+ mtl, containers, directory, filepath, hackage-db
6770 Extensions: PatternGuards, RecordWildCards, CPP
6871 Ghc-Options: -Wall
6972 other-modules: Cabal2Nix.CorePackages
@@ -73,3 +76,7 @@ Executable hackage4nix
7376 Cabal2Nix.Name
7477 Cabal2Nix.Normalize
7578 Cabal2Nix.PostProcess
79+ Distribution.NixOS.Derivation.Cabal
80+ Distribution.NixOS.Derivation.License
81+ Distribution.NixOS.Derivation.Meta
82+ Distribution.NixOS.PrettyPrinting
src/Distribution/NixOS/Derivation/Cabal.hsnew file+135
@@ -0,0 +1,135 @@
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 Distribution.PackageDescription ( FlagAssignment, FlagName(..) )
30+import Data.Version
31+import Data.List
32+import Data.Char
33+import Text.Regex.Posix hiding ( empty )
34+
35+-- | A represtation of Nix expressions for building Haskell packages.
36+-- The data type correspond closely to the definition of
37+-- 'PackageDescription' from Cabal.
38+--
39+-- Note that the "Text" instance definition provides pretty-printing,
40+-- but no parsing as of now!
41+
42+data Derivation = MkDerivation
43+ { pname :: String
44+ , version :: Version
45+ , sha256 :: String
46+ , isLibrary :: Bool
47+ , isExecutable :: Bool
48+ , buildDepends :: [String]
49+ , buildTools :: [String]
50+ , extraLibs :: [String]
51+ , pkgConfDeps :: [String]
52+ , configureFlags :: [String]
53+ , cabalFlags :: FlagAssignment
54+ , runHaddock :: Bool
55+ , metaSection :: Meta
56+ }
57+ deriving (Show, Eq, Ord)
58+
59+instance Text Derivation where
60+ disp = renderDerivation
61+ parse = error "parsing Distribution.NixOS.Derivation.Cabal.Derivation is not supported yet"
62+
63+instance Package Derivation where
64+ packageId deriv = PackageIdentifier (PackageName (pname deriv)) (version deriv)
65+
66+renderDerivation :: Derivation -> Doc
67+renderDerivation deriv = funargs (map text ("cabal" : inputs)) $$ vcat
68+ [ text ""
69+ , text "cabal.mkDerivation" <+> lparen <> text "self" <> colon <+> lbrace
70+ , nest 2 $ vcat
71+ [ attr "pname" $ string (pname deriv)
72+ , attr "version" $ doubleQuotes (disp (version deriv))
73+ , attr "sha256" $ string (sha256 deriv)
74+ , boolattr "isLibrary" (not (isLibrary deriv) || (isExecutable deriv)) (isLibrary deriv)
75+ , boolattr "isExecutable" (not (isLibrary deriv) || (isExecutable deriv)) (isExecutable deriv)
76+ , listattr "buildDepends" (buildDepends deriv)
77+ , listattr "buildTools" (buildTools deriv)
78+ , listattr "extraLibraries" (extraLibs deriv)
79+ , listattr "pkgconfigDepends" (pkgConfDeps deriv)
80+ , onlyIf renderedFlags $ attr "configureFlags" $ doubleQuotes (sep renderedFlags)
81+ , boolattr "noHaddock" (not (runHaddock deriv)) (not (runHaddock deriv))
82+ , disp (metaSection deriv)
83+ ]
84+ , rbrace <> rparen
85+ , text ""
86+ ]
87+ where
88+ inputs = nub $ sortBy (\x y -> compare (map toLower x) (map toLower y)) $
89+ filter (/="cabal") $ filter (/="Cabal") $
90+ buildDepends deriv ++ buildTools deriv ++ extraLibs deriv ++ pkgConfDeps deriv
91+ renderedFlags = [ text "-f" <> (if enable then empty else char '-') <> text f | (FlagName f, enable) <- cabalFlags deriv ]
92+ ++ map text (configureFlags deriv)
93+
94+-- | A very incomplete parser that extracts 'pname', 'version',
95+-- 'sha256', 'platforms', 'maintainers', and 'runHaddock' from the given
96+-- Nix expression.
97+
98+parseDerivation :: String -> Maybe Derivation
99+parseDerivation buf
100+ | buf =~ "cabal.mkDerivation"
101+ , [name] <- buf `regsubmatch` "pname *= *\"([^\"]+)\""
102+ , [vers'] <- buf `regsubmatch` "version *= *\"([^\"]+)\""
103+ , Just vers <- simpleParse vers'
104+ , [sha] <- buf `regsubmatch` "sha256 *= *\"([^\"]+)\""
105+ , plats <- buf `regsubmatch` "platforms *= *([^;]+);"
106+ , maint <- buf `regsubmatch` "maintainers *= *\\[([^\"]+)]"
107+ , noHaddock <- buf `regsubmatch` "noHaddock *= *(true|false) *;"
108+ = Just $ MkDerivation
109+ { pname = name
110+ , version = vers
111+ , sha256 = sha
112+ , isLibrary = False
113+ , isExecutable = False
114+ , buildDepends = []
115+ , buildTools = []
116+ , extraLibs = []
117+ , pkgConfDeps = []
118+ , configureFlags = []
119+ , cabalFlags = []
120+ , runHaddock = case noHaddock of "true":[] -> False
121+ _ -> True
122+ , metaSection = Meta
123+ { homepage = ""
124+ , description = ""
125+ , license = Unknown Nothing
126+ , maintainers = concatMap words maint
127+ , platforms = concatMap words (map (map (\c -> if c == '+' then ' ' else c)) plats)
128+ }
129+ }
130+ | otherwise = Nothing
131+
132+regsubmatch :: String -> String -> [String]
133+regsubmatch buf patt = let (_,_,_,x) = f in x
134+ where f :: (String,String,String,[String])
135+ f = match (makeRegexOpts compExtended execBlank patt) buf
src/Distribution/NixOS/Derivation/License.hsnew file+51
@@ -0,0 +1,51 @@
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.hsnew file+68
@@ -0,0 +1,68 @@
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.hsnew file+57
@@ -0,0 +1,57 @@
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+ ]