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/hopenssl.cabal b/hopenssl.cabal
--- a/hopenssl.cabal
+++ b/hopenssl.cabal
@@ -1,5 +1,5 @@
 name:                   hopenssl
-version:                2.2
+version:                2.2.1
 copyright:              (c) 2004-2017 Peter Simons
 license:                BSD3
 license-file:           LICENSE
@@ -14,9 +14,15 @@
                         RIPEMD160, and various SHA variants through the EVP
                         digest interface.
 cabal-version:          >= 1.8
-build-type:             Simple
+build-type:             Custom
 tested-with:            GHC > 7.6 && < 8.1
 
+custom-setup
+  setup-depends:
+    base >= 4 && <5,
+    Cabal,
+    cabal-doctest >= 1 && <1.1
+
 source-repository head
   type:                 git
   location:             git://github.com/peti/hopenssl.git
@@ -36,6 +42,7 @@
                         OpenSSL.EVP.Digest.Context
                         OpenSSL.EVP.Digest.Error
                         OpenSSL.EVP.Digest.Initialization
+  build-tools:          hsc2hs
 
 test-suite check-low-level-digest-api
   type:                 exitcode-stdio-1.0
diff --git a/test/doctests.hs b/test/doctests.hs
--- a/test/doctests.hs
+++ b/test/doctests.hs
@@ -1,12 +1,10 @@
-module Main (main) where
+module Main where
 
-import Test.DocTest
-import System.Environment
-import Data.Maybe
+import Build_doctests (flags, pkgs, module_sources)
+import Data.Foldable (traverse_)
+import Test.DocTest (doctest)
 
 main :: IO ()
-main = do
-  distDir <- fromMaybe "dist" `fmap` lookupEnv "HASKELL_DIST_DIR"
-  let hscFilesDir = distDir ++ "/build"
-      packageDB = distDir ++ "/package.conf.inplace"
-  doctest [ "-package-db=" ++ packageDB, "-package=hopenssl", "src", hscFilesDir ]
+main = do let args = flags ++ pkgs ++ module_sources
+          traverse_ putStrLn args -- optionally print arguments
+          doctest args
