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,40 +0,0 @@
-#! /usr/bin/env runhaskell
-
-> module Main ( main ) where
->
-> import Data.Char
-> import Data.Maybe
-> import Distribution.PackageDescription
-> import Distribution.Simple
-> import Distribution.Simple.LocalBuildInfo
-> import Distribution.Simple.Program
-> import Distribution.Simple.Setup
->
-> main :: IO ()
-> main = defaultMainWithHooks simpleUserHooks
->        { hookedPrograms = map simpleProgram programs
->        , confHook = configure
->        }
->
-> programs :: [String]
-> programs = ["nix-instantiate", "nix-build", "nix-env", "nix-store"]
->
-> configure :: (GenericPackageDescription, HookedBuildInfo) -> ConfigFlags -> IO LocalBuildInfo
-> configure (gpd, hbi) flags = do
->   lbi' <- confHook simpleUserHooks (gpd, hbi) flags
->   let paths = map (\p -> (p, maybe p programPath (lookupProgram (simpleProgram p) (withPrograms lbi)))) programs
->       descr = localPkgDescr lbi'
->       lib = fromJust (library descr)
->       libbi = libBuildInfo lib
->       lbi = lbi' { localPkgDescr = descr
->                    { library = Just (lib { libBuildInfo = libbi { cppOptions = cppOptions libbi ++ map fmtCppArg paths } })
->                    }
->                  }
->   return lbi
->
-> fmtCppArg :: (String, FilePath) -> String
-> fmtCppArg (prg, path) = "-DPATH_TO_" ++ map mangle prg ++ "=" ++ show path
->   where
->     mangle :: Char -> Char
->     mangle '-' = '_'
->     mangle c = toUpper c
diff --git a/cabal2nix.cabal b/cabal2nix.cabal
--- a/cabal2nix.cabal
+++ b/cabal2nix.cabal
@@ -1,9 +1,9 @@
--- This file has been generated from package.yaml by hpack version 0.17.0.
+-- This file has been generated from package.yaml by hpack version 0.18.1.
 --
 -- see: https://github.com/sol/hpack
 
 name:           cabal2nix
-version:        2.3
+version:        2.3.1
 synopsis:       Convert Cabal files into Nix build instructions.
 description:    Convert Cabal files into Nix build instructions. Users of Nix can install the latest version by running:
                 .
@@ -67,7 +67,7 @@
 license:        BSD3
 license-file:   LICENSE
 tested-with:    GHC > 7.10 && < 8.1
-build-type:     Simple
+build-type:     Custom
 cabal-version:  >= 1.10
 
 extra-source-files:
@@ -76,6 +76,12 @@
 source-repository head
   type: git
   location: https://github.com/nixos/cabal2nix
+
+custom-setup
+  setup-depends:
+      base >= 4 && <5
+    , Cabal
+    , cabal-doctest >= 1 && <1.1
 
 library
   hs-source-dirs:
diff --git a/test/doctests.hs b/test/doctests.hs
--- a/test/doctests.hs
+++ b/test/doctests.hs
@@ -1,6 +1,10 @@
-module Main ( main ) where
+module Main where
 
-import Test.DocTest
+import Build_doctests (flags, pkgs, module_sources)
+import Data.Foldable (traverse_)
+import Test.DocTest (doctest)
 
 main :: IO ()
-main = doctest ["src"]
+main = do let args = flags ++ pkgs ++ module_sources
+          traverse_ putStrLn args -- optionally print arguments
+          doctest args
