Skip to content

Commit fb2d5f8

Browse files
(#1032) Builtins: add path & unsafeDiscardOutputDependency
2 parents 6ce1fb1 + cce482f commit fb2d5f8

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/Nix/Builtins.hs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import Data.Char ( isDigit )
4646
import Data.Foldable ( foldrM )
4747
import Data.Fix ( foldFix )
4848
import Data.List ( partition )
49+
import qualified Data.HashSet as HS
4950
import qualified Data.HashMap.Lazy as M
5051
import Data.Scientific
5152
import 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+
474489
unsafeGetAttrPosNix
475490
:: forall e t f m
476491
. MonadNix e t f m
@@ -870,6 +885,42 @@ builtinsBuiltinNix
870885
=> m (NValue t f m)
871886
builtinsBuiltinNix = 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+
873924
dirOfNix :: MonadNix e t f m => NValue t f m -> m (NValue t f m)
874925
dirOfNix 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

tests/NixLanguageTests.hs

Lines changed: 2 additions & 3 deletions
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"
@@ -74,7 +73,7 @@ deprecatedRareNixQuirkTests :: Set String
7473
deprecatedRareNixQuirkTests = Set.fromList
7574
[ -- A rare quirk of Nix that is proper to fix&enforce then to support (see git commit history)
7675
"eval-okay-strings-as-attrs-names"
77-
-- Nix upstream removed this test alltogather
76+
-- Nix upstream removed this test altogether
7877
, "eval-okay-hash"
7978
]
8079

0 commit comments

Comments
 (0)