Skip to content

Commit 66774d0

Browse files
authored
Add stack-repos and cabal-name executables (input-output-hk#72)
These will be used to generate default cache argument `stackProject` in haskell.nix.
1 parent edad820 commit 66774d0

File tree

14 files changed

+179
-40
lines changed

14 files changed

+179
-40
lines changed

nix-tools/cabal-name/Main.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Main where
2+
3+
import CabalName (doCabalName)
4+
import CabalName.CLI (parseCabalNameArgs)
5+
6+
main :: IO ()
7+
main = parseCabalNameArgs >>= doCabalName

nix-tools/cabal.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
packages: .
1+
packages: .
22

33
-- Needs https://github.com/input-output-hk/iohk-nix/commit/6a8c29117eff36ce975e02e01efc8b25d93fcb90#diff-6fb0c6517b547a8baf082d5d2d604842
44
-- to work with the data-dir issues when building components.

nix-tools/lib/CabalName.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module CabalName
2+
( doCabalName
3+
) where
4+
5+
import Stack2nix.Project (findCabalFiles)
6+
import Cabal2Nix (cabalFilePkgName)
7+
8+
import CabalName.CLI (Args(..))
9+
10+
doCabalName :: Args -> IO ()
11+
doCabalName args =
12+
findCabalFiles (argHpackUse args) (argPackageDir args)
13+
>>= mapM_ (putStr . cabalFilePkgName) . take 1

nix-tools/lib/CabalName/CLI.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module CabalName.CLI
2+
( Args(..)
3+
, HpackUse(..)
4+
, parseCabalNameArgs
5+
) where
6+
7+
import Options.Applicative hiding (option)
8+
import Data.Semigroup ((<>))
9+
import Stack2nix.CLI (HpackUse(..))
10+
11+
--------------------------------------------------------------------------------
12+
-- CLI Arguments
13+
data Args = Args
14+
{ argPackageDir :: FilePath
15+
, argHpackUse :: HpackUse
16+
} deriving Show
17+
18+
-- Argument Parser
19+
args :: Parser Args
20+
args = Args
21+
<$> argument str ( metavar "DIR" <> help "Directory containing the package source" )
22+
<*> flag UsePackageYamlFirst IgnorePackageYaml (long "ignore-package-yaml" <> help "disable hpack run and use only cabal disregarding package.yaml existence")
23+
24+
parseCabalNameArgs :: IO Args
25+
parseCabalNameArgs = execParser opts
26+
where opts = info (args <**> helper)
27+
( fullDesc
28+
<> progDesc "Find the name of the packeage in the specified directory"
29+
<> header "cabal-name - extract the name of a package" )

nix-tools/stack2nix/Stack2nix.hs renamed to nix-tools/lib/Stack2nix.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ packages2nix args pkgs =
152152
writeDoc nixFile =<<
153153
prettyNix <$> cabal2nix True (argDetailLevel args) src cabalFile
154154
return $ fromString pkg $= mkPath False nix
155-
(DVCS (Git url rev) subdirs) ->
155+
(DVCS (Git url rev) _ subdirs) ->
156156
fmap concat . forM subdirs $ \subdir ->
157157
do cacheHits <- liftIO $ cacheHits (argCacheFile args) url rev subdir
158158
case cacheHits of
File renamed without changes.

nix-tools/stack2nix/Stack2nix/Stack.hs renamed to nix-tools/lib/Stack2nix/Stack.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ instance Text CabalRev where
112112
data Dependency
113113
= PkgIndex PackageIdentifier (Maybe (Either Sha256 CabalRev)) -- ^ overridden package in the stackage index
114114
| LocalPath String -- ^ Some local package (potentially overriding a package in the index as well)
115-
| DVCS Location [FilePath] -- ^ One or more packages fetched from git or similar.
115+
| DVCS Location (Maybe Sha256) [FilePath] -- ^ One or more packages fetched from git or similar.
116116
-- TODO: Support archives.
117117
-- | Archive ...
118118
deriving (Show)
@@ -213,6 +213,7 @@ instance FromJSON Dependency where
213213
return . LocalPath . dropTrailingSlash . T.unpack
214214
parseDVCS = withObject "DVCS" $ \o -> DVCS
215215
<$> (o .: "location" <|> parseJSON p)
216+
<*> o .:? "nix-sha256" .!= Nothing
216217
<*> o .:? "subdirs" .!= ["."]
217218

218219
-- drop trailing slashes. Nix doesn't like them much;

nix-tools/lib/StackRepos.hs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{-# LANGUAGE LambdaCase #-}
2+
{-# LANGUAGE NamedFieldPuns #-}
3+
{-# LANGUAGE OverloadedStrings #-}
4+
{-# LANGUAGE DeriveGeneric #-}
5+
6+
module StackRepos
7+
( doStackRepos
8+
, stack2nix
9+
) where
10+
11+
import Data.Aeson (ToJSON(..))
12+
import Data.Aeson.Encode.Pretty (encodePretty)
13+
import qualified Data.ByteString.Lazy as LBS (writeFile)
14+
import Data.Yaml (decodeFileEither)
15+
16+
import GHC.Generics (Generic)
17+
18+
import Stack2nix.Stack (Stack(..), Dependency(..), Location(..))
19+
import Stack2nix.External.Resolve
20+
21+
import StackRepos.CLI (Args(..))
22+
23+
data SourceRepos = SourceRepos
24+
{ url :: String
25+
, rev :: String
26+
, sha256 :: Maybe String
27+
, subdirs :: [FilePath]
28+
} deriving (Show, Eq, Ord, Generic)
29+
30+
instance ToJSON SourceRepos
31+
32+
doStackRepos :: Args -> IO ()
33+
doStackRepos args = do
34+
evalue <- decodeFileEither (argStackYaml args)
35+
case evalue of
36+
Left e -> error (show e)
37+
Right value -> stack2nix args
38+
=<< resolveSnapshot (argStackYaml args) value
39+
40+
stack2nix :: Args -> Stack -> IO ()
41+
stack2nix args (Stack _ _ pkgs _ _) =
42+
LBS.writeFile "repos.json" $ encodePretty (
43+
pkgs >>= (\case
44+
(DVCS (Git url rev) sha256 subdirs) ->
45+
[SourceRepos { url, rev, sha256, subdirs }]
46+
_ -> []))

nix-tools/lib/StackRepos/CLI.hs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module StackRepos.CLI
2+
( Args(..)
3+
, HpackUse(..)
4+
, parseStackReposArgs
5+
) where
6+
7+
import Options.Applicative hiding (option)
8+
import Data.Semigroup ((<>))
9+
import Stack2nix.CLI (HpackUse(..))
10+
11+
--------------------------------------------------------------------------------
12+
-- CLI Arguments
13+
newtype Args = Args
14+
{ argStackYaml :: FilePath
15+
} deriving Show
16+
17+
-- Argument Parser
18+
args :: Parser Args
19+
args = Args
20+
<$> strOption ( long "stack-yaml" <> value "stack.yaml" <> showDefault <> metavar "FILE" <> help "Override project stack.yaml" )
21+
22+
parseStackReposArgs :: IO Args
23+
parseStackReposArgs = execParser opts
24+
where opts = info (args <**> helper)
25+
( fullDesc
26+
<> progDesc "Collect information about remote source packages used by Stack"
27+
<> header "stack-repos - extract the details of remote repos" )

nix-tools/nix-tools.cabal

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,42 @@ library
1717
exposed-modules: Cabal2Nix
1818
, Cabal2Nix.Util
1919
, Cabal2Nix.Plan
20+
, CabalName
21+
, CabalName.CLI
2022
, Distribution.Nixpkgs.Fetch
23+
, StackRepos
24+
, StackRepos.CLI
25+
, Stack2nix
26+
, Stack2nix.Cache
27+
, Stack2nix.CLI
28+
, Stack2nix.External.Resolve
29+
, Stack2nix.Project
30+
, Stack2nix.Stack
2131
build-depends: base >=4 && <4.13
22-
, hnix
23-
, aeson
24-
, unordered-containers
25-
, process
26-
, deepseq
27-
, transformers
28-
, data-fix
2932
, Cabal >= 2.4
30-
, text
31-
, filepath
32-
, directory
33+
, aeson
34+
, aeson-pretty
35+
, base16-bytestring
3336
, bytestring
3437
, cryptohash-sha256
35-
, base16-bytestring
38+
, data-fix
39+
, deepseq
40+
, directory
41+
, extra
42+
, filepath
43+
, hnix
3644
, hpack
45+
, http-client
46+
, http-client-tls
47+
, http-types
48+
, optparse-applicative
49+
, prettyprinter
50+
, process
51+
, text
52+
, transformers
53+
, unordered-containers
54+
, yaml
55+
3756
hs-source-dirs: lib
3857
default-language: Haskell2010
3958

@@ -139,34 +158,8 @@ executable lts-to-nix
139158
executable stack-to-nix
140159
ghc-options: -Wall
141160
main-is: Main.hs
142-
other-modules: Stack2nix
143-
, Stack2nix.Cache
144-
, Stack2nix.CLI
145-
, Stack2nix.External.Resolve
146-
, Stack2nix.Project
147-
, Stack2nix.Stack
148161
build-depends: base >=4 && <4.13
149162
, nix-tools
150-
, transformers
151-
, hnix
152-
, yaml
153-
, aeson
154-
, microlens
155-
, microlens-aeson
156-
, text
157-
, Cabal
158-
, vector
159-
, prettyprinter
160-
, directory
161-
, filepath
162-
, extra
163-
, hpack
164-
, bytestring
165-
, optparse-applicative
166-
, http-client-tls
167-
, http-client
168-
, http-types
169-
, unordered-containers
170163
hs-source-dirs: stack2nix
171164
default-language: Haskell2010
172165

@@ -182,3 +175,19 @@ executable truncate-index
182175
, time
183176
hs-source-dirs: truncate-index
184177
default-language: Haskell2010
178+
179+
executable stack-repos
180+
ghc-options: -Wall
181+
main-is: Main.hs
182+
build-depends: base >=4 && <4.13
183+
, nix-tools
184+
hs-source-dirs: stack-repos
185+
default-language: Haskell2010
186+
187+
executable cabal-name
188+
ghc-options: -Wall
189+
main-is: Main.hs
190+
build-depends: base >=4 && <4.13
191+
, nix-tools
192+
hs-source-dirs: cabal-name
193+
default-language: Haskell2010

nix-tools/stack-repos/Main.hs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Main where
2+
3+
import StackRepos (doStackRepos)
4+
import StackRepos.CLI (parseStackReposArgs)
5+
6+
main :: IO ()
7+
main = parseStackReposArgs >>= doStackRepos

0 commit comments

Comments
 (0)