@@ -46,6 +46,7 @@ import Data.Char ( isDigit )
4646import Data.Foldable ( foldrM )
4747import Data.Fix ( foldFix )
4848import Data.List ( partition )
49+ import qualified Data.HashSet as HS
4950import qualified Data.HashMap.Lazy as M
5051import Data.Scientific
5152import qualified Data.Set as S
@@ -471,6 +472,20 @@ getAttrNix x y =
471472
472473 attrsetGet key aset
473474
475+ unsafeDiscardOutputDependencyNix
476+ :: forall e t f m
477+ . MonadNix e t f m
478+ => NValue t f m
479+ -> m (NValue t f m )
480+ unsafeDiscardOutputDependencyNix nv =
481+ do
482+ (nc, ns) <- (getStringContext &&& ignoreContext) <$> fromValue nv
483+ toValue $ mkNixString (HS. map discard nc) ns
484+ where
485+ discard :: StringContext -> StringContext
486+ discard (StringContext AllOutputs a) = StringContext DirectPath a
487+ discard x = x
488+
474489unsafeGetAttrPosNix
475490 :: forall e t f m
476491 . MonadNix e t f m
@@ -870,6 +885,42 @@ builtinsBuiltinNix
870885 => m (NValue t f m )
871886builtinsBuiltinNix = throwError $ ErrorCall " HNix does not provide builtins.builtins at the moment. Using builtins directly should be preferred"
872887
888+ -- a safer version of `attrsetGet`
889+ attrGetOr
890+ :: forall e t f m v a
891+ . (MonadNix e t f m , FromValue v m (NValue t f m ))
892+ => a
893+ -> (v -> m a )
894+ -> VarName
895+ -> AttrSet (NValue t f m )
896+ -> m a
897+ attrGetOr fallback fun name attrs =
898+ maybe
899+ (pure fallback)
900+ (fun <=< fromValue)
901+ (M. lookup name attrs)
902+
903+
904+ -- NOTE: It is a part of the implementation taken from:
905+ -- https://github.com/haskell-nix/hnix/pull/755
906+ -- look there for `sha256` and/or `filterSource`
907+ pathNix :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m )
908+ pathNix arg =
909+ do
910+ attrs <- fromValue @ (AttrSet (NValue t f m )) arg
911+ path <- fmap (coerce . toString) $ fromStringNoContext =<< coerceToPath =<< attrsetGet " path" attrs
912+
913+ -- TODO: Fail on extra args
914+ -- XXX: This is a very common pattern, we could factor it out
915+ name <- toText <$> attrGetOr (takeFileName path) (fmap (coerce . toString) . fromStringNoContext) " name" attrs
916+ recursive <- attrGetOr True pure " recursive" attrs
917+
918+ Right (coerce . toText . coerce @ StorePath @ String -> s) <- addToStore name path recursive False
919+ -- TODO: Ensure that s matches sha256 when not empty
920+ pure $ mkNVStr $ mkNixStringWithSingletonContext (StringContext DirectPath s) s
921+ where
922+ coerceToPath = coerceToString callFunc DontCopyToStore CoerceAny
923+
873924dirOfNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m )
874925dirOfNix nvdir =
875926 do
@@ -1886,7 +1937,7 @@ builtinsList =
18861937 , add0 Normal " null" (pure nvNull)
18871938 , add Normal " parseDrvName" parseDrvNameNix
18881939 , add2 Normal " partition" partitionNix
1889- -- , add Normal "path" path
1940+ , add Normal " path" pathNix
18901941 , add Normal " pathExists" pathExistsNix
18911942 , add Normal " readDir" readDirNix
18921943 , add Normal " readFile" readFileNix
@@ -1908,7 +1959,7 @@ builtinsList =
19081959 , add0 Normal " true" (pure $ mkNVBool True )
19091960 , add Normal " tryEval" tryEvalNix
19101961 , add Normal " typeOf" typeOfNix
1911- -- , add0 Normal "unsafeDiscardOutputDependency" unsafeDiscardOutputDependency
1962+ , add Normal " unsafeDiscardOutputDependency" unsafeDiscardOutputDependencyNix
19121963 , add Normal " unsafeDiscardStringContext" unsafeDiscardStringContextNix
19131964 , add2 Normal " unsafeGetAttrPos" unsafeGetAttrPosNix
19141965 , add Normal " valueSize" getRecursiveSizeNix
0 commit comments