Skip to content

Commit

Permalink
feat: add --file flag (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackheaven committed Oct 2, 2024
1 parent b471ec7 commit 9c858fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
13 changes: 12 additions & 1 deletion src/NixTree/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ version = VERSION_nix_tree
data Opts = Opts
{ oInstallables :: [Installable],
oStore :: Maybe String,
oFile :: Maybe FilePath,
oVersion :: Bool,
oDerivation :: Bool,
oImpure :: Bool,
Expand Down Expand Up @@ -64,6 +65,15 @@ optsParser =
)
)
)
<*> optional
( Opts.strOption
( Opts.long "file"
<> Opts.metavar "FILE"
<> Opts.helpDoc
( Just $ Opts.vsep ["Interpret installables as attribute paths relative to the Nix expression stored in file."]
)
)
)
<*> 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")
Expand Down Expand Up @@ -113,7 +123,8 @@ main = do
StoreEnvOptions
{ seoIsDerivation = opts & oDerivation,
seoIsImpure = opts & oImpure,
seoStoreURL = opts & oStore
seoStoreURL = opts & oStore,
seoFile = opts & oFile
}

withStoreEnv seo installables $ \env' -> do
Expand Down
16 changes: 11 additions & 5 deletions src/NixTree/StorePath.hs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ data PathInfoOptions = PathInfoOptions
{ pioIsRecursive :: Bool,
pioIsDerivation :: Bool,
pioIsImpure :: Bool,
pioStoreURL :: Maybe FilePath
pioStoreURL :: Maybe FilePath,
pioFile :: Maybe FilePath
}

getPathInfo :: NixStore -> NixVersion -> PathInfoOptions -> NonEmpty Installable -> IO (NonEmpty (StorePath s (StoreName s) ()))
Expand All @@ -172,6 +173,10 @@ getPathInfo nixStore nixVersion options names = do
Nothing -> []
Just url -> ["--store", url]
)
++ ( case options & pioFile of
Nothing -> []
Just file -> ["--file", file]
)
++ (if nixVersion >= Nix2_4 then ["--extra-experimental-features", "nix-command flakes"] else [])
++ map (toString . installableToText) (toList names)
)
Expand Down Expand Up @@ -213,7 +218,8 @@ data StoreEnv s payload = StoreEnv
data StoreEnvOptions = StoreEnvOptions
{ seoIsDerivation :: Bool,
seoIsImpure :: Bool,
seoStoreURL :: Maybe String
seoStoreURL :: Maybe String,
seoFile :: Maybe String
}

withStoreEnv ::
Expand All @@ -223,7 +229,7 @@ withStoreEnv ::
NonEmpty Installable ->
(forall s. StoreEnv s () -> m a) ->
m a
withStoreEnv StoreEnvOptions {seoIsDerivation, seoIsImpure, seoStoreURL} names cb = do
withStoreEnv StoreEnvOptions {seoIsDerivation, seoIsImpure, seoStoreURL, seoFile} names cb = do
nixStore <- liftIO $ getStoreDir seoStoreURL

-- See: https://github.com/utdemir/nix-tree/issues/12
Expand All @@ -238,15 +244,15 @@ withStoreEnv StoreEnvOptions {seoIsDerivation, seoIsImpure, seoStoreURL} names c
getPathInfo
nixStore
nixVersion
(PathInfoOptions {pioIsDerivation = seoIsDerivation, pioIsRecursive = False, pioIsImpure = seoIsImpure, pioStoreURL = seoStoreURL})
(PathInfoOptions {pioIsDerivation = seoIsDerivation, pioIsRecursive = False, pioIsImpure = seoIsImpure, pioStoreURL = seoStoreURL, pioFile = seoFile})
names

paths <-
liftIO $
getPathInfo
nixStore
nixVersion
(PathInfoOptions {pioIsDerivation = seoIsDerivation, pioIsRecursive = True, pioIsImpure = seoIsImpure, pioStoreURL = seoStoreURL})
(PathInfoOptions {pioIsDerivation = seoIsDerivation, pioIsRecursive = True, pioIsImpure = seoIsImpure, pioStoreURL = seoStoreURL, pioFile = seoFile})
(Installable . toText . storeNameToPath . spName <$> roots)

let env =
Expand Down

0 comments on commit 9c858fe

Please sign in to comment.