Skip to content

Commit

Permalink
Graphviz export (#81)
Browse files Browse the repository at this point in the history
* Preliminary --dot support

* Format

* Update README with the dot flag
  • Loading branch information
utdemir authored Jan 21, 2024
1 parent a6017b6 commit 9031080
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ nix run github:utdemir/nix-tree -- --help

```console
$ nix-tree --help
Usage: nix-tree [INSTALLABLE] [--store STORE] [--version] [--derivation] [--impure]
Usage: nix-tree [INSTALLABLE] [--store STORE] [--version] [--derivation] [--impure] [--dot]

Interactively browse dependency graphs of Nix derivations.

Expand All @@ -43,6 +43,7 @@ Available options:
--version Show the nix-tree version
--derivation Operate on the store derivation rather than its outputs
--impure Allow access to mutable paths and repositories
--dot Print the dependency graph in dot format
-h,--help Show this help text

Keybindings:
Expand Down
1 change: 1 addition & 0 deletions nix-tree.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ common common-options
, directory
, optparse-applicative
, microlens
, dot

executable nix-tree
import: common-options
Expand Down
8 changes: 6 additions & 2 deletions src/NixTree/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ data Opts = Opts
oStore :: String,
oVersion :: Bool,
oDerivation :: Bool,
oImpure :: Bool
oImpure :: Bool,
oDot :: Bool
}

optsParser :: Opts.ParserInfo Opts
Expand Down Expand Up @@ -65,6 +66,7 @@ optsParser =
<*> Opts.switch (Opts.long "version" <> Opts.help "Show the nix-tree version")
<*> Opts.switch (Opts.long "derivation" <> Opts.help "Operate on the store derivation rather than its outputs")
<*> Opts.switch (Opts.long "impure" <> Opts.help "Allow access to mutable paths and repositories")
<*> Opts.switch (Opts.long "dot" <> Opts.help "Print the dependency graph in dot format")

keybindingsHelp :: Opts.Doc
keybindingsHelp =
Expand Down Expand Up @@ -123,7 +125,9 @@ main = do
& chunks 50
& mapConcurrently_ (mapM_ (\p -> evaluate (rnf p) >> incProgress bar 1))

run env
if opts & oDot
then putTextLn $ storeEnvToDot env
else run env

chunks :: Int -> [a] -> [[a]]
chunks _ [] = []
Expand Down
36 changes: 36 additions & 0 deletions src/NixTree/StorePath.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module NixTree.StorePath
seBottomUp,
seFetchRefs,
mkStoreName,
storeEnvToDot,
)
where

Expand All @@ -25,7 +26,9 @@ import Data.Aeson.Types (Parser)
import qualified Data.ByteString.Lazy as BL
import qualified Data.HashMap.Strict as HM
import qualified Data.HashSet as HS
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Dot
import System.FilePath.Posix (addTrailingPathSeparator, splitDirectories, (</>))
import System.IO (hPutStrLn)
import System.Process.Typed (proc, readProcessStdout_)
Expand Down Expand Up @@ -319,6 +322,39 @@ seBottomUp f StoreEnv {sePaths, seRoots} =

--------------------------------------------------------------------------------

storeEnvToDot :: StoreEnv s a -> Text
storeEnvToDot env =
seBottomUp go env
& seGetRoots
& toList
& map spPayload
& mconcat
& render
where
go sp =
fromList [Set.singleton (spName sp, spName ref) <> spPayload ref | ref <- spRefs sp]
& mconcat

render :: Set (StoreName s, StoreName s) -> Text
render edges =
Dot.DotGraph
Dot.Strict
Dot.Directed
Nothing
[ Dot.StatementEdge
( Dot.EdgeStatement
(Dot.ListTwo (Dot.EdgeNode (mkNodeId from)) (Dot.EdgeNode (mkNodeId to)) [])
[]
)
| (from, to) <- toList edges
]
& Dot.encode

mkNodeId :: StoreName s -> Dot.NodeId
mkNodeId = fromString . toString . storeNameToShortText

--------------------------------------------------------------------------------

data NixPathInfo = NixPathInfo
{ npiPath :: FilePath,
npiNarSize :: Int,
Expand Down

0 comments on commit 9031080

Please sign in to comment.