Skip to content

Commit 5304ea0

Browse files
committed
Builtins: add path
Courtesy of `layus`, ported from implementation in #755.
1 parent 49bce28 commit 5304ea0

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/Nix/Builtins.hs

+45-1
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,50 @@ builtinsBuiltinNix
885885
=> m (NValue t f m)
886886
builtinsBuiltinNix = 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+
path :: forall e t f m. MonadNix e t f m => NValue t f m -> m (NValue t f m)
916+
path 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+
888932
dirOfNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
889933
dirOfNix 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" path
19031947
, add Normal "pathExists" pathExistsNix
19041948
, add Normal "readDir" readDirNix
19051949
, add Normal "readFile" readFileNix

tests/NixLanguageTests.hs

+2-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ groupBy key = Map.fromListWith (<>) . fmap (key &&& pure)
5656
-- previously passed.
5757
newFailingTests :: Set String
5858
newFailingTests = Set.fromList
59-
[ "eval-okay-path" -- #128
60-
, "eval-okay-fromTOML"
59+
[ "eval-okay-fromTOML"
6160
, "eval-okay-zipAttrsWith"
6261
, "eval-okay-tojson"
6362
, "eval-okay-search-path"
@@ -75,7 +74,7 @@ deprecatedRareNixQuirkTests :: Set String
7574
deprecatedRareNixQuirkTests = Set.fromList
7675
[ -- A rare quirk of Nix that is proper to fix&enforce then to support (see git commit history)
7776
"eval-okay-strings-as-attrs-names"
78-
-- Nix upstream removed this test alltogather
77+
-- Nix upstream removed this test altogether
7978
, "eval-okay-hash"
8079
]
8180

0 commit comments

Comments
 (0)