[Haskell] How to override cabal flags?

I want to try gloss with glfw enabled.

I have no idea how haskellPackages.gloss is build but from the source it seem to be auto-generated.

I am looking for global override config.nix solution or the way to specify the flag in my gloss project’s default.nix (generated using cabal2nix)

{ mkDerivation, base, gloss, stdenv }:
mkDerivation {
  pname = "mapview";
  version = "0.1.0.0";
  src = ./.;
  isLibrary = true;
  isExecutable = true;
  libraryHaskellDepends = [ base ];
  executableHaskellDepends = [ base gloss ];
  doHaddock = false;
  license = stdenv.lib.licenses.mit;
}

I’ve try

# ~/.config/nixpkgs/config.nix

packageOverrides = super: {

    haskellPackages = super.haskellPackages // {

      gloss = super.haskellPackages.gloss.overrideAttrs (oldAttrs: rec {
        buildInputs = [haskellPackages.GLFW-b];
        configureFlags = [
          "-f glfw"
          "-f -glut"
        ];
      });

};

but it failed with mismatch GLFW-b dependency version when I try to build with nix-shell -p haskellPackages.gloss

Setup: Encountered missing dependencies:
GLFW-b >=1.4.1.0 && <2

However my project doesn’t seem to pick the override-ed gloss and still using the default nixpkgs one (it not failed to build)

1 Like
configureFlags = [
  "--ghc-option=-optl=-static"
  "--ghc-option=-optl=-pthread"
  "--ghc-option=-optl=-L${pkgs.gmp6.override { withStatic = true; }}/lib"
  "--ghc-option=-optl=-L${pkgs.zlib.static}/lib"
  "--ghc-option=-optl=-L${pkgs.glibc.static}/lib"
];

Exemple Building Static Haskell Binaries with Nix - Vaibhav Sagar

Exemple (default.nix) :

with import <nixpkgs> {};

let
  drv = (haskellPackages.override {
    overrides = haskellPackagesNew: haskellPackgesOld: rec {
      gloss = pkgs.haskell.lib.appendConfigureFlags haskellPackgesOld.gloss ["-f glfw" "-f -glut"];
    };
  }).callCabal2nix "labo-gloss" ./. {};
in if lib.inNixShell then drv.env.overrideAttrs (old: {
  buildInputs = old.buildInputs ++ [ haskellPackages.ghcid cabal-install ];
}) else drv
2 Likes

It seem like that specific GLFW-b version is missing on my current channel.

I’ve solve my problem by using cabal2nix to generate nix expression for GLFW-b and gloss

$ cabal2nix cabal://gloss > gloss.nix
$ cabal2nix cabal://GLFW-b-1.4.8.4 > GLFW-b.nix
$ cabal2nix . > myproject.nix

and then edit the default.nix

# default.nix
{ nixpkgs ? import <nixpkgs> {}, compiler ? "default" }:

let
  inherit (nixpkgs) pkgs;

  haskellPackages = if compiler == "default"
                       then pkgs.haskellPackages
                       else pkgs.haskell.packages.${compiler};

  GLFW-b = haskellPackages.callPackage ./GLFW-b.nix {};
  modified-GLFW-b = pkgs.haskell.lib.overrideCabal GLFW-b (drv: {
    doCheck = false;
  });

  gloss = haskellPackages.callPackage ./gloss.nix {GLFW-b = modified-GLFW-b;};
  modified-gloss = pkgs.haskell.lib.overrideCabal gloss (drv: {
    configureFlags = [ "-fglfw" "-fexplicitbackend" ];
  });

  prj = haskellPackages.callPackage ./myproject.nix {gloss = modified-gloss;};

in
  prj

also for the completeness sake, I also add additional tools to my shell in shell.nix

# shell.nix
{ nixpkgs ? import <nixpkgs> {}, compiler ? "default"}:
let
  inherit (nixpkgs) pkgs;
  drv = import ./default.nix { inherit nixpkgs compiler; };
  drvWithTools = pkgs.haskell.lib.addBuildDepends drv
    (with pkgs; with haskellPackages; [
      cabal-install
      ghcid
    ]);
in
  if pkgs.lib.inNixShell then drvWithTools.env else drvWithTools
1 Like
2 Likes