Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: NixOS/cabal2nix
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: b71928d4ec5f
Choose a base ref
...
head repository: NixOS/cabal2nix
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 9111bb174350
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Mar 8, 2019

  1. Copy the full SHA
    487263f View commit details
  2. Drop all expectations towards the shell environment.

    cabal2nix works properly in an empty environment, e.g. when run with "env -i".
    Most importantly, this means we're no longer dependent on the system's locale.
    peti committed Mar 8, 2019
    Copy the full SHA
    9111bb1 View commit details
Showing with 14 additions and 25 deletions.
  1. +11 −20 src/Cabal2nix.hs
  2. +3 −5 test/Main.hs
31 changes: 11 additions & 20 deletions src/Cabal2nix.hs
Original file line number Diff line number Diff line change
@@ -3,18 +3,15 @@
{-# LANGUAGE RecordWildCards #-}

module Cabal2nix
( main, cabal2nix, cabal2nix', parseArgs
( main, cabal2nix, parseArgs
, Options(..)
)
where

import Control.Exception ( bracket )
import Control.Lens
import Control.Monad
import Data.List
import Data.List.Split
import Data.Maybe ( fromMaybe, listToMaybe )
import Data.Monoid ( (<>) )
import qualified Distribution.Compat.ReadP as P
import Distribution.Compiler
import Distribution.Nixpkgs.Haskell
@@ -27,11 +24,11 @@ import Distribution.Simple.Utils ( lowercase )
import Distribution.System
import Distribution.Text
import Distribution.Verbosity
import GHC.IO.Encoding ( setLocaleEncoding, utf8 )
import Language.Nix
import Options.Applicative
import Paths_cabal2nix ( version )
import System.Environment ( getArgs )
import System.IO ( hFlush, stdout, stderr )
import Text.PrettyPrint.HughesPJClass ( prettyShow )

data Options = Options
@@ -95,11 +92,9 @@ parsePlatform = parsePlatformParts . splitOn "-"

parsePlatformParts :: [String] -> Maybe Platform
parsePlatformParts = \case
[arch, os] ->
Just $ Platform (parseArch arch) (parseOS os)
(arch : _ : osParts) ->
Just $ Platform (parseArch arch) $ parseOS $ intercalate "-" osParts
_ -> Nothing
[arch, os] -> Just $ Platform (parseArch arch) (parseOS os)
(arch : _ : osParts) -> Just $ Platform (parseArch arch) $ parseOS $ intercalate "-" osParts
_ -> Nothing

pinfo :: ParserInfo Options
pinfo = info
@@ -112,11 +107,13 @@ pinfo = info
)

main :: IO ()
main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() ->
cabal2nix =<< getArgs
main = setLocaleEncoding utf8 >> getArgs >>= parseArgs >>= cabal2nix >>= putStrLn . prettyShow

cabal2nix' :: Options -> IO Derivation
cabal2nix' opts = processPackage opts <$> readGenericPackageDescription normal (optUrl opts)
parseArgs :: [String] -> IO Options
parseArgs = handleParseResult . execParserPure defaultPrefs pinfo

cabal2nix :: Options -> IO Derivation
cabal2nix opts = processPackage opts <$> readGenericPackageDescription silent (optUrl opts)

processPackage :: Options -> GenericPackageDescription -> Derivation
processPackage Options{..} gpd = deriv
@@ -135,12 +132,6 @@ processPackage Options{..} gpd = deriv
& sha256 .~ fromMaybe "" optSha256
& extraFunctionArgs . contains "inherit stdenv" .~ True

cabal2nix :: [String] -> IO ()
cabal2nix = parseArgs >=> cabal2nix' >=> putStrLn . prettyShow

parseArgs :: [String] -> IO Options
parseArgs = handleParseResult . execParserPure defaultPrefs pinfo

-- Utils

readFlagList :: [String] -> FlagAssignment
8 changes: 3 additions & 5 deletions test/Main.hs
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ import Distribution.PackageDescription.Parsec
import Distribution.System
import Distribution.Verbosity
import Distribution.Version
import GHC.IO.Encoding ( setLocaleEncoding, utf8 )
import Language.Nix
import System.Directory
import System.FilePath
@@ -26,10 +27,7 @@ import Text.PrettyPrint.HughesPJClass

main :: IO ()
main = do
-- TODO: Run this test with all kinds of setLocaleEncoding values to ensure we don't
-- depend on the system environment: https://github.com/NixOS/cabal2nix/issues/333.
--
-- TODO: Run this test without $HOME defined to ensure that we don't need that variable.
setLocaleEncoding utf8
cabal2nix <- findExecutable "cabal2nix" >>= maybe (fail "cannot find 'cabal2nix' in $PATH") return
testCases <- listDirectoryFilesBySuffix ".cabal" "test/golden-test-cases"
defaultMain $ testGroup "regression-tests"
@@ -86,7 +84,7 @@ testExecutable exe cabalFile = do
(\ref new -> ["diff", "-u", ref, new])
goldenFile
nixFile
(callCommand (unwords ["env LANG=en_US.UTF-8", exe, "--sha256=deadbeef", "--system=x86_64-linux", "--compiler=ghc-8.2", "--", cabalFile, ">"++nixFile]))
(callCommand (unwords ["env -i", exe, "--sha256=deadbeef", "--system=x86_64-linux", "--compiler=ghc-8.2", "--", cabalFile, ">"++nixFile]))

-------------------------------------------------------------------------------
-- * Helper functions