diff --git a/Setup.hs b/Setup.hs
new file mode 100644
--- /dev/null
+++ b/Setup.hs
@@ -0,0 +1,34 @@
+{-# LANGUAGE CPP #-}
+{-# OPTIONS_GHC -Wall #-}
+
+module Main ( main ) where
+
+#ifndef MIN_VERSION_cabal_doctest
+#define MIN_VERSION_cabal_doctest(x,y,z) 0
+#endif
+
+#if MIN_VERSION_cabal_doctest(1,0,0)
+
+import Distribution.Extra.Doctest ( defaultMainWithDoctests )
+main :: IO ()
+main = defaultMainWithDoctests "doctests"
+
+#else
+
+#ifdef MIN_VERSION_Cabal
+-- If the macro is defined, we have new cabal-install,
+-- but for some reason we don't have cabal-doctest in package-db
+--
+-- Probably we are running cabal sdist, when otherwise using new-build
+-- workflow
+#warning You are configuring this package without cabal-doctest installed. \
+         The doctests test-suite will not work as a result. \
+         To fix this, install cabal-doctest before configuring.
+#endif
+
+import Distribution.Simple
+
+main :: IO ()
+main = defaultMain
+
+#endif
diff --git a/Setup.lhs b/Setup.lhs
deleted file mode 100644
--- a/Setup.lhs
+++ /dev/null
@@ -1,8 +0,0 @@
-#!/usr/bin/env runhaskell
-
-> module Main (main) where
->
-> import Distribution.Simple
->
-> main :: IO ()
-> main = defaultMain
diff --git a/example/Main.hs b/example/Main.hs
--- a/example/Main.hs
+++ b/example/Main.hs
@@ -10,8 +10,9 @@
 import Foreign.C.String ( CStringLen, withCStringLen )
 
 -- This class allows us to log normal Strings, ByteStrings, and pretty much any
--- other type to syslog without. It abstracts the information of how to convert
--- the given type into a CStringLen that can be passed to syslog.
+-- other type to syslog without any explicit conversions. It abstracts the
+-- information of how to convert the given type into a CStringLen that can be
+-- passed to syslog.
 
 class LogMessage m where
   toCStringLen :: m -> (CStringLen -> IO a) -> IO a
diff --git a/hsyslog.cabal b/hsyslog.cabal
--- a/hsyslog.cabal
+++ b/hsyslog.cabal
@@ -1,7 +1,7 @@
 name:           hsyslog
-version:        5
-cabal-version:  >= 1.8
-build-type:     Simple
+version:        5.0.1
+cabal-version:  >= 1.10
+build-type:     Custom
 license:        BSD3
 license-file:   LICENSE
 copyright:      Copyright (c) 2004-2017 by Peter Simons
@@ -11,7 +11,7 @@
 bug-reports:    http://github.com/peti/hsyslog/issues
 synopsis:       FFI interface to syslog(3) from POSIX.1-2001
 category:       Foreign
-tested-with:    GHC >7.6 && <8.1
+tested-with:    GHC > 7.6 && < 8.3
 
 extra-source-files:
   c-bits/simple-syslog.c
@@ -29,6 +29,11 @@
   <https://github.com/peti/hsyslog/blob/master/example/Main.hs examples> directory of
   this package.
 
+custom-setup
+  setup-depends: base >= 4 && <5,
+                 Cabal,
+                 cabal-doctest >= 1 && <1.1
+
 source-repository head
   type:     git
   location: git://github.com/peti/hsyslog.git
@@ -49,20 +54,24 @@
   hs-source-dirs:   src
   c-sources:        c-bits/simple-syslog.c
                     c-bits/make-log-mask.c
+  default-language: Haskell2010
+  build-tools:      hsc2hs
 
-test-suite hsyslog-test
-  type:           exitcode-stdio-1.0
-  main-is:        Main.hs
-  hs-source-dirs: test
-  build-depends:  hsyslog, base, doctest
-  ghc-options:    -threaded
+test-suite doctests
+  type:             exitcode-stdio-1.0
+  main-is:          doctests.hs
+  hs-source-dirs:   test
+  build-depends:    hsyslog, base, doctest
+  ghc-options:      -threaded
+  default-language: Haskell2010
 
 executable hsyslog-example
-  main-is:              Main.hs
-  hs-source-dirs:       example
-  build-depends:        base, hsyslog, bytestring
+  main-is:            Main.hs
+  hs-source-dirs:     example
   if flag(install-examples)
-    buildable:          True
-    other-extensions:   TypeSynonymInstances, FlexibleInstances
+    buildable:        True
+    build-depends:    base, hsyslog, bytestring
+    other-extensions: TypeSynonymInstances, FlexibleInstances
   else
-    buildable:          False
+    buildable:        False
+  default-language:   Haskell2010
diff --git a/test/Main.hs b/test/Main.hs
deleted file mode 100644
--- a/test/Main.hs
+++ /dev/null
@@ -1,11 +0,0 @@
-module Main (main) where
-
-import Test.DocTest
-import System.Environment
-import Data.Maybe
-
-main :: IO ()
-main = do
-  distDir <- fromMaybe "dist" `fmap` lookupEnv "HASKELL_DIST_DIR"
-  let hscFilesDir = distDir ++ "/build"
-  doctest [ "-i" ++ hscFilesDir, "src" ]
diff --git a/test/doctests.hs b/test/doctests.hs
new file mode 100644
--- /dev/null
+++ b/test/doctests.hs
@@ -0,0 +1,10 @@
+module Main where
+
+import Build_doctests (flags, pkgs, module_sources)
+import Data.Foldable (traverse_)
+import Test.DocTest (doctest)
+
+main :: IO ()
+main = do let args = flags ++ pkgs ++ module_sources
+          traverse_ putStrLn args -- optionally print arguments
+          doctest args
