snack icon indicating copy to clipboard operation
snack copied to clipboard

Type Nix through haskell

Open nmattia opened this issue 6 years ago • 5 comments

Most of snack's flakiness comes from the fact that the logic is written in Nix - which is untyped and doesn't really have test frameworks.

An idea I played with a while ago was to wrap Nix with haskell types: https://github.com/nmattia/nix-reloaded

It provides a nice way to write Nix by lifting all expressions to typed haskell values, e.g.

https://github.com/nmattia/nix-reloaded/blob/82489bd9d58e21cc4611b2c00c757a859f5fe70a/Test.hs#L16-L21

test1 :: IO ()
test1 = do
    x <- withNixRepl $ \nixRepl -> do
        seven <- bindVar nixRepl (3 + 4)
        eval nixRepl $ Pkgs.map (+ seven) (toNix [1 .. 10])
    x @=? ((+ 7) <$> [1 .. 10] :: [Int])

an expression like builtins.map would be lifted to a haskell value of type (Var a -> Var b) -> Var [a] -> Var [b] which can then be used and applied like any other haskell function.

nmattia avatar Jun 12 '19 21:06 nmattia

What do you think about moving more of the logic to Haskell directly in addition to this? For example hpack.nix?

smatting avatar Jun 13 '19 10:06 smatting

One of the goals of snack is to allow the user to run nix-build and everything should work. If we push some of the logic to haskell that means that, somehow, the nix build needs to first call something like snack dump-build-procedure > foo.nix and then do some import from derivation. If we manage to get everything nice and typed in nix-reloaded then we can do snack dump-nix-files > lib.nix and just upload lib.nix on GitHub, and everyone else can just use that.

nmattia avatar Jun 13 '19 10:06 nmattia

Ah, I see! The build process should not even depend on snack itself, only nix. So this is basically goal 1 mentioned in the readme, but more explicit. And I guess the output of snack dump-nix-files should not depend on the project at all too, correct? (EDIT: well except on the choice of which config files to use)

smatting avatar Jun 13 '19 11:06 smatting

FWIW there's an existing https://github.com/regnat/ptyx project that attempts to add a type system to Nix.

masaeedu avatar Jun 19 '19 09:06 masaeedu

Last time I talked to @regnat he mentioned it wasn't looking really good. However @jwiegley mentioned there's a rudimentary type system in https://github.com/haskell-nix/hnix, so that could work.

nmattia avatar Jun 19 '19 12:06 nmattia