Combining Stable and Unstable with Home Manager in a Flake

While setting up a new workstation I noticed a few core apps (the Swiss Proton suite of apps) are running behind in updates quite a bit. Since the new versions have both features and security patches I went looking for a way to get them from the unstable nixpkgs instead. The ones I’m trying this with are Electron-wrapped apps making me fairly confident mixing them with stable won’t do any harm.

I found this thread and have been trying to implement @waffle8946’s answer into my flake with the packages being in home-manager. I chose to make a new thread rather than necro that one.

I’ll only post the relevant parts of my configs, obviously. Feel free to ask for more if required.

I started with this in my flake.nix:

inputs = {
  nixpkgs.url = "https://channels.nixos.org/nixos-26.05/nixexprs.tar.xz";
  nixpkgs-unstable.url = "https://channels.nixos.org/nixos-unstable/nixexprs.tar.xz";

  home-manager = {
    url = "github:nix-community/home-manager/release-26.05";
    inputs.nixpkgs.follows = "nixpkgs";
  };
};

outputs = { self, nixpkgs, home-manager, ...} @inputs:
  let
    lib = nixpkgs.lib;
  in {
    nixosConfiguration = {
      workstation = lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          .\configuration.nix
          home-manager.nixosModules.home-manager
          {
            home-manager = {
              useGlobalPkgs = true;
              useUserPackages = true;
              extraSpecialArgs = { inherit inputs; };
              users.name = ./home.nix;
            };
          };
        ];
      };
    };
  };

I figured I only need to add specialArgs above modules if I also want to use it outside of home-manager.

Questions:

  • Do I or do I not add nixpkgs-unstable to the outputs line { self, nixpkgs, nixpkgs-unstable, home-manager, ... }? I only need it if I use it in that same scriptblock, right? I know I need @inputs either way so its purpose might not be clear to me.
  • Unrelated, but I switched to the non-Github channel-tarballs since that’s apparently recommended and am wondering if home-manager has those as well.

Next, the home.nix:

{ config, inputs, pkgs, pkgsUnstable, ... }:

{
  _module.args.pkgsUnstable = import inputs.nixpkgs-unstable {
    inherit (pkgs.stdenv.hostPlatform) system;
    inherit (config.nixpkgs) config;
  };

  home = {
    packages = [
      pkgsUnstable.proton-pass
    ];
  };
}

Now, doing this leads to an error: In module 'nixpkgs.config', you're trying to define a value of type 'null' rather than an attribute set for the option ''!. This usually happens if '' has option definitions inside that are not matched.

If I comment out the inherit (config.nixpkgs) config; it appears to build successfully and I have the app version I was expecting.

Questions:

  • Any idea what is causing the error and how to best deal with it?
  • In this case I added pkgsUnstable on the top line because I am using it further down the file. That said, my scripting brain is telling me that pkgsUnstable is only defined later down the file so it does nothing up there. Guess it is required and it’s another thing I don’t understand?
  • Is the (with pkgs; [ stable-package ]) ++ (with pkgsUnstable; [ unstable-package ]); the cleanest way to combine both in the same file without having to prefix every package? I see a several mentions in the other thread using with pkgs and then prefixing a package with pkgsUnstable. but once again my scripting brain tells me that will resolve to pkgs.pkgsUnstable. and error out.

As always, if anybody has suggestions on how to do this better/cleaner I am happy for the advice. If I’m going to keep using NixOS I need to keep my configs as intuitive and clean as possible.

I would also like to use more recent package versions in some situations. Like in this home-manager module:

{
  inputs,
  moduleWithSystem,
  ...
}: {

  # This is your home.nix, your module where you configure home-manager
  # It's imported both in standalone configuration above, and in your nixos configuration
  flake.homeModules.programs-module = moduleWithSystem ({
    pkgs,
    self',
    inputs',
    ...
  }: {

    services.flameshot = {

      enable = true;

      # package = inputs.nixpkgs-unstable.legacyPackages.x86_64-linux.flameshot;
      # package = inputs'.nixpkgs-unstable.legacyPackages.flameshot;

      # package = self'.legacyPackages.flameshot;
      # error: attribute 'flameshot' missing

      # package = self'.packages.flameshot;
      # error: attribute 'flameshot' missing

      package = inputs'.nixpkgs-unstable.legacyPackages.flameshot;

      # https://mynixos.com/home-manager/option/services.flameshot.settings
      settings = {
        General = {
          disabledTrayIcon = false;
          showStartupLaunchMessage = false;
        };
      };
    };

  });

}

my flake.nix has these inputs:

```

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-26.05";
    nixpkgs-unstable.url = "github:nixos/nixpkgs/nixpkgs-unstable";

    home-manager = {
      url = "github:nix-community/home-manager/release-26.05";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    nixpkgs-devenv.url = "github:cachix/devenv-nixpkgs/rolling";
    devenv = {
      url = "github:cachix/devenv";
      inputs.nixpkgs.follows = "nixpkgs-devenv";
    };

    # https://github.com/hercules-ci/flake-parts
    flake-parts.url = "github:hercules-ci/flake-parts";
    flake-parts.inputs.nixpkgs-lib.follows = "nixpkgs";
    # https://github.com/denful/import-tree
    import-tree.url = "github:denful/import-tree/main";
  };

  outputs = inputs: inputs.flake-parts.lib.mkFlake { inherit inputs; } (inputs.import-tree ./modules);

}

It works and I do have the latest version of flameshot from unstable. However, I don’t know why for example package = self'.packages.flameshot; is not working. I also don’t know if this is even helpful.

For example, I have a devenv-unstable module where self' seems to work fine (maybe because I have (among others) imports = [ inputs.devenv.flakeModule ] in flake-parts.nix?):

{
  inputs,
  moduleWithSystem,
  ...
}: {

  flake.nixosModules.devenv = moduleWithSystem (
    {
      self'
    }: {

      environment = {
        sessionVariables = {
          # KEY = "value";
        };
        systemPackages = with self'.packages; [
          devenv
        ];
      };

    }
  );

}

nix-repl> inputs.nixpkgs.legacyPackages.x86_64-linux.devenv.version          
"2.1.2"

nix-repl> inputs.nixpkgs-unstable.legacyPackages.x86_64-linux.devenv.version
"2.4.0"

nix-repl> inputs.devenv.packages.x86_64-linux.devenv.version       
"2.4.1"

And overall, I have no actual clue what I’m doing here and I’m sure there is a better way and glad for suggestions too.

Between using flake-parts and the ' after self and inputs you have completely lost me. :slight_smile:

My next big question is the overlays vs module args thing. @waffle8946 indicated a preference for module args, I see most places use overlays.

What I’ve done successfully in the past is to inherit (pkgs) config overlays; instead. Hopefully that works better for you.

Correct. No need to bind a variable you won’t use. It’ll be in inputs either way, and that’s the only way you’re accessing it.

No, I’m afraid not. Those urls exist because nixos has it’s own distribution architecture independent of github, home-manager does not.

When you set _module.args.pkgsUnstable you are adding an option definition to the attrset the module outputs, not imperatively setting a variable. In order for that to actually get a variable into scope, you need to bind it in a module argument, just like config or pkgs or whatnot. It’s all laziness and declarative merging, not imperative code. So yes, you really do need that.

with foo; adds the attrs in foo to the lexical scoping (without shadowing anything not bound with with, even something bound deeper than the with itself). So every variable will first be checked to see if it’s bound with a function argument or a let, in scoping order, then it will check for it in each enclosing with, again in in scoping order. So in this case, because pkgsUnstable is bound at the top of the file with a function argument, it never even checks if it exists in pkgs. The static binding trumps the with, even though the with is closer. Though, even if the outer binding was also from a with, it would still fall through so long as the inner with doesn’t set that particular variable.

Various things about this behavior are unintuitive, but to do otherwise would be even worse. Ultimately, this is why a lot of people consider with an antipattern, and either avoid it entirely, or relegate it to very narrow scopes like literal lists of packages or the like, where these issues don’t cause too much of a problem.

Why would you expect that to work? Does your flake export a flameshot package? I’m assuming not. self is just this flake’s outputs. (And self' is the same but specialized to the contextually-relevant system.)

Using overlays for this really doesn’t make much sense. Overlays are for replacing deeply nested dependencies for all consumers of them. People often use them for adding entirely new packages or nesting other variants of nixpkgs because of a mistaken intuition that all packages are supposed to be in pkgs for some reason. They aren’t. Packages are just another kind of value in nix, and pkgs is just a datastructure with a lot of them that’s provided by nixpkgs. There’s absolutely no reason you need to splice everything into there, and several reasons not to, notably including eval time costs. Overlays essentially rewire all of nixpkgs’ internal dependencies, so they’re fairly expensive during eval.

EDIT:
Also, on the subject of pulling multiple values from each of several sources in a list, there is an alternative which doesn’t depend on with at all, which you may want to consider:

home.packages = builtins.attrValues {
  inherit (pkgs)
    stable-package1
    stable-package2
  ;
  inherit (pkgsUnstable)
    unstable-package1
    unstable-package2
  ;
};

It’s a little more verbose overall, but pretty structurally clean, in terms of indentation and the like. The one unlikely caveat you need to keep in mind is that the attribute names of the things you pull from different sources can’t clash.

I’ve configured it a little differently, so I can decide for each module whether I want stable or unstable.

  config = {
    _module.args = {
      pkgsUnstable = import inputs.nixpkgs-unstable {
        inherit system;
        config = { };
        overlays = [ ];
      };
      pkgsUnfree = import inputs.nixpkgs-unstable {
        inherit system;
        config.allowUnfree = true;
        overlays = [ ];
      };
    };
  };