How to make a flutter overlay on shell.nix

let
  version = "2.8.1";
  channel = "stable";
  dartVersion = "2.15.1";
  dartSourceBase = "https://storage.googleapis.com/dart-archive/channels";
  filename = "flutter_linux_${version}-${channel}.tar.xz";


in
{ pkgs ? import <nixpkgs> {
    overlays = [
      (self: super: {
        pkgs.myFlutter = pkgs.flutter.override {

          myDartForFlutter = pkgs.dart.override {
            version = dartVersion;
            sources = {
              "${dartVersion}-x86_64-linux" = super.fetchurl {
                url = "${dartSourceBase}/stable/release/${dartVersion}/sdk/dartsdk-linux-x64-release.zip";
                sha256 = "0f45dcaa53b4090b69b277b885ea9a4cb3a41589c9119113e1b978ad55ce335f";
              };
            };
          };
          stable = super.mkFlutter.override {
            dart = self.myDartForFlutter;
            pname = "flutter";
            src = super.fetchurl {
              url = "https://storage.googleapis.com/flutter_infra_release/releases/${channel}/linux/${filename}";
              sha256 = "47ecdcc5481c51a8fb323f154f8044cb309d55fa8614a97c89bc7c08e43abe01";
            };
          };
        };

      })
    ];
  }
}:
pkgs.mkShell {

  # nativeBuildInputs is usually what you want -- tools you need to run
  nativeBuildInputs = [
    pkgs.myFlutter
  ];
}

These is my shell.nix
But it doesn’t work.
How to specific a flutter version overlay, thanks.

Biggest problem here: there’s no need to use an overlay at all.

Just define myFlutter in a let, and access it directly.

Also, you can’t use pkgs where you’re using it. You’re still in the process of defining the default value of pkgs, so it isn’t in scope yet. You should be pulling from self or super, depending on exactly what you want.
Also, the attribute name in the overlay shouldn’t be prefixed with pkgs.

I change to this but it still not works.

direnv: loading ~/tmp/.envrc
direnv: using nix
error: anonymous function at /nix/store/vpc0fkx890i2hag4kp9famakw36wqg24-nixpkgs/nixpkgs/pkgs/development/compilers/flutter/flutter.nix:8:1 called with unexpected argument 'stable'

       at /nix/store/vpc0fkx890i2hag4kp9famakw36wqg24-nixpkgs/nixpkgs/lib/customisation.nix:69:16:

           68|     let
           69|       result = f origArgs;
             |                ^
           70|
(use '--show-trace' to show detailed location information)
direnv: nix-direnv: renewed cache
direnv: export ~XDG_DATA_DIRS

{ pkgs ? import <nixpkgs> { } }:
let
  version = "2.8.1";
  channel = "stable";
  dartVersion = "2.15.1";
  dartSourceBase = "https://storage.googleapis.com/dart-archive/channels";
  filename = "flutter_linux_${version}-${channel}.tar.xz";
  myDartForFlutter = pkgs.dart.override {
    version = dartVersion;
    sources = {
      "${dartVersion}-x86_64-linux" = pkgs.fetchurl {
        url = "${dartSourceBase}/stable/release/${dartVersion}/sdk/dartsdk-linux-x64-release.zip";
        sha256 = "0f45dcaa53b4090b69b277b885ea9a4cb3a41589c9119113e1b978ad55ce335f";
      };
    };
  };
  myFlutter = pkgs.flutter.override {

    stable = pkgs.flutter.mkFlutter {
      inherit version;
      dart = myDartForFlutter;
      pname = "flutter";
      src = pkgs.fetchurl {
        url = "https://storage.googleapis.com/flutter_infra_release/releases/${channel}/linux/${filename}";
        sha256 = "47ecdcc5481c51a8fb323f154f8044cb309d55fa8614a97c89bc7c08e43abe01";
      };

    };
  };


in
pkgs.mkShell {

  # nativeBuildInputs is usually what you want -- tools you need to run
  nativeBuildInputs = [
    myFlutter
  ];
}

Well, according to the error message, stable isn’t a valid argument here. What caused you to think it was?

Digging into the nixpkgs source a bit, it looks like pkgs.flutter is just an alias for pkgs.flutterPackages.stable, and pkgs.flutterPackages is overrideable (and that’s where dart comes from), so I’d try something like myFlutter = (pkgs.flutterPackages.override { dart = myDartForFlutter; }).stable.

Or this. Might be cleaner, if you also want to override the src:

myFlutter = pkgs.flutterPackages.mkFlutter {
  inherit version;
  dart = myDartForFlutter;
  pname = "flutter";
  src = pkgs.fetchurl {...};
};

Thanks, This works, but it still needs to generate patches to make flutter work.
And how can you find that flutter is a alias for pkgs.flutterPackages.stable

I checked in <nixpkgs/pkgs/top-level/all-packages.nix>. Almost every top-level attribute in pkgs is defined there.

OK, thank you very much