Install additional dependencies via flake.nix when using 'nix develop'

I have a flake.nix for my software project and nix develop starts a development shell as you would expect.

What isn’t clear to me is how I express additional dependencies that I would like to be present in the shell but that are not required to build the software.

For example, I would like the kind Kubernetes cluster to be present in the shell so that I can start a local development cluster and deploy the software. This dependency is not needed to build the software.

Any help gratefully received.

One solution is to write a function with a parameter controlling the list of dependencies and then to define both packages and devshells as applications of this function.

Here’s an example from one of my recent projects of such a function application:

and of its definition:

Good luck!

You want to use nix shell.

What isn’t clear to me is how I express additional dependencies that I would like to be present in the shell but that are not required to build the software.

As sandro said, nix shell nixpkgs#kind would just add kind to your PATH, but this is imperative.

Another declarative option would be to merge the two:

mkShell {
  nativeBuildInputs = [ kind ];

  inputsFrom = [ myPackage ];
}

And expose it as your devShell

If you’re using flakes, this isn’t that painful as you shouldn’t be re-downloading stuff very often.

Many thanks. With your help I’ve figured this out. Since it may help somebody in the future here is a modification of the go-hello flake template that will start a local kind based Kubernetes cluster and deploy your code to it when nix develop is invoked. It deals with macOS and Linux cross-compilation too. Exiting the nix develop shell will destroy the kind cluster. The hooks deal with setting up your kubectl to talk to the cluster inside the shell.

{
  description = "A simple Go package";

  # Nixpkgs / NixOS version to use.
  inputs.nixpkgs.url = "nixpkgs/nixos-22.05";

  outputs = { self, nixpkgs }:
    let

      # to work with older version of flakes
      lastModifiedDate = self.lastModifiedDate or self.lastModified or "19700101";

      # Generate a user-friendly version number.
      version = builtins.substring 0 8 lastModifiedDate;

      # System types to support.
      supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];

      # Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'.
      forAllSystems = nixpkgs.lib.genAttrs supportedSystems;

      # Nixpkgs instantiated for supported system types.
      nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; });

    in
    {

      devShell = forAllSystems (system:
        let
          pkgs = nixpkgsFor.${system};
        in
          pkgs.mkShell {
            nativeBuildInputs = [
              pkgs.envsubst
              pkgs.gzip
              pkgs.kind
              pkgs.kubectl
              pkgs.mktemp
            ];

            inputsFrom = [ 
              self.packages.${system}.container
              self.packages.${system}.go-hello-native
            ];
            
            shellHook = ''
              if [[ "$(kind get clusters -q | grep go-hello | wc -l)" -eq 0 ]]
              then
                kind create cluster --name go-hello
              fi

              t=$(mktemp)
              cp ${self.packages.${system}.container} $t.gz
              gzip -f -d $t.gz

              kind load image-archive $t --name go-hello
              rm -f $t

              kubectl config use-context kind-go-hello

              export tag=${self.packages.${system}.container.imageTag}
              envsubst < deployment.yaml | kubectl apply -f -

              trap "kind delete cluster --name go-hello" EXIT
            '';
          }
      );

      # Provide some binary packages for selected system types.
      packages = forAllSystems (system:
        let
          pkgs = nixpkgsFor.${system};

          # Cross-compilation on macOS produces output at a different path.
          entrypointPath = if pkgs.system == "x86_64-darwin" || pkgs.system == "aarch64-darwin"
          then "/bin/linux_amd64/go-hello"
          else "/bin/go-hello";
        in
        rec {
          go-hello-native = pkgs.buildGo118Module {
            pname = "go-hello";
            inherit version;
            # In 'nix develop', we don't need a copy of the source tree
            # in the Nix store.
            src = ./.;

            # This hash locks the dependencies of this package. It is
            # necessary because of how Go requires network access to resolve
            # VCS.  See https://www.tweag.io/blog/2021-03-04-gomod2nix/ for
            # details. Normally one can build with a fake sha256 and rely on native Go
            # mechanisms to tell you what the hash should be or determine what
            # it should be "out-of-band" with other tooling (eg. gomod2nix).
            # To begin with it is recommended to set this, but one must
            # remeber to bump this hash when your dependencies change.
            #vendorSha256 = pkgs.lib.fakeSha256;

            vendorSha256 = "sha256-pQpattmS9VmO3ZIQUFn66az8GSmB4IvYhTTCFn6SUmo=";
          };

          go-hello-linux = go-hello-native.overrideAttrs (final: prev: {
            GOOS = "linux";
          });

          container = pkgs.dockerTools.buildImage {
            name = "go-hello";
            config = {
              Entrypoint = [ "${go-hello-linux}${entrypointPath}" ] ;
            };
          };

        });

      # The default package for 'nix build'. This makes sense if the
      # flake provides only one package or there is a clear "main"
      # package.
      defaultPackage = forAllSystems (system: self.packages.${system}.go-hello-native);
    };
}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-hello
  labels:
    app: go-hello
spec:
  replicas: 1
  selector:
    matchLabels:
      app: go-hello
  template:
    metadata:
      labels:
        app: go-hello
    spec:
      containers:
      - name: go-hello
        image: docker.io/library/go-hello:${tag}
        imagePullPolicy: Never
Hosted by Flying Circus.