Selective upgrading of packages in home-manager

I installed a minimal 20.03 configuration.nix and I am doing as much of the user configuration as possible via 20.03 home-manager. In this case, I want to install dmenu-wayland which isn’t in 20.03 but is in unstable. There are hints in the answers to previous questions but not enough that I understand what I am doing wrong.

The state at the start:

$ nix-channel --list
home-manager https://github.com/rycee/home-manager/archive/release-20.03.tar.gz

Now I add the unstable channel:

$ nix-channel --add nixpkgs-unstable https://nixos.org/channels/nixpkgs-unstable nixpkgs-unstable
$ nix-channel --update
$ nix-channel --list
home-manager https://github.com/rycee/home-manager/archive/release-20.03.tar.gz
nixpkgs-unstable https://nixos.org/channels/nixpkgs-unstable

And here is how I use it in /home/user/.config/home-manager/user/gui/sway.nix:

{ config, lib, pkgs, ... }:

let

  pkgsUnstable = import <nixpkgs-unstable> {};

in {
  wayland.windowManager.sway = {
    ...
    menu = "${pkgsUnstable.dmenu-wayland}/bin/dmenu-wl_run -i";
    ...
  };
  home.packages = [
    pkgsUnstable.dmenu-wayland
  ];
}

But home-manager can’t find :

$ cd ~/.config/home-manager
$ ./switch.sh
error: file 'nixpkgs-unstable' was not found in the Nix search path (add it using $NIX_PATH or -I), at /home/user/.config/home-manager/user/gui/sway.nix:6:25
(use '--show-trace' to show detailed location information)

I’m confused why is obviously in the channel but home-manager can’t find it. What I am doing wrong?

home-manager only follows the default system nixpkgs, not per-user nix paths.

https://github.com/nix-community/home-manager/issues/376#issuecomment-419666167

Try running home-manager -I nixpkgs-unstable switch=/nix/var/nix/profiles/per-user/$USER/channels/nixpkgs-unstable switch in your ./switch.sh

The switch script you have might do magic, can you provide its content please?

If it’s from the home-manager template, it will remove the ~/.nix-defexpr from NIX_PATH which is required to resolve channels like this.

It is preferable to add the channel to niv and add it to the environment through shell.nix in this case.

I should have mentioned that I am using home-manager-template and that ./switch.sh has not been changed.

Thank you @twoolie for pointing me to that link. I was unable to find it while searching.

Thank you @NobbZ for your explanation. It makes perfect sense now that the behavior of home-manager is pointed out.

Since I am using home-manager-template, I will try the niv and shell.nix approach first.

I added unstable to sources.json and niv update. Here is the new entry:

    "nixos-unstable": {
        "branch": "nixos-unstable",
        "description": "A read-only mirror of NixOS/nixpkgs tracking the released channels. Send issues and PRs to",
        "homepage": "https://github.com/NixOS/nixpkgs",
        "owner": "nixos",
        "repo": "nixpkgs-channels",
        "rev": "84d74ae9c9cbed73274b8e4e00be14688ffc93fe",
        "sha256": "0ww70kl08rpcsxb9xdx8m48vz41dpss4hh3vvsmswll35l158x0v",
        "type": "tarball",
        "url": "https://github.com/nixos/nixpkgs-channels/archive/84d74ae9c9cbed73274b8e4e00be14688ffc93fe.tar.gz",
        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
    }

I then modified shell.nix to add it to the NIX_PATH:

$ diff -u shell.nix.orig shell.nix
--- shell.nix.orig	2020-09-09 21:20:50.758550447 -0400
+++ shell.nix	2020-10-13 08:50:14.008445674 -0400
@@ -3,8 +3,10 @@
   sources = import ./nix/sources.nix;

   nixpkgs = sources."nixos-20.03";
+  nixpkgs-unstable = sources."nixos-unstable";

   pkgs = import nixpkgs {};
+  pkgsUnstable = import nixpkgs-unstable {};

 in pkgs.mkShell rec {

@@ -16,7 +18,7 @@
   ];

   shellHook = ''
-    export NIX_PATH="nixpkgs=${nixpkgs}:home-manager=${sources."home-manager"}"
+    export NIX_PATH="nixpkgs=${nixpkgs}:nixpkgs-unstable=${nixpkgs-unstable}:home-manager=${sources."home-manager"}"
     export HOME_MANAGER_CONFIG="./home.nix"
   '';

It is no longer complaining about not finding nixpkgs-unstable. But it appears that there is a problem with the new module pet.

$ ./switch.sh
error: attribute 'formats' missing, at /nix/store/mq8gnrwsq9blsjlrd4mx9a0sljb7sah0-source/modules/programs/pet.nix:9:12
(use '--show-trace' to show detailed location information)

Unsurprising I guess. But this looks like I bug in the pet module. Suggestions?

I got it to work by going back to the original nix.sources.json (which I had saved) and re-adding nixos-unstable and only updating it:

$ diff -u nix/sources.json.orig nix/sources.json
--- nix/sources.json.orig	2020-09-09 21:21:37.899672514 -0400
+++ nix/sources.json	2020-10-13 09:53:45.820933486 -0400
@@ -34,5 +34,17 @@
         "type": "tarball",
         "url": "https://github.com/nixos/nixpkgs-channels/archive/4bd1938e03e1caa49a6da1ec8cff802348458f05.tar.gz",
         "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
+    },
+    "nixos-unstable": {
+        "branch": "nixos-unstable",
+        "description": "A read-only mirror of NixOS/nixpkgs tracking the released channels. Send issues and PRs to",
+        "homepage": "https://github.com/NixOS/nixpkgs",
+        "owner": "nixos",
+        "repo": "nixpkgs-channels",
+        "rev": "84d74ae9c9cbed73274b8e4e00be14688ffc93fe",
+        "sha256": "0ww70kl08rpcsxb9xdx8m48vz41dpss4hh3vvsmswll35l158x0v",
+        "type": "tarball",
+        "url": "https://github.com/nixos/nixpkgs-channels/archive/84d74ae9c9cbed73274b8e4e00be14688ffc93fe.tar.gz",
+        "url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
     }
 }
$ niv update nixos-unstable

I might need to pin home-manager to something other than master.