hnix-store/hnix-store-db/README.md
2023-11-19 12:20:41 +01:00

2.4 KiB

hnix-store-db

Nix SQLite database implementation.

Only read-only functionality provided for database schema version 10.

API

The interface is experimental and might change wildly.

Example

This example is runnable via cabal run db-readme.

{-# LANGUAGE OverloadedStrings #-}

import Data.Default.Class (Default(def))

import qualified Control.Monad
import qualified Control.Monad.IO.Class

import qualified Database.Esqueleto.Experimental

import qualified System.Nix.StorePath
import qualified System.Nix.Store.DB.Run
import qualified System.Nix.Store.DB.Schema

import System.Nix.Store.DB.Query

main :: IO ()
main = do
  System.Nix.Store.DB.Run.runSystemSqlite $ do
    (paths, refs, drvOuts) <- queryEverything

    Control.Monad.IO.Class.liftIO $ do
      putStrLn $ "Stats: "
      let stat name v = putStrLn $ "- " ++ name ++ ": " ++ show (length v)
      stat "ValidPath(s)" paths
      stat "Ref(s)" refs
      stat "DerivationOutput(s)" drvOuts

    maybeValidPath <- queryOneValidDerivationEntity
    case maybeValidPath of
      Nothing -> pure ()
      Just validPathEntity -> do
        let pth =
              System.Nix.Store.DB.Schema.validPathPath
              $ Database.Esqueleto.Experimental.entityVal validPathEntity

        (same, samePath, references, referrers, validDerivers, outputs) <- (,,,,,)
          <$> queryPathInfo pth
          <*> queryPathFromHashPart def (System.Nix.StorePath.storePathHash pth)
          <*> queryReferences validPathEntity
          <*> queryReferrers pth
          <*> queryValidDerivers pth
          <*> queryDerivationOutputs validPathEntity

        Control.Monad.unless (same == Just (Database.Esqueleto.Experimental.entityVal validPathEntity))
          $ error "queryPathInfo failed to roundtrip"
        Control.Monad.unless (samePath == Just pth)
          $ error "queryPathFromHashPart failed to roundtrip"

        Control.Monad.IO.Class.liftIO $ do
          putStrLn $ "References: "
          print references
          putStrLn $ "Referrers: "
          print referrers
          putStrLn $ "Valid derivers: "
          print validDerivers
          putStrLn $ "Derivation outputs: "
          print outputs

    pure ()