cabal2nix icon indicating copy to clipboard operation
cabal2nix copied to clipboard

Best way to automate the addition of a custom shellHook?

Open bbarker opened this issue 5 years ago • 1 comments

I was looking at the generated nix expression when using the --shell option, and while it is probably possible to generate a script to patch in a custom shellHook, I'm not seeing a simple way to do it. Possible solutions:

  1. add in a --hook option that takes a file containing the contents of the shellhook and adds it in.
  2. put in an empty shellHook that acts as an anchor for the user to write their own patch script more easily (I don't really like this option).
  3. probably the best option, something obvious that I missed but don't know about :-)

bbarker avatar Sep 08 '20 00:09 bbarker

I don't think it is wise to extend --shell so it could generate every possible shell the user could since that would add a lot of complexity to the tool. Additionally --shell is not the method for getting a development shell I'd necessarily recommend generally, since it is ultimately flawed since it is based on the nix builder's environment (so doesn't include cabal-install by default for example).

What I'd generally recommend is to use the normal expression generated by cabal2nix (without the --shell) and use overriding to get the rest:

$ cabal2nix . > pkg.nix
$ nix-shell shell.nix

where shell.nix would be something like:

{ pkgs ? import <nixpkgs> { } }:

let
  haskellPackages = pkgs.haskellPackages.override {
    overrides = self: super: {
      my-package = self.callPackage ./pkg.nix { };
    };
  };
in

haskellPackages.shellFor {
  packages = p: [ p.my-package ];
  buildInputs = [
    # extra build tools
  ];
  shellHook = "echo hello";
}

As you can see, the generated file keeps getting generated and all custom settings live in a separate file.

Unfortunately documentation on all used parts is still lacking which I pledge to work on improving soon :)

In short: I think supporting all permutations you could want for --shell is not really feasible and supporting an arbitrary subset of those is also not great.

sternenseemann avatar Jun 16 '21 16:06 sternenseemann