How to allow unfree for unstable packages?

I am trying to use both unstable and stable repo for packages. I want to pick and choose which packages will be from unstable and which will be from stable. This is my flake.nix file:

{
  description = "Nixos config flake";

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

  outputs = { self, nixpkgs, nixpkgs-unstable, home-manager, ... }:
    let
      lib = nixpkgs.lib;
      system = "x86_64-linux";
      pkgs = nixpkgs.legacyPackages.${system};
      pkgs-unstable = nixpkgs-unstable.legacyPackages.${system};
    in {
      nixosConfigurations.default = lib.nixosSystem {
        inherit system;
        modules = [ ./hosts/default/configuration.nix ];
        specialArgs = { inherit pkgs-unstable; };
      };
      nixosConfigurations.plasma = lib.nixosSystem {
        inherit system;
        modules = [ ./hosts/plasma/configuration.nix ];
        specialArgs = { inherit pkgs-unstable; };
      };

      homeConfigurations = {
        name = home-manager.lib.homeManagerConfiguration {
          inherit pkgs;
          modules = [ ./home.nix ];
          extraSpecialArgs = { inherit pkgs-unstable; };
        };
      };
    };
}

and this is my home.nix file:

{ config, lib, inputs, pkgs, pkgs-unstable, ... }:

{
  # Home Manager needs a bit of information about you and the paths it should
  # manage.
  home.username = "name";
  home.homeDirectory = "/home/name";
  imports = [
    ./modules/home-manager/terminals/kitty.nix
    ./modules/home-manager/shells/zsh.nix
  ];
  # This value determines the Home Manager release that your configuration is
  # compatible with. This helps avoid breakage when a new Home Manager release
  # introduces backwards incompatible changes.
  #
  # You should not change this value, even if you update Home Manager. If you do
  # want to update the value, then make sure to first check the Home Manager
  # release notes.
  home.stateVersion = "23.11"; # Please read the comment before changing.

  # The home.packages option allows you to install Nix packages into your
  # environment.
  # # Adds the 'hello' command to your environment. It prints a friendly
  # # "Hello, world!" when run.
  
  # # It is sometimes useful to fine-tune packages, for example, by applying
  # # overrides. You can do that directly here, just don't forget the
  # # parentheses. Maybe you want to install Nerd Fonts with a limited number of
  # # fonts?
  # (pkgs.nerdfonts.override { fonts = [ "FantasqueSansMono" ]; })

  # # You can also create simple shell scripts directly inside your
  # # configuration. For example, this adds a command 'my-hello' to your
  # # environment:
  # (pkgs.writeShellScriptBin "my-hello" ''
  #   echo "Hello, ${config.home.username}!"
  # '')
  home.packages = 
    (with pkgs; [
      racket
      mpv
      vlc
      hello
      tree
      (discord.override {
        # remove any overrides that you don't want
        withOpenASAR = true;
        withVencord = true;
      })
      teams-for-linux
      evince
      neofetch
      vesktop
      nixfmt
      opentabletdriver
      unzip
      zsh-powerlevel10k
      zsh-autocomplete
      zsh-autosuggestions
      zsh-syntax-highlighting
    ])

    ++

    (with pkgs-unstable; [
      minecraft
      vscode
      ani-cli
      opentabletdriver
    ]);

  # Home Manager is pretty good at managing dotfiles. The primary way to manage
  # plain files is through 'home.file'.
  home.file = {
    # # Building this configuration will create a copy of 'dotfiles/screenrc' in
    # # the Nix store. Activating the configuration will then make '~/.screenrc' a
    # # symlink to the Nix store copy.
    # ".screenrc".source = dotfiles/screenrc;

    # # You can also set the file content immediately.
    # ".gradle/gradle.properties".text = ''
    #   org.gradle.console=verbose
    #   org.gradle.daemon.idletimeout=3600000
    # '';
  };

  # Home Manager can also manage your environment variables through
  # 'home.sessionVariables'. If you don't want to manage your shell through Home
  # Manager then you have to manually source 'hm-session-vars.sh' located at
  # either
  #
  #  ~/.nix-profile/etc/profile.d/hm-session-vars.sh
  #
  # or
  #
  #  ~/.local/state/nix/profiles/profile/etc/profile.d/hm-session-vars.sh
  #
  # or
  #
  #  /etc/profiles/per-user/name/etc/profile.d/hm-session-vars.sh
  #
  home.sessionVariables = { EDITOR = "nvim"; };

  # Let Home Manager install and manage itself.
  programs.home-manager.enable = true;

  # The below code does work.
  nixpkgs = {
    config = {
      allowUnfree = true;
      allowUnfreePredicate = (_: true);
    };
  };

# The below code does NOT work.
nixpkgs-unstable = {
    config = {
      allowUnfree = true;
      allowUnfreePredicate = (_: true);
    };
  };

  programs.git = {
    enable = true;
    userName = "name";
    userEmail = "email";
    extraConfig = {
      init.defaultBranch = "main";
    };
  };

}

Right now, in home.nix, the nixpkgs config to allow unfree works perfectly fine. However, since I am using unstable branch, I also need to tell nix to allow unfree for that branch. How can I do that? My attempt to get it to work with the nixpkgs-unstable = {…} doesn’t work because nixpkgs-unstable doesn’t exist.

I can’t help you with flakes, but if you’re using NixOS the non-experimental way, this FAQ entry tells you where to put the allowUnfree attribute when importing <nixos-unstable>.

1 Like

Yea the FAQ entry is showing you how to do it without home-manager and flakes so not really helpful for someone new to Nix like me :frowning:

I’d advise using packageoverrides for this, it works in nixos and I assume in home-manager as well (?)

nixpkgs.config.packageOverrides = old: {
    inherit (nixpkgs-unstable.legacyPackages.${pkgs.system}) package1 package2 package3;
};

In your flake.nix:

- pkgs-unstable = nixpkgs-unstable.legacyPackages.${system};
+ pkgs-unstable = import nixpkgs-unstable {inherit system; config.allowUnfree = true; };

The nixpkgs.config.* options in your NixOS configuration will set up the Nixpkgs instance that gets generated by lib.nixosSystem (aka pkgs), any external instance of Nixpkgs must be configured separately.

You can also do:

- pkgs = nixpkgs.legacyPackages.${system};

+ commonArgs = { inherit system; config.allowUnfree = true; };
+ pkgs = import nixpkgs commonArgs;
+ pkgs-unstable = import nixpkgs-unstable commonArgs;

And use that instance of pkgs in your NixOS configuration:

 nixosConfigurations.default = lib.nixosSystem {
-         inherit system;
+         inherit pkgs;

Then remove nixpkgs.config.*.

1 Like

That seemed to do it! Thanks for your help!