Building haskell projects using haskell.nix and flakes

So I’m trying to build a haskell project locally through nix using flakes and haskell.nix.

{
  description = "Package build for lastpass-tui";
  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  inputs.haskellNix.url = "github:input-output-hk/haskell.nix/master";
  outputs = { self, haskellNix, nixpkgs }: {
    # setup derivation for x86_64-linux
    defaultPackage.x86_64-linux =
      let pkgs = haskellNix.legacyPackages."x86_64-linux";
      drv = pkgs.haskell-nix.project {
        src = pkgs.haskell-nix.haskellLib.cleanGit {
          name = "lastpass-tui";
          src = ./.;
        };
        compiler-nix-name = "ghc884";
      };
      in drv.lastpass-tui.components.exes.lpt;
  };
}

However I’m running into an error. Whenever I try to nix build I get the error attribute 'components' missing in drv.components.exes.lpt. According to the haskell.nix docs you’re supposed to run the command nix-build -A your-package-name.components.exes.your-exe-name. So I’ve tried to access the your-exe-name attribute directly inside the flake for the defaultPackage.x86_64-linux attribute, but nix build fails with the previously mentioned attribute error.Perhaps it’s possible to pass some args to nix build like they’ve done with nix-build in the docs, but the --attr / -A flag no longer exists. nix build .#lastpass-tui.components.exes.lpt didn’t work either and fails with the error

flake 'git+file:///home/skykanin/Projects/lastpass-tui' does not provide attribute 'packages.x86_64-linux.lastpass-tui.components.exes.lpt', 'legacyPackages.x86_64-linux.lastpass-tui.components.exes.lpt' or 'lastpass-tui.components.exes.lpt'

So I’m not sure how to translate the example in the docs to nix flakes.

EDIT: I’ve now tried building the project using haskell.nix, but this time without the flake just following the docs. Running nix-build -A your-package-name.components.exes.your-exe-name works just fine so the trouble here is just translating this to the flake.

1 Like

Hi, the Haskell.nix flake is quite a recent development, so may not be fully documented and working.

I haven’t tried building your project, or any Haskell.nix flake for that matter, but going by the error message, perhaps this change would help:

diff --git a/flake.nix b/flake.nix
index a655dac..db777c1 100644
--- a/flake.nix
+++ b/flake.nix
@@ -13,6 +13,6 @@
         };
         compiler-nix-name = "ghc884";
       };
-      in drv.lastpass-tui.components.exes.lpt;
+      in drv.lastpass-tui;
   };
 }

Hi, using that patch I get this error instead flake output attribute 'defaultPackage.x86_64-linux' is not a derivation

do you have cabal.project in your folder? and has with-compiler in it?

I haven’t continue my project with flakes + haskell.nix, but based on my last attempt, my package is built fine (the error because I have 2 separate folder and cabal.project cannot detect ./../../ path). Here is my setup:

pkgs.haskell-nix.cabalProject {
  projectFileName = "cabal.project";
  src = pkgs.haskell-nix.haskellLib.cleanGit {
    name = "todo-haskell";
    src  = ../../backend/haskell;
  };
  compiler-nix-name = "ghc8102";
  index-state = "2020-11-10T00:00:00Z";
#   plan-sha256 = sha256;

  cabalProject =
    builtins.replaceStrings
      [ "with-compiler" ]
      [ "-- with-compiler" ]
      (builtins.readFile ../../backend/haskell/cabal.project);

  modules = [{
    # smaller files
    packages.todo-haskell.dontStrip = false;
  }]; 
}

Note that in cabalProject, we need to make with-compiler a comment. I forget where I read it, but it says that haskell.nix isn’t get along with the with-compiler