@@ -885,6 +885,50 @@ builtinsBuiltinNix
885885 => m (NValue t f m )
886886builtinsBuiltinNix = throwError $ ErrorCall " HNix does not provide builtins.builtins at the moment. Using builtins directly should be preferred"
887887
888+ attrGetOr'
889+ :: forall e t f m v a
890+ . (MonadNix e t f m , FromValue v m (NValue t f m ))
891+ => AttrSet (NValue t f m )
892+ -> VarName
893+ -> m a
894+ -> (v -> m a )
895+ -> m a
896+ attrGetOr' attrs n d f =
897+ maybe
898+ d
899+ (f <=< fromValue)
900+ (M. lookup n attrs)
901+
902+ attrGetOr
903+ :: forall e t f m v a
904+ . (MonadNix e t f m , FromValue v m (NValue t f m ))
905+ => AttrSet (NValue t f m )
906+ -> VarName
907+ -> a
908+ -> (v -> m a )
909+ -> m a
910+ attrGetOr attrs name fallback = attrGetOr' attrs name (pure fallback)
911+
912+ -- NOTE: It is a part of the implementation taken from:
913+ -- https://github.com/haskell-nix/hnix/pull/755
914+ -- look there for `sha256` and/or `filterSource`
915+ pathNix :: forall e t f m . MonadNix e t f m => NValue t f m -> m (NValue t f m )
916+ pathNix arg =
917+ do
918+ attrs <- fromValue @ (AttrSet (NValue t f m )) arg
919+ path <- fmap (coerce . toString) $ fromStringNoContext =<< coerceToPath =<< attrsetGet " path" attrs
920+
921+ -- TODO: Fail on extra args
922+ -- XXX: This is a very common pattern, we could factor it out
923+ name <- toText <$> attrGetOr attrs " name" (takeFileName path) (fmap (coerce . toString) . fromStringNoContext)
924+ recursive <- attrGetOr attrs " recursive" True pure
925+
926+ Right (coerce . toText . coerce @ StorePath @ String -> s) <- addToStore name path recursive False
927+ -- TODO: Ensure that s matches sha256 when not empty
928+ pure $ mkNVStr $ mkNixStringWithSingletonContext (StringContext DirectPath s) s
929+ where
930+ coerceToPath = coerceToString callFunc DontCopyToStore CoerceAny
931+
888932dirOfNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m )
889933dirOfNix nvdir =
890934 do
@@ -1899,7 +1943,7 @@ builtinsList =
18991943 , add0 Normal " null" (pure nvNull)
19001944 , add Normal " parseDrvName" parseDrvNameNix
19011945 , add2 Normal " partition" partitionNix
1902- -- , add Normal "path" path
1946+ , add Normal " path" pathNix
19031947 , add Normal " pathExists" pathExistsNix
19041948 , add Normal " readDir" readDirNix
19051949 , add Normal " readFile" readFileNix
0 commit comments