Allow passing --impure flag to Nix

Closes #40
This commit is contained in:
Utku Demir 2022-11-19 21:07:31 +13:00
parent 2d656bbe2d
commit bc3a40c37c
3 changed files with 25 additions and 8 deletions

View File

@ -3,6 +3,7 @@
## Unreleased
* feat: Improved help text.
* feat: Allow passing '--impure' flag to Nix (issue: [@40][])
* fix: Use -O1 instead of -O2 to save on compile times.
## 0.2.1 - 2022-10-24:

View File

@ -20,7 +20,8 @@ version = VERSION_nix_tree
data Opts = Opts
{ oInstallables :: [Installable],
oVersion :: Bool,
oDerivation :: Bool
oDerivation :: Bool,
oImpure :: Bool
}
optsParser :: Opts.ParserInfo Opts
@ -48,6 +49,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")
keybindingsHelp :: Opts.Doc
keybindingsHelp =
@ -85,7 +87,13 @@ main = do
[] -> showAndFail "No store path given."
p : ps -> return . fmap (Installable . toText) $ p :| ps
withStoreEnv (opts & oDerivation) installables $ \env' -> do
let seo =
StoreEnvOptions
{ seoIsDerivation = opts & oDerivation,
seoIsImpure = opts & oImpure
}
withStoreEnv seo installables $ \env' -> do
let env = calculatePathStats env'
allPaths = seAll env

View File

@ -7,6 +7,7 @@ module NixTree.StorePath
StorePath (..),
Installable (..),
StoreEnv (..),
StoreEnvOptions (..),
withStoreEnv,
seLookup,
seAll,
@ -138,7 +139,8 @@ newtype Installable = Installable {installableToText :: Text}
data PathInfoOptions = PathInfoOptions
{ pioIsRecursive :: Bool,
pioIsDerivation :: Bool
pioIsDerivation :: Bool,
pioIsImpure :: Bool
}
getPathInfo :: NixStore -> NixVersion -> PathInfoOptions -> NonEmpty Installable -> IO (NonEmpty (StorePath s (StoreName s) ()))
@ -149,6 +151,7 @@ getPathInfo nixStore nixVersion options names = do
( proc
"nix"
( ["path-info", "--json"]
++ (if options & pioIsImpure then ["--impure"] else [])
++ (if options & pioIsRecursive then ["--recursive"] else [])
++ (if (options & pioIsDerivation) && nixVersion >= Nix2_4 then ["--derivation"] else [])
++ (if nixVersion >= Nix2_4 then ["--extra-experimental-features", "nix-command flakes"] else [])
@ -189,20 +192,25 @@ data StoreEnv s payload = StoreEnv
}
deriving (Functor, Generic, NFData)
data StoreEnvOptions = StoreEnvOptions
{ seoIsDerivation :: Bool,
seoIsImpure :: Bool
}
withStoreEnv ::
forall m a.
MonadIO m =>
Bool ->
StoreEnvOptions ->
NonEmpty Installable ->
(forall s. StoreEnv s () -> m a) ->
m a
withStoreEnv isDerivation names cb = do
withStoreEnv StoreEnvOptions {seoIsDerivation, seoIsImpure} names cb = do
nixStore <- liftIO getNixStore
-- See: https://github.com/utdemir/nix-tree/issues/12
nixVersion <- liftIO getNixVersion
when (isDerivation && nixVersion < Nix2_4) $
when (seoIsDerivation && nixVersion < Nix2_4) $
liftIO $
hPutStrLn stderr "Warning: --derivation flag is ignored on Nix versions older than 2.4."
@ -211,7 +219,7 @@ withStoreEnv isDerivation names cb = do
getPathInfo
nixStore
nixVersion
(PathInfoOptions {pioIsDerivation = isDerivation, pioIsRecursive = False})
(PathInfoOptions {pioIsDerivation = seoIsDerivation, pioIsRecursive = False, pioIsImpure = seoIsImpure})
names
paths <-
@ -219,7 +227,7 @@ withStoreEnv isDerivation names cb = do
getPathInfo
nixStore
nixVersion
(PathInfoOptions {pioIsDerivation = isDerivation, pioIsRecursive = True})
(PathInfoOptions {pioIsDerivation = seoIsDerivation, pioIsRecursive = True, pioIsImpure = seoIsImpure})
(Installable . toText . storeNameToPath . spName <$> roots)
let env =