{-|
Description : Build related types
Maintainer  : srk <srk@48.io>
|-}
module System.Nix.Build
  ( BuildMode(..)
  , BuildStatus(..)
  , buildSuccess
  , BuildResult(..)
  ) where

import Data.Map (Map)
import Data.Time (UTCTime)
import Data.Text (Text)
import GHC.Generics (Generic)

import System.Nix.OutputName (OutputName)
import System.Nix.Realisation (DerivationOutput, Realisation)

-- | Mode of the build operation
-- Keep the order of these Enums to match enums from reference implementations
-- src/libstore/store-api.hh
data BuildMode
  = BuildMode_Normal -- ^ Perform normal build
  | BuildMode_Repair -- ^ Try to repair corrupted or missing paths by re-building or re-downloading them
  | BuildMode_Check -- ^ Check if the build is reproducible (rebuild and compare to previous build)
  deriving (BuildMode
BuildMode -> BuildMode -> Bounded BuildMode
forall a. a -> a -> Bounded a
$cminBound :: BuildMode
minBound :: BuildMode
$cmaxBound :: BuildMode
maxBound :: BuildMode
Bounded, BuildMode -> BuildMode -> Bool
(BuildMode -> BuildMode -> Bool)
-> (BuildMode -> BuildMode -> Bool) -> Eq BuildMode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BuildMode -> BuildMode -> Bool
== :: BuildMode -> BuildMode -> Bool
$c/= :: BuildMode -> BuildMode -> Bool
/= :: BuildMode -> BuildMode -> Bool
Eq, (forall x. BuildMode -> Rep BuildMode x)
-> (forall x. Rep BuildMode x -> BuildMode) -> Generic BuildMode
forall x. Rep BuildMode x -> BuildMode
forall x. BuildMode -> Rep BuildMode x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BuildMode -> Rep BuildMode x
from :: forall x. BuildMode -> Rep BuildMode x
$cto :: forall x. Rep BuildMode x -> BuildMode
to :: forall x. Rep BuildMode x -> BuildMode
Generic, Eq BuildMode
Eq BuildMode =>
(BuildMode -> BuildMode -> Ordering)
-> (BuildMode -> BuildMode -> Bool)
-> (BuildMode -> BuildMode -> Bool)
-> (BuildMode -> BuildMode -> Bool)
-> (BuildMode -> BuildMode -> Bool)
-> (BuildMode -> BuildMode -> BuildMode)
-> (BuildMode -> BuildMode -> BuildMode)
-> Ord BuildMode
BuildMode -> BuildMode -> Bool
BuildMode -> BuildMode -> Ordering
BuildMode -> BuildMode -> BuildMode
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: BuildMode -> BuildMode -> Ordering
compare :: BuildMode -> BuildMode -> Ordering
$c< :: BuildMode -> BuildMode -> Bool
< :: BuildMode -> BuildMode -> Bool
$c<= :: BuildMode -> BuildMode -> Bool
<= :: BuildMode -> BuildMode -> Bool
$c> :: BuildMode -> BuildMode -> Bool
> :: BuildMode -> BuildMode -> Bool
$c>= :: BuildMode -> BuildMode -> Bool
>= :: BuildMode -> BuildMode -> Bool
$cmax :: BuildMode -> BuildMode -> BuildMode
max :: BuildMode -> BuildMode -> BuildMode
$cmin :: BuildMode -> BuildMode -> BuildMode
min :: BuildMode -> BuildMode -> BuildMode
Ord, Int -> BuildMode
BuildMode -> Int
BuildMode -> [BuildMode]
BuildMode -> BuildMode
BuildMode -> BuildMode -> [BuildMode]
BuildMode -> BuildMode -> BuildMode -> [BuildMode]
(BuildMode -> BuildMode)
-> (BuildMode -> BuildMode)
-> (Int -> BuildMode)
-> (BuildMode -> Int)
-> (BuildMode -> [BuildMode])
-> (BuildMode -> BuildMode -> [BuildMode])
-> (BuildMode -> BuildMode -> [BuildMode])
-> (BuildMode -> BuildMode -> BuildMode -> [BuildMode])
-> Enum BuildMode
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: BuildMode -> BuildMode
succ :: BuildMode -> BuildMode
$cpred :: BuildMode -> BuildMode
pred :: BuildMode -> BuildMode
$ctoEnum :: Int -> BuildMode
toEnum :: Int -> BuildMode
$cfromEnum :: BuildMode -> Int
fromEnum :: BuildMode -> Int
$cenumFrom :: BuildMode -> [BuildMode]
enumFrom :: BuildMode -> [BuildMode]
$cenumFromThen :: BuildMode -> BuildMode -> [BuildMode]
enumFromThen :: BuildMode -> BuildMode -> [BuildMode]
$cenumFromTo :: BuildMode -> BuildMode -> [BuildMode]
enumFromTo :: BuildMode -> BuildMode -> [BuildMode]
$cenumFromThenTo :: BuildMode -> BuildMode -> BuildMode -> [BuildMode]
enumFromThenTo :: BuildMode -> BuildMode -> BuildMode -> [BuildMode]
Enum, Int -> BuildMode -> ShowS
[BuildMode] -> ShowS
BuildMode -> String
(Int -> BuildMode -> ShowS)
-> (BuildMode -> String)
-> ([BuildMode] -> ShowS)
-> Show BuildMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BuildMode -> ShowS
showsPrec :: Int -> BuildMode -> ShowS
$cshow :: BuildMode -> String
show :: BuildMode -> String
$cshowList :: [BuildMode] -> ShowS
showList :: [BuildMode] -> ShowS
Show)

-- | Build result status
data BuildStatus =
    BuildStatus_Built -- ^ Build performed successfully
  | BuildStatus_Substituted -- ^ Path substituted from cache
  | BuildStatus_AlreadyValid -- ^ Path is already valid (available in local store)
  | BuildStatus_PermanentFailure
  | BuildStatus_InputRejected
  | BuildStatus_OutputRejected
  | BuildStatus_TransientFailure -- ^ Possibly transient build failure
  | BuildStatus_CachedFailure -- ^ Obsolete
  | BuildStatus_TimedOut -- ^ Build timed out
  | BuildStatus_MiscFailure
  | BuildStatus_DependencyFailed -- ^ Build dependency failed to build
  | BuildStatus_LogLimitExceeded
  | BuildStatus_NotDeterministic
  | BuildStatus_ResolvesToAlreadyValid
  | BuildStatus_NoSubstituters
  deriving (BuildStatus
BuildStatus -> BuildStatus -> Bounded BuildStatus
forall a. a -> a -> Bounded a
$cminBound :: BuildStatus
minBound :: BuildStatus
$cmaxBound :: BuildStatus
maxBound :: BuildStatus
Bounded, BuildStatus -> BuildStatus -> Bool
(BuildStatus -> BuildStatus -> Bool)
-> (BuildStatus -> BuildStatus -> Bool) -> Eq BuildStatus
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BuildStatus -> BuildStatus -> Bool
== :: BuildStatus -> BuildStatus -> Bool
$c/= :: BuildStatus -> BuildStatus -> Bool
/= :: BuildStatus -> BuildStatus -> Bool
Eq, (forall x. BuildStatus -> Rep BuildStatus x)
-> (forall x. Rep BuildStatus x -> BuildStatus)
-> Generic BuildStatus
forall x. Rep BuildStatus x -> BuildStatus
forall x. BuildStatus -> Rep BuildStatus x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BuildStatus -> Rep BuildStatus x
from :: forall x. BuildStatus -> Rep BuildStatus x
$cto :: forall x. Rep BuildStatus x -> BuildStatus
to :: forall x. Rep BuildStatus x -> BuildStatus
Generic, Eq BuildStatus
Eq BuildStatus =>
(BuildStatus -> BuildStatus -> Ordering)
-> (BuildStatus -> BuildStatus -> Bool)
-> (BuildStatus -> BuildStatus -> Bool)
-> (BuildStatus -> BuildStatus -> Bool)
-> (BuildStatus -> BuildStatus -> Bool)
-> (BuildStatus -> BuildStatus -> BuildStatus)
-> (BuildStatus -> BuildStatus -> BuildStatus)
-> Ord BuildStatus
BuildStatus -> BuildStatus -> Bool
BuildStatus -> BuildStatus -> Ordering
BuildStatus -> BuildStatus -> BuildStatus
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: BuildStatus -> BuildStatus -> Ordering
compare :: BuildStatus -> BuildStatus -> Ordering
$c< :: BuildStatus -> BuildStatus -> Bool
< :: BuildStatus -> BuildStatus -> Bool
$c<= :: BuildStatus -> BuildStatus -> Bool
<= :: BuildStatus -> BuildStatus -> Bool
$c> :: BuildStatus -> BuildStatus -> Bool
> :: BuildStatus -> BuildStatus -> Bool
$c>= :: BuildStatus -> BuildStatus -> Bool
>= :: BuildStatus -> BuildStatus -> Bool
$cmax :: BuildStatus -> BuildStatus -> BuildStatus
max :: BuildStatus -> BuildStatus -> BuildStatus
$cmin :: BuildStatus -> BuildStatus -> BuildStatus
min :: BuildStatus -> BuildStatus -> BuildStatus
Ord, Int -> BuildStatus
BuildStatus -> Int
BuildStatus -> [BuildStatus]
BuildStatus -> BuildStatus
BuildStatus -> BuildStatus -> [BuildStatus]
BuildStatus -> BuildStatus -> BuildStatus -> [BuildStatus]
(BuildStatus -> BuildStatus)
-> (BuildStatus -> BuildStatus)
-> (Int -> BuildStatus)
-> (BuildStatus -> Int)
-> (BuildStatus -> [BuildStatus])
-> (BuildStatus -> BuildStatus -> [BuildStatus])
-> (BuildStatus -> BuildStatus -> [BuildStatus])
-> (BuildStatus -> BuildStatus -> BuildStatus -> [BuildStatus])
-> Enum BuildStatus
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: BuildStatus -> BuildStatus
succ :: BuildStatus -> BuildStatus
$cpred :: BuildStatus -> BuildStatus
pred :: BuildStatus -> BuildStatus
$ctoEnum :: Int -> BuildStatus
toEnum :: Int -> BuildStatus
$cfromEnum :: BuildStatus -> Int
fromEnum :: BuildStatus -> Int
$cenumFrom :: BuildStatus -> [BuildStatus]
enumFrom :: BuildStatus -> [BuildStatus]
$cenumFromThen :: BuildStatus -> BuildStatus -> [BuildStatus]
enumFromThen :: BuildStatus -> BuildStatus -> [BuildStatus]
$cenumFromTo :: BuildStatus -> BuildStatus -> [BuildStatus]
enumFromTo :: BuildStatus -> BuildStatus -> [BuildStatus]
$cenumFromThenTo :: BuildStatus -> BuildStatus -> BuildStatus -> [BuildStatus]
enumFromThenTo :: BuildStatus -> BuildStatus -> BuildStatus -> [BuildStatus]
Enum, Int -> BuildStatus -> ShowS
[BuildStatus] -> ShowS
BuildStatus -> String
(Int -> BuildStatus -> ShowS)
-> (BuildStatus -> String)
-> ([BuildStatus] -> ShowS)
-> Show BuildStatus
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BuildStatus -> ShowS
showsPrec :: Int -> BuildStatus -> ShowS
$cshow :: BuildStatus -> String
show :: BuildStatus -> String
$cshowList :: [BuildStatus] -> ShowS
showList :: [BuildStatus] -> ShowS
Show)

-- | Result of the build
data BuildResult = BuildResult
  { BuildResult -> BuildStatus
buildResultStatus             :: BuildStatus
  -- ^ Build status, MiscFailure should be the default
  , BuildResult -> Maybe Text
buildResultErrorMessage       :: Maybe Text
  -- ^ Possible build error message
  , BuildResult -> Maybe Int
buildResultTimesBuilt         :: Maybe Int
  -- ^ How many times this build was performed (since 1.29)
  , BuildResult -> Maybe Bool
buildResultIsNonDeterministic :: Maybe Bool
  -- ^ If timesBuilt > 1, whether some builds did not produce the same result (since 1.29)
  , BuildResult -> Maybe UTCTime
buildResultStartTime          :: Maybe UTCTime
  -- ^ Start time of this build (since 1.29)
  , BuildResult -> Maybe UTCTime
buildResultStopTime           :: Maybe UTCTime
  -- ^ Stop time of this build (since 1.29)
  , BuildResult
-> Maybe (Map (DerivationOutput OutputName) Realisation)
buildResultBuiltOutputs       :: Maybe (Map (DerivationOutput OutputName) Realisation)
  -- ^ Mapping of the output names to @Realisation@s (since 1.28)
  -- (paths with additional info and their dependencies)
  }
  deriving (BuildResult -> BuildResult -> Bool
(BuildResult -> BuildResult -> Bool)
-> (BuildResult -> BuildResult -> Bool) -> Eq BuildResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BuildResult -> BuildResult -> Bool
== :: BuildResult -> BuildResult -> Bool
$c/= :: BuildResult -> BuildResult -> Bool
/= :: BuildResult -> BuildResult -> Bool
Eq, (forall x. BuildResult -> Rep BuildResult x)
-> (forall x. Rep BuildResult x -> BuildResult)
-> Generic BuildResult
forall x. Rep BuildResult x -> BuildResult
forall x. BuildResult -> Rep BuildResult x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BuildResult -> Rep BuildResult x
from :: forall x. BuildResult -> Rep BuildResult x
$cto :: forall x. Rep BuildResult x -> BuildResult
to :: forall x. Rep BuildResult x -> BuildResult
Generic, Eq BuildResult
Eq BuildResult =>
(BuildResult -> BuildResult -> Ordering)
-> (BuildResult -> BuildResult -> Bool)
-> (BuildResult -> BuildResult -> Bool)
-> (BuildResult -> BuildResult -> Bool)
-> (BuildResult -> BuildResult -> Bool)
-> (BuildResult -> BuildResult -> BuildResult)
-> (BuildResult -> BuildResult -> BuildResult)
-> Ord BuildResult
BuildResult -> BuildResult -> Bool
BuildResult -> BuildResult -> Ordering
BuildResult -> BuildResult -> BuildResult
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: BuildResult -> BuildResult -> Ordering
compare :: BuildResult -> BuildResult -> Ordering
$c< :: BuildResult -> BuildResult -> Bool
< :: BuildResult -> BuildResult -> Bool
$c<= :: BuildResult -> BuildResult -> Bool
<= :: BuildResult -> BuildResult -> Bool
$c> :: BuildResult -> BuildResult -> Bool
> :: BuildResult -> BuildResult -> Bool
$c>= :: BuildResult -> BuildResult -> Bool
>= :: BuildResult -> BuildResult -> Bool
$cmax :: BuildResult -> BuildResult -> BuildResult
max :: BuildResult -> BuildResult -> BuildResult
$cmin :: BuildResult -> BuildResult -> BuildResult
min :: BuildResult -> BuildResult -> BuildResult
Ord, Int -> BuildResult -> ShowS
[BuildResult] -> ShowS
BuildResult -> String
(Int -> BuildResult -> ShowS)
-> (BuildResult -> String)
-> ([BuildResult] -> ShowS)
-> Show BuildResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BuildResult -> ShowS
showsPrec :: Int -> BuildResult -> ShowS
$cshow :: BuildResult -> String
show :: BuildResult -> String
$cshowList :: [BuildResult] -> ShowS
showList :: [BuildResult] -> ShowS
Show)

buildSuccess :: BuildStatus -> Bool
buildSuccess :: BuildStatus -> Bool
buildSuccess BuildStatus
x =
  BuildStatus
x BuildStatus -> [BuildStatus] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem`
    [ BuildStatus
BuildStatus_Built
    , BuildStatus
BuildStatus_Substituted
    , BuildStatus
BuildStatus_AlreadyValid
    ]