Skip to content

Commit

Permalink
Builtins: add path
Browse files Browse the repository at this point in the history
Courtesy of `layus`, ported from implementation in
haskell-nix#755.
  • Loading branch information
Anton-Latukha committed Jan 17, 2022
1 parent 49bce28 commit 5304ea0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
46 changes: 45 additions & 1 deletion src/Nix/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,50 @@ builtinsBuiltinNix
=> m (NValue t f m)
builtinsBuiltinNix = throwError $ ErrorCall "HNix does not provide builtins.builtins at the moment. Using builtins directly should be preferred"

attrGetOr'
:: forall e t f m v a
. (MonadNix e t f m, FromValue v m (NValue t f m))
=> AttrSet (NValue t f m)
-> VarName
-> m a
-> (v -> m a)
-> m a
attrGetOr' attrs n d f =
maybe
d
(f <=< fromValue)
(M.lookup n attrs)

attrGetOr
:: forall e t f m v a
. (MonadNix e t f m, FromValue v m (NValue t f m))
=> AttrSet (NValue t f m)
-> VarName
-> a
-> (v -> m a)
-> m a
attrGetOr attrs name fallback = attrGetOr' attrs name (pure fallback)

-- NOTE: It is a part of the implementation taken from:
-- https://github.com/haskell-nix/hnix/pull/755
-- look there for `sha256` and/or `filterSource`
path :: forall e t f m. MonadNix e t f m => NValue t f m -> m (NValue t f m)
path arg =
do
attrs <- fromValue @(AttrSet (NValue t f m)) arg
path <- fmap (coerce . toString) $ fromStringNoContext =<< coerceToPath =<< attrsetGet "path" attrs

-- TODO: Fail on extra args
-- XXX: This is a very common pattern, we could factor it out
name <- toText <$> attrGetOr attrs "name" (takeFileName path) (fmap (coerce . toString) . fromStringNoContext)
recursive <- attrGetOr attrs "recursive" True pure

Right (coerce . toText . coerce @StorePath @String -> s) <- addToStore name path recursive False
-- TODO: Ensure that s matches sha256 when not empty
pure $ mkNVStr $ mkNixStringWithSingletonContext (StringContext DirectPath s) s
where
coerceToPath = coerceToString callFunc DontCopyToStore CoerceAny

dirOfNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
dirOfNix nvdir =
do
Expand Down Expand Up @@ -1899,7 +1943,7 @@ builtinsList =
, add0 Normal "null" (pure nvNull)
, add Normal "parseDrvName" parseDrvNameNix
, add2 Normal "partition" partitionNix
--, add Normal "path" path
, add Normal "path" path
, add Normal "pathExists" pathExistsNix
, add Normal "readDir" readDirNix
, add Normal "readFile" readFileNix
Expand Down
5 changes: 2 additions & 3 deletions tests/NixLanguageTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ groupBy key = Map.fromListWith (<>) . fmap (key &&& pure)
-- previously passed.
newFailingTests :: Set String
newFailingTests = Set.fromList
[ "eval-okay-path" -- #128
, "eval-okay-fromTOML"
[ "eval-okay-fromTOML"
, "eval-okay-zipAttrsWith"
, "eval-okay-tojson"
, "eval-okay-search-path"
Expand All @@ -75,7 +74,7 @@ deprecatedRareNixQuirkTests :: Set String
deprecatedRareNixQuirkTests = Set.fromList
[ -- A rare quirk of Nix that is proper to fix&enforce then to support (see git commit history)
"eval-okay-strings-as-attrs-names"
-- Nix upstream removed this test alltogather
-- Nix upstream removed this test altogether
, "eval-okay-hash"
]

Expand Down

0 comments on commit 5304ea0

Please sign in to comment.