21

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?

illabout
  • 3,517
  • 1
  • 18
  • 39
  • 2
    Take a look at how `fetchTarball` is used here: https://stackoverflow.com/questions/48733553/import-unstable-and-inherit-config You should be able to import nixos-unstable the same way. – Emmanuel Rosa Feb 16 '18 at 22:11
  • Would you be able to add an answer explaining that? I keep getting `infinite recursion encountered` errors when trying to do something like that. The answer in that question doesn't seem to quite work. – illabout Feb 17 '18 at 02:28
  • @EmmanuelRosa Actually, after playing around with it a little more, I was able to get this working. I'll add an answer explaining it. – illabout Feb 17 '18 at 05:19

1 Answers1

24

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
  ];
Sarah
  • 6,565
  • 1
  • 33
  • 44
illabout
  • 3,517
  • 1
  • 18
  • 39
  • 3
    In a separate question, Robert Hensing explains how to replace entire nixos modules with those from unstable: https://stackoverflow.com/a/48842655/3040129 – illabout Feb 20 '18 at 15:10
  • 6
    This solution works, I would just like to point out that in order to have a reproducible system you must pin both the stable channel and the unstable one inside the configuration, e.g. by using fetchFromGithub with the "rev" parameter – David Costa Feb 21 '18 at 09:45
  • 14
    @DavidCosta if you posted an answer to this question showing how to do that, I would vote it up. (And I'm sure it would be appreciated by people other than just me.) – illabout Feb 21 '18 at 10:28
  • I'm wondering if we can just do this in the `pkgs` argument itself. But I'm not sure how all the other arguments will work. – CMCDragonkai May 04 '18 at 10:15
  • it's better to use `fetchTarball https://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz` – srghma Oct 20 '18 at 19:09
  • @srghma Why is that? Do you a link that explains the difference between the two? – illabout Oct 21 '18 at 00:11
  • @illabout, this is exactly what is used on your question - `sudo nix-channel --add https://nixos.org/channels/nixpkgs-unstable`. How did I know: I had the same url in my channel, then I added `unstableTarball = fetchTarball https://github.com/NixOS/nixpkgs-channels/archive/nixos-unstable.tar.gz;` and made `sudo nixos-rebuild switch` and all my packages started to rebuild, but when I changed to `fetchTarball https://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz` - `sudo nixos-rebuild switch` worked without rebuild – srghma Oct 21 '18 at 07:17
  • 1
    @srghma I imagine that is because https://github.com/NixOS/nixpkgs-channels/archive/nixos-unstable.tar.gz and https://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz are different versions of unstable. But that doesn't mean you should be using the one from nixos.org over the one from GitHub. In fact, it seems like most people pin the version of unstable by using something like `fetchFromGitHub`, instead of using `fetchTarball`. – illabout Oct 21 '18 at 23:04
  • pin packages and control via parameter (e.g. in environments) https://stackoverflow.com/questions/66124085/how-to-pin-an-import-nixpkgs-call-to-a-specific-commit/66720361#66720361 – InLaw Mar 20 '21 at 11:36