Why do I see `Dependency is not of a valid type` when I try to use this flake.nix

36 Views Asked by At

I need to bind swag to a development environment at version 1.8.1. I have a module which I believe builds it at that version:

# .deps/swag.nix
{ pkgs }:
{
  swag = pkgs.buildGoModule rec {
    name = "go-swag-1.8.1";
    version = "1.8.1";

    src = pkgs.fetchFromGitHub {
      owner = "swaggo";
      repo = "swag";
      rev = "v${version}";
      sha256 = "sha256-r19SYRHewbPL6S7bZGGqJk9QX8V9IxmRx3zyDeKGdn0=";
    };

    vendorHash = "sha256-QphjiJSQRULphWjrJ8RzrUblTDYL/fYoSNT3+g0tP48=";
    subPackages = [ "cmd/swag" ];
  };
}

I've included it in my flake.nix like so:

{
  inputs = {
    flake-utils.url = "github:numtide/flake-utils";
    nixpkgs.url = "github:nixos/nixpkgs";
  };

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem (system:
      let pkgs = import nixpkgs { inherit system; };
      in {
        devShells.default = pkgs.mkShell {
          packages = [ (import ./.deps/swag.nix { inherit pkgs; }) ];
        };
      });
}

When I run nix develop I see this error:

error: while calling the 'derivationStrict' builtin
pkgs/stdenv/generic/make-derivation.nix:375:7:
nativeBuildInputs           = elemAt (elemAt dependencies 0) 1;
^
depsBuildTarget             = elemAt (elemAt dependencies 0) 2;
error: Dependency is not of a valid type: element 1 of nativeBuildInputs for nix-shell

How can I debug this kind of error?

1

There are 1 best solutions below

0
MatrixManAtYrService On

packages in mkShell ends up getting merged into nativeBuildInputs which is a parameter on stdenv.mkDerivation which needs to be a list of derivations.

In your case, nativeBuildInputs was empty, so element 1 obviously refers to the only element supplied to packages. In other cases the index in the error might be offset by the merge.

You can check what your module is actually exporting like so:

$ nix repl
nix-repl> pkgs = import<nixpkgs>{ }
nix-repl> import ./swag.nix { inherit pkgs; }
{ swag = «derivation /nix/store/frkbcnwr0kak5yvp847zq4w3d5nb84zk-go-swag-1.8.1.drv»; }

You'll notice that it's not actually a derivation, it's an attribute set containing a derivation. Modify the module to remove swag = and just let it export the output of pkgs.buildGoModule directly.

{ pkgs }:
pkgs.buildGoModule rec {
  name = "go-swag-1.8.1";
  version = "1.8.1";

  src = pkgs.fetchFromGitHub {
    owner = "swaggo";
    repo = "swag";
    rev = "v${version}";
    sha256 = "sha256-r19SYRHewbPL6S7bZGGqJk9QX8V9IxmRx3zyDeKGdn0=";
  };

  vendorHash = "sha256-QphjiJSQRULphWjrJ8RzrUblTDYL/fYoSNT3+g0tP48=";
  subPackages = [ "cmd/swag" ];
}

Notice the difference when you check it out in the repl:

nix-repl> import ./swag.nix { inherit pkgs; }
«derivation /nix/store/frkbcnwr0kak5yvp847zq4w3d5nb84zk-go-swag-1.8.1.drv»

Following this change, what you're supplying at mkShell.packages will actually be a list of derivations.