Haskell.nix and envvars (in a flake)

For various reasons I have the situation that the values of a few envvars must be a embedded in the output during a CI build. Doing this when using developPackage in nixpkgs is straight forward using the modifiers argument, but I’m struggling with finding a way to do this using haskell.nix.

Added to this is the somewhat added complication of having to do this in a flake.

What I have so far is basically what’s found in the docs.

  • an overlay of the project with some tools

    appOverlay = final: _prev: {
      project = final.haskell-nix.project {
        compiler-nix-name = "ghc96";
        src = lib.sourceFilesBySuffices ./. [ ".cabal" ".hs" "LICENSE" ];
        shell.tools = {
          cabal = "latest";
          cabal-fmt = "latest";
          haskell-language-server = "latest";
          hlint = "latest";
          implicit-hie = "latest";
          ormolu = "latest";
        };
      };
    };
    
  • a list of overlays

    overlays = [ haskell-nix.overlay appOverlay ];
    
  • definition of pkags and flake

    pkgs = import nixpkgs {
      inherit system overlays;
      inherit (haskell-nix) config;
    };
    flake = pkgs.project.flake { };
    
  • For some reason I’m having problems using the full flake like in the example, so instead I’ve defined explicitly what I want like this (this is a slightly shortened version)

    in {
      proj.pkg = flake.packages."foo:exe:bar";
      proj.shell = flake.devShell;
    
      packages.default = self.proj.${system}.pkg;
      devShells.default = self.proj.${system}.shell;
    });
    

I know that flake.packages."foo:exe:bar" is a derivation and that I can use its overrideAttrs to inject the envvars I want. That’s not quite enough though as the package comprices both an executable and a library, and as haskell.nix creates a package for each I need to inject the envvars in both.

Am I missing something in the documentation for haskell.nix, or is there a common pattern for doing this that I haven’t learned yet?

1 Like