How to execute a script once during the shell build?

I’m trying to add nix to a project that I’m working on and so far this is the state of the shell.nix:

let
  pkgs = import <nixpkgs> { };
  kubefwd = pkgs.buildGoModule rec {
    pname = "kubefwd";
    owner = "txn2";
    version = "1.14.7";

    src = pkgs.fetchFromGitHub {
      owner = "${owner}";
      repo = "${pname}";
      rev = "${version}";
      sha256 = "1dx3m08gazmljpvwnivw1rdsd46vk55jidxs9gwc71dyzi4m77hc";
    };

    vendorSha256 = "1pg4sh09rndxpqn7v5f597m9k9xxmnryzag609j172dsrhzv6ppj";
    buildFlags = ["--trimpath"];
    subPackages = ["cmd/kubefwd"];

    meta = with pkgs.lib; {
      homepage = "https://github.com/${owner}/${pname}";
      platforms = platforms.linux ++ platforms.darwin;
    };
  };
in pkgs.mkShell rec {
  buildInputs = [
    kubefwd
    pkgs.go
    pkgs.wget
    pkgs.awscli
    pkgs.jq
    pkgs.git-secrets
  ];
}

I’m using nix with direnv. There is just one thing left here, I would like to execute a script during the setup, it’s possible?

The idea is to run a make target to configure the repository. The most important task of this configuration is the git-secrets setup that needs to be executed at the git repository in order to install the git hooks.

1 Like

you’re looking for shellHook

pkgs.mkShell rec {
  buildInputs = [
    kubefwd
    pkgs.go
    pkgs.wget
    pkgs.awscli
    pkgs.jq
    pkgs.git-secrets
  ];

  shellHook = ''
    echo Hi
  '';
}
4 Likes

Thanks for the answer @jonringer! shellHook actually solves my problem. One question left, there is other kinds of hooks that I can choose from?

With the shellHook I need to write extra commands to verify if the thing that I want to do has already been done. Maybe a postBuildHook or something like this would be ideal for me.

1 Like

if there are, then they are likely to be ecosystem specific. For example, in python we expose a venvShellHook which will also look for a postVenvCreation (which runs only runs once) and will listen for the normal shellHook (which runs everytime).

https://github.com/NixOS/nixpkgs/blob/cde2e109d6c30b74b270669e3dbe76aec4e5bdfb/doc/languages-frameworks/python.section.md#how-to-consume-python-modules-using-pip-in-a-virtual-environment-like-i-am-used-to-on-other-operating-systems

Would you share the shell script that you ended up using as a shell hook?