git.iko.soy/typeable/cabal2nix
on master help sign in
cabal2nix: add the --shell flag to generate output suitable for nix-shell
ad66b29Peter Simons
src/cabal2nix.hs+19−1
@@ -10,6 +10,7 @@ import Distribution.NixOS.Fetch
10import Control.Exception ( bracket )
10import Control.Exception ( bracket )
11import Control.Monad ( when )
11import Control.Monad ( when )
12import Distribution.Text ( disp )
12import Distribution.Text ( disp )
13+import Distribution.NixOS.PrettyPrinting
13import System.Console.GetOpt ( OptDescr(..), ArgDescr(..), ArgOrder(..), usageInfo, getOpt )
14import System.Console.GetOpt ( OptDescr(..), ArgDescr(..), ArgOrder(..), usageInfo, getOpt )
14import System.Environment ( getArgs )
15import System.Environment ( getArgs )
15import System.Exit ( exitFailure, exitSuccess )
16import System.Exit ( exitFailure, exitSuccess )
@@ -27,6 +28,7 @@ data Configuration = Configuration
27 , optRevision :: String
28 , optRevision :: String
28 , optHyperlinkSource :: Bool
29 , optHyperlinkSource :: Bool
29 , optHackageDb :: Maybe FilePath
30 , optHackageDb :: Maybe FilePath
31+ , optNixShellOutput :: Bool
30 }
32 }
31 deriving (Show)
33 deriving (Show)
32
34
@@ -43,6 +45,7 @@ defaultConfiguration = Configuration
43 , optRevision = ""
45 , optRevision = ""
44 , optHyperlinkSource = True
46 , optHyperlinkSource = True
45 , optHackageDb = Nothing
47 , optHackageDb = Nothing
48+ , optNixShellOutput = False
46 }
49 }
47
50
48options :: [OptDescr (Configuration -> Configuration)]
51options :: [OptDescr (Configuration -> Configuration)]
@@ -58,6 +61,7 @@ options =
58 , Option "" ["no-check"] (NoArg (\o -> o { optDoCheck = False })) "don't run regression test suites of this package"
61 , Option "" ["no-check"] (NoArg (\o -> o { optDoCheck = False })) "don't run regression test suites of this package"
59 , Option "" ["rev"] (ReqArg (\x o -> o { optRevision = x }) "REVISION") "revision, only used when fetching from VCS"
62 , Option "" ["rev"] (ReqArg (\x o -> o { optRevision = x }) "REVISION") "revision, only used when fetching from VCS"
60 , Option "" ["no-hyperlink-source"] (NoArg (\o -> o { optHyperlinkSource = False })) "don't add pretty-printed source code to the documentation"
63 , Option "" ["no-hyperlink-source"] (NoArg (\o -> o { optHyperlinkSource = False })) "don't add pretty-printed source code to the documentation"
64+ , Option "" ["shell"] (NoArg (\o -> o { optNixShellOutput = True })) "generate output suitable for nix-shell"
61 ]
65 ]
62
66
63usage :: String
67usage :: String
@@ -107,4 +111,18 @@ main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
107 , extraFunctionArgs = "stdenv" : extraFunctionArgs deriv
111 , extraFunctionArgs = "stdenv" : extraFunctionArgs deriv
108 }
112 }
109
113
114+ deriv'' :: Doc
110− putStr (show (disp (normalize deriv')))
115+ deriv'' = disp (normalize deriv')
116+
117+ shell :: Doc -> Doc
118+ shell expr = vcat
119+ [ text "with (import <nixpkgs> {}).pkgs;"
120+ , text "let pkg = " <> hang (text "haskellngPackages.callPackage") 2 (parens (expr)) <+> braces empty <> semi
121+ , text "in"
122+ , text " pkg.env"
123+ ]
124+
125+ deriv''' | optNixShellOutput cfg = shell deriv''
126+ | otherwise = deriv''
127+
128+ putStrLn (show deriv''')