@@ -0,0 +1,45 @@
1+#! /usr/bin/env runhaskell
2+
3+> module Main ( main ) where
4+>
5+> import Data.Char
6+> import Distribution.PackageDescription
7+> import Distribution.Simple
8+> import Distribution.Simple.BuildPaths
9+> import Distribution.Simple.LocalBuildInfo
10+> import Distribution.Simple.Program
11+> import Distribution.Simple.Setup
12+> import System.Directory ( createDirectoryIfMissing )
13+> import System.FilePath
14+>
15+> main :: IO ()
16+> main = defaultMainWithHooks simpleUserHooks
17+> { hookedPrograms = map simpleProgram programs
18+> , confHook = configure
19+> }
20+>
21+> programs :: [String]
22+> programs = ["nix-instantiate", "nix-build", "nix-env", "nix-store"]
23+>
24+> configure :: (GenericPackageDescription, HookedBuildInfo) -> ConfigFlags -> IO LocalBuildInfo
25+> configure (gpd, hbi) flags = do
26+> lbi <- confHook simpleUserHooks (gpd, hbi) flags
27+> let paths = [ (p, programPath pc) | p <- programs, Just pc <- [lookupProgram (simpleProgram p) (withPrograms lbi)] ]
28+> dirPath = autogenModulesDir lbi </> "Nix"
29+> createDirectoryIfMissing True dirPath
30+> writeFile (dirPath </> "Paths.hs") $
31+> unlines $
32+> "module Nix.Paths where\n" : map (uncurry fmtPath) paths
33+> return lbi
34+>
35+> fmtPath :: String -> FilePath -> String
36+> fmtPath name path = unlines
37+> [ "-- | Complete path to the @" ++ name ++ "@ executable."
38+> , mangle name ++ " :: FilePath"
39+> , mangle name ++ " = " ++ show path
40+> ]
41+>
42+> mangle :: String -> String
43+> mangle [] = []
44+> mangle ('-':c:cs) = toUpper c : mangle cs
45+> mangle (c:cs) = c : mangle cs