packages feed

cabal2nix (empty) → 1.0

raw patch · 4 files changed

+185/−0 lines, 4 filesdep +Cabaldep +basesetup-changed

Dependencies added: Cabal, base

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Redistribution and use in source and binary forms, with or+without modification, are permitted provided that the+following conditions are met:++  * Redistributions of source code must retain the above+    copyright notice, this list of conditions and the+    following disclaimer.++  * Redistributions in binary form must reproduce the above+    copyright notice, this list of conditions and the+    following disclaimer in the documentation and/or other+    materials provided with the distribution.++  * The names of its contributors may not be used to endorse+    or promote products derived from this software without+    specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND+CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,+INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR+CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR+OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.lhs view
@@ -0,0 +1,8 @@+#!/usr/bin/env runhaskell++> module Main (main) where+>+> import Distribution.Simple+>+> main :: IO ()+> main = defaultMain
+ cabal2nix.cabal view
@@ -0,0 +1,33 @@+Name:                   cabal2nix+Version:                1.0+Copyright:              (c) 2011 Peter Simons+License:                BSD3+License-File:           LICENSE+Author:                 Peter Simons <simons@cryp.to>+Maintainer:             Peter Simons <simons@cryp.to>+Homepage:               http://github.com/peti/cabal2nix+Category:               Distribution+Synopsis:               Convert Cabal files into Nix build instructions+Cabal-Version:          >= 1.6+Build-Type:             Simple+Tested-With:            GHC == 7.0.2+Description:            This utility converts Cabal files into Nix build+			instructions. Run the binary as follows:+			.+			> cabal2nix foo.cabal 57ae0330a3d0520bae3d1e447d7d5c88de9ecbf0de133f31f2cc42961c81d47f+			.+			The first argument is the path to the cabal file+			to be converted, the second argument is the+			SHA256 hash of the corresponding source tarball.+			.+			The Nix build instructions will be written to+			stdout.++Source-Repository head+  Type:                 git+  Location:             git://github.com/peti/cabal2nix.git++Executable cabal2nix+  main-is:              cabal2nix.hs+  Build-Depends:        base >= 3 && < 5, Cabal+  Ghc-Options:          -Wall
+ cabal2nix.hs view
@@ -0,0 +1,114 @@+-- cabal2nix.hs+--+-- Copyright (c) 20011 Peter Simons <simons@cryp.to>+-- See LICENSE file for licensing details.++module Main ( main ) where++import System.IO+import System.Environment+import Control.Exception+import System.Exit+import Distribution.PackageDescription.Parse+import Distribution.Verbosity ( silent )+import Distribution.PackageDescription+import Distribution.Package+import Distribution.License+import Data.Version+import Data.List+import Control.Monad+import Data.Char++type PkgName = String+type PkgVersion = [Int]+type PkgSHA256 = String+type PkgURL = String+type PkgDescription = String+type PkgLicense = License+type PkgDependencies = [CondTree ConfVar [Dependency] ()]+type PkgExtraLibs = [String]++data Pkg = Pkg PkgName PkgVersion PkgSHA256 PkgURL PkgDescription PkgLicense PkgDependencies PkgExtraLibs+  deriving (Show)++toNixName :: String -> String+toNixName name = f name+  where+    f []                            = []+    f ('-':c:cs) | c `notElem` "-"  = toUpper c : f cs+    f ('-':_)                       = error ("unexpected package name " ++ show name)+    f (c:cs)                        = c : f cs++toNix :: Pkg -> String+toNix (Pkg name ver sha256 url desc lic deps libs) =+       "{" ++ exprArgs ++"}:\n\n"+    ++ "cabal.mkDerivation (self : {\n"+    ++ "  pname = " ++ show name ++ ";\n"+    ++ "  version = \"" ++ showVer ++ "\";\n"+    ++ "  sha256 = " ++ show sha256 ++ ";\n"+    ++ "  propagatedBuildInputs = [" ++ depList ++ "];\n"+    ++ "  meta = {\n"+    ++ "    homepage = \"" ++ url ++ "\";\n"+    ++ "    description = " ++ show desc ++ ";\n"+    ++ "    license = " ++ showLic lic ++ ";\n"+    ++ "  };\n"+    ++ "})\n"+    where+      exprArgs = concat (intersperse "," ((if "cabal" `notElem` pkgDeps then ["cabal"] else []) ++ pkgDeps))+      showVer = concat (intersperse "." (map show ver))+      depList = concat (intersperse " " pkgDeps)+      pkgDeps :: [String]+      pkgDeps = map toNixName $+                  nub $ sort $ libs +++                    [ n | dep <- deps, Dependency (PackageName n) _ <- condTreeConstraints dep+                        , n `notElem` ["base","containers"]+                    ]+      showLic (GPL Nothing)                     = show "GPL"+      showLic (GPL (Just (Version [2] [])))     = "self.stdenv.lib.licenses.gpl2"+      showLic (GPL (Just (Version [3] [])))     = "self.stdenv.lib.licenses.gpl3"+      showLic (LGPL Nothing)                    = show "LGPL"+      showLic (LGPL (Just (Version [2,1] [])))  = "self.stdenv.lib.licenses.lgpl21"+      showLic (LGPL (Just (Version [3] [])))    = "self.stdenv.lib.licenses.lgpl3"+      showLic BSD3                              = "self.stdenv.lib.licenses.bsd3"+      showLic BSD4                              = "self.stdenv.lib.licenses.bsd4"+      showLic MIT                               = "self.stdenv.lib.licenses.mit"+      showLic PublicDomain                      = "self.stdenv.lib.licenses.publicDomain"+      showLic AllRightsReserved                 = "unknown"+      showLic OtherLicense                      = "unknown"+      showLic l                                 = error $ "unknown license: " ++ show l++readPackage :: FilePath -> String -> IO Pkg+readPackage cabalFile sha256 = do+  cabal <- readPackageDescription silent cabalFile+  let pkg = packageDescription cabal+      PackageName pkgname = pkgName (package pkg)+      pkgver = versionBranch (pkgVersion (package pkg))+      lic = license pkg+      url = homepage pkg+      desc = synopsis pkg+      -- globalDeps = buildDepends pkg+      libDeps = maybe [] (\x -> [x]) (condLibrary cabal)+      exeDeps = [ tree | (_,tree) <- condExecutables cabal ]+      libs = concat [ extraLibs (libBuildInfo (condTreeData x)) | x <- libDeps ]+      libs' = concat [ extraLibs (buildInfo (condTreeData x)) | x <- exeDeps ]+  return (Pkg pkgname pkgver sha256 url desc lic (map simplify libDeps ++ map simplify exeDeps) (libs++libs'))+    where++simplify :: CondTree ConfVar [Dependency] a -> CondTree ConfVar [Dependency] ()+simplify (CondNode _ deps nodes) = CondNode () deps (map simp nodes)+  where+    simp (cond,tree,mtree) = (cond, simplify tree, maybe Nothing (Just . simplify) mtree)++main :: IO ()+main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do+  args <- getArgs++  when (length args /= 2) $ do+    mapM_ (hPutStrLn stderr) [ "*** invalid command line syntax"+                             , "Usage: cabal2nix cabal-file sha256-hash"+                             ]+    exitFailure++  let cabalFile:sha256:[] = args+  pkg <- readPackage cabalFile sha256+  putStr (toNix pkg)