Specifying unstable commit directly in configuration.nix

As the title says, instead of the channel being managed by nix, I’d like to specify it directly in the configuration.nix file. Is this possible?

1 Like

Yup, I have deleted all my channels and do this:

let
  channelRelease = "nixos-19.09pre182429.98e3b90b6c8";  # 2019-06-11 02:29:22
  channelName = "unstable";
  url = "https://releases.nixos.org/nixos/${channelName}/${channelRelease}/nixexprs.tar.xz";
  sha256 = "1x5c4w5yh839zvinjiprl4gj8w8cpqg215iab1qy62rrfpwyx55b";

  pinnedNixpkgs = builtins.fetchTarball {
    inherit url sha256;
  };
in
....
nix.nixPath = [
  "nixpkgs=${pinnedNixpkgs}"
   "nixos-config=/etc/nixos/configuration.nix"
];
programs.command-not-found = {
  enable = true;
  dbPath = "${pinnedNixpkgs}/programs.sqlite";
};

Usually you’d need to rebuild twice, because the first rebuild doesn’t use the nixPath in the config.
Or you can build with

sudo nixos-rebuild -I nixpkgs=https://releases.nixos.org/nixos/unstable/nixos-19.09pre182429.98e3b90b6c8/nixexprs.tar.xz test

I fetch from releases.nixos.org becaues there a programs.sqlite database was generated with all the programs and in which package they are.

Edit: I made this answer complete, including the corrections in the following answers.

5 Likes

Seems to work fine for me, thanks! A question though: How do I find out the channelRelease for the most current nixos-unstable?
Edit: Also, after removing my only channel via nix-channel --remove, nix-shell gives me a an error about file 'nixpkgs' was not found. Do I simply have to re-login so some environment variable gets updated?

Yes this option set an environment variable used by the nix commands, you will have to relogin or open a new shell to use it.

1 Like

Oh yes and also you have to rebuild twice because for the first rebuild your current channel will be used.
To have it build in one go, you can use

sudo nixos-rebuild -I nixpkgs=https://releases.nixos.org/nixos/unstable/nixos-19.09pre182429.98e3b90b6c8/nixexprs.tar.xz test

You can find the latest channel information here: https://nixos.org/channels/nixos-unstable

If you wanted, you could also fetch a commit directly from the source repo, but as I said, those don’t contain programs.sqlite and you command-not-found will not work. (Well you could use that from the channel. It doesn’t really matter if it’s out of date)

1 Like

Excellent, seems to work fine. Thanks!

Strange. Now when I try to nixos-rebuild switch, I get:

error: file 'nixos-config' was not found in the Nix search path (add it using $NIX_PATH or -I), at /nix/store/4xv8ps2vlsaz8gam9w4kvq2rz54fp8kp-nixexprs.tar.xz/nixos/default.nix:1:60

My NIX_PATH is:

/root/.nix-defexpr/channels:nixpkgs=/nix/store/3k66012dfv1cw5bybm1jcx64gg98ymaf-source

Edit: After manually augmenting my NIX_PATH with nixos-config=/etc/nixos/configuration.nix, rebuilding works. But you apparently don’t need that @JohnAZoidberg?
Edit 2: Also, rebuilding now doesn’t work with sudo anymore.

I do need nixos-config=/etc/nixos/configuration.nix, sorry. It’s in my config but I omitted it because I thought it wasn’t needed.

Should work with sudo though.

I’ve also got this (zoid being my username) but I think I needed that for a different reason (I think for installing unsigned derivations e.g. from cachix)

    nix.trustedUsers = [ "root" "zoid" ];

I’m slowly realizing that now, my system ist broken. Updating via "change configuration.nix, then sudo nixos-rebuild switch doesn’t work anymore. It doesn’t switch to a “whole” new configuration. When I restart my system and then execute nixos-rebuild switch, it “really” switches. nix-shell also downloads new packages after the “pseudo-switch” when I ask for them.

https://github.com/NixOS/nixpkgs/issues/62832 essentially the same question. Bottom line is that this doesn’t work well though.

1 Like

@lheckemann Oh, thanks for that link! I’ll go back to channels for now, then.

It’s best to create a little wrapper script that sets the NIX_PATH for you instead.

./rebuild

#!/usr/bin/env bash
set -euo pipefail

# allow to call the script from any $PWD
cd "$(dirname "$0")"

# replace with a pinned version if you want
NIX_PATH=nixpkgs=channel:nixos-unstable

# get the config from the same repo instead of /etc/nixos
NIX_PATH=$NIX_PATH:nixos-config=$PWD/machines/$(hostname)/configuration.nix

export NIX_PATH

exec nixos-rebuild "$@"

Put everything in a repo and then instead run sudo ./rebuild switch to update the system.

Once you have multiple machines it also makes it easy to share configuration using the module imports mechanism.

4 Likes

When I do that I lose the ability to start a nix-shell as a normal user:

warning: Nix search path entry '/nix/var/nix/profiles/per-user/root/channels/nixos' does not exist, ignoring
error: file 'nixpkgs' was not found in the Nix search path (add it using $NIX_PATH or -I), at (string):1: 

You can solve this by also setting the NIX_PATH in the configuration.nix:

nix.nixPath = [
  "nixpkgs=${toString pkgs.path}"
];