Skip to content

Instantly share code, notes, and snippets.

@chris-martin
Created November 16, 2021 02:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-martin/0a58beab176bacca7da65d06dee41921 to your computer and use it in GitHub Desktop.
Save chris-martin/0a58beab176bacca7da65d06dee41921 to your computer and use it in GitHub Desktop.
import Control.Monad
import Data.Foldable
import Data.Functor
import Data.List
import Data.Maybe
import Data.Time.Clock
import System.Directory
import System.Process
main = listPackages >>= filterM needsUpdate >>= traverse_ update
listPackages = listDirectory "hackage" <&> mapMaybe (dropSuffix ".nix")
needsUpdate pkg = do
now <- getCurrentTime
before <- getModificationTime (pkgFile pkg)
return $ diffUTCTime now before > nominalDay
pkgFile pkg = "hackage/" <> pkg <> ".nix"
update pkg = cabal2nix pkg >>= writeNixFile pkg
cabal2nix pkg = readCreateProcess (proc "cabal2nix" ["cabal://" <> pkg]) ""
writeNixFile pkg expr = writeFile (pkgFile pkg) expr
dropSuffix sfx x
| sfx `isSuffixOf` x = Just $ take (length x - length sfx) x
| otherwise = Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment