Created
March 4, 2020 18:38
-
-
Save sorki/f1091ea36128f81fce2b5504259bb6fc to your computer and use it in GitHub Desktop.
cachix no clobber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Control.Monad | |
import qualified Data.ByteString.Char8 as BSC | |
import System.Nix.Internal.StorePath | |
import Network.HTTP.Client | |
import Network.HTTP.Client.TLS | |
import Network.HTTP.Types.Status | |
cache = "cache.nixos.org" | |
inCache :: Manager -> StorePath -> IO Bool | |
inCache man p = do | |
let narinfoPath = storePathToNarinfo p | |
r = defaultRequest { | |
host = cache | |
, port = 443 | |
, path = narinfoPath | |
, secure = True | |
} | |
res <- httpLbs r man | |
--print (statusCode $ responseStatus res, narinfoPath) | |
case statusCode $ responseStatus res of | |
200 -> return True | |
_ -> return False | |
uncachedPaths ps = withTLS $ \man -> filterM (\x -> not <$> inCache man x) ps | |
uncachedPathsParse ps = withTLS $ \man -> | |
filterM (\raw -> do | |
case parsePath "/nix/store" raw of | |
Left er -> error er | |
Right x -> not <$> inCache man x | |
) ps | |
withTLS act = newManager tlsManagerSettings >>= act | |
main = do | |
x <- BSC.getContents | |
y <- uncachedPathsParse $ filter (not . (".drv" `isSuffixOf`)) $ BSC.lines x | |
BSC.putStr $ BSC.unlines y | |
-- | |
-- DRAGONS | |
-- | |
set = defaultManagerSettings | |
{ managerConnCount = 1000 } | |
withMan act = newManager set >>= act | |
--inCache :: StorePath -> IO Bool | |
inCacheNoTLS man p = do | |
let narinfoPath = storePathToNarinfo p | |
r = defaultRequest { | |
host = cache | |
, path = narinfoPath | |
} | |
res <- httpLbs r man | |
print (statusCode $ responseStatus res, narinfoPath) | |
case statusCode $ responseStatus res of | |
200 -> return True | |
_ -> return False | |
cachedPathsNoTLS ps = withMan $ \man -> filterM (inCacheNoTLS man) ps | |
cachedPathsParseNoTLS ps = withMan $ \man -> filterM (\raw -> do | |
case parsePath "/nix/store" raw of | |
Left er -> error er | |
Right x -> inCacheNoTLS man x | |
) ps | |
test = do | |
{-- | |
let p = "/nix/store/x5m45fcnky99r0k41kmdwmjb7zw5k4z4-binutils-2.31.1" | |
Right pp = parsePath "/nix/store" p | |
c <- cachedPathsParse [ p ] | |
print c | |
m <- newManager tlsManagerSettings | |
d <- inCache m pp | |
print c | |
--} | |
-- | |
{-- | |
x <- BSC.readFile "inPaths" | |
y <- uncachedPathsParse $ BSC.lines x | |
BSC.writeFile "outPaths" $ BSC.unlines y | |
--} | |
-- much duplication | |
cachedPaths ps = withTLS $ \man -> filterM (inCache man) ps | |
cachedPathsParse ps = withTLS $ \man -> | |
filterM (\raw -> do | |
case parsePath "/nix/store" raw of | |
Left er -> error er | |
Right x -> inCache man x | |
) ps | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment