Use writeHaskellBin with path to source

From reading the source of makeBinWriter i think it should be possible to use a path to my source.

I have the following in my nixos configuration

 environment.systemPackages = [
    (pkgs.writers.writeHaskellBin "scan" { libraries = [ pkgs.haskellPackages.turtle ]; } ./scripts/Scan.hs)
  ];

When i try to build it i got

> sudo nixos-rebuild test --flake ~/nixosconfiguration
error: builder for '/nix/store/a0rvpac36snk75172fbpam9qz4445g0d-scan.drv' failed with exit code 1;
       last 2 log lines:
       > [1 of 1] Compiling Scan             ( tmp.hs, tmp.o )
       > mv: cannot stat 'tmp': No such file or directory
       For full logs, run 'nix log /nix/store/a0rvpac36snk75172fbpam9qz4445g0d-scan.drv'.

When I write the source directly as a string it works. For examle

  environment.systemPackages = [
    (pkgs.writers.writeHaskellBin "scan" { libraries = [ pkgs.haskellPackages.turtle ]; } ''
        main = print "hi"
     ''
    )
  ];

I wasn’t able to reproduce this error. On my system:

$ nix-build -E 'with import <nixpkgs> {}; writers.writeHaskellBin "scan" { libraries = [ haskellPackages.turtle ]; } /path/to/my/haskell/file.hs'
/nix/store/92nps5j9ahfa0v7n8gb01ichas9y4pv5-scan

I’m on a recent nixos-21.05 commit.

Here’s a couple things you can check:

  1. Are you sure ./scripts/Scan.hs has been committed to your repo? If I remember correctly, weird things happen with flakes if you don’t have everything committed.
  2. Stop using flakes and try with normal nixos-rebuild and channels.
  3. Instead of directly passing a path, try doing builtins.readFile ./scripts/Scan.hs.

I’ve tested another simple Haskell program and it works. Seems to be a problem with my Haskell code. I’m just a little confused, because the error doesn’t look like a Haskell error.

Also running

nix develop
runhaskell scripts/Scan.hs

works

I found the proplem.

I had

module Scan where
...

With

module Main where

it works

1 Like