Search code examples
nixnixos

how to add NixOS unstable channel declaratively in configuration.nix


The NixOS cheatsheet describes how to install packages from unstable in configuration.nix.

It starts off by saying to add the unstable channel like so:

$ sudo nix-channel --add https://nixos.org/channels/nixpkgs-unstable
$ sudo nix-channel --update

Then, it is easy to use this channel in configuration.nix (since it should now be on NIX_PATH):

nixpkgs.config = {
  allowUnfree = true;
  packageOverrides = pkgs: {
    unstable = import <nixos-unstable> {
      config = config.nixpkgs.config;
    };
  };
};

environment = {
  systemPackages = with pkgs; [
    unstable.google-chrome
  ];
};

I would like to not have to do the manual nix-channel --add and nix-channel --update steps.

I would like to be able to install my system from configuration.nix without first having to run the nix-channel --add and nix-channel --update steps.

Is there a way to automate this from configuration.nix?


Solution

  • I was able to get this working with a suggestion by @EmmanuelRosa.

    Here are the relevant parts of my /etc/nixos/configuration.nix:

    { config, pkgs, ... }:
    
    let
      unstableTarball =
        fetchTarball
          https://github.com/NixOS/nixpkgs/archive/nixos-unstable.tar.gz;
    in
    {
      imports =
        [ # Include the results of the hardware scan.
          /etc/nixos/hardware-configuration.nix
        ];
    
      nixpkgs.config = {
        packageOverrides = pkgs: {
          unstable = import unstableTarball {
            config = config.nixpkgs.config;
          };
        };
      };
    
      ...
    };
    

    This adds an unstable derivative that can be used in environment.systemPackages.

    Here is an example of using it to install the htop package from nixos-unstable:

      environment.systemPackages = with pkgs; [
        ...
        unstable.htop
      ];