diff --git a/default.nix b/default.nix index 0dbef43..0bedb66 100644 --- a/default.nix +++ b/default.nix @@ -1,10 +1,10 @@ { mkDerivation, aeson, annotated-exception, base, bytestring , containers, directory, ekg-core, exceptions, filepath -, generic-lens, githash, http-client, http-types, katip, lens -, lens-aeson, lib, monad-control, mtl, optparse-generic, pandoc -, pipes, pipes-concurrency, retry, servant, servant-server, stm -, streamly, streamly-core, text, time, transformers-base, unliftio -, unordered-containers, wai, warp, wreq +, generic-lens, http-client, http-types, katip, lens, lens-aeson +, lib, monad-control, mtl, optparse-generic, pandoc, pipes +, pipes-concurrency, retry, servant, servant-server, stm, streamly +, streamly-core, template-haskell, text, time, transformers-base +, unliftio, unordered-containers, wai, warp, wreq }: mkDerivation { pname = "wallabag-tagger"; @@ -14,10 +14,10 @@ mkDerivation { isExecutable = true; libraryHaskellDepends = [ aeson annotated-exception base bytestring containers directory - ekg-core exceptions filepath generic-lens githash http-client - http-types katip lens lens-aeson monad-control mtl optparse-generic - pandoc pipes pipes-concurrency retry servant servant-server stm - streamly streamly-core text time transformers-base unliftio + ekg-core exceptions filepath generic-lens http-client http-types + katip lens lens-aeson monad-control mtl optparse-generic pandoc + pipes pipes-concurrency retry servant servant-server stm streamly + streamly-core template-haskell text time transformers-base unliftio unordered-containers wai warp wreq ]; executableHaskellDepends = [ base ]; diff --git a/flake.nix b/flake.nix index 2d5cd1c..5e6029b 100644 --- a/flake.nix +++ b/flake.nix @@ -12,7 +12,11 @@ let pkgs = nixpkgs.legacyPackages.${system}; hp = nixpkgs.legacyPackages.${system}.haskellPackages.override (old: { overrides = pkgs.lib.composeExtensions (old.overrides or (_: _: {})) (f: p: { - wallabag-tagger = f.callPackage ./. {}; + wallabag-tagger = pkgs.haskell.lib.overrideCabal (f.callPackage ./. {}) (_: { + preBuild = '' + export COMMIT="${self.rev or "unknown"}" + ''; + }); }); }); in rec { diff --git a/src/MyLib.hs b/src/MyLib.hs index 13c829d..f5ae6dc 100644 --- a/src/MyLib.hs +++ b/src/MyLib.hs @@ -30,10 +30,12 @@ import UnliftIO.Exception (displayException, isAsyncException) import UnliftIO.IORef (IORef, newIORef, writeIORef) import Wallabag import Pipes.Concurrent (performGC) +import qualified Version data Options = Quick {verbose :: Bool} | Full {verbose :: Bool} + | Version | Daemon { verbose :: Bool, credentials :: FilePath "Path to the credentials.json", @@ -170,6 +172,7 @@ defaultMain = do case opts of Quick {verbose} -> run (severity verbose) Unarchived Full {verbose} -> run (severity verbose) Everything + Version -> print Version.compileTimeVersion Daemon {verbose, credentials, port} -> runDaemon (unHelpful $ unDefValue port) (unHelpful credentials) (severity verbose) where diff --git a/src/Server.hs b/src/Server.hs index a634a6b..03ee3b0 100644 --- a/src/Server.hs +++ b/src/Server.hs @@ -21,7 +21,7 @@ import System.Metrics.Distribution qualified as D import UnliftIO (liftIO) import UnliftIO.IORef (readIORef) import Data.Coerce (coerce) -import Version (Version, version) +import Version (Version, compileTimeVersion) data Health = OK @@ -77,7 +77,7 @@ getHealth :: IORef Health -> M Health getHealth = readIORef getVersion :: M Version -getVersion = pure version -- Comes from TH during compile time +getVersion = pure compileTimeVersion getMetrics :: M Metrics getMetrics = do diff --git a/src/Version.hs b/src/Version.hs index 4c41e38..b4e970e 100644 --- a/src/Version.hs +++ b/src/Version.hs @@ -3,32 +3,22 @@ {-# HLINT ignore "Unused LANGUAGE pragma" #-} module Version where -import GitHash (tGitInfoCwd, giHash, giDirty, giCommitDate, giCommitMessage) import qualified Data.Aeson as A -import qualified Data.ByteString.Lazy.Char8 as BL import GHC.Generics (Generic) +import System.Environment (lookupEnv) +import Language.Haskell.TH (runIO, litE, stringL) +import Data.Maybe (fromMaybe) -- Populate the version via the 'githash' package -data Version = Version - { versionCommit :: String - , versionDirty :: Bool - , versionCommitDate :: String - , versionCommitMessage :: String - } +newtype Version = Version + { versionCommit :: String } deriving (Show, Generic) instance A.ToJSON Version -version :: Version -version = Version - { versionCommit = giHash gi - , versionDirty = giDirty gi - , versionCommitDate = giCommitDate gi - , versionCommitMessage = giCommitMessage gi - } - where - gi = $$tGitInfoCwd +versionFromEnv :: IO Version +versionFromEnv = maybe (Version "unknown") Version <$> lookupEnv "COMMIT" -test :: IO () -test = BL.putStrLn $ A.encode version +compileTimeVersion :: Version +compileTimeVersion = Version $(runIO (fromMaybe "unknown" <$> lookupEnv "COMMIT") >>= litE . stringL) diff --git a/wallabag-tagger.cabal b/wallabag-tagger.cabal index 19c67f9..d201249 100644 --- a/wallabag-tagger.cabal +++ b/wallabag-tagger.cabal @@ -65,7 +65,7 @@ library , generic-lens , optparse-generic , containers - , githash + , template-haskell hs-source-dirs: src default-language: GHC2021