{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

-- | Nix types.

module Stack.Types.Nix where

import Pantry.Internal.AesonExtended
import Stack.Prelude
import Generics.Deriving.Monoid (mappenddefault, memptydefault)

-- | Nix configuration. Parameterize by resolver type to avoid cyclic
-- dependency.
data NixOpts = NixOpts
  {NixOpts -> Bool
nixEnable   :: !Bool
  ,NixOpts -> Bool
nixPureShell :: !Bool
  ,NixOpts -> [Text]
nixPackages :: ![Text]
    -- ^ The system packages to be installed in the environment before it runs
  ,NixOpts -> Maybe FilePath
nixInitFile :: !(Maybe FilePath)
    -- ^ The path of a file containing preconfiguration of the environment (e.g shell.nix)
  ,NixOpts -> [Text]
nixShellOptions :: ![Text]
    -- ^ Options to be given to the nix-shell command line
  ,NixOpts -> Bool
nixAddGCRoots :: !Bool
    -- ^ Should we register gc roots so running nix-collect-garbage doesn't remove nix dependencies
  }
  deriving (Int -> NixOpts -> ShowS
[NixOpts] -> ShowS
NixOpts -> FilePath
(Int -> NixOpts -> ShowS)
-> (NixOpts -> FilePath) -> ([NixOpts] -> ShowS) -> Show NixOpts
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [NixOpts] -> ShowS
$cshowList :: [NixOpts] -> ShowS
show :: NixOpts -> FilePath
$cshow :: NixOpts -> FilePath
showsPrec :: Int -> NixOpts -> ShowS
$cshowsPrec :: Int -> NixOpts -> ShowS
Show)

-- | An uninterpreted representation of nix options.
-- Configurations may be "cascaded" using mappend (left-biased).
data NixOptsMonoid = NixOptsMonoid
  {NixOptsMonoid -> First Bool
nixMonoidEnable :: !(First Bool)
    -- ^ Is using nix-shell enabled?
  ,NixOptsMonoid -> First Bool
nixMonoidPureShell :: !(First Bool)
    -- ^ Should the nix-shell be pure
  ,NixOptsMonoid -> First [Text]
nixMonoidPackages :: !(First [Text])
    -- ^ System packages to use (given to nix-shell)
  ,NixOptsMonoid -> First FilePath
nixMonoidInitFile :: !(First FilePath)
    -- ^ The path of a file containing preconfiguration of the environment (e.g shell.nix)
  ,NixOptsMonoid -> First [Text]
nixMonoidShellOptions :: !(First [Text])
    -- ^ Options to be given to the nix-shell command line
  ,NixOptsMonoid -> First [Text]
nixMonoidPath :: !(First [Text])
    -- ^ Override parts of NIX_PATH (notably 'nixpkgs')
  ,NixOptsMonoid -> FirstFalse
nixMonoidAddGCRoots :: !FirstFalse
    -- ^ Should we register gc roots so running nix-collect-garbage doesn't remove nix dependencies
  }
  deriving (NixOptsMonoid -> NixOptsMonoid -> Bool
(NixOptsMonoid -> NixOptsMonoid -> Bool)
-> (NixOptsMonoid -> NixOptsMonoid -> Bool) -> Eq NixOptsMonoid
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NixOptsMonoid -> NixOptsMonoid -> Bool
$c/= :: NixOptsMonoid -> NixOptsMonoid -> Bool
== :: NixOptsMonoid -> NixOptsMonoid -> Bool
$c== :: NixOptsMonoid -> NixOptsMonoid -> Bool
Eq, Int -> NixOptsMonoid -> ShowS
[NixOptsMonoid] -> ShowS
NixOptsMonoid -> FilePath
(Int -> NixOptsMonoid -> ShowS)
-> (NixOptsMonoid -> FilePath)
-> ([NixOptsMonoid] -> ShowS)
-> Show NixOptsMonoid
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [NixOptsMonoid] -> ShowS
$cshowList :: [NixOptsMonoid] -> ShowS
show :: NixOptsMonoid -> FilePath
$cshow :: NixOptsMonoid -> FilePath
showsPrec :: Int -> NixOptsMonoid -> ShowS
$cshowsPrec :: Int -> NixOptsMonoid -> ShowS
Show, (forall x. NixOptsMonoid -> Rep NixOptsMonoid x)
-> (forall x. Rep NixOptsMonoid x -> NixOptsMonoid)
-> Generic NixOptsMonoid
forall x. Rep NixOptsMonoid x -> NixOptsMonoid
forall x. NixOptsMonoid -> Rep NixOptsMonoid x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep NixOptsMonoid x -> NixOptsMonoid
$cfrom :: forall x. NixOptsMonoid -> Rep NixOptsMonoid x
Generic)

-- | Decode uninterpreted nix options from JSON/YAML.
instance FromJSON (WithJSONWarnings NixOptsMonoid) where
  parseJSON :: Value -> Parser (WithJSONWarnings NixOptsMonoid)
parseJSON = FilePath
-> (Object -> WarningParser NixOptsMonoid)
-> Value
-> Parser (WithJSONWarnings NixOptsMonoid)
forall a.
FilePath
-> (Object -> WarningParser a)
-> Value
-> Parser (WithJSONWarnings a)
withObjectWarnings FilePath
"NixOptsMonoid"
    (\Object
o -> do First Bool
nixMonoidEnable        <- Maybe Bool -> First Bool
forall a. Maybe a -> First a
First (Maybe Bool -> First Bool)
-> WriterT WarningParserMonoid Parser (Maybe Bool)
-> WriterT WarningParserMonoid Parser (First Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Text -> WriterT WarningParserMonoid Parser (Maybe Bool)
forall a. FromJSON a => Object -> Text -> WarningParser (Maybe a)
..:? Text
nixEnableArgName
              First Bool
nixMonoidPureShell     <- Maybe Bool -> First Bool
forall a. Maybe a -> First a
First (Maybe Bool -> First Bool)
-> WriterT WarningParserMonoid Parser (Maybe Bool)
-> WriterT WarningParserMonoid Parser (First Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Text -> WriterT WarningParserMonoid Parser (Maybe Bool)
forall a. FromJSON a => Object -> Text -> WarningParser (Maybe a)
..:? Text
nixPureShellArgName
              First [Text]
nixMonoidPackages      <- Maybe [Text] -> First [Text]
forall a. Maybe a -> First a
First (Maybe [Text] -> First [Text])
-> WriterT WarningParserMonoid Parser (Maybe [Text])
-> WriterT WarningParserMonoid Parser (First [Text])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Text -> WriterT WarningParserMonoid Parser (Maybe [Text])
forall a. FromJSON a => Object -> Text -> WarningParser (Maybe a)
..:? Text
nixPackagesArgName
              First FilePath
nixMonoidInitFile      <- Maybe FilePath -> First FilePath
forall a. Maybe a -> First a
First (Maybe FilePath -> First FilePath)
-> WriterT WarningParserMonoid Parser (Maybe FilePath)
-> WriterT WarningParserMonoid Parser (First FilePath)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object
-> Text -> WriterT WarningParserMonoid Parser (Maybe FilePath)
forall a. FromJSON a => Object -> Text -> WarningParser (Maybe a)
..:? Text
nixInitFileArgName
              First [Text]
nixMonoidShellOptions  <- Maybe [Text] -> First [Text]
forall a. Maybe a -> First a
First (Maybe [Text] -> First [Text])
-> WriterT WarningParserMonoid Parser (Maybe [Text])
-> WriterT WarningParserMonoid Parser (First [Text])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Text -> WriterT WarningParserMonoid Parser (Maybe [Text])
forall a. FromJSON a => Object -> Text -> WarningParser (Maybe a)
..:? Text
nixShellOptsArgName
              First [Text]
nixMonoidPath          <- Maybe [Text] -> First [Text]
forall a. Maybe a -> First a
First (Maybe [Text] -> First [Text])
-> WriterT WarningParserMonoid Parser (Maybe [Text])
-> WriterT WarningParserMonoid Parser (First [Text])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Text -> WriterT WarningParserMonoid Parser (Maybe [Text])
forall a. FromJSON a => Object -> Text -> WarningParser (Maybe a)
..:? Text
nixPathArgName
              FirstFalse
nixMonoidAddGCRoots    <- Maybe Bool -> FirstFalse
FirstFalse (Maybe Bool -> FirstFalse)
-> WriterT WarningParserMonoid Parser (Maybe Bool)
-> WriterT WarningParserMonoid Parser FirstFalse
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Text -> WriterT WarningParserMonoid Parser (Maybe Bool)
forall a. FromJSON a => Object -> Text -> WarningParser (Maybe a)
..:? Text
nixAddGCRootsArgName
              NixOptsMonoid -> WarningParser NixOptsMonoid
forall (m :: * -> *) a. Monad m => a -> m a
return NixOptsMonoid :: First Bool
-> First Bool
-> First [Text]
-> First FilePath
-> First [Text]
-> First [Text]
-> FirstFalse
-> NixOptsMonoid
NixOptsMonoid{First Bool
First FilePath
First [Text]
FirstFalse
nixMonoidAddGCRoots :: FirstFalse
nixMonoidPath :: First [Text]
nixMonoidShellOptions :: First [Text]
nixMonoidInitFile :: First FilePath
nixMonoidPackages :: First [Text]
nixMonoidPureShell :: First Bool
nixMonoidEnable :: First Bool
nixMonoidAddGCRoots :: FirstFalse
nixMonoidPath :: First [Text]
nixMonoidShellOptions :: First [Text]
nixMonoidInitFile :: First FilePath
nixMonoidPackages :: First [Text]
nixMonoidPureShell :: First Bool
nixMonoidEnable :: First Bool
..})

-- | Left-biased combine Nix options
instance Semigroup NixOptsMonoid where
  <> :: NixOptsMonoid -> NixOptsMonoid -> NixOptsMonoid
(<>) = NixOptsMonoid -> NixOptsMonoid -> NixOptsMonoid
forall a. (Generic a, Monoid' (Rep a)) => a -> a -> a
mappenddefault

-- | Left-biased combine Nix options
instance Monoid NixOptsMonoid where
  mempty :: NixOptsMonoid
mempty = NixOptsMonoid
forall a. (Generic a, Monoid' (Rep a)) => a
memptydefault
  mappend :: NixOptsMonoid -> NixOptsMonoid -> NixOptsMonoid
mappend = NixOptsMonoid -> NixOptsMonoid -> NixOptsMonoid
forall a. Semigroup a => a -> a -> a
(<>)

-- | Nix enable argument name.
nixEnableArgName :: Text
nixEnableArgName :: Text
nixEnableArgName = Text
"enable"

-- | Nix run in pure shell argument name.
nixPureShellArgName :: Text
nixPureShellArgName :: Text
nixPureShellArgName = Text
"pure"

-- | Nix packages (build inputs) argument name.
nixPackagesArgName :: Text
nixPackagesArgName :: Text
nixPackagesArgName = Text
"packages"

-- | shell.nix file path argument name.
nixInitFileArgName :: Text
nixInitFileArgName :: Text
nixInitFileArgName = Text
"shell-file"

-- | Extra options for the nix-shell command argument name.
nixShellOptsArgName :: Text
nixShellOptsArgName :: Text
nixShellOptsArgName = Text
"nix-shell-options"

-- | NIX_PATH override argument name
nixPathArgName :: Text
nixPathArgName :: Text
nixPathArgName = Text
"path"

-- | Add GC roots arg name
nixAddGCRootsArgName :: Text
nixAddGCRootsArgName :: Text
nixAddGCRootsArgName = Text
"add-gc-roots"