git.iko.soy/typeable/cabal2nix
on master help sign in
Merge pull request #371 from kquick/output_fetchsub

Output fetchSubmodules setting in src directive

commit1109639366c36ca42b074291622d1903847102dc authorPeter Simons <simons@cryp.to> dateFri 17 Aug 2018, 19:01 (8 years ago) parentb8dc5b1066718f
5 files changed+45−22
hackage2nix/Main.hs+1−1 src/Distribution/Nixpkgs/Fetch.hs+38−4 src/Distribution/Nixpkgs/Haskell/Derivation.hs+1−13 src/Distribution/Nixpkgs/Haskell/PackageSourceSpec.hs+4−4 test/Main.hs+1
hackage2nix/Main.hs+1−1
@@ -151,7 +151,7 @@ main = do
151151
152152 drv :: Derivation
153153 drv = fromGenericPackageDescription haskellResolver nixpkgsResolver targetPlatform (compilerInfo config) flagAssignment [] descr
154− & src .~ DerivationSource "url" ("mirror://hackage/" ++ display pkgId ++ ".tar.gz") "" tarballSHA256
154+ & src .~ urlDerivationSource ("mirror://hackage/" ++ display pkgId ++ ".tar.gz") tarballSHA256
155155 & editedCabalFile .~ cabalSHA256
156156 & metaSection.hydraPlatforms %~ (`Set.difference` Map.findWithDefault Set.empty name (dontDistributePackages config))
157157 & metaSection.maintainers .~ Map.findWithDefault Set.empty name globalPackageMaintainers
src/Distribution/Nixpkgs/Fetch.hs+38−4
@@ -6,7 +6,7 @@
66module Distribution.Nixpkgs.Fetch
77 ( Source(..)
88 , Hash(..)
9− , DerivationSource(..), fromDerivationSource
9+ , DerivationSource(..), fromDerivationSource, urlDerivationSource
1010 , fetch
1111 , fetchWith
1212 ) where
@@ -18,7 +18,10 @@ import Control.Monad.IO.Class
1818import Control.Monad.Trans.Maybe
1919import Data.Aeson
2020import qualified Data.ByteString.Lazy.Char8 as BS
21+import qualified Data.List as L
22+import Data.Maybe
2123import GHC.Generics ( Generic )
24+import Language.Nix.PrettyPrinting as PP
2225import System.Directory
2326import System.Environment
2427import System.Exit
@@ -56,6 +59,7 @@ data DerivationSource = DerivationSource
5659 , derivUrl :: String -- ^ URL to fetch from.
5760 , derivRevision :: String -- ^ Revision to use. Leave empty if the fetcher doesn't support revisions.
5861 , derivHash :: String -- ^ The hash of the source.
62+ , derivSubmodule :: Maybe Bool -- ^ The fetchSubmodule setting (if any)
5963 }
6064 deriving (Show, Eq, Ord, Generic)
6165
@@ -66,8 +70,30 @@ instance FromJSON DerivationSource where
6670 <$> o .: "url"
6771 <*> o .: "rev"
6872 <*> o .: "sha256"
73+ <*> o .: "fetchSubmodules"
6974 parseJSON _ = error "invalid DerivationSource"
7075
76+instance PP.Pretty DerivationSource where
77+ pPrint DerivationSource {..} =
78+ let isHackagePackage = "mirror://hackage/" `L.isPrefixOf` derivUrl
79+ fetched = derivKind /= ""
80+ in if isHackagePackage then attr "sha256" $ string derivHash
81+ else if not fetched then attr "src" $ text derivUrl
82+ else vcat
83+ [ text "src" <+> equals <+> text ("fetch" ++ derivKind) <+> lbrace
84+ , nest 2 $ vcat
85+ [ attr "url" $ string derivUrl
86+ , attr "sha256" $ string derivHash
87+ , if derivRevision /= "" then attr "rev" (string derivRevision) else PP.empty
88+ , boolattr "fetchSubmodules" (isJust derivSubmodule) (fromJust derivSubmodule)
89+ ]
90+ , rbrace PP.<> semi
91+ ]
92+
93+
94+urlDerivationSource :: String -> String -> DerivationSource
95+urlDerivationSource url hash = DerivationSource "url" url "" hash Nothing
96+
7197fromDerivationSource :: DerivationSource -> Source
7298fromDerivationSource DerivationSource{..} = Source derivUrl derivRevision (Certain derivHash) "."
7399
@@ -102,17 +128,19 @@ fetch optSubModules f = runMaybeT . fetchers where
102128 guard $ existsDir || existsFile
103129 let path' | '/' `elem` path = path
104130 | otherwise = "./" ++ path
105− process (DerivationSource "" path' "" "", path') <|> localArchive path'
131+ process (localDerivationSource path', path') <|> localArchive path'
106132
107133 localArchive :: FilePath -> MaybeT IO (DerivationSource, a)
108134 localArchive path = do
109135 absolutePath <- liftIO $ canonicalizePath path
110136 unpacked <- snd <$> fetchWith (False, "url", ["--unpack"]) (Source ("file://" ++ absolutePath) "" UnknownHash ".")
111− process (DerivationSource "" absolutePath "" "", unpacked)
137+ process (localDerivationSource absolutePath, unpacked)
112138
113139 process :: (DerivationSource, FilePath) -> MaybeT IO (DerivationSource, a)
114140 process (derivSource, file) = (,) derivSource <$> f file
115141
142+ localDerivationSource p = DerivationSource "" p "" "" Nothing
143+
116144-- | Like 'fetch', but allows to specify which script to use.
117145fetchWith :: (Bool, String, [String]) -> Source -> MaybeT IO (DerivationSource, FilePath)
118146fetchWith (supportsRev, kind, addArgs) source = do
@@ -137,7 +165,13 @@ fetchWith (supportsRev, kind, addArgs) source = do
137165 buf' = BS.unlines (reverse ls)
138166 case length ls of
139167 0 -> return Nothing
140− 1 -> return (Just (DerivationSource kind (sourceUrl source) "" (BS.unpack (head ls)) , sourceUrl source))
168+ 1 -> return (Just (DerivationSource { derivKind = kind
169+ , derivUrl = sourceUrl source
170+ , derivRevision = ""
171+ , derivHash = BS.unpack (head ls)
172+ , derivSubmodule = Nothing
173+ }
174+ , sourceUrl source))
141175 _ -> case eitherDecode buf' of
142176 Left err -> error ("invalid JSON: " ++ err ++ " in " ++ show buf')
143177 Right ds -> return (Just (ds { derivKind = kind }, BS.unpack l))
src/Distribution/Nixpkgs/Haskell/Derivation.hs+1−13
@@ -112,7 +112,7 @@ instance Pretty Derivation where
112112 , nest 2 $ vcat
113113 [ attr "pname" $ doubleQuotes $ disp (packageName _pkgid)
114114 , attr "version" $ doubleQuotes $ disp (packageVersion _pkgid)
115− , sourceAttr _src
115+ , pPrint _src
116116 , onlyIf (_subpath /= ".") $ attr "postUnpack" postUnpack
117117 , onlyIf (_revision > 0) $ attr "revision" $ doubleQuotes $ int _revision
118118 , onlyIf (not (null _editedCabalFile) && _revision > 0) $ attr "editedCabalFile" $ string _editedCabalFile
@@ -148,17 +148,5 @@ instance Pretty Derivation where
148148 renderedFlags = [ text "-f" <> (if enable then empty else char '-') <> text (unFlagName f) | (f, enable) <- unFlagAssignment _cabalFlags ]
149149 ++ map text (toAscList _configureFlags)
150150 isHackagePackage = "mirror://hackage/" `isPrefixOf` derivUrl _src
151− sourceAttr DerivationSource {..}
152− | isHackagePackage = attr "sha256" $ string derivHash
153− | derivKind /= "" = vcat
154− [ text "src" <+> equals <+> text ("fetch" ++ derivKind) <+> lbrace
155− , nest 2 $ vcat
156− [ attr "url" $ string derivUrl
157− , attr "sha256" $ string derivHash
158− , if derivRevision /= "" then attr "rev" (string derivRevision) else empty
159− ]
160− , rbrace <> semi
161− ]
162− | otherwise = attr "src" $ text derivUrl
163151
164152 postUnpack = string $ "sourceRoot+=/" ++ _subpath ++ "; echo source root reset to $sourceRoot"
src/Distribution/Nixpkgs/Haskell/PackageSourceSpec.hs+4−4
@@ -98,7 +98,7 @@ fromDB hackageDBIO pkg = do
9898 vd <- maybe unknownPackageError return (DB.lookup name hackageDB >>= lookupVersion)
9999 let ds = case DB.tarballSha256 vd of
100100 Nothing -> Nothing
101− Just hash -> Just (DerivationSource "url" url "" hash)
101+ Just hash -> Just (urlDerivationSource url hash)
102102 return (ds, setCabalFileHash (DB.cabalFileSha256 vd) (DB.cabalFile vd))
103103 where
104104 pkgId :: Cabal.PackageIdentifier
@@ -144,20 +144,20 @@ sourceFromHackage optHash pkgId cabalDir = do
144144 -- Use the cached hash (either from cache file or given on cmdline via sha256 opt)
145145 -- if available, otherwise download from hackage to compute hash.
146146 case cachedHash of
147− Guess hash -> return $ DerivationSource "url" url "" hash
147+ Guess hash -> return $ urlDerivationSource url hash
148148 Certain hash ->
149149 -- We need to force the hash here. If we didn't do this, then when reading the
150150 -- hash from the cache file, the cache file will still be open for reading
151151 -- (because lazy io) when writeFile opens the file again for writing. By forcing
152152 -- the hash here, we ensure that the file is closed before opening it again.
153153 seq (length hash) $
154− DerivationSource "url" url "" hash <$ writeFile cacheFile hash
154+ urlDerivationSource url hash <$ writeFile cacheFile hash
155155 UnknownHash -> do
156156 maybeHash <- runMaybeT (derivHash . fst <$> fetchWith (False, "url", []) (Source url "" UnknownHash cabalDir))
157157 case maybeHash of
158158 Just hash ->
159159 seq (length hash) $
160− DerivationSource "url" url "" hash <$ writeFile cacheFile hash
160+ urlDerivationSource url hash <$ writeFile cacheFile hash
161161 Nothing -> do
162162 hPutStr stderr $ unlines
163163 [ "*** cannot compute hash. (Not a hackage project?)"
test/Main.hs+1
@@ -57,6 +57,7 @@ testLibrary cabalFile = do
5757 , derivUrl = "mirror://hackage/foo.tar.gz"
5858 , derivRevision = ""
5959 , derivHash = "deadbeef"
60+ , derivSubmodule = Nothing
6061 }
6162 & extraFunctionArgs %~ Set.union (Set.singleton "inherit stdenv")
6263 goldenVsFileDiff