how to add NixOS unstable channel declaratively in configuration.nix

十年热恋 提交于 2019-12-20 09:31:33

问题


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?


回答1:


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-channels/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
  ];


来源:https://stackoverflow.com/questions/48831392/how-to-add-nixos-unstable-channel-declaratively-in-configuration-nix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!