Using multiple nixpkgs versions in a flake


I’m currently building my system with nixos-23.05 as my main package source, but I need to install packages from the unstable branch (mainly emacs 29). I followed this tutorial to achieve the multiple nixpkgs version cohabitation but my flake does not want to build anymore since the changes.

I added nixpkgs-unstable.url = "nixpkgs/nixos-unstable"; to my inputs, then I added nixpkgs-unstable = import nixpkgs-unstable; to my configurations specialArgs and then I tried to add the emacs package to my systemPackages as follows:

environment.systemPackages = with pkgs; [
     #  Editors

Now when I try to rebuild my system, nix complains about ‘nixpkgs-unstable.emacs’ being a function while a set was expected.

What is the correct way to install packages from unstable branch ?

Thanks for help,

nixpkgs-unstable = import nixpkgs-unstable; does not result in an initialized plugs instance. You should either pass { system = "<your arch>";} in the import statement or can use nixpkgs-unstable.legacyPackages.<your arch>.<packagename> when not initializing the pkgs.
(I have typed that on the phone so it could contain typos) but line 32ff in the example that you followed is doing that

Okay, this works like a charm but I’m a bit confused about why it works.

Do this mean that import nixpkgs-unstable returns a function and that { system = "x86_64-linux"; } is its parameter ?

Yeah kind of. So importing nixpks, should run the default.nix in the directory. That forwards to, where you can see the arguments you can/have to provide.
The instantiated nixpks needs at least the system (either in string way or the attrs (local system)) passed, but you can pass more content, notably is the config here, that you may need when wanting to enable unfree on the instance also.

Not 100% if the path is correct, tbh as I am writing that on the phone and looking up code is kind of pain and it’s quite some time ago I played around that thing.