git.iko.soy/typeable/cabal2nix
on master help sign in
Simplify type construction with Lenses.
commit37a7878ee7f1c3a9c80cf2331fdef3cdd8e18c80 authorPeter Simons <simons@cryp.to> dateThu 20 Aug 2015, 17:43 (11 years ago) parentc79b858
9 files changed+70−52
generate-cabal-file.hs+1 release.nix+2−2 src/Cabal2Nix/Generate.hs+7−7 src/Cabal2Nix/Normalize.hs+4−4 src/Distribution/Nixpkgs/Haskell.hs+25−25 src/Distribution/Nixpkgs/Meta.hs+12−12 src/Internal/Lens.hs+13 src/Language/Nix/Identifier.hs+3−1 src/Language/Nix/Path.hs+3−1
generate-cabal-file.hs+1
@@ -64,6 +64,7 @@ commonBuildDepends =
6464 , unconstrained "bytestring"
6565 , package "Cabal" (gtEq [1,22,2])
6666 , unconstrained "containers"
67+ , unconstrained "data-default"
6768 , unconstrained "deepseq-generics"
6869 , unconstrained "directory"
6970 , unconstrained "filepath"
release.nix+2−2
@@ -27,13 +27,13 @@ in
2727 aeson base bytestring Cabal containers deepseq deepseq-generics
2828 directory filepath hackage-db monad-par monad-par-extras mtl pretty
2929 process QuickCheck regex-posix SHA split transformers
30− utf8-string lens optparse-applicative
30+ utf8-string lens optparse-applicative data-default
3131 ];
3232 testDepends = with haskellPackages; [
3333 aeson base bytestring Cabal containers deepseq deepseq-generics
3434 directory doctest filepath hackage-db hspec monad-par
3535 monad-par-extras mtl pretty process QuickCheck
36− regex-posix SHA split transformers utf8-string
36+ regex-posix SHA split transformers utf8-string data-default
3737 ];
3838 homepage = "http://github.com/NixOS/cabal2nix";
3939 description = "Convert Cabal files into Nix build instructions";
src/Cabal2Nix/Generate.hs+7−7
@@ -7,7 +7,7 @@ import Cabal2Nix.License
77import Cabal2Nix.Name
88import Cabal2Nix.Normalize
99import Cabal2Nix.PostProcess
10−import Control.Lens
10+import Internal.Lens
1111import Data.Maybe
1212import qualified Data.Set as Set
1313import Data.Version
@@ -41,7 +41,7 @@ cabal2nix' PackageDescription {..} = normalize $ postProcess $
4141 let
4242 xrev = maybe 0 read (lookup "x-revision" customFieldsPD)
4343 in
44− nullDerivation
44+ def
4545 & pkgid .~ package
4646 & revision .~ xrev
4747 & src .~ DerivationSource
@@ -68,7 +68,7 @@ cabal2nix' PackageDescription {..} = normalize $ postProcess $
6868 & editedCabalFile .~ (if xrev > 0 then fromMaybe (error (display package ++ ": X-Cabal-File-Hash field is missing"))
6969 (lookup "X-Cabal-File-Hash" customFieldsPD)
7070 else "")
71− & metaSection .~ ( Nix.nullMeta
71+ & metaSection .~ ( def
7272 & Nix.homepage .~ homepage
7373 & Nix.description .~ synopsis
7474 & Nix.license .~ fromCabalLicense license
@@ -80,7 +80,7 @@ cabal2nix' PackageDescription {..} = normalize $ postProcess $
8080
8181convertBuildInfo :: Cabal.BuildInfo -> Nix.BuildInfo
8282convertBuildInfo Cabal.BuildInfo {..} = mempty
83− & haskell .~ Set.fromList [ set ident y undefined | (Dependency (PackageName y) _) <- targetBuildDepends ]
84− & system .~ Set.fromList [ set ident y undefined | x <- extraLibs, y <- libNixName x, not (null y) ]
85− & pkgconfig .~ Set.fromList [ set ident y undefined | Dependency (PackageName x) _ <- pkgconfigDepends, y <- libNixName x, not (null y) ]
86− & tool .~ Set.fromList [ set ident y undefined | Dependency (PackageName x) _ <- buildTools, y <- buildToolNixName x, not (null y) ]
83+ & haskell .~ Set.fromList [ create ident y | (Dependency (PackageName y) _) <- targetBuildDepends ]
84+ & system .~ Set.fromList [ create ident y | x <- extraLibs, y <- libNixName x, not (null y) ]
85+ & pkgconfig .~ Set.fromList [ create ident y | Dependency (PackageName x) _ <- pkgconfigDepends, y <- libNixName x, not (null y) ]
86+ & tool .~ Set.fromList [ create ident y | Dependency (PackageName x) _ <- buildTools, y <- buildToolNixName x, not (null y) ]
src/Cabal2Nix/Normalize.hs+4−4
@@ -2,7 +2,7 @@ module Cabal2Nix.Normalize ( normalize, normalizeCabalFlags ) where
22
33import Cabal2Nix.CorePackages
44import Cabal2Nix.Name
5−import Control.Lens
5+import Internal.Lens
66import Data.Function
77import Data.List
88import Data.Set ( Set )
@@ -25,8 +25,8 @@ normalize drv = drv
2525
2626normalizeBuildInfo :: PackageName -> BuildInfo -> BuildInfo
2727normalizeBuildInfo (PackageName pname) bi = bi
28− & haskell . contains (set ident pname undefined) .~ False
29− & tool . contains (set ident pname undefined) .~ False
28+ & haskell . contains (create ident pname) .~ False
29+ & tool . contains (create ident pname) .~ False
3030 & tool %~ normalizeNixBuildTools . Set.filter (\n -> n^.ident `notElem` coreBuildTools)
3131
3232 {-
@@ -68,7 +68,7 @@ normalizeNixBuildTools :: Set Identifier -> Set Identifier
6868normalizeNixBuildTools = Set.fromList . concatMap buildToolNixName' . Set.toList
6969 where
7070 buildToolNixName' :: Identifier -> [Identifier]
71− buildToolNixName' n = [ set ident n' undefined | n' <- buildToolNixName (n^.ident) ]
71+ buildToolNixName' n = [ create ident n' | n' <- buildToolNixName (n^.ident) ]
7272
7373-- |Strip any kind of path prefix from maintainer names, filter duplicates, and
7474-- sort the resulting list alphabetically.
src/Distribution/Nixpkgs/Haskell.hs+25−25
@@ -4,7 +4,7 @@
44{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
55
66module Distribution.Nixpkgs.Haskell
7− ( Derivation, nullDerivation, pkgid, revision, src, isLibrary, isExecutable
7+ ( Derivation, pkgid, revision, src, isLibrary, isExecutable
88 , extraFunctionArgs, libraryDepends, executableDepends, testDepends, configureFlags
99 , cabalFlags, runHaddock, jailbreak, doCheck, testTarget, hyperlinkSource, enableSplitObjs
1010 , phaseOverrides, editedCabalFile, metaSection
@@ -14,7 +14,7 @@ module Distribution.Nixpkgs.Haskell
1414 where
1515
1616import Control.DeepSeq.Generics
17−import Control.Lens
17+import Internal.Lens
1818import Data.List
1919import Data.Set ( Set )
2020import qualified Data.Set as Set
@@ -73,29 +73,29 @@ data Derivation = MkDerivation
7373 }
7474 deriving (Show, Eq, Generic)
7575
76−nullDerivation :: Derivation
77−nullDerivation = MkDerivation
78− { _pkgid = error "undefined _pkgid"
79− , _revision = error "undefined _revision"
80− , _src = error "undefined _src"
81− , _isLibrary = error "undefined _isLibrary"
82− , _isExecutable = error "undefined _isExecutable"
83− , _extraFunctionArgs = error "undefined _extraFunctionArgs"
84− , _libraryDepends = error "undefined _libraryDepends"
85− , _executableDepends = error "undefined _executableDepends"
86− , _testDepends = error "undefined _testDepends"
87− , _configureFlags = error "undefined _configureFlags"
88− , _cabalFlags = error "undefined _cabalFlags"
89− , _runHaddock = error "undefined _runHaddock"
90− , _jailbreak = error "undefined _jailbreak"
91− , _doCheck = error "undefined _doCheck"
92− , _testTarget = error "undefined _testTarget"
93− , _hyperlinkSource = error "undefined _hyperlinkSource"
94− , _enableSplitObjs = error "undefined _enableSplitObjs"
95− , _phaseOverrides = error "undefined _phaseOverrides"
96− , _editedCabalFile = error "undefined _editedCabalFile"
97− , _metaSection = error "undefined _metaSection"
98− }
76+instance Default Derivation where
77+ def = MkDerivation
78+ { _pkgid = error "undefined Derivation.pkgid"
79+ , _revision = error "undefined Derivation.revision"
80+ , _src = error "undefined Derivation.src"
81+ , _isLibrary = error "undefined Derivation.isLibrary"
82+ , _isExecutable = error "undefined Derivation.isExecutable"
83+ , _extraFunctionArgs = error "undefined Derivation.extraFunctionArgs"
84+ , _libraryDepends = error "undefined Derivation.libraryDepends"
85+ , _executableDepends = error "undefined Derivation.executableDepends"
86+ , _testDepends = error "undefined Derivation.testDepends"
87+ , _configureFlags = error "undefined Derivation.configureFlags"
88+ , _cabalFlags = error "undefined Derivation.cabalFlags"
89+ , _runHaddock = error "undefined Derivation.runHaddock"
90+ , _jailbreak = error "undefined Derivation.jailbreak"
91+ , _doCheck = error "undefined Derivation.doCheck"
92+ , _testTarget = error "undefined Derivation.testTarget"
93+ , _hyperlinkSource = error "undefined Derivation.hyperlinkSource"
94+ , _enableSplitObjs = error "undefined Derivation.enableSplitObjs"
95+ , _phaseOverrides = error "undefined Derivation.phaseOverrides"
96+ , _editedCabalFile = error "undefined Derivation.editedCabalFile"
97+ , _metaSection = error "undefined Derivation.metaSection"
98+ }
9999
100100makeLenses ''Derivation
101101
src/Distribution/Nixpkgs/Meta.hs+12−12
@@ -7,13 +7,13 @@
77 -}
88
99module Distribution.Nixpkgs.Meta
10− ( Meta, nullMeta
10+ ( Meta
1111 , homepage, description, license, platforms, hydraPlatforms, maintainers, broken
1212 ) where
1313
1414
1515import Control.DeepSeq.Generics
16−import Control.Lens
16+import Internal.Lens
1717import Distribution.Nixpkgs.License
1818import Distribution.Nixpkgs.Util.PrettyPrinting
1919import GHC.Generics ( Generic )
@@ -72,13 +72,13 @@ renderPlatformList :: [String] -> Doc
7272renderPlatformList plats =
7373 nest 2 (fsep $ punctuate (text " ++") $ map text plats) <> semi
7474
75−nullMeta :: Meta
76−nullMeta = Meta
77− { _homepage = error "undefined Nix._homepage"
78− , _description = error "undefined Nix._description"
79− , _license = error "undefined Nix._license"
80− , _platforms = error "undefined Nix._platforms"
81− , _hydraPlatforms = error "undefined Nix._hydraPlatforms"
82− , _maintainers = error "undefined Nix._maintainers"
83− , _broken = error "undefined Nix._broken"
84− }
75+instance Default Meta where
76+ def = Meta
77+ { _homepage = error "undefined Meta.homepage"
78+ , _description = error "undefined Meta.description"
79+ , _license = error "undefined Meta.license"
80+ , _platforms = error "undefined Meta.platforms"
81+ , _hydraPlatforms = error "undefined Meta.hydraPlatforms"
82+ , _maintainers = error "undefined Meta.maintainers"
83+ , _broken = error "undefined Meta.broken"
84+ }
src/Internal/Lens.hsnew file+13
@@ -0,0 +1,13 @@
1+module Internal.Lens
2+ ( create
3+ , module Data.Default
4+ , module Control.Lens
5+ ) where
6+
7+import Control.Lens
8+import Data.Default
9+
10+-- | A convenient helper function for using Lenses as constructors.
11+
12+create :: Default s => ASetter s t a b -> b -> t
13+create f x = set f x def
src/Language/Nix/Identifier.hs+3−1
@@ -4,7 +4,7 @@
44module Language.Nix.Identifier ( Identifier, ident, quote, needsQuoting ) where
55
66import Control.DeepSeq.Generics
7−import Control.Lens
7+import Internal.Lens
88import Data.Char
99import Data.Function ( on )
1010import Data.String
@@ -42,6 +42,8 @@ import GHC.Generics ( Generic )
4242newtype Identifier = Identifier String
4343 deriving (Show, Eq, IsString, Generic)
4444
45+instance Default Identifier where def = Identifier ""
46+
4547instance Pretty Identifier where
4648 pPrint i = text (i ^. ident . to quote)
4749
src/Language/Nix/Path.hs+3−1
@@ -1,6 +1,6 @@
11module Language.Nix.Path ( Path, path ) where
22
3−import Control.Lens
3+import Internal.Lens
44import Distribution.Nixpkgs.Util.PrettyPrinting ( Pretty (..), char, hcat, punctuate )
55import Language.Nix.Identifier
66
@@ -32,6 +32,8 @@ import Language.Nix.Identifier
3232newtype Path = Path { _segments :: [Identifier] }
3333 deriving (Show, Eq, Ord)
3434
35+instance Default Path where def = Path (error "undefined Nix.Path")
36+
3537path :: Lens' Path [Identifier]
3638path f (Path ids) = mkPath `fmap` f ids
3739 where