git.iko.soy/typeable/cabal2nix
on master help sign in
Simplify type construction with Lenses.
37a7878Peter Simons
generate-cabal-file.hs+1
@@ -64,6 +64,7 @@ commonBuildDepends =
64 , unconstrained "bytestring"
64 , unconstrained "bytestring"
65 , package "Cabal" (gtEq [1,22,2])
65 , package "Cabal" (gtEq [1,22,2])
66 , unconstrained "containers"
66 , unconstrained "containers"
67+ , unconstrained "data-default"
67 , unconstrained "deepseq-generics"
68 , unconstrained "deepseq-generics"
68 , unconstrained "directory"
69 , unconstrained "directory"
69 , unconstrained "filepath"
70 , unconstrained "filepath"
release.nix+2−2
@@ -27,13 +27,13 @@ in
27 aeson base bytestring Cabal containers deepseq deepseq-generics
27 aeson base bytestring Cabal containers deepseq deepseq-generics
28 directory filepath hackage-db monad-par monad-par-extras mtl pretty
28 directory filepath hackage-db monad-par monad-par-extras mtl pretty
29 process QuickCheck regex-posix SHA split transformers
29 process QuickCheck regex-posix SHA split transformers
30− utf8-string lens optparse-applicative
30+ utf8-string lens optparse-applicative data-default
31 ];
31 ];
32 testDepends = with haskellPackages; [
32 testDepends = with haskellPackages; [
33 aeson base bytestring Cabal containers deepseq deepseq-generics
33 aeson base bytestring Cabal containers deepseq deepseq-generics
34 directory doctest filepath hackage-db hspec monad-par
34 directory doctest filepath hackage-db hspec monad-par
35 monad-par-extras mtl pretty process QuickCheck
35 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
37 ];
37 ];
38 homepage = "http://github.com/NixOS/cabal2nix";
38 homepage = "http://github.com/NixOS/cabal2nix";
39 description = "Convert Cabal files into Nix build instructions";
39 description = "Convert Cabal files into Nix build instructions";
src/Cabal2Nix/Generate.hs+7−7
@@ -7,7 +7,7 @@ import Cabal2Nix.License
7import Cabal2Nix.Name
7import Cabal2Nix.Name
8import Cabal2Nix.Normalize
8import Cabal2Nix.Normalize
9import Cabal2Nix.PostProcess
9import Cabal2Nix.PostProcess
10−import Control.Lens
10+import Internal.Lens
11import Data.Maybe
11import Data.Maybe
12import qualified Data.Set as Set
12import qualified Data.Set as Set
13import Data.Version
13import Data.Version
@@ -41,7 +41,7 @@ cabal2nix' PackageDescription {..} = normalize $ postProcess $
41 let
41 let
42 xrev = maybe 0 read (lookup "x-revision" customFieldsPD)
42 xrev = maybe 0 read (lookup "x-revision" customFieldsPD)
43 in
43 in
44− nullDerivation
44+ def
45 & pkgid .~ package
45 & pkgid .~ package
46 & revision .~ xrev
46 & revision .~ xrev
47 & src .~ DerivationSource
47 & src .~ DerivationSource
@@ -68,7 +68,7 @@ cabal2nix' PackageDescription {..} = normalize $ postProcess $
68 & editedCabalFile .~ (if xrev > 0 then fromMaybe (error (display package ++ ": X-Cabal-File-Hash field is missing"))
68 & editedCabalFile .~ (if xrev > 0 then fromMaybe (error (display package ++ ": X-Cabal-File-Hash field is missing"))
69 (lookup "X-Cabal-File-Hash" customFieldsPD)
69 (lookup "X-Cabal-File-Hash" customFieldsPD)
70 else "")
70 else "")
71− & metaSection .~ ( Nix.nullMeta
71+ & metaSection .~ ( def
72 & Nix.homepage .~ homepage
72 & Nix.homepage .~ homepage
73 & Nix.description .~ synopsis
73 & Nix.description .~ synopsis
74 & Nix.license .~ fromCabalLicense license
74 & Nix.license .~ fromCabalLicense license
@@ -80,7 +80,7 @@ cabal2nix' PackageDescription {..} = normalize $ postProcess $
80
80
81convertBuildInfo :: Cabal.BuildInfo -> Nix.BuildInfo
81convertBuildInfo :: Cabal.BuildInfo -> Nix.BuildInfo
82convertBuildInfo Cabal.BuildInfo {..} = mempty
82convertBuildInfo Cabal.BuildInfo {..} = mempty
83− & haskell .~ Set.fromList [ set ident y undefined | (Dependency (PackageName y) _) <- targetBuildDepends ]
83+ & haskell .~ Set.fromList [ create ident y | (Dependency (PackageName y) _) <- targetBuildDepends ]
84− & system .~ Set.fromList [ set ident y undefined | x <- extraLibs, y <- libNixName x, not (null y) ]
84+ & system .~ Set.fromList [ create ident y | 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) ]
85+ & pkgconfig .~ Set.fromList [ create ident y | 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) ]
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
2
2
3import Cabal2Nix.CorePackages
3import Cabal2Nix.CorePackages
4import Cabal2Nix.Name
4import Cabal2Nix.Name
5−import Control.Lens
5+import Internal.Lens
6import Data.Function
6import Data.Function
7import Data.List
7import Data.List
8import Data.Set ( Set )
8import Data.Set ( Set )
@@ -25,8 +25,8 @@ normalize drv = drv
25
25
26normalizeBuildInfo :: PackageName -> BuildInfo -> BuildInfo
26normalizeBuildInfo :: PackageName -> BuildInfo -> BuildInfo
27normalizeBuildInfo (PackageName pname) bi = bi
27normalizeBuildInfo (PackageName pname) bi = bi
28− & haskell . contains (set ident pname undefined) .~ False
28+ & haskell . contains (create ident pname) .~ False
29− & tool . contains (set ident pname undefined) .~ False
29+ & tool . contains (create ident pname) .~ False
30 & tool %~ normalizeNixBuildTools . Set.filter (\n -> n^.ident `notElem` coreBuildTools)
30 & tool %~ normalizeNixBuildTools . Set.filter (\n -> n^.ident `notElem` coreBuildTools)
31
31
32 {-
32 {-
@@ -68,7 +68,7 @@ normalizeNixBuildTools :: Set Identifier -> Set Identifier
68normalizeNixBuildTools = Set.fromList . concatMap buildToolNixName' . Set.toList
68normalizeNixBuildTools = Set.fromList . concatMap buildToolNixName' . Set.toList
69 where
69 where
70 buildToolNixName' :: Identifier -> [Identifier]
70 buildToolNixName' :: Identifier -> [Identifier]
71− buildToolNixName' n = [ set ident n' undefined | n' <- buildToolNixName (n^.ident) ]
71+ buildToolNixName' n = [ create ident n' | n' <- buildToolNixName (n^.ident) ]
72
72
73-- |Strip any kind of path prefix from maintainer names, filter duplicates, and
73-- |Strip any kind of path prefix from maintainer names, filter duplicates, and
74-- sort the resulting list alphabetically.
74-- sort the resulting list alphabetically.
src/Distribution/Nixpkgs/Haskell.hs+25−25
@@ -4,7 +4,7 @@
4{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
4{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
5
5
6module Distribution.Nixpkgs.Haskell
6module Distribution.Nixpkgs.Haskell
7− ( Derivation, nullDerivation, pkgid, revision, src, isLibrary, isExecutable
7+ ( Derivation, pkgid, revision, src, isLibrary, isExecutable
8 , extraFunctionArgs, libraryDepends, executableDepends, testDepends, configureFlags
8 , extraFunctionArgs, libraryDepends, executableDepends, testDepends, configureFlags
9 , cabalFlags, runHaddock, jailbreak, doCheck, testTarget, hyperlinkSource, enableSplitObjs
9 , cabalFlags, runHaddock, jailbreak, doCheck, testTarget, hyperlinkSource, enableSplitObjs
10 , phaseOverrides, editedCabalFile, metaSection
10 , phaseOverrides, editedCabalFile, metaSection
@@ -14,7 +14,7 @@ module Distribution.Nixpkgs.Haskell
14 where
14 where
15
15
16import Control.DeepSeq.Generics
16import Control.DeepSeq.Generics
17−import Control.Lens
17+import Internal.Lens
18import Data.List
18import Data.List
19import Data.Set ( Set )
19import Data.Set ( Set )
20import qualified Data.Set as Set
20import qualified Data.Set as Set
@@ -73,29 +73,29 @@ data Derivation = MkDerivation
73 }
73 }
74 deriving (Show, Eq, Generic)
74 deriving (Show, Eq, Generic)
75
75
76−nullDerivation :: Derivation
76+instance Default Derivation where
77−nullDerivation = MkDerivation
77+ def = MkDerivation
78− { _pkgid = error "undefined _pkgid"
78+ { _pkgid = error "undefined Derivation.pkgid"
79− , _revision = error "undefined _revision"
79+ , _revision = error "undefined Derivation.revision"
80− , _src = error "undefined _src"
80+ , _src = error "undefined Derivation.src"
81− , _isLibrary = error "undefined _isLibrary"
81+ , _isLibrary = error "undefined Derivation.isLibrary"
82− , _isExecutable = error "undefined _isExecutable"
82+ , _isExecutable = error "undefined Derivation.isExecutable"
83− , _extraFunctionArgs = error "undefined _extraFunctionArgs"
83+ , _extraFunctionArgs = error "undefined Derivation.extraFunctionArgs"
84− , _libraryDepends = error "undefined _libraryDepends"
84+ , _libraryDepends = error "undefined Derivation.libraryDepends"
85− , _executableDepends = error "undefined _executableDepends"
85+ , _executableDepends = error "undefined Derivation.executableDepends"
86− , _testDepends = error "undefined _testDepends"
86+ , _testDepends = error "undefined Derivation.testDepends"
87− , _configureFlags = error "undefined _configureFlags"
87+ , _configureFlags = error "undefined Derivation.configureFlags"
88− , _cabalFlags = error "undefined _cabalFlags"
88+ , _cabalFlags = error "undefined Derivation.cabalFlags"
89− , _runHaddock = error "undefined _runHaddock"
89+ , _runHaddock = error "undefined Derivation.runHaddock"
90− , _jailbreak = error "undefined _jailbreak"
90+ , _jailbreak = error "undefined Derivation.jailbreak"
91− , _doCheck = error "undefined _doCheck"
91+ , _doCheck = error "undefined Derivation.doCheck"
92− , _testTarget = error "undefined _testTarget"
92+ , _testTarget = error "undefined Derivation.testTarget"
93− , _hyperlinkSource = error "undefined _hyperlinkSource"
93+ , _hyperlinkSource = error "undefined Derivation.hyperlinkSource"
94− , _enableSplitObjs = error "undefined _enableSplitObjs"
94+ , _enableSplitObjs = error "undefined Derivation.enableSplitObjs"
95− , _phaseOverrides = error "undefined _phaseOverrides"
95+ , _phaseOverrides = error "undefined Derivation.phaseOverrides"
96− , _editedCabalFile = error "undefined _editedCabalFile"
96+ , _editedCabalFile = error "undefined Derivation.editedCabalFile"
97− , _metaSection = error "undefined _metaSection"
97+ , _metaSection = error "undefined Derivation.metaSection"
98− }
98+ }
99
99
100makeLenses ''Derivation
100makeLenses ''Derivation
101
101
src/Distribution/Nixpkgs/Meta.hs+12−12
@@ -7,13 +7,13 @@
7 -}
7 -}
8
8
9module Distribution.Nixpkgs.Meta
9module Distribution.Nixpkgs.Meta
10− ( Meta, nullMeta
10+ ( Meta
11 , homepage, description, license, platforms, hydraPlatforms, maintainers, broken
11 , homepage, description, license, platforms, hydraPlatforms, maintainers, broken
12 ) where
12 ) where
13
13
14
14
15import Control.DeepSeq.Generics
15import Control.DeepSeq.Generics
16−import Control.Lens
16+import Internal.Lens
17import Distribution.Nixpkgs.License
17import Distribution.Nixpkgs.License
18import Distribution.Nixpkgs.Util.PrettyPrinting
18import Distribution.Nixpkgs.Util.PrettyPrinting
19import GHC.Generics ( Generic )
19import GHC.Generics ( Generic )
@@ -72,13 +72,13 @@ renderPlatformList :: [String] -> Doc
72renderPlatformList plats =
72renderPlatformList plats =
73 nest 2 (fsep $ punctuate (text " ++") $ map text plats) <> semi
73 nest 2 (fsep $ punctuate (text " ++") $ map text plats) <> semi
74
74
75−nullMeta :: Meta
75+instance Default Meta where
76−nullMeta = Meta
76+ def = Meta
77− { _homepage = error "undefined Nix._homepage"
77+ { _homepage = error "undefined Meta.homepage"
78− , _description = error "undefined Nix._description"
78+ , _description = error "undefined Meta.description"
79− , _license = error "undefined Nix._license"
79+ , _license = error "undefined Meta.license"
80− , _platforms = error "undefined Nix._platforms"
80+ , _platforms = error "undefined Meta.platforms"
81− , _hydraPlatforms = error "undefined Nix._hydraPlatforms"
81+ , _hydraPlatforms = error "undefined Meta.hydraPlatforms"
82− , _maintainers = error "undefined Nix._maintainers"
82+ , _maintainers = error "undefined Meta.maintainers"
83− , _broken = error "undefined Nix._broken"
83+ , _broken = error "undefined Meta.broken"
84− }
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 @@
4module Language.Nix.Identifier ( Identifier, ident, quote, needsQuoting ) where
4module Language.Nix.Identifier ( Identifier, ident, quote, needsQuoting ) where
5
5
6import Control.DeepSeq.Generics
6import Control.DeepSeq.Generics
7−import Control.Lens
7+import Internal.Lens
8import Data.Char
8import Data.Char
9import Data.Function ( on )
9import Data.Function ( on )
10import Data.String
10import Data.String
@@ -42,6 +42,8 @@ import GHC.Generics ( Generic )
42newtype Identifier = Identifier String
42newtype Identifier = Identifier String
43 deriving (Show, Eq, IsString, Generic)
43 deriving (Show, Eq, IsString, Generic)
44
44
45+instance Default Identifier where def = Identifier ""
46+
45instance Pretty Identifier where
47instance Pretty Identifier where
46 pPrint i = text (i ^. ident . to quote)
48 pPrint i = text (i ^. ident . to quote)
47
49
src/Language/Nix/Path.hs+3−1
@@ -1,6 +1,6 @@
1module Language.Nix.Path ( Path, path ) where
1module Language.Nix.Path ( Path, path ) where
2
2
3−import Control.Lens
3+import Internal.Lens
4import Distribution.Nixpkgs.Util.PrettyPrinting ( Pretty (..), char, hcat, punctuate )
4import Distribution.Nixpkgs.Util.PrettyPrinting ( Pretty (..), char, hcat, punctuate )
5import Language.Nix.Identifier
5import Language.Nix.Identifier
6
6
@@ -32,6 +32,8 @@ import Language.Nix.Identifier
32newtype Path = Path { _segments :: [Identifier] }
32newtype Path = Path { _segments :: [Identifier] }
33 deriving (Show, Eq, Ord)
33 deriving (Show, Eq, Ord)
34
34
35+instance Default Path where def = Path (error "undefined Nix.Path")
36+
35path :: Lens' Path [Identifier]
37path :: Lens' Path [Identifier]
36path f (Path ids) = mkPath `fmap` f ids
38path f (Path ids) = mkPath `fmap` f ids
37 where
39 where