Skip to content

Commit 70a8e14

Browse files
Merge #942: Quickfix for #941 & refactors
* `Nix.Type.Env`: * added instances: * `Env`: `{Semigroup,Monoid,One}` * `Nix`: * changed argument order: * `nixEval`: ```haskell -- was: => Maybe FilePath -> Transform g (m a) -> Alg g (m a) -> Fix g -> m a -- became: => Transform g (m a) -> Alg g (m a) -> Maybe FilePath -> Fix g -> m a ``` * `Nix.Normal` * add `thunkVal` literal & use it where appropriate `{deThunk, removeEffects}` * rename `opaque(,->Val)`, indicate that it is a literal.
2 parents 99a38c4 + f275508 commit 70a8e14

File tree

5 files changed

+57
-42
lines changed

5 files changed

+57
-42
lines changed

ChangeLog.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,25 @@ Breaking:
2121
* `Nix.Types.Assumption`:
2222
* added instances:
2323
* `Assumption`: `{Semigroup,Monoid,One}`
24+
2425
* `Nix.Type.Env`:
2526
* added instances:
2627
* `Env`: `{Semigroup,Monoid,One}`
28+
* `Nix`:
29+
* changed argument order:
30+
* `nixEval`:
31+
32+
```haskell
33+
-- was:
34+
=> Maybe FilePath -> Transform g (m a) -> Alg g (m a) -> Fix g -> m a
35+
-- became:
36+
=> Transform g (m a) -> Alg g (m a) -> Maybe FilePath -> Fix g -> m a
37+
```
38+
39+
* `Nix.Normal`
40+
* add `thunkVal` literal & use it where appropriate `{deThunk, removeEffects}`
41+
* rename `opaque(,->Val)`, indicate that it is a literal.
42+
2743

2844

2945
### [(diff)](https://github.com/haskell-nix/hnix/compare/0.13.0.1...0.13.1#files_bucket) 0.13.1 (2021-05-22)

main/Main.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ main =
157157
printer
158158
| finder opts = findAttrs <=< fromValue @(AttrSet (StdValue (StandardT (StdIdT IO))))
159159
| xml opts = liftIO . Text.putStrLn . stringIgnoreContext . toXML <=< normalForm
160-
| json opts = liftIO . Text.putStrLn . stringIgnoreContext <=< nvalueToJSONNixString
160+
-- 2021-05-27: NOTE: With naive fix of the #941
161+
-- This is overall a naive printer implementation, as options should interact/respect one another.
162+
-- A nice question: "Should respect one another to what degree?": Go full combinator way, for which
163+
-- old Nix CLI is nototrious for (and that would mean to reimplement the old Nix CLI),
164+
-- OR: https://github.com/haskell-nix/hnix/issues/172 and have some sane standart/default behaviour for (most) keys.
165+
| json opts = liftIO . Text.putStrLn . stringIgnoreContext <=< nvalueToJSONNixString <=< normalForm
161166
| strict opts = liftIO . print . prettyNValue <=< normalForm
162167
| values opts = liftIO . print . prettyNValueProv <=< removeEffects
163168
| otherwise = liftIO . print . prettyNValue <=< removeEffects

src/Nix.hs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,20 @@ import Nix.XML
5555
-- transformations, allowing them to be easily composed.
5656
nixEval
5757
:: (MonadNix e t f m, Has e Options, Functor g)
58-
=> Maybe FilePath
59-
-> Transform g (m a)
58+
=> Transform g (m a)
6059
-> Alg g (m a)
60+
-> Maybe FilePath
6161
-> Fix g
6262
-> m a
63-
nixEval mpath xform alg = withNixContext mpath . adi alg xform
63+
nixEval transform alg mpath = withNixContext mpath . adi alg transform
6464

6565
-- | Evaluate a nix expression in the default context
6666
nixEvalExpr
6767
:: (MonadNix e t f m, Has e Options)
6868
=> Maybe FilePath
6969
-> NExpr
7070
-> m (NValue t f m)
71-
nixEvalExpr mpath = nixEval mpath id Eval.eval
71+
nixEvalExpr = nixEval id Eval.eval
7272

7373
-- | Evaluate a nix expression in the default context
7474
nixEvalExprLoc
@@ -77,9 +77,8 @@ nixEvalExprLoc
7777
=> Maybe FilePath
7878
-> NExprLoc
7979
-> m (NValue t f m)
80-
nixEvalExprLoc mpath =
80+
nixEvalExprLoc =
8181
nixEval
82-
mpath
8382
Eval.addMetaInfo
8483
Eval.evalContent
8584

@@ -126,7 +125,7 @@ evaluateExpression mpath evaluator handler expr =
126125

127126
eval' = normalForm <=< nixEvalExpr mpath
128127

129-
argmap args = nvSet mempty (M.fromList args)
128+
argmap args = nvSet mempty $ M.fromList args
130129

131130
processResult
132131
:: forall e t f m a
@@ -135,34 +134,26 @@ processResult
135134
-> NValue t f m
136135
-> m a
137136
processResult h val = do
138-
opts :: Options <- asks (view hasLens)
137+
opts :: Options <- asks $ view hasLens
139138
maybe
140139
(h val)
141-
(\ (Text.splitOn "." -> keys) -> go keys val)
140+
(\ (Text.splitOn "." -> keys) -> processKeys keys val)
142141
(attr opts)
143142
where
144-
go :: [Text] -> NValue t f m -> m a
145-
go [] v = h v
146-
go ((Text.decimal -> Right (n,"")) : ks) v =
147-
(\case
148-
NVList xs ->
149-
list
150-
h
151-
go
152-
ks
153-
(xs !! n)
154-
_ -> errorWithoutStackTrace $ "Expected a list for selector '" <> show n <> "', but got: " <> show v
155-
) =<< demand v
156-
go (k : ks) v =
157-
(\case
158-
NVSet xs _ ->
159-
maybe
160-
(errorWithoutStackTrace $ toString $ "Set does not contain key '" <> k <> "'")
161-
(list
162-
h
163-
go
164-
ks
165-
)
166-
(M.lookup k xs)
167-
_ -> errorWithoutStackTrace $ toString $ "Expected a set for selector '" <> k <> "', but got: " <> show v
168-
) =<< demand v
143+
processKeys :: [Text] -> NValue t f m -> m a
144+
processKeys kys v =
145+
list
146+
(h v)
147+
(\ (k : ks) ->
148+
do
149+
v' <- demand v
150+
case (k, v') of
151+
(Text.decimal -> Right (n,""), NVList xs) -> processKeys ks $ xs !! n
152+
(_, NVSet xs _) ->
153+
maybe
154+
(errorWithoutStackTrace $ toString $ "Set does not contain key '" <> k <> "'")
155+
(processKeys ks)
156+
(M.lookup k xs)
157+
(_, _) -> errorWithoutStackTrace $ toString $ "Expected a set or list for selector '" <> k <> "', but got: " <> show v
158+
)
159+
kys

src/Nix/Normal.hs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ normalForm_
140140
-> m ()
141141
normalForm_ t = void $ normalizeValue t
142142

143+
opaqueVal :: Applicative f => NValue t f m
144+
opaqueVal = nvStr $ makeNixStringWithoutContext "<cycle>"
145+
143146
-- | Detect cycles & stub them.
144147
stubCycles
145148
:: forall t f m
@@ -161,7 +164,10 @@ stubCycles =
161164
)
162165
Free
163166
where
164-
Free (NValue' cyc) = opaque
167+
Free (NValue' cyc) = opaqueVal
168+
169+
thunkVal :: Applicative f => NValue t f m
170+
thunkVal = nvStr $ makeNixStringWithoutContext "<thunk>"
165171

166172
removeEffects
167173
:: (MonadThunk t m (NValue t f m), MonadDataContext f m)
@@ -171,14 +177,11 @@ removeEffects =
171177
iterNValueM
172178
id
173179
-- 2021-02-25: NOTE: Please, unflip this up the stack
174-
(\ t f -> f =<< queryM (pure opaque) t)
180+
(\ t f -> f =<< queryM (pure thunkVal) t)
175181
(fmap Free . sequenceNValue' id)
176182

177-
opaque :: Applicative f => NValue t f m
178-
opaque = nvStr $ makeNixStringWithoutContext "<cycle>"
179-
180183
dethunk
181184
:: (MonadThunk t m (NValue t f m), MonadDataContext f m)
182185
=> t
183186
-> m (NValue t f m)
184-
dethunk = removeEffects <=< queryM (pure opaque)
187+
dethunk = removeEffects <=< queryM (pure thunkVal)

src/Nix/Render/Frame.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ renderValue
206206
-> NValue t f m
207207
-> m (Doc ann)
208208
renderValue _level _longLabel _shortLabel v = do
209-
opts :: Options <- asks (view hasLens)
209+
opts :: Options <- asks $ view hasLens
210210
bool
211211
prettyNValue
212212
prettyNValueProv

0 commit comments

Comments
 (0)