@@ -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))