diff --git a/cabal-install/src/Distribution/Client/Setup.hs b/cabal-install/src/Distribution/Client/Setup.hs index daf42d11739..7bf16edb4c3 100644 --- a/cabal-install/src/Distribution/Client/Setup.hs +++ b/cabal-install/src/Distribution/Client/Setup.hs @@ -357,10 +357,14 @@ globalCommand commands = CommandUI { globalHttpTransport (\v flags -> flags { globalHttpTransport = v }) (reqArgFlag "HttpTransport") - ,option [] ["nix"] - "Nix integration: run commands through nix-shell if a 'shell.nix' file exists" - globalNix (\v flags -> flags { globalNix = v }) - (boolOpt [] []) + ,multiOption "nix" + globalNix (\v flags -> flags { globalNix = v }) + [ + noArg (Flag True) [] ["enable-nix"] + "Enable Nix integration: run commands through nix-shell if a 'shell.nix' file exists", + noArg (Flag False) [] ["disable-nix"] + "Disable Nix integration" + ] ,option [] ["store-dir", "storedir"] "The location of the build store" diff --git a/cabal-install/tests/IntegrationTests2.hs b/cabal-install/tests/IntegrationTests2.hs index 6403f14fbf3..43917db1852 100644 --- a/cabal-install/tests/IntegrationTests2.hs +++ b/cabal-install/tests/IntegrationTests2.hs @@ -49,7 +49,9 @@ import Distribution.Package import Distribution.PackageDescription import Distribution.InstalledPackageInfo (InstalledPackageInfo) import Distribution.Simple.Setup (toFlag, HaddockFlags(..), defaultHaddockFlags) +import Distribution.Client.Setup (globalCommand) import Distribution.Simple.Compiler +import Distribution.Simple.Command import Distribution.System import Distribution.Version import Distribution.ModuleName (ModuleName) @@ -71,6 +73,9 @@ import Test.Tasty.Options import Data.Tagged (Tagged(..)) import qualified Data.ByteString as BS +import Distribution.Client.GlobalFlags (GlobalFlags, globalNix) +import Distribution.Simple.Flag (Flag (Flag, NoFlag)) +import Data.Maybe (fromJust) #if !MIN_VERSION_directory(1,2,7) removePathForcibly :: FilePath -> IO () @@ -139,9 +144,12 @@ tests config = , testCase "program options scope local" (testProgramOptionsLocal config) , testCase "program options scope specific" (testProgramOptionsSpecific config) ] + , testGroup "Flag tests" $ + [ + testCase "Test Nix Flag" testNixFlags + ] ] - testFindProjectRoot :: Assertion testFindProjectRoot = do Left (BadProjectRootExplicitFile file) <- findProjectRoot (Just testdir) @@ -1927,3 +1935,25 @@ tryFewTimes action = go (3 :: Int) where hPutStrLn stderr $ "Trying " ++ show n ++ " after " ++ show e threadDelay 10000 go (n - 1) + +testNixFlags :: Assertion +testNixFlags = do + let gc = globalCommand [] + -- changing from the v1 to v2 build command does not change whether the "--enable-nix" flag + -- sets the globalNix param of the GlobalFlags type to True even though the v2 command doesn't use it + let nixEnabledFlags = getFlags gc . commandParseArgs gc True $ ["--enable-nix", "build"] + let nixDisabledFlags = getFlags gc . commandParseArgs gc True $ ["--disable-nix", "build"] + let nixDefaultFlags = getFlags gc . commandParseArgs gc True $ ["build"] + True @=? isJust nixDefaultFlags + True @=? isJust nixEnabledFlags + True @=? isJust nixDisabledFlags + Just True @=? (fromFlag . globalNix . fromJust $ nixEnabledFlags) + Just False @=? (fromFlag . globalNix . fromJust $ nixDisabledFlags) + Nothing @=? (fromFlag . globalNix . fromJust $ nixDefaultFlags) + where + fromFlag :: Flag Bool -> Maybe Bool + fromFlag (Flag x) = Just x + fromFlag NoFlag = Nothing + getFlags :: CommandUI GlobalFlags -> CommandParse (GlobalFlags -> GlobalFlags, [String]) -> Maybe GlobalFlags + getFlags cui (CommandReadyToGo (mkflags, _)) = Just . mkflags . commandDefaultFlags $ cui + getFlags _ _ = Nothing \ No newline at end of file diff --git a/changelog.d/issue-8036 b/changelog.d/issue-8036 new file mode 100644 index 00000000000..28f6ad18678 --- /dev/null +++ b/changelog.d/issue-8036 @@ -0,0 +1,4 @@ +synopsis: Make enable/disable nix flags easier to read +packages: cabal-install +issues: #8036 +prs: #8054 \ No newline at end of file