git.iko.soy/typeable/cabal2nix
on master help sign in
Support local-only projects
1a871ebBenno Fünfstück
src/Cabal2Nix/Generate.hs+5−5
@@ -1,23 +1,23 @@
1module Cabal2Nix.Generate ( cabal2nix ) where
1module Cabal2Nix.Generate ( cabal2nix ) where
2
2
3+import Cabal2Nix.Flags
3import Cabal2Nix.License
4import Cabal2Nix.License
4−import Cabal2Nix.PostProcess
5import Cabal2Nix.Normalize
5import Cabal2Nix.Normalize
6−import Cabal2Nix.Flags
6+import Cabal2Nix.PostProcess
7import Data.Maybe
7import Data.Maybe
8import Distribution.Compiler
8import Distribution.Compiler
9+import Distribution.NixOS.Derivation.Cabal
9import qualified Distribution.Package as Cabal
10import qualified Distribution.Package as Cabal
10import qualified Distribution.PackageDescription as Cabal
11import qualified Distribution.PackageDescription as Cabal
11import Distribution.PackageDescription.Configuration
12import Distribution.PackageDescription.Configuration
12import Distribution.System
13import Distribution.System
13−import Distribution.Version
14−import Distribution.NixOS.Derivation.Cabal
15
14
16cabal2nix :: Cabal.GenericPackageDescription -> Derivation
15cabal2nix :: Cabal.GenericPackageDescription -> Derivation
17cabal2nix cabal = normalize $ postProcess MkDerivation
16cabal2nix cabal = normalize $ postProcess MkDerivation
18 { pname = let Cabal.PackageName x = Cabal.pkgName pkg in x
17 { pname = let Cabal.PackageName x = Cabal.pkgName pkg in x
19 , version = Cabal.pkgVersion pkg
18 , version = Cabal.pkgVersion pkg
20− , sha256 = "cabal2nix left the sha256 field undefined"
19+ , sha256 = error "cabal2nix left the sha256 field undefined"
20+ , src = Nothing
21 , isLibrary = isJust (Cabal.library tpkg)
21 , isLibrary = isJust (Cabal.library tpkg)
22 , isExecutable = not (null (Cabal.executables tpkg))
22 , isExecutable = not (null (Cabal.executables tpkg))
23 , buildDepends = map unDep deps
23 , buildDepends = map unDep deps
src/Distribution/NixOS/Derivation/Cabal.hs+13−7
@@ -19,10 +19,11 @@ module Distribution.NixOS.Derivation.Cabal
19 )
19 )
20 where
20 where
21
21
22+import Data.Maybe
22import Distribution.NixOS.Derivation.Meta
23import Distribution.NixOS.Derivation.Meta
23import Distribution.NixOS.PrettyPrinting
24import Distribution.NixOS.PrettyPrinting
24−import Distribution.Text
25import Distribution.Package
25import Distribution.Package
26+import Distribution.Text
26#ifdef __HADDOCK__
27#ifdef __HADDOCK__
27import Distribution.PackageDescription ( PackageDescription )
28import Distribution.PackageDescription ( PackageDescription )
28#endif
29#endif
@@ -43,7 +44,8 @@ import Text.Regex.Posix hiding ( empty )
43data Derivation = MkDerivation
44data Derivation = MkDerivation
44 { pname :: String
45 { pname :: String
45 , version :: Version
46 , version :: Version
47+ , sha256 :: Maybe String
46− , sha256 :: String
48+ , src :: Maybe String
47 , isLibrary :: Bool
49 , isLibrary :: Bool
48 , isExecutable :: Bool
50 , isExecutable :: Bool
49 , buildDepends :: [String]
51 , buildDepends :: [String]
@@ -73,11 +75,13 @@ renderDerivation :: Derivation -> Doc
73renderDerivation deriv = funargs (map text ("cabal" : inputs)) $$ vcat
75renderDerivation deriv = funargs (map text ("cabal" : inputs)) $$ vcat
74 [ text ""
76 [ text ""
75 , text "cabal.mkDerivation" <+> lparen <> text "self" <> colon <+> lbrace
77 , text "cabal.mkDerivation" <+> lparen <> text "self" <> colon <+> lbrace
76− , nest 2 $ vcat
78+ , nest 2 $ vcat $
77 [ attr "pname" $ string (pname deriv)
79 [ attr "pname" $ string (pname deriv)
78 , attr "version" $ doubleQuotes (disp (version deriv))
80 , attr "version" $ doubleQuotes (disp (version deriv))
79− , attr "sha256" $ string (sha256 deriv)
81+ ] ++ maybeToList (fmap (attr "sha256" . string) $ sha256 deriv)
82+ ++ maybeToList (fmap (attr "src" . string) $ src deriv)
83+ ++
80− , boolattr "isLibrary" (not (isLibrary deriv) || isExecutable deriv) (isLibrary deriv)
84+ [ boolattr "isLibrary" (not (isLibrary deriv) || isExecutable deriv) (isLibrary deriv)
81 , boolattr "isExecutable" (not (isLibrary deriv) || isExecutable deriv) (isExecutable deriv)
85 , boolattr "isExecutable" (not (isLibrary deriv) || isExecutable deriv) (isExecutable deriv)
82 , listattr "buildDepends" (buildDepends deriv)
86 , listattr "buildDepends" (buildDepends deriv)
83 , listattr "testDepends" (testDepends deriv)
87 , listattr "testDepends" (testDepends deriv)
@@ -111,7 +115,8 @@ parseDerivation buf
111 , [name] <- buf `regsubmatch` "pname *= *\"([^\"]+)\""
115 , [name] <- buf `regsubmatch` "pname *= *\"([^\"]+)\""
112 , [vers'] <- buf `regsubmatch` "version *= *\"([^\"]+)\""
116 , [vers'] <- buf `regsubmatch` "version *= *\"([^\"]+)\""
113 , Just vers <- simpleParse vers'
117 , Just vers <- simpleParse vers'
114− , [sha] <- buf `regsubmatch` "sha256 *= *\"([^\"]+)\""
118+ , sha <- buf `regsubmatch` "sha256 *= *\"([^\"]+)\""
119+ , sr <- buf `regsubmatch` "src *= *([^;]+);"
115 , plats <- buf `regsubmatch` "platforms *= *([^;]+);"
120 , plats <- buf `regsubmatch` "platforms *= *([^;]+);"
116 , hplats <- buf `regsubmatch` "hydraPlatforms *= *([^;]+);"
121 , hplats <- buf `regsubmatch` "hydraPlatforms *= *([^;]+);"
117 , maint <- buf `regsubmatch` "maintainers *= *\\[([^\"]+)]"
122 , maint <- buf `regsubmatch` "maintainers *= *\\[([^\"]+)]"
@@ -121,7 +126,8 @@ parseDerivation buf
121 = Just MkDerivation
126 = Just MkDerivation
122 { pname = name
127 { pname = name
123 , version = vers
128 , version = vers
124− , sha256 = sha
129+ , sha256 = listToMaybe sha
130+ , src = listToMaybe sr
125 , isLibrary = False
131 , isLibrary = False
126 , isExecutable = False
132 , isExecutable = False
127 , buildDepends = []
133 , buildDepends = []
src/cabal2nix.hs+9−4
@@ -1,7 +1,7 @@
1module Main ( main ) where
1module Main ( main ) where
2
2
3−import Cabal2Nix.Hackage ( hashPackage, readCabalFile )
4import Cabal2Nix.Generate ( cabal2nix )
3import Cabal2Nix.Generate ( cabal2nix )
4+import Cabal2Nix.Hackage ( hashPackage, readCabalFile )
5import Cabal2Nix.Normalize ( normalize )
5import Cabal2Nix.Normalize ( normalize )
6import Distribution.NixOS.Derivation.Cabal
6import Distribution.NixOS.Derivation.Cabal
7
7
@@ -24,6 +24,7 @@ data Configuration = Configuration
24 , optHaddock :: Bool
24 , optHaddock :: Bool
25 , optDoCheck :: Bool
25 , optDoCheck :: Bool
26 , optJailbreak :: Bool
26 , optJailbreak :: Bool
27+ , optLocal :: Bool
27 }
28 }
28 deriving (Show)
29 deriving (Show)
29
30
@@ -37,17 +38,19 @@ defaultConfiguration = Configuration
37 , optHaddock = True
38 , optHaddock = True
38 , optDoCheck = True
39 , optDoCheck = True
39 , optJailbreak = False
40 , optJailbreak = False
41+ , optLocal = False
40 }
42 }
41
43
42options :: [OptDescr (Configuration -> Configuration)]
44options :: [OptDescr (Configuration -> Configuration)]
43options =
45options =
44 [ Option "h" ["help"] (NoArg (\o -> o { optPrintHelp = True })) "show this help text"
46 [ Option "h" ["help"] (NoArg (\o -> o { optPrintHelp = True })) "show this help text"
45− , Option "" ["sha256"] (ReqArg (\x o -> o { optSha256 = Just x }) "HASH") "sha256 hash of source tarball"
47+ , Option "" ["sha256"] (ReqArg (\x o -> o { optSha256 = Just x }) "HASH") "sha256 hash of source tarball"
46 , Option "m" ["maintainer"] (ReqArg (\x o -> o { optMaintainer = x : optMaintainer o }) "MAINTAINER") "maintainer of this package (may be specified multiple times)"
48 , Option "m" ["maintainer"] (ReqArg (\x o -> o { optMaintainer = x : optMaintainer o }) "MAINTAINER") "maintainer of this package (may be specified multiple times)"
47 , Option "p" ["platform"] (ReqArg (\x o -> o { optPlatform = x : optPlatform o }) "PLATFORM") "supported build platforms (may be specified multiple times)"
49 , Option "p" ["platform"] (ReqArg (\x o -> o { optPlatform = x : optPlatform o }) "PLATFORM") "supported build platforms (may be specified multiple times)"
48 , Option "" ["no-haddock"] (NoArg (\o -> o { optHaddock = False })) "don't run Haddock when building this package"
50 , Option "" ["no-haddock"] (NoArg (\o -> o { optHaddock = False })) "don't run Haddock when building this package"
49 , Option "" ["no-check"] (NoArg (\o -> o { optDoCheck = False })) "don't run regression test suites of this package"
51 , Option "" ["no-check"] (NoArg (\o -> o { optDoCheck = False })) "don't run regression test suites of this package"
50 , Option "" ["jailbreak"] (NoArg (\o -> o { optJailbreak = True })) "don't honor version restrictions on build inputs"
52 , Option "" ["jailbreak"] (NoArg (\o -> o { optJailbreak = True })) "don't honor version restrictions on build inputs"
53+ , Option "" ["local"] (NoArg (\o -> o { optLocal = True })) "assume a local package that is not on hackage yet. "
51 ]
54 ]
52
55
53usage :: String
56usage :: String
@@ -85,8 +88,9 @@ main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
85
88
86 let packageId = package (packageDescription cabal)
89 let packageId = package (packageDescription cabal)
87 sha <- case optSha256 cfg of
90 sha <- case optSha256 cfg of
88− Just hash -> return hash
91+ Just hash -> return $ Just hash
92+ Nothing | optLocal cfg -> return Nothing
89− Nothing -> hashPackage packageId
93+ Nothing -> fmap Just $ hashPackage packageId
90
94
91 let deriv = (cabal2nix cabal) { sha256 = sha, runHaddock = optHaddock cfg, jailbreak = optJailbreak cfg }
95 let deriv = (cabal2nix cabal) { sha256 = sha, runHaddock = optHaddock cfg, jailbreak = optJailbreak cfg }
92 deriv' = deriv { metaSection = (metaSection deriv)
96 deriv' = deriv { metaSection = (metaSection deriv)
@@ -94,6 +98,7 @@ main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
94 , platforms = optPlatform cfg
98 , platforms = optPlatform cfg
95 }
99 }
96 , doCheck = doCheck deriv && optDoCheck cfg
100 , doCheck = doCheck deriv && optDoCheck cfg
101+ , src = if optLocal cfg then Just "./." else Nothing
97 }
102 }
98
103
99 putStr (show (disp (normalize deriv')))
104 putStr (show (disp (normalize deriv')))